Skip to content

Instantly share code, notes, and snippets.

@nodirg
Last active April 7, 2021 18:53
Show Gist options
  • Save nodirg/8c8539e7824bd72d1f39d0badd3b2ccd to your computer and use it in GitHub Desktop.
Save nodirg/8c8539e7824bd72d1f39d0badd3b2ccd to your computer and use it in GitHub Desktop.
import json
import posixpath
import os
checkout = '/home/nodir/chromium/src'
with open('/home/nodir/tmp/dirmd/computed.json') as f:
computed = json.load(f)
with open('/home/nodir/tmp/dirmd/reduced.json') as f:
reduced = json.load(f)
# Simplify dicts
for md in (computed, reduced):
for d in md['dirs'].itervalues():
if 'monorail' in d and 'component' in d['monorail']:
d['component'] = d['monorail']['component']
for root, dirs, files in os.walk(checkout):
rel = os.path.relpath(root, checkout)
if 'DIR_METADATA' not in files or 'OWNERS' not in files or (rel.startswith('third_party/blink') or rel.startswith('third_party/')):
continue
inherit_from = ''
with open(os.path.join(root, 'OWNERS')) as f:
for line in f:
line = line.strip()
if line.startswith('file://') and line.endswith('/OWNERS') and line != 'file://OWNERS':
inherit_from = line[5:-7]
break
if not inherit_from or inherit_from == posixpath.dirname(rel):
continue
current = computed['dirs'].get(rel, {})
own = reduced['dirs'].get(rel, {})
inheritable = computed['dirs'].get(inherit_from[2:], {})
gain = []
loss = []
change = []
for key in ('component', 'teamEmail', 'os'):
if key in current and key not in own and key not in inheritable:
loss.append(key)
elif key in current and key in inheritable and inheritable[key] != current[key]:
change.append(key)
elif key in inheritable and key not in current:
gain.append(key)
if loss or change or not gain:
continue
meta_file = os.path.join(root, 'DIR_METADATA')
with open(meta_file, 'r+') as f:
lines = list(f)
f.seek(0)
while lines and (lines[0].startswith('#') or lines[0] == '\n'):
f.write(lines[0])
lines.pop(0)
f.write('inherit_from: "%s"\n' % inherit_from)
for line in lines:
f.write(line)
print 'updated inheritance'
print ' parent: %s' % inherit_from
print ' child: //%s' % rel
for key in gain:
print ' gain: %s: %r -> %r' % (key, current.get(key), inheritable[key])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment