From 9ca7d58cac43e8171e866906bfa86c638586c8b8 Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Mon, 24 Jun 2013 15:19:18 -0400 Subject: [PATCH] Create minified_assets.json in the deployment root This file is a dict mapping filenames like "min/api.js" to "min/api.HEX_HASH_HERE.js". This will be useful later for figuring out which hashed version of a generated file belongs to a specific deploy, for reuse in future deploys with the same source files. (imported from commit d03c408b37a4e2d494abd4b1577a70b5ff5c2e34) --- zephyr/storage.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/zephyr/storage.py b/zephyr/storage.py index 8aa67bd15f..28c47033ad 100644 --- a/zephyr/storage.py +++ b/zephyr/storage.py @@ -2,6 +2,7 @@ from django.conf import settings from django.contrib.staticfiles.storage import CachedFilesMixin, StaticFilesStorage from pipeline.storage import PipelineMixin import os.path +import ujson class AddHeaderMixin(object): def post_process(self, paths, dry_run=False, **kwargs): @@ -55,5 +56,35 @@ class AddHeaderMixin(object): return ret_dict.itervalues() -class HumbugStorage(PipelineMixin, AddHeaderMixin, CachedFilesMixin, StaticFilesStorage): + +class DumpManifestMixin(object): + """ This mixin creates a JSON manifest file in the deployment root + with a dict mapping generated files' base names (like api.js) to their + names with the hash for the most recent version (api.HEX_HASH_HERE.js). + """ + def post_process(self, paths, dry_run=False, **kwargs): + if dry_run: + return + + super_class = super(DumpManifestMixin, self) + if hasattr(super_class, 'post_process'): + super_ret = super_class.post_process(paths, dry_run, **kwargs) + else: + super_ret = [] + + hashed_versions = {} # Latest hashed version of each file. + for old_path, new_path, processed in super_ret: + if old_path[:4] == 'min/': + hashed_versions[old_path] = new_path + + manifest_filename = os.path.join(settings.DEPLOY_ROOT, + 'minified_assets.json') + with open(manifest_filename, 'w') as manifest_file: + manifest_file.write(ujson.dumps(hashed_versions)) + + return super_ret + + +class HumbugStorage(PipelineMixin, AddHeaderMixin, DumpManifestMixin, + CachedFilesMixin, StaticFilesStorage): pass