Last active
March 23, 2023 17:09
-
-
Save nodirg/b59d3df338a72a8944f957c5b3a15e22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
checkout = '/home/nodir/chromium/src' | |
def read_mapping(file_name): | |
with open(file_name) as f: | |
mapping = json.load(f)['dirs'] | |
for d in mapping.itervalues(): | |
if 'monorail' in d and 'component' in d['monorail']: | |
d['component'] = d['monorail']['component'] | |
return mapping | |
def read_edges(original, reduced): | |
ret = {} | |
for root, dirs, files in os.walk(checkout): | |
rel = os.path.relpath(root, checkout) | |
if 'OWNERS' not in files: | |
continue | |
mx = '' | |
with open(os.path.join(root, 'OWNERS')) as f: | |
for line in f: | |
line = line.strip() | |
if not line.startswith('file://') or not line.endswith('/OWNERS') or line == 'file://OWNERS': | |
continue | |
candidate = line[7:-7] | |
if rel.startswith(candidate) or os.path.basename(candidate) != os.path.basename(rel): | |
continue | |
mx = candidate | |
break | |
if not mx: | |
continue | |
own = reduced.get(rel, {}) | |
imported = original.get(mx, {}) | |
for key in ('component', 'teamEmail', 'os'): | |
if key in imported and (key not in own or own[key] == imported[key]): | |
ret[rel] = mx | |
break | |
return ret | |
original = read_mapping('/home/nodir/tmp/dirmd/original.json') | |
reduced = read_mapping('/home/nodir/tmp/dirmd/reduced.json') | |
edges = read_edges(original, reduced) | |
def split_file(f): | |
header = [] | |
body = [] | |
for line in f: | |
stripped_line = line.strip() | |
if body: | |
body.append(line) | |
elif not stripped_line or stripped_line.startswith('#'): | |
header.append(line) | |
else: | |
body.append(line) | |
return header, body | |
# Trim useless edges. | |
for target, mx in edges.iteritems(): | |
while mx in edges and all(original[mx][k] == original[edges[mx]][k] for k in original[mx]): | |
mx = edges[mx] | |
edges[target] = mx | |
def write_lines(f, lines): | |
for line in lines: | |
f.write(line) | |
# Create COMMON_METADATA files | |
common_metadata_files = set(edges.itervalues()) | |
for mx in common_metadata_files: | |
with open(os.path.join(checkout, mx, 'DIR_METADATA')) as f: | |
comments, meta = split_file(f) | |
with open(os.path.join(checkout, mx, 'COMMON_METADATA'), 'w') as f: | |
write_lines(f, meta) | |
# Update DIR_METADATA files. | |
for target in set(edges.iterkeys()) | common_metadata_files: | |
mxs = [] | |
mx = edges.get(target) | |
if mx: | |
mxs.append(mx) | |
while mx in edges: | |
mx = edges[mx] | |
assert mx not in mxs, 'loop: %s' % mx | |
mxs.append(mx) | |
mxs.reverse() | |
if target in common_metadata_files: | |
mxs.append(target) | |
meta_path = os.path.join(checkout, target, 'DIR_METADATA') | |
if not os.path.isfile(meta_path): | |
with open(meta_path, 'w') as f: | |
for mx in mxs: | |
f.write('mixins: "//%s/COMMON_METADATA"\n' % mx) | |
else: | |
with open(meta_path, 'r+') as f: | |
comments, meta = split_file(f) | |
f.seek(0) | |
write_lines(f, comments) | |
for mx in mxs: | |
f.write('mixins: "//%s/COMMON_METADATA"\n' % mx) | |
write_lines(f, meta) | |
f.truncate() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is stupid my husband used it and we will fight all day watch cuz he will see this