def setup_bandit_tool_plugin(binary=None): """Create an instance of the bandit plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output", ) arg_parser.add_argument("--bandit-bin", dest="bandit_bin") arg_parser.add_argument( "--mapping-file-suffix", dest="mapping_file_suffix", type=str ) resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")] ) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) btp = BanditToolPlugin() if binary: plugin_context.args.bandit_bin = binary btp.set_plugin_context(plugin_context) return btp
def setup_cppcheck_tool_plugin(use_plugin_context=True, binary=None): """Initialize and return an instance of the cppcheck plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output", ) arg_parser.add_argument("--cppcheck-bin", dest="cppcheck_bin") arg_parser.add_argument("--mapping-file-suffix", dest="mapping_file_suffix", type=str) resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) cctp = CppcheckToolPlugin() if binary: plugin_context.args.cppcheck_bin = binary if use_plugin_context: cctp.set_plugin_context(plugin_context) return cctp
def setup_uncrustify_tool_plugin(extra_path=None, use_plugin_context=True, binary=None): """Initialize and return an instance of the uncrustify plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--uncrustify-bin", dest="uncrustify_bin") arg_parser.add_argument( "--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output", ) paths = [] if extra_path: paths.append(extra_path) paths.append( os.path.join(os.path.dirname(statick_tool.__file__), "plugins")) resources = Resources(paths) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) utp = UncrustifyToolPlugin() if binary: plugin_context.args.uncrustify_bin = binary if use_plugin_context: utp.set_plugin_context(plugin_context) return utp
def setup_clang_format_tool_plugin_non_default(): """Initialize and return an instance of the clang-format plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output", ) arg_parser.add_argument("--clang-format-bin", dest="clang_format_bin") arg_parser.add_argument( "--clang-format-raise-exception", dest="clang_format_raise_exception", action="store_true", default=False, ) arg_parser.add_argument("--output-directory", dest="output_directory") resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) cftp = ClangFormatToolPlugin() cftp.set_plugin_context(plugin_context) return cftp
def fortify_tool_plugin(): """Create an instance of the fortify plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_true", help="Show tool output") arg_parser.add_argument("--fortify-python", dest="fortify_python", type=int) arg_parser.add_argument("--mapping-file-suffix", dest="mapping_file_suffix", default=None) arg_parser.add_argument("--fortify-version", dest="fortify_version", default="18.20") resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) ftp = FortifyToolPlugin() ftp.set_plugin_context(plugin_context) return ftp
def __init__(self, user_paths: List[str]) -> None: """Initialize Statick.""" self.resources = Resources(user_paths) self.manager = PluginManager() self.manager.setPluginPlaces(self.resources.get_plugin_paths()) self.manager.setCategoriesFilter({ "Discovery": DiscoveryPlugin, "Tool": ToolPlugin, "Reporting": ReportingPlugin, }) self.manager.collectPlugins() self.discovery_plugins: Dict[str, Any] = {} for plugin_info in self.manager.getPluginsOfCategory("Discovery"): self.discovery_plugins[plugin_info.plugin_object.get_name( )] = plugin_info.plugin_object self.tool_plugins: Dict[str, Any] = {} for plugin_info in self.manager.getPluginsOfCategory("Tool"): self.tool_plugins[plugin_info.plugin_object.get_name( )] = plugin_info.plugin_object self.reporting_plugins: Dict[str, Any] = {} for plugin_info in self.manager.getPluginsOfCategory("Reporting"): self.reporting_plugins[plugin_info.plugin_object.get_name( )] = plugin_info.plugin_object self.config: Optional[Config] = None self.exceptions: Optional[Exceptions] = None
def __init__(self, user_paths): """Initialize Statick.""" self.resources = Resources(user_paths) self.manager = PluginManager() self.manager.setPluginPlaces(self.resources.get_plugin_paths()) self.manager.setCategoriesFilter({ "Discovery": DiscoveryPlugin, "Tool": ToolPlugin, "Reporting": ReportingPlugin }) self.manager.collectPlugins() self.discovery_plugins = {} for plugin_info in self.manager.getPluginsOfCategory("Discovery"): self.discovery_plugins[plugin_info.plugin_object.get_name()] = \ plugin_info.plugin_object self.tool_plugins = {} for plugin_info in self.manager.getPluginsOfCategory("Tool"): self.tool_plugins[plugin_info.plugin_object.get_name()] = \ plugin_info.plugin_object self.reporting_plugins = {} for plugin_info in self.manager.getPluginsOfCategory("Reporting"): self.reporting_plugins[plugin_info.plugin_object.get_name()] = \ plugin_info.plugin_object self.config = [] self.exceptions = []
def setup_clang_format_tool_plugin(use_plugin_context=True, binary=None, do_raise=False): """Initialize and return an instance of the clang-format plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--clang-format-bin", dest="clang_format_bin") if do_raise: arg_parser.add_argument( "--clang-format-raise-exception", dest="clang_format_raise_exception", action="store_false", ) else: arg_parser.add_argument( "--clang-format-raise-exception", dest="clang_format_raise_exception", action="store_true", ) resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) cftp = ClangFormatToolPlugin() if binary is not None: plugin_context.args.clang_format_bin = binary if use_plugin_context: cftp.set_plugin_context(plugin_context) return cftp
def test_tool_dependencies(): """Verify that dependencies are reported correctly.""" arg_parser = argparse.ArgumentParser() resources = Resources([os.path.join(os.path.dirname(__file__), 'user_flags_config')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) tp = ToolPlugin() tp.set_plugin_context(plugin_context) assert tp.get_tool_dependencies() == []
def test_resources_get_file_doesnt_exist(): """ Test get_file where the file doesn't exist Expected results: None is returned """ with TemporaryDirectory() as tmp_dir: resources = Resources([tmp_dir]) os.mkdir(os.path.join(tmp_dir, "rsc")) assert resources.get_file("nope") is None
def test_tool_plugin_get_user_flags_invalid_level(): """Test that we return an empty list for invalid levels.""" arg_parser = argparse.ArgumentParser() resources = Resources([os.path.join(os.path.dirname(__file__), 'user_flags_config')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) tp = ToolPlugin() tp.set_plugin_context(plugin_context) flags = tp.get_user_flags('level2', name='test') assert flags == []
def test_resources_get_file_exists(): """ Test get_file where the file exists. Expected results: The file path is returned """ with TemporaryDirectory() as tmp_dir: resources = Resources([tmp_dir]) os.mkdir(os.path.join(tmp_dir, 'rsc')) with tempfile.NamedTemporaryFile(dir=os.path.join(tmp_dir, 'rsc')) as tmpfile: assert resources.get_file(tmpfile.name) == os.path.join(tmp_dir, 'rsc', tmpfile.name)
def test_resources_get_plugin_paths_dirs_dont_exist(): """ Test get_plugin_paths where some dirs don't exist as expected Expected results: get_plugin_paths should contain only the default dir with '/plugins' appended """ with TemporaryDirectory() as tmp_dir: resources = Resources([tmp_dir]) plugin_paths = resources.get_plugin_paths() assert plugin_paths[0] == os.path.join( os.path.dirname(statick_tool.resources.__file__), "plugins")
def test_tool_plugin_get_user_flags_valid_flags(): """Test that we return a list of user flags.""" arg_parser = argparse.ArgumentParser() resources = Resources( [os.path.join(os.path.dirname(__file__), "user_flags_config")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) tp = ToolPlugin() tp.set_plugin_context(plugin_context) flags = tp.get_user_flags("level", name="test") assert flags == ["look", "a", "flag"]
def setup_yamllint_tool_plugin(): """Construct and return an instance of the YAMLLint plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_true", help="Show tool output") resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) yltp = YamllintToolPlugin() yltp.set_plugin_context(plugin_context) return yltp
def setup_pydocstyle_tool_plugin(): """Initialize and return a PyCodeStyle plugin.""" arg_parser = argparse.ArgumentParser() resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) pdstp = PydocstyleToolPlugin() pdstp.set_plugin_context(plugin_context) return pdstp
def setup_cpplint_tool_plugin(): """Initialize and return an instance of the cpplint plugin.""" arg_parser = argparse.ArgumentParser() resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")] ) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) ctp = CpplintToolPlugin() ctp.set_plugin_context(plugin_context) return ctp
def setup_mypy_tool_plugin(): """Create and return an instance of the Mypy plugin.""" arg_parser = argparse.ArgumentParser() resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")] ) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) mtp = MypyToolPlugin() mtp.set_plugin_context(plugin_context) return mtp
def setup_jshint_tool_plugin(): """Initialize and return an instance of the jshint plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output") resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins'), os.path.join(os.path.dirname(__file__), 'valid_package')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin = JSHintToolPlugin() plugin.set_plugin_context(plugin_context) return plugin
def setup_json_reporting_plugin(file_path, use_plugin_context=True): """Create an instance of the file writer plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("output_directory") resources = Resources([os.path.join(os.path.dirname(__file__), "config")]) config = Config(resources.get_file("config.yaml")) jrp = JsonReportingPlugin() if use_plugin_context: plugin_context = PluginContext(arg_parser.parse_args([file_path]), resources, config) jrp.set_plugin_context(plugin_context) return jrp
def setup_json_reporting_plugin(): """Create an instance of the xmllint plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("output_directory") arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_true", help="Show tool output") resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([os.getcwd()]), resources, config) jrarp = JSONRiskAssessmentReportingPlugin() jrarp.set_plugin_context(plugin_context) return jrarp
def test_resources_get_plugin_paths_dirs_exist(): """ Test get_plugin_paths where all dirs exist as expected Expected results: get_plugin_paths should contain all of resources.paths with '/plugins' appended """ with TemporaryDirectory() as tmp_dir: resources = Resources([tmp_dir]) os.mkdir(os.path.join(tmp_dir, "plugins")) plugin_paths = resources.get_plugin_paths() assert plugin_paths[0] == os.path.join(tmp_dir, "plugins") assert plugin_paths[1] == os.path.join( os.path.dirname(statick_tool.resources.__file__), "plugins")
def setup_cmake_discovery_plugin(): """Create an instance of the CMake discovery plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output") resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) cmdp = CMakeDiscoveryPlugin() cmdp.set_plugin_context(plugin_context) return cmdp
def setup_pyflakes_tool_plugin(): """Initialize and return a Pyflakes plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output") resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) pftp = PyflakesToolPlugin() pftp.set_plugin_context(plugin_context) return pftp
def setup_write_jenkins_warnings_ng_reporting_plugin(file_path, use_plugin_context=True): """Create an instance of the file writer plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("output_directory") resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) wfrp = WriteJenkinsWarningsNGReportingPlugin() if use_plugin_context: plugin_context = PluginContext(arg_parser.parse_args([file_path]), resources, config) wfrp.set_plugin_context(plugin_context) return wfrp
def setup_cmake_discovery_plugin(add_plugin_context=True, cmake_flags=""): """Create an instance of the CMake discovery plugin.""" arg_parser = argparse.ArgumentParser() resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) cmdp = CMakeDiscoveryPlugin() if add_plugin_context: plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) plugin_context.args.cmake_flags = cmake_flags cmdp.set_plugin_context(plugin_context) return cmdp
def setup_make_tool_plugin(): """Construct and return an instance of the Make plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--mapping-file-suffix", dest="mapping_file_suffix", type=str) resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) mtp = MakeToolPlugin() mtp.set_plugin_context(plugin_context) return mtp
def setup_shellcheck_tool_plugin(binary=None): """Construct and return an instance of the Shellcheck plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--shellcheck-bin", dest="shellcheck_bin") resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) sctp = ShellcheckToolPlugin() if binary: plugin_context.args.shellcheck_bin = binary sctp.set_plugin_context(plugin_context) return sctp
def setup_spotbugs_tool_plugin(): """Initialize and return a spotbugs plugin.""" arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--show-tool-output", dest="show_tool_output", action="store_false", help="Show tool output") arg_parser.add_argument('--mapping-file-suffix', dest="mapping_file_suffix", type=str) resources = Resources([os.path.join(os.path.dirname(statick_tool.__file__), 'plugins')]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) sbtp = SpotbugsToolPlugin() sbtp.set_plugin_context(plugin_context) return sbtp
def setup_lizard_tool_plugin(custom_rsc_path=None): """Initialize and return an instance of the lizard plugin.""" arg_parser = argparse.ArgumentParser() if custom_rsc_path is not None: resources = Resources([custom_rsc_path]) else: resources = Resources( [os.path.join(os.path.dirname(statick_tool.__file__), "plugins")]) config = Config(resources.get_file("config.yaml")) plugin_context = PluginContext(arg_parser.parse_args([]), resources, config) plugin_context.args.output_directory = os.path.dirname(__file__) ltp = LizardToolPlugin() ltp.set_plugin_context(plugin_context) return ltp