Beispiel #1
0
def merge(merged_path):
	merge_dir = os.path.dirname(merged_path)
	s = time.time()
	ext = merged_path.split(".")[-1]
	if ext == "js": # If merging .js find .coffee too
		find_ext = ["js", "coffee"]
	else:
		find_ext = [ext]

	# If exits check the other files modification date
	if os.path.isfile(merged_path): 
		merged_mtime = os.path.getmtime(merged_path)
		changed = False
		for file_path in findfiles(merge_dir, find_ext):
			if os.path.getmtime(file_path) > merged_mtime: 
				changed = True
				break
		if not changed: return # Assets not changed, nothing to do

	# Merge files
	parts = []
	for file_path in findfiles(merge_dir, find_ext):
		parts.append("\n\n/* ---- %s ---- */\n\n" % file_path.replace("\\", "/"))
		if file_path.endswith(".coffee"): # Compile coffee script
			if not config.coffeescript_compiler: 
				logging.error("No coffeescript compiler definied, skipping compiling %s" % merged_path)
				return False # No coffeescript compiler, skip this file
			command = config.coffeescript_compiler % file_path.replace("/", "\\")
			s = time.time()
			compiler = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
			logging.debug("Running: %s (Done in %.2fs)" % (command, time.time()-s))
			source = compiler.stdout.read()
			if source:
				parts.append(source)
			else:
				error = compiler.stderr.read()
				parts.append("alert('%s compile error: %s');" % (file_path, re.escape(error)) )
		else: # Add to parts
			parts.append(open(file_path).read())

	merged = "\n".join(parts)
	if ext == "css": # Vendor prefix css
		from lib.cssvendor import cssvendor
		merged = cssvendor.prefix(merged)
	merged = merged.replace("\r", "")
	open(merged_path, "wb").write(merged)
	logging.debug("Merged %s (%.2fs)" % (merged_path, time.time()-s))
