PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/pack_scripts.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 67 lines | 35 code | 17 blank | 15 comment | 14 complexity | d39daf698cf508748d740925f7c6de81 MD5 | raw file
 1#!/usr/bin/env python
 2
 3import sys, os
 4
 5from glob import glob
 6from subprocess import call
 7from shutil import copyfile
 8from os import path
 9
10# Scripts that should not be packed -- just copied
11do_not_pack = set()
12
13cmd = "java -jar ../../scripts/yuicompressor.jar --charset utf-8 --type js %(fname)s -o packed/%(fname)s"
14# cmd = "java -jar ../../scripts/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js %(fname)s --js_output_file packed/%(fname)s"
15
16# If specific scripts specified on command line, just pack them, otherwise pack
17# all.
18
19def recursive_glob( pattern, excluded_dirs ):
20    """
21    Returns all items that match pattern in root and subdirectories.
22    """
23    
24    a_dir, a_pattern = path.split( pattern )
25    
26    # Skip excluded dirs.
27    if a_dir in excluded_dirs:
28        return []
29    
30    # Search current dir.
31    # print a_dir, a_pattern
32    rval = glob( pattern )
33    for item in glob( path.join( a_dir, "*" ) ):
34        if path.isdir( item ):
35            rval.extend( recursive_glob( path.join( item, a_pattern ), excluded_dirs ) )
36    
37    return rval
38
39# Get files to pack.
40if len( sys.argv ) > 1:
41    to_pack = sys.argv[1:]
42else:
43    to_pack = recursive_glob( "*.js", [ "packed" ] )
44
45
46for fname in to_pack:
47    d = dict( fname=fname )
48    packed_fname = path.join( 'packed', fname )
49    
50    # Only copy if full version is newer than packed version.
51    if path.exists( packed_fname ) and ( path.getmtime( fname ) < path.getmtime( packed_fname ) ):
52        print "Packed is current: %s" % fname
53        continue
54    
55    print "%(fname)s --> packed/%(fname)s" % d
56    
57    # Create destination dir if necessary.
58    dir, name = os.path.split( packed_fname )
59    if not path.exists( dir ):
60        print "Creating needed directory %s" % dir
61        os.makedirs( dir )
62    
63    # Copy/pack.
64    if fname in do_not_pack:
65        copyfile( fname, path.join( packed_fname ) )
66    else:
67        out = call( cmd % d, shell=True )