2015-07-12 20:36:46 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import time
|
2019-03-15 21:06:59 +01:00
|
|
|
import functools
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2015-01-12 02:03:45 +01:00
|
|
|
from Config import config
|
2015-09-27 02:08:53 +02:00
|
|
|
from util import helper
|
2015-01-12 02:03:45 +01:00
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2015-01-12 02:03:45 +01:00
|
|
|
# Find files with extension in path
|
|
|
|
def findfiles(path, find_ext):
|
2016-03-18 23:52:16 +01:00
|
|
|
def sorter(f1, f2):
|
|
|
|
f1 = f1[0].replace(path, "")
|
|
|
|
f2 = f2[0].replace(path, "")
|
|
|
|
if f1 == "":
|
|
|
|
return 1
|
|
|
|
elif f2 == "":
|
|
|
|
return -1
|
|
|
|
else:
|
2019-03-15 21:06:59 +01:00
|
|
|
return helper.cmp(f1.lower(), f2.lower())
|
2016-03-18 23:52:16 +01:00
|
|
|
|
2019-03-15 21:06:59 +01:00
|
|
|
for root, dirs, files in sorted(os.walk(path, topdown=False), key=functools.cmp_to_key(sorter)):
|
2015-07-12 20:36:46 +02:00
|
|
|
for file in sorted(files):
|
|
|
|
file_path = root + "/" + file
|
|
|
|
file_ext = file.split(".")[-1]
|
|
|
|
if file_ext in find_ext and not file.startswith("all."):
|
|
|
|
yield file_path.replace("\\", "/")
|
2015-01-12 02:03:45 +01:00
|
|
|
|
|
|
|
|
2022-05-11 23:33:43 +02:00
|
|
|
# Generates: all.js: merge *.js, all.css: merge *.css, vendor prefix features
|
2015-01-12 02:03:45 +01:00
|
|
|
def merge(merged_path):
|
2019-08-03 01:34:00 +02:00
|
|
|
merged_path = merged_path.replace("\\", "/")
|
2015-07-12 20:36:46 +02:00
|
|
|
merge_dir = os.path.dirname(merged_path)
|
|
|
|
s = time.time()
|
|
|
|
ext = merged_path.split(".")[-1]
|
2022-05-11 23:33:43 +02:00
|
|
|
find_ext = [ext]
|
2015-03-06 02:31:51 +01:00
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
# If exist check the other files modification date
|
|
|
|
if os.path.isfile(merged_path):
|
|
|
|
merged_mtime = os.path.getmtime(merged_path)
|
|
|
|
else:
|
|
|
|
merged_mtime = 0
|
2015-03-06 02:31:51 +01:00
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
changed = {}
|
|
|
|
for file_path in findfiles(merge_dir, find_ext):
|
2016-08-10 12:45:48 +02:00
|
|
|
if os.path.getmtime(file_path) > merged_mtime + 1:
|
2015-07-12 20:36:46 +02:00
|
|
|
changed[file_path] = True
|
|
|
|
if not changed:
|
|
|
|
return # Assets not changed, nothing to do
|
2015-03-06 02:31:51 +01:00
|
|
|
|
2019-04-08 18:12:43 +02:00
|
|
|
old_parts = {}
|
2015-07-12 20:36:46 +02:00
|
|
|
if os.path.isfile(merged_path): # Find old parts to avoid unncessary recompile
|
2019-03-15 21:06:59 +01:00
|
|
|
merged_old = open(merged_path, "rb").read()
|
|
|
|
for match in re.findall(rb"(/\* ---- (.*?) ---- \*/(.*?)(?=/\* ----|$))", merged_old, re.DOTALL):
|
2019-04-09 15:06:09 +02:00
|
|
|
old_parts[match[1].decode()] = match[2].strip(b"\n\r")
|
2015-01-12 02:03:45 +01:00
|
|
|
|
2019-04-08 18:12:43 +02:00
|
|
|
logging.debug("Merging %s (changed: %s, old parts: %s)" % (merged_path, changed, len(old_parts)))
|
2015-07-12 20:36:46 +02:00
|
|
|
# Merge files
|
|
|
|
parts = []
|
|
|
|
s_total = time.time()
|
|
|
|
for file_path in findfiles(merge_dir, find_ext):
|
2019-08-03 01:34:00 +02:00
|
|
|
file_relative_path = file_path.replace(merge_dir + "/", "")
|
|
|
|
parts.append(b"\n/* ---- %s ---- */\n\n" % file_relative_path.encode("utf8"))
|
2022-05-11 23:33:43 +02:00
|
|
|
parts.append(open(file_path, "rb").read())
|
2015-01-12 02:03:45 +01:00
|
|
|
|
2019-03-15 21:06:59 +01:00
|
|
|
merged = b"\n".join(parts)
|
2015-07-12 20:36:46 +02:00
|
|
|
if ext == "css": # Vendor prefix css
|
|
|
|
from lib.cssvendor import cssvendor
|
|
|
|
merged = cssvendor.prefix(merged)
|
2019-03-15 21:06:59 +01:00
|
|
|
merged = merged.replace(b"\r", b"")
|
|
|
|
open(merged_path, "wb").write(merged)
|
2015-07-12 20:36:46 +02:00
|
|
|
logging.debug("Merged %s (%.2fs)" % (merged_path, time.time() - s_total))
|
2015-03-06 02:31:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-07-12 20:36:46 +02:00
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
os.chdir("..")
|
2015-09-21 20:09:32 +02:00
|
|
|
merge("data/12Hw8rTgzrNo4DSh2AkqwPRqDyTticwJyH/js/all.js")
|