def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) args = [ self.executable, ] if self.is_sourcemap_enabled: args.append("-m") args.extend([ full_source_path, "-o", os.path.dirname(full_output_path), ]) full_output_dirname = os.path.dirname(full_output_path) if not os.path.exists(full_output_dirname): os.makedirs(full_output_dirname) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath( os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) out, errors = utils.run_command(args, cwd=cwd) if errors: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) args = [ self.executable, ] if self.is_sourcemap_enabled: args.append("-m") args.extend([ full_source_path, "-o", os.path.dirname(full_output_path), ]) full_output_dirname = os.path.dirname(full_output_path) if not os.path.exists(full_output_dirname): os.makedirs(full_output_dirname) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath(os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) out, errors = utils.run_command(args, cwd=cwd) if errors: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) args = [ self.executable, "--sourcemap={}".format("auto" if self.is_sourcemap_enabled else "none"), ] + self.get_extra_args() args.extend([ self.get_full_source_path(source_path), full_output_path, ]) full_output_dirname = os.path.dirname(full_output_path) if not os.path.exists(full_output_dirname): os.makedirs(full_output_dirname) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath(os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) out, errors = utils.run_command(args, None, cwd=cwd) if errors: if os.path.exists(full_output_path): os.remove(full_output_path) raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) args = [ self.executable, "--sourcemap={}".format("auto" if self.is_sourcemap_enabled else "none"), ] + self.get_extra_args() args.extend([ self.get_full_source_path(source_path), full_output_path, ]) full_output_dirname = os.path.dirname(full_output_path) if not os.path.exists(full_output_dirname): os.makedirs(full_output_dirname) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath(os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) return_code, out, errors = utils.run_command(args, None, cwd=cwd) if return_code: if os.path.exists(full_output_path): os.remove(full_output_path) raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath(os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) args = [self.executable] if self.is_sourcemap_enabled: args.extend(["--source-map"]) if self.global_vars: for variable_name, variable_value in self.global_vars.items(): args.extend(["--global-var={}={}".format(variable_name, variable_value)]) args.extend([self.get_full_source_path(source_path), full_output_path]) out, errors = utils.run_command(args, cwd=cwd) if errors: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath( os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) args = [self.executable] if self.is_sourcemap_enabled: args.extend(["--source-map"]) if self.global_vars: for variable_name, variable_value in self.global_vars.items(): args.extend([ "--global-var={}={}".format(variable_name, variable_value), ]) args.extend([ self.get_full_source_path(source_path), full_output_path, ]) return_code, out, errors = utils.run_command(args, cwd=cwd) if return_code: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) full_output_dirname = os.path.dirname(full_output_path) if not os.path.exists(full_output_dirname): os.makedirs(full_output_dirname) sourcemap_path = full_output_path + ".map" sourcemap = "" try: if self.is_sourcemap_enabled: compiled, sourcemap = sass.compile( filename=full_source_path, source_map_filename=sourcemap_path, include_paths=self.load_paths ) else: compile_kwargs = {} if self.load_paths: compile_kwargs["include_paths"] = self.load_paths if self.precision: compile_kwargs["precision"] = self.precision if self.output_style: compile_kwargs["output_style"] = self.output_style compiled = sass.compile(filename=full_source_path, **compile_kwargs) except sass.CompileError as e: raise exceptions.StaticCompilationError(encoding.force_str(e)) compiled = encoding.force_str(compiled) sourcemap = encoding.force_str(sourcemap) with open(full_output_path, "w+") as output_file: output_file.write(compiled) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: with open(sourcemap_path, "w+") as output_file: output_file.write(sourcemap) utils.fix_sourcemap(sourcemap_path, source_path, full_output_path) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath(os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) args = [ self.executable ] if self.is_sourcemap_enabled: args.extend([ "--source-map" ]) args.extend([ self.get_full_source_path(source_path), full_output_path, ]) out, errors = utils.run_command(args, cwd=cwd) if errors: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: sourcemap_full_path = full_output_path + ".map" with open(sourcemap_full_path) as sourcemap_file: sourcemap = json.loads(sourcemap_file.read()) # LESS, unlike SASS, can't add correct relative paths in source map when the compiled file # is not in the same dir as the source file. We fix it here. sourcemap["sourceRoot"] = "../" * len(source_path.split("/")) + posixpath.dirname(source_path) sourcemap["file"] = posixpath.basename(full_output_path) with open(sourcemap_full_path, "w") as sourcemap_file: sourcemap_file.write(json.dumps(sourcemap)) return self.get_output_path(source_path)
def compile_file(self, source_path): full_source_path = self.get_full_source_path(source_path) full_output_path = self.get_full_output_path(source_path) # `cwd` is a directory containing `source_path`. # Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc' cwd = os.path.normpath( os.path.join(full_source_path, *([".."] * len(source_path.split("/"))))) args = [self.executable] if self.is_sourcemap_enabled: args.extend(["--source-map"]) args.extend([ self.get_full_source_path(source_path), full_output_path, ]) out, errors = utils.run_command(args, cwd=cwd) if errors: raise exceptions.StaticCompilationError(errors) utils.convert_urls(full_output_path, source_path) if self.is_sourcemap_enabled: sourcemap_full_path = full_output_path + ".map" with open(sourcemap_full_path) as sourcemap_file: sourcemap = json.loads(sourcemap_file.read()) # LESS, unlike SASS, can't add correct relative paths in source map when the compiled file # is not in the same dir as the source file. We fix it here. sourcemap["sourceRoot"] = "../" * len( source_path.split("/")) + posixpath.dirname(source_path) sourcemap["file"] = posixpath.basename(full_output_path) with open(sourcemap_full_path, "w") as sourcemap_file: sourcemap_file.write(json.dumps(sourcemap)) return self.get_output_path(source_path)
def postprocess(self, compiled, source_path): return convert_urls(compiled, source_path)