This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Generate gzipped versions of JS/CSS static files.

Also, flip the symlinking so that unmangled points to the latest
mangled file.
  • Loading branch information...
spladug committed Oct 30, 2011
1 parent 07072d0 commit 4d2c3c2835df38874b2737358f927c2bd9803220
Showing with 25 additions and 12 deletions.
  1. +2 −0 .gitignore
  2. +13 −2 r2/Makefile
  3. +10 −10 r2/r2/lib/static.py
View
@@ -33,6 +33,8 @@ r2/myproduction.ini
.DS_Store
r2/r2.egg-info/**
r2/r2/public/static/sprite*.png
+r2/r2/public/static/*.gz
+r2/r2/public/static/js/lib/*.gz
r2/r2/lib/_normalized_hot.c
r2/r2/lib/mr_tools/_mr_tools.c
r2/r2/lib/db/_sorts.c
View
@@ -67,6 +67,7 @@ PYXSO_FILES := $(PYX_FILES:.pyx=.so)
NAMED = $(JSOUTPUTS) $(CSSTARGETS) $(RTLCSS) $(SPRITES)
NAMELINKS = $(patsubst %.css,%.*.css,$(patsubst %.js,%.*.js,$(NAMED)))
+GZIPPED = $(foreach file, $(filter %.js %.css, $(wildcard $(NAMELINKS))) $(static_dir)/js/lib/jquery.js, $(file).gz)
INIUPDATE = $(wildcard *.update)
INIS = $(INIUPDATE:.update=.ini)
@@ -83,7 +84,7 @@ endif
all: pyx static names $(INIS)
-.PHONY: pyx js css rtl static names i18n clean clean_static clean_pyx all
+.PHONY: pyx js css rtl static names i18n clean clean_static clean_pyx all gzip clean_gzip
$(NAMES): $(JSTARGETS) $(CSSTARGETS) $(RTLCSS) $(SPRITES)
$(UPDATE_NAMES) $(NAMES) $(NAMED)
@@ -92,9 +93,11 @@ $(JSTARGETS): $(JSSOURCES)
$(JS_COMPRESS)
$(main_sprite) $(static_dir)/$(main_css): $(static_dir)/css/$(main_css)
+ rm -f $@ # delete symlink so we don't just overwrite the old mangled file
$(PYTHON) r2/lib/contrib/nymph.py sprite-main.png $< | $(CSS_COMPRESS) > $@
$(compact_sprite) $(static_dir)/$(compact_css) : $(static_dir)/css/$(compact_css)
+ rm -f $@ # delete symlink so we don't just overwrite the old mangled file
$(PYTHON) r2/lib/contrib/nymph.py sprite-compact.png $< | $(CSS_COMPRESS) > $@
$(static_dir)/%.css : $(static_dir)/css/%.css
@@ -130,7 +133,12 @@ i18n:
paster run run.ini r2/lib/strings.py -c "print_rand_strings()" > r2/lib/rand_strings.py
python setup.py extract_messages -o $(I18NPATH)/r2.pot
-clean: clean_static clean_pyx
+gzip: $(GZIPPED)
+
+%.gz: %
+ gzip -c $< > $@
+
+clean: clean_static clean_pyx clean_gzip
rm -f $(INIS) r2/lib/rand_strings.py
clean_pyx:
@@ -141,3 +149,6 @@ clean_static: clean_names
clean_names:
rm -f $(NAMES) $(NAMELINKS) $(static_dir)/*.md5
+
+clean_gzip:
+ rm -f $(static_dir)/*.css.gz $(static_dir)/*.js.gz $(static_dir)/js/lib/*.gz
View
@@ -4,6 +4,7 @@
import hashlib
import json
import base64
+import shutil
def generate_static_name(name, base=None, shorthash=None):
"""Generate a unique filename.
@@ -17,7 +18,7 @@ def generate_static_name(name, base=None, shorthash=None):
name, ext = os.path.splitext(name)
return name + '.' + shorthash + ext
-def update_static_names(names_file, files, make_links=False):
+def update_static_names(names_file, files):
"""Generate a unique file name mapping for ``files`` and write it to a
JSON file at ``names_file``."""
if os.path.exists(names_file):
@@ -27,20 +28,19 @@ def update_static_names(names_file, files, make_links=False):
base = os.path.dirname(names_file)
for path in files:
+ name = os.path.relpath(path, base)
+ mangled_name = generate_static_name(name, base)
+ names[name] = mangled_name
+
if not os.path.islink(path):
- name = os.path.relpath(path, base)
- names[name] = generate_static_name(name, base)
+ mangled_path = os.path.join(base, mangled_name)
+ shutil.move(path, mangled_path)
+ os.symlink(mangled_name, path)
json_enc = json.JSONEncoder(indent=2, sort_keys=True)
open(names_file, "w").write(json_enc.encode(names))
- if make_links:
- for name, staticname in names.iteritems():
- link = os.path.join(base, staticname)
- if not os.path.exists(link):
- os.symlink(name, link)
-
return names
if __name__ == "__main__":
- update_static_names(sys.argv[1], sys.argv[2:], make_links=True)
+ update_static_names(sys.argv[1], sys.argv[2:])

0 comments on commit 4d2c3c2

Please sign in to comment.