PageRenderTime 164ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/bundle.py

https://github.com/adampresley/trackathon
Python | 121 lines | 58 code | 25 blank | 38 comment | 9 complexity | a93cad642758220a3aa5895c413e8294 MD5 | raw file
  1. import os, re, shutil
  2. COMPRESSOR = "yuicompressor-2.4.7.jar"
  3. #
  4. # Function: compress
  5. # Takes a series of CSS or JS files, combines them into a single temp file,
  6. # then minifies it.
  7. #
  8. # Author:
  9. # Adam Presley
  10. #
  11. def compress(inFiles, outFile, type = "js", verbose = False, tempFile = ".temp"):
  12. temp = open(tempFile, "w")
  13. for f in inFiles:
  14. fp = open(f, "r")
  15. data = fp.read() + "\n"
  16. fp.close()
  17. temp.write(data)
  18. print " + %s" % f
  19. temp.close()
  20. options = ['-o "%s"' % outFile, '--type %s' % type]
  21. if verbose:
  22. options.append("-v")
  23. os.system('java -jar "%s" %s "%s"' % (COMPRESSOR, " ".join(options), tempFile))
  24. os.remove(tempFile)
  25. #
  26. # Function: getCssFileList
  27. # Reads a layout/template file, looks for specific CSS markers, and
  28. # returns an array of relative paths for each file reference found.
  29. #
  30. # Author:
  31. # Adam Presley
  32. #
  33. def getCssFileList(templateFile):
  34. fp = open(templateFile, "r")
  35. raw = fp.read()
  36. fp.close()
  37. pattern = re.compile(r"<!---CSS-START--->(.*?)<!---CSS-END--->", re.I | re.S)
  38. matches = pattern.findall(raw)
  39. if matches:
  40. pattern = re.compile(r"<link\srel=\"stylesheet\"\stype=\"text/css\"\shref=\"(.*?)\"\s/>", re.I | re.S)
  41. matches = ["..%s" % pattern.sub("\\1", i.strip()) for i in matches[0].strip().split("\n")]
  42. return matches
  43. #
  44. # Function: getJsFileList
  45. # Reads a layout/template file, looks for specific JS markers, and
  46. # returns an array of relative paths for each file reference found.
  47. #
  48. # Author:
  49. # Adam Presley
  50. #
  51. def getJsFileList(templateFile):
  52. fp = open(templateFile, "r")
  53. raw = fp.read()
  54. fp.close()
  55. pattern = re.compile(r"<!---JS-START--->(.*?)<!---JS-END--->", re.I | re.S)
  56. matches = pattern.findall(raw)
  57. if matches:
  58. pattern = re.compile(r"<script\ssrc=\"(.*?)\"></script>", re.I | re.S)
  59. matches = ["..%s" % pattern.sub("\\1", i.strip()) for i in matches[0].strip().split("\n")]
  60. return matches
  61. #
  62. # Function: updateLayoutFile
  63. # Reads a layout/template file, looks for specific CSS and JS markers, then
  64. # replaces the contents between those markers with a new CSS or JS include
  65. # to newly combined and minified files.
  66. #
  67. # Author:
  68. # Adam Presley
  69. #
  70. def updateLayoutFile(templateFile):
  71. fp = open(templateFile, "r")
  72. raw = fp.read()
  73. fp.close()
  74. pattern = re.compile(r"<!---CSS-START--->(.*?)<!---CSS-END--->", re.I | re.S)
  75. raw = pattern.sub('<link rel="stylesheet" type="text/css" href="/resources/css/all.css" />', raw)
  76. pattern = re.compile(r"<!---JS-START--->(.*?)<!---JS-END--->", re.I | re.S)
  77. raw = pattern.sub('<script src="/resources/js/all.js"></script>', raw)
  78. fp = open(templateFile, "w")
  79. fp.write(raw)
  80. fp.close()
  81. #
  82. # Main program. This will iterate over a list of layout files,
  83. # bundle and minify the CSS and JavaScript, then replace
  84. # the references to them in the layout file.
  85. #
  86. if __name__ == "__main__":
  87. layoutFiles = ("../app/views/mainLayout.tpl",)
  88. for layoutFile in layoutFiles:
  89. print "Bundling and minifying CSS..."
  90. compress(getCssFileList(layoutFile), "../resources/css/all.css", type = "css")
  91. print ""
  92. print "Bundling and minifying JS..."
  93. compress(getJsFileList(layoutFile), "../resources/js/all.js", type = "js")
  94. updateLayoutFile(layoutFile)