Skip to content

Instantly share code, notes, and snippets.

@nojvek
Last active March 23, 2019 00:36
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 nojvek/ccbf63ca27a9acff3fcdb372426f879b to your computer and use it in GitHub Desktop.
Save nojvek/ccbf63ca27a9acff3fcdb372426f879b to your computer and use it in GitHub Desktop.
diff asset manifests
import codecs
import json
import os
import sys
from os import path
from urllib import urlopen
asset_diff_dir = '/tmp/_asset_diff'
def load_file(file):
if file.startswith('gs://'):
file_name = path.basename(file)
dest_path = '{}/{}'.format(asset_diff_dir, file_name)
cmd = 'gsutil cp {} {}'.format(file, dest_path)
print('# {}'.format(cmd))
os.system(cmd)
file = dest_path
with open(file, 'r') as f:
return json.load(f)
def unminify_str(minified_str):
# Basically re-indent by parsing {}; characters
chars = list(minified_str)
indent = 0
for i, char in enumerate(chars):
if char == ';' or char == '{' or char == '}':
if char == '{':
chars[i] = '\n' + (' ' * indent) + char
indent += 1
elif char == '}':
indent -= 1
chars[i] = '\n' + (' ' * indent) + char
chars[i] += '\n' + (' ' * indent)
return ''.join(chars)
def write_unminified_assets(file_name, changed_assets, asset_paths):
dir_path = path.join(asset_diff_dir, path.splitext(path.basename(file_name))[0])
if not os.path.exists(dir_path):
os.makedirs(dir_path)
for asset_url in changed_assets:
asset_path = asset_paths[asset_url]
asset_path = asset_path.replace('/', '_')
asset_path = asset_path.replace('compiled_', '') # remove compiled prefix
asset_url = 'https:' + asset_url
asset_dir_path = path.join(dir_path, asset_path)
if not os.path.exists(asset_dir_path):
print('Downloading {}'.format(asset_url))
asset_content = urlopen(asset_url).read().decode('utf-8', errors='ignore')
if asset_path.endswith('.min.js') or asset_path.endswith('.min.css'):
asset_content = unminify_str(asset_content)
with codecs.open(asset_dir_path, 'w', 'utf-8') as f:
f.write(asset_content)
return dir_path
def diff_manifests(manifest_old_file, manifest_new_file):
manifest_files = [manifest_old_file, manifest_new_file]
manifests = list(map(load_file, manifest_files))
inverted = list(map(lambda m: dict([[v,k] for k,v in m.items()]), manifests))
manifest_assets = list(map(lambda m: set(m.keys()), inverted))
same_assets = manifest_assets[0].intersection(manifest_assets[1])
changed_assets = list(map(lambda assets: assets - same_assets, manifest_assets))
dir_paths = [
write_unminified_assets(manifest_files[0], changed_assets[0], inverted[0]),
write_unminified_assets(manifest_files[1], changed_assets[1], inverted[1]),
]
print('\nChanged Assets:')
print('\n'.join(changed_assets[0]))
return dir_paths
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: {} manifest_old.json manifest_new.json'.format(sys.argv[0]))
sys.exit(1)
dir_paths = diff_manifests(sys.argv[1], sys.argv[2])
diff_path = path.join(asset_diff_dir, 'asset_manifests.diff')
cmd = 'diff -urb {dir1} {dir2} > {diff_path}'.format(
**{'dir1': dir_paths[0], 'dir2': dir_paths[1], 'diff_path': diff_path}
)
print('# {}'.format(cmd))
os.system(cmd)
print('\nDiff saved at {}'.format(diff_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment