DebugMedia.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os, subprocess, re, logging, time
  2. from Config import config
  3. # Find files with extension in path
  4. def findfiles(path, find_ext):
  5. for root, dirs, files in os.walk(path, topdown = False):
  6. for file in sorted(files):
  7. file_path = root+"/"+file
  8. file_ext = file.split(".")[-1]
  9. if file_ext in find_ext and not file.startswith("all."): yield file_path
  10. # Generates: all.js: merge *.js, compile coffeescript, all.css: merge *.css, vendor prefix features
  11. def merge(merged_path):
  12. merge_dir = os.path.dirname(merged_path)
  13. s = time.time()
  14. ext = merged_path.split(".")[-1]
  15. if ext == "js": # If merging .js find .coffee too
  16. find_ext = ["js", "coffee"]
  17. else:
  18. find_ext = [ext]
  19. # If exits check the other files modification date
  20. if os.path.isfile(merged_path):
  21. merged_mtime = os.path.getmtime(merged_path)
  22. changed = False
  23. for file_path in findfiles(merge_dir, find_ext):
  24. if os.path.getmtime(file_path) > merged_mtime:
  25. changed = True
  26. break
  27. if not changed: return # Assets not changed, nothing to do
  28. # Merge files
  29. parts = []
  30. for file_path in findfiles(merge_dir, find_ext):
  31. parts.append("\n\n/* ---- %s ---- */\n\n" % file_path)
  32. if file_path.endswith(".coffee"): # Compile coffee script
  33. if not config.coffeescript_compiler:
  34. logging.error("No coffeescript compiler definied, skipping compiling %s" % merged_path)
  35. return False # No coffeescript compiler, skip this file
  36. command = config.coffeescript_compiler % file_path.replace("/", "\\")
  37. s = time.time()
  38. compiler = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  39. logging.debug("Running: %s (Done in %.2fs)" % (command, time.time()-s))
  40. source = compiler.stdout.read()
  41. if source:
  42. parts.append(source)
  43. else:
  44. error = compiler.stderr.read()
  45. parts.append("alert('%s compile error: %s');" % (file_path, re.escape(error)) )
  46. else: # Add to parts
  47. parts.append(open(file_path).read())
  48. merged = "\n".join(parts)
  49. if ext == "css": # Vendor prefix css
  50. from lib.cssvendor import cssvendor
  51. merged = cssvendor.prefix(merged)
  52. merged = merged.replace("\r", "")
  53. open(merged_path, "wb").write(merged)
  54. logging.debug("Merged %s (%.2fs)" % (merged_path, time.time()-s))