Ejemplo n.º 1
0
    def scan(  # pylint: disable=too-many-branches
            self, package: Package, level: str) -> Optional[List[Issue]]:
        """Run tool and gather output."""
        if "c_src" not in package.keys() or not package["c_src"]:
            return []

        if self.plugin_context is None:
            return None

        cccc_bin = "cccc"
        if self.plugin_context.args.cccc_bin is not None:
            cccc_bin = self.plugin_context.args.cccc_bin

        cccc_config = "cccc.opt"
        if self.plugin_context.args.cccc_config is not None:
            cccc_config = self.plugin_context.args.cccc_config
        config_file = self.plugin_context.resources.get_file(cccc_config)
        if config_file is not None:
            opts = "--opt_infile=" + config_file
        else:
            return []

        for src in package["c_src"]:
            try:
                subproc_args: List[str] = [cccc_bin] + [opts] + [src]
                log_output: bytes = subprocess.check_output(
                    subproc_args, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as ex:
                if ex.returncode == 1:
                    log_output = ex.output
                else:
                    logging.warning("Problem %d", ex.returncode)
                    logging.warning("%s exception: %s", self.get_name(),
                                    ex.output)
                    return None

            except OSError as ex:
                logging.warning("Couldn't find cccc executable! (%s)", ex)
                return None

            logging.debug("%s", log_output)

        if self.plugin_context and self.plugin_context.args.output_directory:
            with open(self.get_name() + ".log", "wb") as flog:
                flog.write(log_output)

        with open(".cccc/cccc.xml") as fconfig:
            tool_output = xmltodict.parse(fconfig.read())

        issues: List[Issue] = self.parse_output(tool_output, package,
                                                config_file)
        return issues
Ejemplo n.º 2
0
    def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
        """Run tool and gather output."""
        if "c_src" not in package.keys():
            return []

        if self.plugin_context is None:
            return None

        cccc_bin = "cccc"
        if self.plugin_context.args.cccc_bin is not None:
            cccc_bin = self.plugin_context.args.cccc_bin

        config_file = self.plugin_context.resources.get_file("cccc.opt")
        if config_file is not None:
            opts = "--opt_infile=" + config_file
        else:
            return []

        for src in package["c_src"]:
            try:
                subproc_args = [cccc_bin] + [opts] + [src]  # type: List[str]
                log_output = subprocess.check_output(
                    subproc_args, stderr=subprocess.STDOUT)  # type: bytes
            except subprocess.CalledProcessError as ex:
                if ex.returncode == 1:
                    log_output = ex.output
                else:
                    print("Problem {}".format(ex.returncode))
                    print("{}".format(ex.output))
                    return None

            except OSError as ex:
                print("Couldn't find cccc executable! ({})".format(ex))
                return None

            if self.plugin_context.args.show_tool_output:
                print("{}".format(log_output))  # type: ignore

        if self.plugin_context.args.output_directory:
            with open(self.get_name() + ".log", "wb") as flog:
                flog.write(log_output)

        with open(".cccc/cccc.xml") as fconfig:
            tool_output = xmltodict.parse(fconfig.read())

        issues = self.parse_output(tool_output, package, config_file)
        return issues
Ejemplo n.º 3
0
    def scan(  # pylint: disable=too-many-branches,too-many-locals
            self, package: Package, level: str) -> Optional[List[Issue]]:
        """Run tool and gather output."""
        if "c_src" not in package.keys() or not package["c_src"]:
            return []

        if self.plugin_context is None:
            return None

        cccc_bin = "cccc"
        if self.plugin_context.args.cccc_bin is not None:
            cccc_bin = self.plugin_context.args.cccc_bin

        cccc_config = "cccc.opt"
        if self.plugin_context.args.cccc_config is not None:
            cccc_config = self.plugin_context.args.cccc_config
        config_file = self.plugin_context.resources.get_file(cccc_config)
        if config_file is not None:
            opts = ["--opt_infile=" + config_file]
        else:
            return []
        opts.append(" --lang=c++")

        issues: List[Issue] = []

        for src in package["c_src"]:
            tool_output_dir: str = ".cccc-" + Path(src).name
            opts.append("--outdir=" + tool_output_dir)

            try:
                subproc_args: List[str] = [cccc_bin] + opts + [src]
                logging.debug(" ".join(subproc_args))
                log_output: bytes = subprocess.check_output(
                    subproc_args, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as ex:
                if ex.returncode == 1:
                    log_output = ex.output
                else:
                    logging.warning("Problem %d", ex.returncode)
                    logging.warning("%s exception: %s", self.get_name(),
                                    ex.output)
                    return None

            except OSError as ex:
                logging.warning("Couldn't find cccc executable! (%s)", ex)
                return None

            logging.debug("%s", log_output)

            if self.plugin_context and self.plugin_context.args.output_directory:
                with open(self.get_name() + ".log", "ab") as flog:
                    flog.write(log_output)

            try:
                with open(tool_output_dir + "/cccc.xml",
                          encoding="utf8") as fresults:
                    tool_output = xmltodict.parse(fresults.read(),
                                                  dict_constructor=dict)
            except FileNotFoundError:
                continue

            issues.extend(self.parse_output(tool_output, src, config_file))
        return issues