2013-10-10 21:37:26 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-07-04 13:33:16 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2013-06-12 00:01:13 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.staticfiles.storage import CachedFilesMixin, StaticFilesStorage
|
|
|
|
from pipeline.storage import PipelineMixin
|
|
|
|
|
|
|
|
class AddHeaderMixin(object):
|
|
|
|
def post_process(self, paths, dry_run=False, **kwargs):
|
|
|
|
if dry_run:
|
2016-01-27 02:13:23 +01:00
|
|
|
return []
|
2013-06-12 00:01:13 +02:00
|
|
|
|
|
|
|
with open(settings.STATIC_HEADER_FILE) as header_file:
|
|
|
|
header = header_file.read().decode(settings.FILE_CHARSET)
|
|
|
|
|
|
|
|
# A dictionary of path to tuples of (old_path, new_path,
|
|
|
|
# processed). The return value of this method is the values
|
|
|
|
# of this dictionary
|
|
|
|
ret_dict = {}
|
|
|
|
|
|
|
|
for name in paths:
|
|
|
|
storage, path = paths[name]
|
|
|
|
|
Reuse minified JS from previous deploys
This is a big change affecting lots of areas:
* Pipeline no longer deals with JS (though it still minifies CSS)
* A new script, tools/minify-js (called from update-prod-static),
minifies JavaScripts
* A command-line argument --prev-deploy, if passed to minify-js or
update-prod-static, is used to copy minified JS from a previous
deploy (i.e., a previous git checkout), if the source files have
not changed
* update-deployment passes --prev-deploy
* Scripts are now included with the minified_js template tag, rather
than Pipeline's compressed_js
Also, as a side benefit of this commit, our Handlebars templates will
no longer be copied into prod-static/ and accessible in production.
Unminification is probably broken, but, per Zev and Trac ticket #1377,
it wasn't working perfectly before this change either.
(Based on code review, this commit has been revised to:
* Warn if git returns an error in minify-js
* Add missing output redirects in update-prod-static
* Use DEPLOY_ROOT instead of manually constructing that directory
* Use old style formatting)
(imported from commit e67722ea252756db8519d5c0bd6a421d59374185)
2013-07-03 22:42:25 +02:00
|
|
|
if not path.startswith('min/') or not path.endswith('.css'):
|
2013-06-12 00:01:13 +02:00
|
|
|
ret_dict[path] = (path, path, False)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Prepend the header
|
|
|
|
with storage.open(path) as orig_file:
|
|
|
|
orig_contents = orig_file.read().decode(settings.FILE_CHARSET)
|
|
|
|
|
|
|
|
storage.delete(path)
|
|
|
|
|
|
|
|
with storage.open(path, 'w') as new_file:
|
|
|
|
new_file.write(header + orig_contents)
|
|
|
|
|
|
|
|
ret_dict[path] = (path, path, True)
|
|
|
|
|
2016-01-27 21:17:12 +01:00
|
|
|
super_class = super(AddHeaderMixin, self) # type: ignore # https://github.com/JukkaL/mypy/issues/857
|
2013-06-12 00:01:13 +02:00
|
|
|
if hasattr(super_class, 'post_process'):
|
|
|
|
super_ret = super_class.post_process(paths, dry_run, **kwargs)
|
|
|
|
else:
|
|
|
|
super_ret = []
|
|
|
|
|
|
|
|
# Merge super class's return value with ours
|
|
|
|
for val in super_ret:
|
|
|
|
old_path, new_path, processed = val
|
|
|
|
if processed:
|
|
|
|
ret_dict[old_path] = val
|
|
|
|
|
2016-01-25 01:27:18 +01:00
|
|
|
return list(ret_dict.values())
|
2013-06-12 00:01:13 +02:00
|
|
|
|
2013-06-24 21:19:18 +02:00
|
|
|
|
2016-07-04 13:33:16 +02:00
|
|
|
class RemoveUnminifiedFilesMixin(object):
|
|
|
|
def post_process(self, paths, dry_run=False, **kwargs):
|
|
|
|
if dry_run:
|
|
|
|
return []
|
|
|
|
|
|
|
|
root = settings.STATIC_ROOT
|
|
|
|
to_remove = ['templates', 'styles', 'js']
|
|
|
|
|
|
|
|
for tree in to_remove:
|
|
|
|
shutil.rmtree(os.path.join(root, tree))
|
|
|
|
|
|
|
|
is_valid = lambda p: all([not p.startswith(k) for k in to_remove])
|
|
|
|
|
|
|
|
paths = {k: v for k, v in paths.items() if is_valid(k)}
|
|
|
|
super_class = super(RemoveUnminifiedFilesMixin, self) # type: ignore # https://github.com/JukkaL/mypy/issues/857
|
|
|
|
if hasattr(super_class, 'post_process'):
|
|
|
|
return super_class.post_process(paths, dry_run, **kwargs)
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
class ZulipStorage(PipelineMixin,
|
|
|
|
AddHeaderMixin, RemoveUnminifiedFilesMixin,
|
|
|
|
CachedFilesMixin, StaticFilesStorage):
|
2013-06-12 00:01:13 +02:00
|
|
|
pass
|