Beispiel #2
0
def merge(merged_path):
    merge_dir = os.path.dirname(merged_path)
    s = time.time()
    ext = merged_path.split(".")[-1]
    if ext == "js":  # If merging .js find .coffee too
        find_ext = ["js", "coffee"]
    else:
        find_ext = [ext]

    # 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

    changed = {}
    for file_path in findfiles(merge_dir, find_ext):
        if os.path.getmtime(file_path) > merged_mtime:
            changed[file_path] = True
    if not changed:
        return  # Assets not changed, nothing to do

    if os.path.isfile(merged_path):  # Find old parts to avoid unncessary recompile
        merged_old = open(merged_path, "rb").read().decode("utf8")
        old_parts = {}
        for match in re.findall("(/\* ---- (.*?) ---- \*/(.*?)(?=/\* ----|$))", merged_old, re.DOTALL):
            old_parts[match[1]] = match[2].strip("\n\r")

    # Merge files
    parts = []
    s_total = time.time()
    for file_path in findfiles(merge_dir, find_ext):
        parts.append("\n\n/* ---- %s ---- */\n\n" % file_path)
        if file_path.endswith(".coffee"):  # Compile coffee script
            if file_path in changed or file_path not in old_parts:  # Only recompile if changed or its not compiled before
                if config.coffeescript_compiler is None:
                    config.coffeescript_compiler = findCoffeescriptCompiler()
                if not config.coffeescript_compiler:
                    logging.error("No coffeescript compiler definied, skipping compiling %s" % merged_path)
                    return False  # No coffeescript compiler, skip this file
                if "%s" in config.coffeescript_compiler:  # Replace %s with coffeescript file
                    command = config.coffeescript_compiler % os.path.join(*file_path.split("/"))
                else:  # Put coffeescript file to end
                    command = config.coffeescript_compiler + " " + os.path.join(*file_path.split("/"))
                s = time.time()
                compiler = subprocess.Popen(command, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
                out = compiler.stdout.read().decode("utf8")
                compiler.wait()
                logging.debug("Running: %s (Done in %.2fs)" % (command, time.time() - s))
                if out and out.startswith("("):
                    parts.append(out)
                else:
                    error = out
                    logging.error("%s Compile error: %s" % (file_path, error))
                    parts.append(
                        "alert('%s compile error: %s');" %
                        (file_path, re.escape(error).replace("\n", "\\n").replace(r"\\n", r"\n"))
                    )
            else:  # Not changed use the old_part
                parts.append(old_parts[file_path])
        else:  # Add to parts
            parts.append(open(file_path).read().decode("utf8"))

    merged = u"\n".join(parts)
    if ext == "css":  # Vendor prefix css
        from lib.cssvendor import cssvendor
        merged = cssvendor.prefix(merged)
    merged = merged.replace("\r", "")
    open(merged_path, "wb").write(merged.encode("utf8"))
    logging.debug("Merged %s (%.2fs)" % (merged_path, time.time() - s_total))
Beispiel #3
0
def merge(merged_path):
    merge_dir = os.path.dirname(merged_path)
    s = time.time()
    ext = merged_path.split(".")[-1]
    if ext == "js":  # If merging .js find .coffee too
        find_ext = ["js", "coffee"]
    else:
        find_ext = [ext]

    # 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

    changed = {}
    for file_path in findfiles(merge_dir, find_ext):
        if os.path.getmtime(file_path) > merged_mtime + 1:
            changed[file_path] = True
    if not changed:
        return  # Assets not changed, nothing to do

    if os.path.isfile(
            merged_path):  # Find old parts to avoid unncessary recompile
        merged_old = open(merged_path, "rb").read().decode("utf8")
        old_parts = {}
        for match in re.findall("(/\* ---- (.*?) ---- \*/(.*?)(?=/\* ----|$))",
                                merged_old, re.DOTALL):
            old_parts[match[1]] = match[2].strip("\n\r")

    # Merge files
    parts = []
    s_total = time.time()
    for file_path in findfiles(merge_dir, find_ext):
        parts.append("\n\n/* ---- %s ---- */\n\n" %
                     file_path.replace(config.data_dir, ""))
        if file_path.endswith(".coffee"):  # Compile coffee script
            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
                if config.coffeescript_compiler is None:
                    config.coffeescript_compiler = findCoffeescriptCompiler()
                if not config.coffeescript_compiler:
                    logging.error(
                        "No coffeescript compiler defined, skipping compiling %s"
                        % merged_path)
                    return False  # No coffeescript compiler, skip this file

                # Replace / with os separators and escape it
                file_path_escaped = helper.shellquote(
                    os.path.join(*file_path.split("/")))

                if "%s" in config.coffeescript_compiler:  # Replace %s with coffeescript file
                    command = config.coffeescript_compiler % file_path_escaped
                else:  # Put coffeescript file to end
                    command = config.coffeescript_compiler + " " + file_path_escaped

                # Start compiling
                s = time.time()
                compiler = subprocess.Popen(command,
                                            shell=True,
                                            stderr=subprocess.STDOUT,
                                            stdout=subprocess.PIPE)
                out = compiler.stdout.read().decode("utf8")
                compiler.wait()
                logging.debug("Running: %s (Done in %.2fs)" %
                              (command, time.time() - s))

                # Check errors
                if out and out.startswith("("):  # No error found
                    parts.append(out)
                else:  # Put error message in place of source code
                    error = out
                    logging.error("%s Compile error: %s" % (file_path, error))
                    parts.append("alert('%s compile error: %s');" %
                                 (file_path, re.escape(error).replace(
                                     "\n", "\\n").replace(r"\\n", r"\n")))
            else:  # Not changed use the old_part
                parts.append(old_parts[file_path.replace(config.data_dir, "")])
        else:  # Add to parts
            parts.append(open(file_path).read().decode("utf8"))

    merged = u"\n".join(parts)
    if ext == "css":  # Vendor prefix css
        from lib.cssvendor import cssvendor
        merged = cssvendor.prefix(merged)
    merged = merged.replace("\r", "")
    open(merged_path, "wb").write(merged.encode("utf8"))
    logging.debug("Merged %s (%.2fs)" % (merged_path, time.time() - s_total))