def lint_code(self, input_df): """ Lints all files in the data frame :return: """ scores = [] for index, row in input_df.iterrows(): input_file = row['File'] result = Run([input_file], exit=False) scores.append(result.linter.stats['global_note']) input_df['pylint_score'] = scores return input_df
def run_tests(self): from pylint.lint import Run Run([ "graypy", "--persistent", "y", "--rcfile", ".pylintrc", "--output-format", "colorized", ])
def _run(self, path): try: from pylint.lint import Run except ImportError: print('Unable to import pylint', file=sys.stderr) else: args = [path] rcfile = self.root_dir / '.pylintrc' if rcfile.is_file(): args.append('--rcfile={}'.format(rcfile)) error = Run(args, do_exit=False).linter.msg_status if error: sys.exit(error)
def test_pylint_errors(self): """ check if pylint reports errors """ out = DummyReporter() run = Run( ['--errors-only', "--jobs=8", 'standalone', 'server', 'tests'], reporter=out, exit=False) self.assertEqual( run.linter.stats["error"], 0, "%i pylint error found: " "please run `make static-test{2,3}' " "for detail report" % run.linter.stats["error"])
def test_run(tmp_path, name, git_repo): """ Runs pylint against external sources """ checkoutdir = tmp_path / name checkoutdir.mkdir() os.system(f"git clone --depth=1 {git_repo} {checkoutdir}") filepaths = _get_py_files(scanpath=str(checkoutdir)) print("Have %d files" % len(filepaths)) runner = Run(filepaths, reporter=Reporter(), do_exit=False) print("Had %d files with %d messages" % (len(filepaths), len(runner.linter.reporter.messages))) pprint.pprint(runner.linter.reporter.messages)
def main(): """ Main function """ log_file = open("pylint.log", "wt") py_files = [y for x in os.walk("..") for y in glob(os.path.join(x[0], '*.py'))] for py_file in py_files: if "__init__.py" not in py_file: results = Run([py_file], exit=False) log_file.write(("%s: %.2f\n") % (py_file, results.linter.stats['global_note'])) log_file.close()
def _create_pylint_report(package_path): from pylint.lint import Run runner = Run([ "--rcfile=./.pylintrc", "-r", "y", "--output-format=text", package_path, "--exit-zero" ], do_exit=False) res = runner.linter.msg_status print("Pylintreport result:", res) # NOTE: pylint report is not intended to fail return True
def lint(): print "PyLint Report" print "=============" from pylint.lint import Run Run(['--rcfile=pylintrc', 'scrapper'], exit=False) print "Flake8 Report" print "=============" from flake8.engine import get_style_guide flake8_style = get_style_guide(config_file="flake8.cfg", parse_argv=False, paths=["scrapper"]) flake8_style.check_files()
def test_load_plugin_command_line() -> None: dummy_plugin_path = join(REGRTEST_DATA_DIR, "dummy_plugin") sys.path.append(dummy_plugin_path) run = Run( ["--load-plugins", "dummy_plugin", join(REGRTEST_DATA_DIR, "empty.py")], exit=False, ) assert ( len([ch.name for ch in run.linter.get_checkers() if ch.name == "dummy_plugin"]) == 2 ) sys.path.remove(dummy_plugin_path)
def test_coding(self): from pylint.lint import Run from pylint.reporters.text import TextReporter reporter = QualityAssuranceCodingStyle.SilentReporter() Run([("--rcfile=%s" % self.style), self.src_basepath], reporter=reporter, exit=False) if len(reporter.msg): print for msg in reporter.msg: print ' '.join(msg) self.assertTrue(len(reporter.msg) == 0)
def Pylint(params, output_filename): from pylint.lint import Run os.chdir('source/python') sys_stdout = sys.stdout sys.stdout = file(output_filename, 'w') try: Run(params.split()) except SystemExit as e: assert e.code == 0, "Expecting a successful run of pylint" finally: sys.stdout.close() sys.stdout = sys_stdout
def _runner(args): try: output = _WritableObject() stdout_ = sys.stderr stream = StringIO() sys.stderr = stream Run(args, reporter=JSONReporter(output), exit=False) finally: sys.stderr = stdout_ try: output = "".join(output.content) return json.loads(output) except ValueError: return []
def test_lint_package(self): filter = '--disable={rules}'\ .format(rules=','.join([ 'fixme', 'missing-docstring', 'too-many-lines', 'inconsistent-return-statements', 'invalid-name', ])) with self.assertRaises(SystemExit) as lint_check: Run([filter, 'falcon_redis_cache']) self.assertEqual(lint_check.exception.code, 0)
def run_tests(self): from pylint.lint import Run if (__name__ == "__main__" ): # this is needed for windows not to raise a RuntimeError Run([ "recumpiler", "--persistent", "y", "--rcfile", ".pylintrc", "--output-format", "colorized", ])
def test_load_plugin_command_line(): dummy_plugin_path = join(HERE, "regrtest_data", "dummy_plugin") sys.path.append(dummy_plugin_path) run = Run( ["--load-plugins", "dummy_plugin", join(HERE, "regrtest_data", "empty.py")], do_exit=False, ) assert ( len([ch.name for ch in run.linter.get_checkers() if ch.name == "dummy_plugin"]) == 2 ) sys.path.remove(dummy_plugin_path)
def test_with_plugin(capsys, error): try: Run([ "tests/example.py", "--errors-only", "--exit-zero", "--load-plugins", "pylint_flask_sqlalchemy", ]) except SystemExit: pass captured = capsys.readouterr() for message in captured.out.split("\n"): assert error not in message
def main(): parser = argparse.ArgumentParser(description="Write all todo items as rst") parser.add_argument("input", help="Path to source files") parser.add_argument("output", help="Path to output rst file") args = parser.parse_args() cmd = [ "pylint", "--disable=all", "--enable=fixme", "--exit-zero", "-f", "json", Path(args.input).name, ] cwd = os.getcwd() os.chdir(Path(args.input).parent) try: with redirect() as stdout: Run(cmd, exit=False) result = json.loads(stdout.getvalue()) finally: os.chdir(cwd) todos = defaultdict(list) for item in result: # Remove given search path from module path name = ".".join(item["module"].split(".")[1:]) message = todo_msg(item["message"]) todos[name].append({"message": todo_msg(item["message"]), "line": item["line"]}) output = ["****", "TODO", "****", ""] for module, items in sorted(todos.items()): items.sort(key=lambda item: item["line"]) output.append(f"{module}:") output.append("=" * (len(module) + 1)) output.append("") output.append(".. csv-table::") output.append(" :header: \"Line\", \"Message\"") output.append(" :widths: 10, 40") output.append("") for item in items: output.append(" \"{line}\", \"{message}\"".format(**item)) output.append("") with open(args.output, "w") as outfile: outfile.write("\n".join(output))
def dump_checks(self): """ Dumps a list of the checks in this linter to the terminal. :return: formatted list of the checkers in the linter :rtype: str """ print("Checkers in Surround's linter") print("=============================") try: Run(['--list-msgs-enabled']) except SystemExit: pass
def test_load_plugin_config_file(): dummy_plugin_path = join(HERE, "regrtest_data", "dummy_plugin") sys.path.append(dummy_plugin_path) config_path = join(HERE, "regrtest_data", "dummy_plugin.rc") run = Run( ["--rcfile", config_path, join(HERE, "regrtest_data", "empty.py")], do_exit=False, ) assert ( len([ch.name for ch in run.linter.get_checkers() if ch.name == "dummy_plugin"]) == 2 ) sys.path.remove(dummy_plugin_path)
def run(self): """ Gathers files and runs the linter :raises AssertionError: project not 10.0 """ self._walk_dir(self.project_path) if not self.rc_file: self.args.append('--rcfile={}'.format(self.rc_file)) self.args.extend(self.files) lint = Run(self.args, reporter=self.reporter, exit=self.exit) score = lint.linter.stats['global_note'] assert score == 10.0, 'Imperfect score of {:0.2f}'.format(score)
def _runtest(self, args, reporter=None, out=None, code=28): if out is None: out = StringIO() if args and args[-1].startswith('pylint.lint'): try: import cProfile, pstats except ImportError: code += 1 try: sys.stderr = sys.stdout = out try: Run(args, reporter=reporter) except SystemExit, ex: self.assertEqual(ex.code, code) else:
def test_load_plugin_config_file() -> None: dummy_plugin_path = join(REGRTEST_DATA_DIR, "dummy_plugin") sys.path.append(dummy_plugin_path) config_path = join(REGRTEST_DATA_DIR, "dummy_plugin.rc") run = Run( ["--rcfile", config_path, join(REGRTEST_DATA_DIR, "empty.py")], exit=False, ) assert ( len([ch.name for ch in run.linter.get_checkers() if ch.name == "dummy_plugin"]) == 2 ) sys.path.remove(dummy_plugin_path)
def test_load_plugin_configuration(): dummy_plugin_path = join(REGRTEST_DATA_DIR, "dummy_plugin") sys.path.append(dummy_plugin_path) run = Run( [ "--load-plugins", "dummy_conf_plugin", "--ignore", "foo,bar", join(REGRTEST_DATA_DIR, "empty.py"), ], exit=False, ) assert run.linter.config.black_list == ["foo", "bar", "bin"]
def run_pylint(self, path): results = Run([ '--load-plugins=pylint_django', '--rcfile=pylintrc_test_cases', path ], do_exit=False) if results.linter.stats['error'] > ERROR_COUNT or results.linter.stats['convention'] > CONVENTION_COUNT or \ results.linter.stats['warning'] > WARNINGS: print( "Codebase has failed set standards, Please correct above mentioned issues," "Current Score is: %s, Errors: %s, Convention issues: %s, Warnings: %s" % (results.linter.stats['global_note'], results.linter.stats['error'], results.linter.stats['convention'], results.linter.stats['warning']))
def run_pylint(rc_path, files, error): """Run pylint using rcfile on files. @param rc_path: The path to rc file. @param files: The files to run pylint. @param error: Output error only. """ logging.debug('run pylint on %s with rcfile %s', pprint.pformat(files), rc_path) arguments = [] if error: arguments = ['-E'] else: arguments = ['--rcfile', rc_path] arguments += files Run(arguments)
def test_lint(self): """ test lint """ for filename in Path('greencandle').rglob('*.py'): file = open(str(filename)) count = len(file.readlines()) file.close() if 'test' not in str(filename) and count > 5: results = Run([str(filename)], do_exit=False) try: print(filename) score = results.linter.stats['global_note'] self.assertGreaterEqual(score, 9.7) except KeyError: pass
def run_pylint(filename): """ Runs pylint on a given file :param filename: :return: list of pylint errors """ ARGS = ['-r', 'n'] pylint_output = WritableObject() Run([filename] + ARGS, reporter=TextReporter(pylint_output), exit=False) lines = [] for line in pylint_output.read(): if not line.startswith('*') and line != '\n': lines.append(line) return lines
def main(): """Perform command line operations. Parse command line arguments for a fail-under value, execute Pylint, and determine if the resulting score is greater than or equal to the fail-under value. """ exit_code = SUCCESS cmd_line_args = list(sys.argv[1:]) logger = logging.getLogger(__name__) if cmd_line_args: logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) if "--fail_under" in cmd_line_args: # If "--fail_under" appears in command line arguments, extract the value and remove the # argument/value elements from the argument list that is passed to Pylint. fail_under_index = cmd_line_args.index("--fail_under") fail_under_value = float(cmd_line_args[fail_under_index + 1]) del cmd_line_args[fail_under_index:fail_under_index + 2] else: logger.warning( "no fail_under argument provided, defaulting to 10.0") fail_under_value = 10.0 results = Run(args=cmd_line_args, do_exit=False) sys.stdout.flush() try: score = results.linter.stats["global_note"] except KeyError: logger.error("no score parsed from Pylint output") exit_code = NO_SCORE_PARSED else: if score < fail_under_value: logger.error("score %s is less than fail-under value %s", score, fail_under_value) exit_code = SCORE_TOO_LOW else: print( "usage: pylint-fail-under [--fail_under SCORE] [Pylint Command Line Arguments]" ) return exit_code
def run_using_a_configuration_file( configuration_path: Union[Path, str], file_to_lint: str = __file__) -> Tuple[Mock, Mock, Run]: """Simulate a run with a configuration without really launching the checks.""" configuration_path = str(configuration_path) args = ["--rcfile", configuration_path, file_to_lint] # We do not capture the `SystemExit` as then the `runner` variable # would not be accessible outside the `with` block. with unittest.mock.patch("sys.exit") as mocked_exit: # Do not actually run checks, that could be slow. We don't mock # `Pylinter.check`: it calls `Pylinter.initialize` which is # needed to properly set up messages inclusion/exclusion # in `_msg_states`, used by `is_message_enabled`. check = "pylint.lint.pylinter.check_parallel" with unittest.mock.patch(check) as mocked_check_parallel: runner = Run(args) return mocked_exit, mocked_check_parallel, runner
def main(): """ Script entry point. Return exit code. """ args, include_pattern, exclude_pattern = probe_args() if "-h" in args or "--help" in args: print_line(__doc__) return 0 if os.getenv("RUN_PYLINT_DISABLED", "") != "": return 0 files = probe_dir(os.getcwd(), re.compile(include_pattern), re.compile(exclude_pattern)) show_files(files) args.extend(files) sys.argv[0] = "pylint" return Run(args, None, False).linter.msg_status