Skip to content
This repository has been archived by the owner on Sep 17, 2018. It is now read-only.

Commit

Permalink
Add gzip support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobes Vandermeer committed Aug 15, 2014
1 parent 1c9a758 commit b193422
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.rst
Expand Up @@ -109,6 +109,7 @@ file, e.g.

output.directory: appengine/static
output.hashed: true
output.gzip: true
output.manifest: appengine/assets.json

profile.dev:
Expand All @@ -129,6 +130,13 @@ parameter to tell ``assetgen`` to monitor file changes and rebuild all
appropriate files. Watch also monitors changes to the ``assetgen.yaml`` file,
so you can update the config without having to restart ``assetgen``.

``output.hashed: true`` will put a unique file hash in the file name so that you
can set a long expiry date for HTTP caching on those files

``output.gzip: true`` will write an additional file with ``.gz`` on the end for
use with web servers that support sending a pre-compressed file, such as nginx's
``gzip_static`` module.

During development, one often runs ``--watch`` with a dev profile, e.g.

::
Expand Down
22 changes: 19 additions & 3 deletions assetgen/main.py
Expand Up @@ -80,7 +80,8 @@
'output.hashed': False,
'output.manifest': None,
'output.manifest.force': False,
'output.template': '%(hash)s-%(filename)s'
'output.template': '%(hash)s-%(filename)s',
'output.gzip' : False
}

DOWNLOADS_PATH = environ.get(
Expand Down Expand Up @@ -388,7 +389,7 @@ def get_embed_file(self, path):
data = open(filepath, 'rb').read()
except IOError:
log.error("!! Couldn't find %r for %r in %r" % (
filepath, self.path, self.embed_path_root
realpath(filepath), self.path, realpath(self.embed_path_root)
))
return self.cache.setdefault(
path,
Expand Down Expand Up @@ -844,6 +845,7 @@ def __init__(self, path, profile='default', force=None, nuke=None):
self.output_dir = output_dir = join(base_dir, output_dir)
self.output_template = config['output.template']
self.hashed = config['output.hashed']
self.gzip = config['output.gzip']

manifest_path = config['output.manifest']
if manifest_path:
Expand Down Expand Up @@ -1031,11 +1033,20 @@ def emit(self, key, depends, path, content, extension=''):
self.prereq_data.setdefault(key, set()).add(path)
log.info("Generated prereq: %s" % output_path)
return output_path
self.output_data.setdefault(key, set()).add(output_path)
outputs = self.output_data.setdefault(key, set())
outputs.add(output_path)
if digest:
log.info("Generated output: %s (%s)" % (path, digest[:6]))
else:
log.info("Generated output: %s" % path)

if self.gzip:
import gzip
gzip_output_path = real_output_path+'.gz'
with gzip.open(gzip_output_path, 'wb') as f:
f.write(content)
outputs.add(gzip_output_path)

manifest = self.manifest
if path in manifest:
ex_output_path = manifest[path]
Expand All @@ -1045,6 +1056,11 @@ def emit(self, key, depends, path, content, extension=''):
if isfile(ex_path):
remove(ex_path)
log.info(".. Removed stale: %s" % ex_output_path)
ex_path_gz = ex_path + '.gz'
if isfile(ex_path_gz):
remove(ex_path_gz)
log.info(".. Removed stale: %s.gz" % ex_output_path)

manifest[path] = output_path
self.manifest_changed = 1
return output_path
Expand Down

0 comments on commit b193422

Please sign in to comment.