/static/scripts/pack_scripts.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 67 lines · 35 code · 17 blank · 15 comment · 11 complexity · d39daf698cf508748d740925f7c6de81 MD5 · raw file

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