2017-02-23 23:55:14 +08:00
|
|
|
#!/usr/bin/env python3
|
2014-06-07 03:12:18 +08:00
|
|
|
import argparse
|
2010-02-01 22:16:01 +08:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2015-07-17 05:49:02 +08:00
|
|
|
try:
|
|
|
|
import closure
|
|
|
|
except ImportError:
|
|
|
|
closure_compiler = None
|
|
|
|
else:
|
|
|
|
closure_compiler = os.path.join(os.path.dirname(closure.__file__), 'closure.jar')
|
|
|
|
|
2013-02-03 05:53:43 +08:00
|
|
|
js_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static', 'admin', 'js')
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2010-02-01 22:16:01 +08:00
|
|
|
def main():
|
|
|
|
description = """With no file paths given this script will automatically
|
2010-04-13 18:29:44 +08:00
|
|
|
compress all jQuery-based files of the admin app. Requires the Google Closure
|
|
|
|
Compiler library and Java version 6 or later."""
|
2014-06-07 03:12:18 +08:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
parser.add_argument('file', nargs='*')
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"-c", dest="compiler", default="~/bin/compiler.jar",
|
|
|
|
help="path to Closure Compiler jar file",
|
|
|
|
)
|
|
|
|
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose")
|
|
|
|
parser.add_argument("-q", "--quiet", action="store_false", dest="verbose")
|
2014-06-07 03:12:18 +08:00
|
|
|
options = parser.parse_args()
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2015-07-17 05:49:02 +08:00
|
|
|
compiler = closure_compiler if closure_compiler else os.path.expanduser(options.compiler)
|
2010-02-01 22:16:01 +08:00
|
|
|
if not os.path.exists(compiler):
|
2014-09-04 20:15:09 +08:00
|
|
|
sys.exit(
|
|
|
|
"Google Closure compiler jar file %s not found. Please use the -c "
|
|
|
|
"option to specify the path." % compiler
|
|
|
|
)
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2014-06-07 03:12:18 +08:00
|
|
|
if not options.file:
|
2010-02-01 22:16:01 +08:00
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("No filenames given; defaulting to admin scripts\n")
|
2016-03-29 06:33:29 +08:00
|
|
|
files = [
|
|
|
|
os.path.join(js_path, f) for f in
|
|
|
|
["actions.js", "collapse.js", "inlines.js", "prepopulate.js"]
|
|
|
|
]
|
2014-06-07 03:12:18 +08:00
|
|
|
else:
|
|
|
|
files = options.file
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2014-06-07 03:12:18 +08:00
|
|
|
for file_name in files:
|
|
|
|
if not file_name.endswith(".js"):
|
|
|
|
file_name = file_name + ".js"
|
|
|
|
to_compress = os.path.expanduser(file_name)
|
2010-02-01 22:16:01 +08:00
|
|
|
if os.path.exists(to_compress):
|
2014-06-07 03:12:18 +08:00
|
|
|
to_compress_min = "%s.min.js" % "".join(file_name.rsplit(".js"))
|
2010-02-01 22:16:01 +08:00
|
|
|
cmd = "java -jar %s --js %s --js_output_file %s" % (compiler, to_compress, to_compress_min)
|
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("Running: %s\n" % cmd)
|
|
|
|
subprocess.call(cmd.split())
|
|
|
|
else:
|
|
|
|
sys.stdout.write("File %s not found. Sure it exists?\n" % to_compress)
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-02-01 22:16:01 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|