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
|
2016-08-08 11:24:50 +02:00
|
|
|
from typing import List, Any, Tuple
|
2016-07-04 13:33:16 +02:00
|
|
|
|
2013-06-12 00:01:13 +02:00
|
|
|
from django.conf import settings
|
2017-02-06 22:12:03 +01:00
|
|
|
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
|
2013-06-12 00:01:13 +02:00
|
|
|
from pipeline.storage import PipelineMixin
|
|
|
|
|
|
|
|
class AddHeaderMixin(object):
|
|
|
|
def post_process(self, paths, dry_run=False, **kwargs):
|
2016-08-08 11:24:50 +02:00
|
|
|
# type: (Dict[str, Tuple[ZulipStorage, str]], bool, **Any) -> List[Tuple[str, str, bool]]
|
2013-06-12 00:01:13 +02:00
|
|
|
if dry_run:
|
2016-01-27 02:13:23 +01:00
|
|
|
return []
|
2013-06-12 00:01:13 +02:00
|
|
|
|
2016-10-10 16:13:51 +02:00
|
|
|
with open(settings.STATIC_HEADER_FILE, 'rb') as header_file:
|
2013-06-12 00:01:13 +02:00
|
|
|
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
|
2016-10-10 16:13:51 +02:00
|
|
|
with storage.open(path, 'rb') as orig_file:
|
2013-06-12 00:01:13 +02:00
|
|
|
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):
|
2016-08-08 11:24:50 +02:00
|
|
|
# type: (Dict[str, Tuple[ZulipStorage, str]], bool, **Any) -> List[Tuple[str, str, bool]]
|
2016-07-04 13:33:16 +02:00
|
|
|
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 []
|
|
|
|
|
2017-02-06 22:12:03 +01:00
|
|
|
if settings.PRODUCTION:
|
|
|
|
# This is a hack to use staticfiles.json from within the
|
|
|
|
# deployment, rather than a directory under STATIC_ROOT. By doing
|
|
|
|
# so, we can use a different copy of staticfiles.json for each
|
|
|
|
# deployment, which ensures that we always use the correct static
|
|
|
|
# assets for each deployment.
|
|
|
|
ManifestStaticFilesStorage.manifest_name = os.path.join(settings.DEPLOY_ROOT,
|
|
|
|
"staticfiles.json")
|
|
|
|
orig_path = ManifestStaticFilesStorage.path
|
|
|
|
|
|
|
|
def path(self, name):
|
|
|
|
# type: (Any, str) -> str
|
|
|
|
if name == ManifestStaticFilesStorage.manifest_name:
|
|
|
|
return name
|
|
|
|
return orig_path(self, name)
|
|
|
|
ManifestStaticFilesStorage.path = path
|
2016-07-04 13:33:16 +02:00
|
|
|
|
|
|
|
class ZulipStorage(PipelineMixin,
|
2016-12-03 00:04:17 +01:00
|
|
|
AddHeaderMixin, RemoveUnminifiedFilesMixin,
|
2017-02-06 22:12:03 +01:00
|
|
|
ManifestStaticFilesStorage):
|
2013-06-12 00:01:13 +02:00
|
|
|
pass
|