~roadmr/isitdeployable/revision-disappeared-ohnoes

« back to all changes in this revision

Viewing changes to scripts/js/jsmin_all.py

  • Committer: James Westby
  • Date: 2012-10-04 21:33:13 UTC
  • Revision ID: james.westby@canonical.com-20121004213313-mv91ejfgpel7n91l
Try with convoy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Handle minifying all javascript files in the build directory by walking
 
4
 
 
5
$ jsmin_all.py $lp_js_root
 
6
 
 
7
"""
 
8
import os
 
9
import re
 
10
import sys
 
11
from jsmin import JavascriptMinify
 
12
 
 
13
 
 
14
def dirwalk(dir):
 
15
    "walk a directory tree, using a generator"
 
16
    for f in os.listdir(dir):
 
17
        fullpath = os.path.join(dir,f)
 
18
        if os.path.isdir(fullpath) and not os.path.islink(fullpath):
 
19
            for x in dirwalk(fullpath):  # recurse into subdir
 
20
                yield x
 
21
        else:
 
22
            yield fullpath
 
23
 
 
24
 
 
25
def is_min(filename):
 
26
    """Check if this file is alrady a minified file"""
 
27
    return re.search("min.js$", filename)
 
28
 
 
29
def minify(filename):
 
30
    """Given a filename, handle minifying it as -min.js"""
 
31
    if not is_min(filename):
 
32
        new_filename = re.sub(".js$", "-min.js", filename)
 
33
 
 
34
        with open(filename) as shrink_me:
 
35
            with open(new_filename, 'w') as tobemin:
 
36
                jsm = JavascriptMinify()
 
37
                jsm.minify(shrink_me, tobemin)
 
38
 
 
39
 
 
40
if __name__ == '__main__':
 
41
    root = sys.argv[1]
 
42
 
 
43
    if os.path.isfile(root):
 
44
        minify(root)
 
45
    else:
 
46
        [minify(f) for f in dirwalk(root)]