compress.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. import os
  3. import optparse
  4. import subprocess
  5. import sys
  6. here = os.path.dirname(__file__)
  7. def main():
  8. usage = "usage: %prog [file1..fileN]"
  9. description = """With no file paths given this script will automatically
  10. compress all jQuery-based files of the admin app. Requires the Google Closure
  11. Compiler library and Java version 6 or later."""
  12. parser = optparse.OptionParser(usage, description=description)
  13. parser.add_option("-c", dest="compiler", default="~/bin/compiler.jar",
  14. help="path to Closure Compiler jar file")
  15. parser.add_option("-v", "--verbose",
  16. action="store_true", dest="verbose")
  17. parser.add_option("-q", "--quiet",
  18. action="store_false", dest="verbose")
  19. (options, args) = parser.parse_args()
  20. compiler = os.path.expanduser(options.compiler)
  21. if not os.path.exists(compiler):
  22. sys.exit("Google Closure compiler jar file %s not found. Please use the -c option to specify the path." % compiler)
  23. if not args:
  24. if options.verbose:
  25. sys.stdout.write("No filenames given; defaulting to admin scripts\n")
  26. args = [os.path.join(here, f) for f in [
  27. "actions.js", "collapse.js", "inlines.js", "prepopulate.js"]]
  28. for arg in args:
  29. if not arg.endswith(".js"):
  30. arg = arg + ".js"
  31. to_compress = os.path.expanduser(arg)
  32. if os.path.exists(to_compress):
  33. to_compress_min = "%s.min.js" % "".join(arg.rsplit(".js"))
  34. cmd = "java -jar %s --js %s --js_output_file %s" % (compiler, to_compress, to_compress_min)
  35. if options.verbose:
  36. sys.stdout.write("Running: %s\n" % cmd)
  37. subprocess.call(cmd.split())
  38. else:
  39. sys.stdout.write("File %s not found. Sure it exists?\n" % to_compress)
  40. if __name__ == '__main__':
  41. main()