Permalink
Browse files

Refactor the code

  • Loading branch information...
1 parent 11bbea7 commit dc764d1cfea69fa8a3d4bee00f52ba273e9407f8 @gkoberger gkoberger committed Sep 22, 2011
Showing with 71 additions and 67 deletions.
  1. +71 −67 jingo_minify/management/commands/compress_assets.py
View
138 jingo_minify/management/commands/compress_assets.py
@@ -23,10 +23,10 @@ class Command(BaseCommand): # pragma: no cover
requires_model_validation = False
do_update_only = False
- checked_repos = {}
checked_hash = {}
+ bundle_hashes = {}
- def update_hashes(self, update=False, bundle_hashes={}):
+ def update_hashes(self, update=False):
def gitid(path):
id = (git.repo.Repo(os.path.join(settings.ROOT, path))
.log('-1')[0].id_abbrev)
@@ -44,7 +44,7 @@ def gitid(path):
f.write("\n")
f.write('BUILD_ID_IMG = "%s"' % gitid('media/img'))
f.write("\n")
- f.write('BUNDLE_HASHES = %s' % bundle_hashes)
+ f.write('BUNDLE_HASHES = %s' % self.bundle_hashes)
f.write("\n")
def handle(self, **options):
@@ -56,81 +56,84 @@ def handle(self, **options):
'yuicompressor-2.4.4.jar')
path_to_jar = os.path.realpath(os.path.join(*jar_path))
- v = ''
- if 'verbosity' in options and options['verbosity'] == '2':
- v = '-v'
-
- bundle_hashes = {}
+ self.v = '-v' if options.get('verbosity', False) == 2 else ''
cachebust_imgs = getattr(settings, 'CACHEBUST_IMGS', False)
if not cachebust_imgs:
print "To turn on cache busting, use settings.CACHEBUST_IMGS"
+ # This will loop through every bundle, and do the following:
+ # - Concat all files into one
+ # - Cache bust all images in CSS files
+ # - Minify the concatted files
+
for ftype, bundle in settings.MINIFY_BUNDLES.iteritems():
for name, files in bundle.iteritems():
- # Compile LESS files
- files_all = []
- for fn in files:
- if fn.endswith('.less'):
- fp = path(fn.lstrip('/'))
- call('%s %s %s.css' % (settings.LESS_BIN, fp, fp),
- shell=True)
- # Outputs a *.less.css file.
- files_all.append('%s.css' % fn)
- else:
- files_all.append(fn)
-
+ # Set the paths to the files
concatted_file = path(ftype, '%s-all.%s' % (name, ftype,))
compressed_file = path(ftype, '%s-min.%s' % (name, ftype,))
- real_files = [path(f.lstrip('/')) for f in files_all]
-
- file_path = os.path.join(settings.ROOT, concatted_file)
+ files_all = [self._preprocess_file(fn) for fn in files]
- # Concats the files.
- call("cat %s > %s" % (' '.join(real_files), concatted_file),
+ # Concat all the files.
+ tmp_concatted = '%s.tmp' % concatted_file
+ call("cat %s > %s" % (' '.join(files_all), tmp_concatted),
shell=True)
# Cache bust individual images in the CSS
if cachebust_imgs and ftype == "css":
- css_content = ''
- with open(concatted_file, 'r') as css_in:
- css_content = css_in.read()
-
- parse = lambda url: self.cachebust(url, concatted_file)
- css_parsed = re.sub('url\(([^)]*?)\)', parse, css_content)
-
- bundle_hash = hashlib.md5(css_parsed).hexdigest()[0:7]
- bundle_hashes["%s:%s" % (ftype, name)] = bundle_hash
- with open(concatted_file, 'w') as css_out:
- css_out.write(css_parsed)
- print "Cache busted images in %s" % file_path
-
- # Compresses the concatenation.
- if ftype == 'js' and hasattr(settings, 'UGLIFY_BIN'):
- print "Minifying %s (using UglifyJS)" % concatted_file
- call("%s %s -nc -o %s %s" % (settings.UGLIFY_BIN, v,
- compressed_file, concatted_file),
- shell=True, stdout=PIPE)
- elif ftype == 'css' and hasattr(settings, 'CLEANCSS_BIN'):
- print "Minifying %s (using clean-css)" % concatted_file
- call("%s -o %s %s" % (settings.CLEANCSS_BIN,
- compressed_file, concatted_file),
- shell=True, stdout=PIPE)
- else:
- print "Minifying %s (using YUICompressor)" % concatted_file
- call("%s -jar %s %s %s -o %s" % (settings.JAVA_BIN,
- path_to_jar, v, concatted_file, compressed_file),
- shell=True, stdout=PIPE)
-
- self.update_hashes(bundle_hashes=bundle_hashes)
-
- def get_repo(self, file_path):
- folder = os.path.dirname(file_path)
- repo = self.checked_repos[folder] if folder in self.checked_repos else git.repo.Repo(file_path)
- self.checked_repos[folder] = repo
- return repo
-
- def file_hash(self, url):
+ bundle_hash = self._cachebust(concatted_file, name)
+ self.bundle_hashes["%s:%s" % (ftype, name)] = bundle_hash
+
+ # Compresses the concatenations.
+ self._minify(ftype, concatted_file, compressed_file)
+
+ # Write out the hashes
+ self.update_hashes()
+
+ # Preprocess files and return new filenames.
+ def _preprocess_file(self, filename):
+ if filename.endswith('.less'):
+ fp = path(filename.lstrip('/'))
+ call('%s %s %s.css' % (settings.LESS_BIN, fp, fp), shell=True, stdout=PIPE)
+ filename = '%s.css' % filename
+ return path(filename.lstrip('/'))
+
+ # Cache bust images. Return a new bundle hash.
+ def _cachebust(self, css_file, bundle_name):
+ print "Cache busting images in %s" % css_file
+ css_content = ''
+ with open(css_file, 'r') as css_in:
+ css_content = css_in.read()
+
+ parse = lambda url: self._cachebust_regex(url, css_file)
+ css_parsed = re.sub('url\(([^)]*?)\)', parse, css_content)
+
+ with open(css_file, 'w') as css_out:
+ css_out.write(css_parsed)
+
+ # Return bundle hash for cachebusting JS/CSS files.
+ return hashlib.md5(css_parsed).hexdigest()[0:7]
+
+ # Run the proper minifier on the file.
+ def _minify(self, ftype, file_in, file_out):
+ if ftype == 'js' and hasattr(settings, 'UGLIFY_BIN'):
+ o = {'method': 'UglifyJS', 'bin': settings.UGLIFY_BIN}
+ call("%s %s -nc -o %s %s" % (o['bin'], self.v, file_out, file_in),
+ shell=True, stdout=PIPE)
+ elif ftype == 'css' and hasattr(settings, 'CLEANCSS_BIN'):
+ o = {'method': 'clean-css', 'bin': settings.CLEANCSS_BIN}
+ call("%s -o %s %s" % (o['bin'], file_out, file_in),
+ shell=True, stdout=PIPE)
+ else:
+ o = {'method': 'YUI Compressor', 'bin': settings.JAVA_BIN}
+ variables = (o['bin'], path_to_jar, self.v, file_in, file_out)
+ call("%s -jar %s %s %s -o %s" % variables,
+ shell=True, stdout=PIPE)
+
+ print "Minifying %s (using %s)" % (file_in, o['method'])
+
+ # Open the file and get a hash of it.
+ def _file_hash(self, url):
if url in self.checked_hash:
return self.checked_hash[url]
@@ -139,12 +142,13 @@ def file_hash(self, url):
with open(url) as f:
file_hash = hashlib.md5(f.read()).hexdigest()[0:7]
except IOError:
- print " - Couldn't find file %s" % full_url
+ print " - Couldn't find file %s" % url
self.checked_hash[url] = file_hash
return file_hash
- def cachebust(self, img, parent):
+ # This is called when we run over the CSS with a regex.
+ def _cachebust_regex(self, img, parent):
# We get a structural regex object back, hence the "group()"
url = img.group(1).strip('"\'')
if url.startswith('data:') or url.startswith('http'):
@@ -154,5 +158,5 @@ def cachebust(self, img, parent):
full_url = os.path.join(settings.ROOT, os.path.dirname(parent),
url)
- return "url(%s?%s)" % (url, self.file_hash(full_url))
+ return "url(%s?%s)" % (url, self._file_hash(full_url))

0 comments on commit dc764d1

Please sign in to comment.