Skip to content

Instantly share code, notes, and snippets.

@nodirg
Last active May 15, 2021 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodirg/a4803af94ffe258ba0a6e0a4807141d8 to your computer and use it in GitHub Desktop.
Save nodirg/a4803af94ffe258ba0a6e0a4807141d8 to your computer and use it in GitHub Desktop.
import json
import re
import os
def is_empty(contents):
for line in contents.splitlines():
line = line.strip()
if line and not line.startswith('#'):
return False
return True
def read_mapping(file_name):
with open(file_name) as f:
mapping = json.load(f)['dirs']
return mapping
original = read_mapping('/home/nodir/tmp/dirmd/original.json')
reduced = read_mapping('/home/nodir/tmp/dirmd/reduced.json')
for dir, orig_md in original.iteritems():
if dir.startswith('tools/binary_size/libsupersize/testdata/'):
continue
reduced_md = reduced.get(dir) or {}
strip_monorail = 'monorail' in orig_md and 'monorail' not in reduced_md
strip_os = 'os' in orig_md and 'os' not in reduced_md
strip_email = 'teamEmail' in orig_md and 'teamEmail' not in reduced_md
if strip_monorail or strip_os or strip_email:
file_name = os.path.join('/home/nodir/chromium/src', dir, 'DIR_METADATA')
keep_file = True
if not os.path.exists(file_name):
continue
with open(file_name, 'r+') as f:
data = f.read()
if strip_monorail:
data = re.sub('monorail:?\s*{[^}]+}\n?', '', data, flags=re.M)
if strip_email:
data = re.sub('team_email:.+\n?', '', data)
if strip_os:
data = re.sub('os:.+\n?', '', data)
# Replace new double blank lines
data = data.replace('\n\n\n', '\n\n')
keep_file = not is_empty(data)
if keep_file:
f.seek(0)
f.write(data)
f.truncate()
if not keep_file:
os.remove(file_name)
print 'wrote or deleted %s' % file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment