Beispiel #1
0
    def find_dependencies(self, source_path):
        source = self.get_source(source_path)
        source_dir = posixpath.dirname(source_path)
        dependencies = set()
        imported_files = set()
        for import_path in self.find_imports(source):
            if import_path.endswith(".styl"):
                # @import "foo.styl"
                imported_files.add(
                    self.locate_imported_file(source_dir, import_path))
            elif import_path.endswith("/*"):
                # @import "foo/*"
                imported_dir = posixpath.join(source_dir, import_path[:-2])
                try:
                    imported_dir_full_path = self.get_full_source_path(
                        imported_dir)
                except ValueError:
                    raise exceptions.StaticCompilationError(
                        "Can't locate the imported directory: {0}".format(
                            import_path))
                if not os.path.isdir(imported_dir_full_path):
                    raise exceptions.StaticCompilationError(
                        "Imported path is not a directory: {0}".format(
                            import_path))
                for filename in os.listdir(imported_dir_full_path):
                    if filename.endswith(".styl"):
                        imported_files.add(
                            self.locate_imported_file(imported_dir, filename))
            else:
                try:
                    # @import "foo" -> @import "foo/index.styl"
                    imported_dir = posixpath.join(source_dir, import_path)
                    imported_dir_full_path = self.get_full_source_path(
                        imported_dir)
                    if os.path.isdir(imported_dir_full_path):
                        imported_files.add(
                            self.locate_imported_file(imported_dir,
                                                      "index.styl"))
                except ValueError:
                    # @import "foo" -> @import "foo.styl"
                    imported_files.add(
                        self.locate_imported_file(source_dir,
                                                  import_path + ".styl"))

        dependencies.update(imported_files)
        for imported_file in imported_files:
            dependencies.update(self.find_dependencies(imported_file))

        return sorted(dependencies)
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    def compile_file(self, source_path):
        args = [
            self.executable,
        ] + self.get_extra_args()

        if self.is_sourcemap_enabled:
            args.append("-s")

        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)

        args.extend(["-o", full_output_path])
        args.append(self.get_full_source_path(source_path))

        out, errors = utils.run_command(args)
        if errors:
            raise exceptions.StaticCompilationError(errors)

        if self.is_sourcemap_enabled:
            utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path)

        return self.get_output_path(source_path)
Beispiel #5
0
    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)
Beispiel #6
0
    def compile_file(self, source_path):
        full_output_path = self.get_full_output_path(source_path)
        # LiveScript bug with source map if the folder isn't already present
        if not os.path.exists(os.path.dirname(full_output_path)):
            os.makedirs(os.path.dirname(full_output_path))
        args = [
            self.executable,
            "-c",
        ]
        if self.is_sourcemap_enabled:
            args.append("-m")
            args.append("linked")
        args.extend([
            "-o",
            os.path.dirname(full_output_path),
            self.get_full_source_path(source_path),
        ])
        return_code, out, errors = utils.run_command(args)

        if return_code:
            raise exceptions.StaticCompilationError(errors)

        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_output_path = self.get_full_output_path(source_path)
        output_dir = os.path.dirname(full_output_path)

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        template_extension = os.path.splitext(source_path)[1].lstrip(".")

        args = [
            self.executable,
            self.get_full_source_path(source_path),
            "-e",
            template_extension,
            "-f",
            full_output_path,
        ] + self.get_extra_args()

        if self.is_sourcemap_enabled:
            args += ["--map", full_output_path + ".map"]

        out, errors = utils.run_command(args)

        if errors:
            raise exceptions.StaticCompilationError(errors)

        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_output_path = self.get_full_output_path(source_path)
        args = [
            self.executable,
            "-c",
        ]
        if self.is_sourcemap_enabled:
            args.append("-m")
        if self.transpile:
            args.append("-t")
        if not self.header:
            args.append("--no-header")
        args.extend([
            "-o",
            os.path.dirname(full_output_path),
            self.get_full_source_path(source_path),
        ])
        return_code, out, errors = utils.run_command(args)

        if return_code:
            raise exceptions.StaticCompilationError(errors)

        if self.is_sourcemap_enabled:
            utils.fix_sourcemap(
                os.path.splitext(full_output_path)[0] + ".map", source_path,
                full_output_path)

        return self.get_output_path(source_path)
Beispiel #9
0
    def compile_file(self, source_path):
        full_output_path = self.get_full_output_path(source_path)
        args = [self.executable]

        args.append("{}:{}".format(self.get_full_source_path(source_path),
                                   full_output_path))

        return_code, out, errors = utils.run_command(args)

        if return_code:
            raise exceptions.StaticCompilationError(errors)

        if self.compress:
            args = [
                'google-closure-compiler', '--compilation_level=SIMPLE',
                '--js=' + full_output_path,
                '--create_source_map=' + full_output_path + '.map',
                '--assume_function_wrapper'
            ]

            return_code, out, errors = utils.run_command(args)

            with open(full_output_path, 'w') as file:
                file.write(out)

        return self.get_output_path(source_path)
Beispiel #10
0
    def compile_source(self, source):
        try:
            compiled = sass.compile(string=source, indented=self.indented)
        except sass.CompileError as e:
            raise exceptions.StaticCompilationError(encoding.force_str(e))

        compiled = encoding.force_str(compiled)

        return compiled
Beispiel #11
0
    def compile_source(self, source):
        args = [self.executable, "-"]

        return_code, out, errors = utils.run_command(args, source)

        if return_code:
            raise exceptions.StaticCompilationError(errors)

        return out
Beispiel #12
0
    def compile_source(self, source):
        args = [
            self.executable,
        ] + self.get_extra_args()

        out, errors = utils.run_command(args, source)
        if errors:
            raise exceptions.StaticCompilationError(errors)

        return out
    def compile_source(self, source):
        args = [self.executable, "-"]
        if self.include_path:
            args.extend(["--include-path={}".format(self.include_path)])

        return_code, out, errors = utils.run_command(args, source)

        if return_code:
            raise exceptions.StaticCompilationError(errors)

        return out
Beispiel #14
0
    def compile_source(self, source):
        args = [
            self.executable,
            "-p",
        ]
        out, errors = utils.run_command(args, input=source)

        if errors:
            raise exceptions.StaticCompilationError(errors)

        return out
Beispiel #15
0
    def compile_source(self, source):
        args = [self.executable]

        if self.modules is not None:
            args.extend(["--modules", self.modules])

        out, errors = utils.run_command(args, source)
        if errors:
            raise exceptions.StaticCompilationError(errors)

        return out
    def locate_imported_file(self, source_dir, import_path):
        """ Locate the imported file in the source directory.
            Return the path to the imported file relative to STATIC_ROOT

        :param source_dir: source directory
        :type source_dir: str
        :param import_path: path to the imported file
        :type import_path: str
        :returns: str

        """
        import_filename = posixpath.basename(import_path)
        import_dirname = posixpath.dirname(import_path)
        import_filename_root, import_filename_extension = posixpath.splitext(
            import_filename)

        if import_filename_extension:
            filenames_to_try = [import_filename]
        else:
            # No extension is specified for the imported file, try all supported extensions
            filenames_to_try = [
                import_filename_root + "." + extension
                for extension in self.import_extensions
            ]

        if not import_filename.startswith("_"):
            # Try the files with "_" prefix
            filenames_to_try += [
                "_" + filename for filename in filenames_to_try
            ]

        # Try to locate the file in the directory relative to `source_dir`
        for filename in filenames_to_try:
            source_path = posixpath.normpath(
                posixpath.join(source_dir, import_dirname, filename))
            try:
                self.get_full_source_path(source_path)
                return source_path
            except ValueError:
                pass

        # Try to locate the file in the directories listed in `load_paths`
        for dirname in self.load_paths:
            for filename in filenames_to_try:
                source_path = posixpath.join(import_dirname, filename)
                if os.path.exists(
                        os.path.join(dirname,
                                     utils.normalize_path(source_path))):
                    return source_path

        raise exceptions.StaticCompilationError(
            "Can't locate the imported file: {0}".format(import_path))
Beispiel #17
0
    def compile_source(self, source):
        args = [
            self.executable,
            "-s",
        ]
        if self.executable.endswith("scss"):
            args.append("--sass")

        out, errors = utils.run_command(args, source)
        if errors:
            raise exceptions.StaticCompilationError(errors)

        return out
    def compile_source(self, source):
        args = [
            self.executable,
            "-s",
        ] + self.get_extra_args()

        if self.executable.endswith("sass"):
            args.append("--scss")

        return_code, out, errors = utils.run_command(args, source)
        if return_code:
            raise exceptions.StaticCompilationError(errors)

        return out
    def compile_source(self, source):
        args = [
            self.executable,
            "-c",
            "-s",
            "-p",
        ]
        if self.transpile:
            args.append("-t")
        if not self.header:
            args.append("--no-header")
        return_code, out, errors = utils.run_command(args, source)
        if return_code:
            raise exceptions.StaticCompilationError(errors)

        return out
Beispiel #20
0
    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)
Beispiel #21
0
    def compile_file(self, source_path):
        args = [
            self.executable,
        ]

        if self.is_sourcemap_enabled:
            args.append("-s")

        if self.modules is not None:
            args.extend(["--modules", self.modules])

        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)

        args.extend(["-o", full_output_path])
        args.append(self.get_full_source_path(source_path))

        out, errors = utils.run_command(args)
        if errors:
            raise exceptions.StaticCompilationError(errors)

        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())

            # Babel 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["sources"] = [
                os.path.basename(source) for source in sourcemap["sources"]
            ]
            sourcemap["file"] = posixpath.basename(
                os.path.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)
Beispiel #22
0
    def locate_imported_file(self, source_dir, import_path):
        """ Locate the imported file in the source directory.
            Return the path to the imported file relative to STATIC_ROOT

        :param source_dir: source directory
        :type source_dir: str
        :param import_path: path to the imported file
        :type import_path: str
        :returns: str

        """
        path = posixpath.normpath(posixpath.join(source_dir, import_path))

        try:
            self.get_full_source_path(path)
        except ValueError:
            raise exceptions.StaticCompilationError(
                "Can't locate the imported file: {0}".format(import_path))
        return path
Beispiel #23
0
    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)
Beispiel #24
0
    def locate_imported_file(self, source_dir, import_path):
        """ Locate the imported file in the source directory.
            Return the path to the imported file relative to STATIC_ROOT

        :param source_dir: source directory
        :type source_dir: str
        :param import_path: path to the imported file
        :type import_path: str
        :returns: str

        """
        for extension in self.import_extensions:
            import_path_probe = import_path
            if not import_path_probe.endswith("." + extension):
                import_path_probe += "." + extension
            path = posixpath.normpath(
                posixpath.join(source_dir, import_path_probe))

            try:
                self.get_full_source_path(path)
                return path
            except ValueError:
                pass

            filename = posixpath.basename(import_path_probe)
            if filename[0] != "_":
                path = posixpath.normpath(
                    posixpath.join(
                        source_dir,
                        posixpath.dirname(import_path_probe),
                        "_" + filename,
                    ))

                try:
                    self.get_full_source_path(path)
                    return path
                except ValueError:
                    pass

        raise exceptions.StaticCompilationError(
            "Can't locate the imported file: {0}".format(import_path))
Beispiel #25
0
    def compile_file(self, source_path):
        full_output_path = self.get_full_output_path(source_path)
        args = [
            self.executable,
            "-c",
        ]
        if self.is_sourcemap_enabled:
            args.append("-m")
        args.extend([
            "-o",
            os.path.dirname(full_output_path),
            self.get_full_source_path(source_path),
        ])
        out, errors = utils.run_command(args)

        if errors:
            raise exceptions.StaticCompilationError(errors)

        if self.is_sourcemap_enabled:
            # Coffeescript writes source maps to compiled.map, not compiled.js.map
            sourcemap_full_path = os.path.splitext(
                full_output_path)[0] + ".map"

            with open(sourcemap_full_path) as sourcemap_file:
                sourcemap = json.loads(sourcemap_file.read())

            # CoffeeScript, 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["sources"] = [
                os.path.basename(source) for source in sourcemap["sources"]
            ]
            sourcemap["file"] = posixpath.basename(
                os.path.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)
Beispiel #26
0
 def compile_source(self, source):
     raise exceptions.StaticCompilationError(
         "This is currently not supported")