DebugMedia.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import os
  2. import subprocess
  3. import re
  4. import logging
  5. import time
  6. from Config import config
  7. from util import helper
  8. # Find files with extension in path
  9. def findfiles(path, find_ext):
  10. def sorter(f1, f2):
  11. f1 = f1[0].replace(path, "")
  12. f2 = f2[0].replace(path, "")
  13. if f1 == "":
  14. return 1
  15. elif f2 == "":
  16. return -1
  17. else:
  18. return cmp(f1.lower(), f2.lower())
  19. for root, dirs, files in sorted(os.walk(path, topdown=False), cmp=sorter):
  20. for file in sorted(files):
  21. file_path = root + "/" + file
  22. file_ext = file.split(".")[-1]
  23. if file_ext in find_ext and not file.startswith("all."):
  24. yield file_path.replace("\\", "/")
  25. # Try to find coffeescript compiler in path
  26. def findCoffeescriptCompiler():
  27. coffeescript_compiler = None
  28. try:
  29. import distutils.spawn
  30. coffeescript_compiler = helper.shellquote(distutils.spawn.find_executable("coffee")) + " --no-header -p"
  31. except:
  32. pass
  33. if coffeescript_compiler:
  34. return coffeescript_compiler
  35. else:
  36. return False
  37. # Generates: all.js: merge *.js, compile coffeescript, all.css: merge *.css, vendor prefix features
  38. def merge(merged_path):
  39. merge_dir = os.path.dirname(merged_path)
  40. s = time.time()
  41. ext = merged_path.split(".")[-1]
  42. if ext == "js": # If merging .js find .coffee too
  43. find_ext = ["js", "coffee"]
  44. else:
  45. find_ext = [ext]
  46. # If exist check the other files modification date
  47. if os.path.isfile(merged_path):
  48. merged_mtime = os.path.getmtime(merged_path)
  49. else:
  50. merged_mtime = 0
  51. changed = {}
  52. for file_path in findfiles(merge_dir, find_ext):
  53. if os.path.getmtime(file_path) > merged_mtime + 1:
  54. changed[file_path] = True
  55. if not changed:
  56. return # Assets not changed, nothing to do
  57. if os.path.isfile(merged_path): # Find old parts to avoid unncessary recompile
  58. merged_old = open(merged_path, "rb").read().decode("utf8")
  59. old_parts = {}
  60. for match in re.findall(r"(/\* ---- (.*?) ---- \*/(.*?)(?=/\* ----|$))", merged_old, re.DOTALL):
  61. old_parts[match[1]] = match[2].strip("\n\r")
  62. # Merge files
  63. parts = []
  64. s_total = time.time()
  65. for file_path in findfiles(merge_dir, find_ext):
  66. parts.append("\n\n/* ---- %s ---- */\n\n" % file_path.replace(config.data_dir, ""))
  67. if file_path.endswith(".coffee"): # Compile coffee script
  68. if file_path in changed or file_path.replace(config.data_dir, "") not in old_parts: # Only recompile if changed or its not compiled before
  69. if config.coffeescript_compiler is None:
  70. config.coffeescript_compiler = findCoffeescriptCompiler()
  71. if not config.coffeescript_compiler:
  72. logging.error("No coffeescript compiler defined, skipping compiling %s" % merged_path)
  73. return False # No coffeescript compiler, skip this file
  74. # Replace / with os separators and escape it
  75. file_path_escaped = helper.shellquote(file_path.replace("/", os.path.sep))
  76. if "%s" in config.coffeescript_compiler: # Replace %s with coffeescript file
  77. command = config.coffeescript_compiler % file_path_escaped
  78. else: # Put coffeescript file to end
  79. command = config.coffeescript_compiler + " " + file_path_escaped
  80. # Start compiling
  81. s = time.time()
  82. compiler = subprocess.Popen(command, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
  83. out = compiler.stdout.read().decode("utf8")
  84. compiler.wait()
  85. logging.debug("Running: %s (Done in %.2fs)" % (command, time.time() - s))
  86. # Check errors
  87. if out and out.startswith("("): # No error found
  88. parts.append(out)
  89. else: # Put error message in place of source code
  90. error = out
  91. logging.error("%s Compile error: %s" % (file_path, error))
  92. parts.append(
  93. "alert('%s compile error: %s');" %
  94. (file_path, re.escape(error).replace("\n", "\\n").replace(r"\\n", r"\n"))
  95. )
  96. else: # Not changed use the old_part
  97. parts.append(old_parts[file_path.replace(config.data_dir, "")])
  98. else: # Add to parts
  99. parts.append(open(file_path).read().decode("utf8"))
  100. merged = u"\n".join(parts)
  101. if ext == "css": # Vendor prefix css
  102. from lib.cssvendor import cssvendor
  103. merged = cssvendor.prefix(merged)
  104. merged = merged.replace("\r", "")
  105. open(merged_path, "wb").write(merged.encode("utf8"))
  106. logging.debug("Merged %s (%.2fs)" % (merged_path, time.time() - s_total))
  107. if __name__ == "__main__":
  108. logging.getLogger().setLevel(logging.DEBUG)
  109. os.chdir("..")
  110. config.coffeescript_compiler = r'type "%s" | tools\coffee-node\bin\node.exe tools\coffee-node\bin\coffee --no-header -s -p'
  111. merge("data/12Hw8rTgzrNo4DSh2AkqwPRqDyTticwJyH/js/all.js")