diff options
author | Ganesh Kathiresan <ganesh3597@gmail.com> | 2021-02-16 13:20:59 +0530 |
---|---|---|
committer | Ganesh Kathiresan <ganesh3597@gmail.com> | 2021-03-07 11:18:23 +0530 |
commit | 0e1fea2fb3cf33b25f57e71c4390a01d1c134780 (patch) | |
tree | faafc38e97481462808a884cb2475310a3f2b888 | |
parent | 0bcf690951899608db2ab8aa6aebebbe6723b307 (diff) | |
download | numpy-0e1fea2fb3cf33b25f57e71c4390a01d1c134780.tar.gz |
ENH: Added Linter script
-rw-r--r-- | tools/lint_diff.ini | 5 | ||||
-rw-r--r-- | tools/linter.py | 52 |
2 files changed, 57 insertions, 0 deletions
diff --git a/tools/lint_diff.ini b/tools/lint_diff.ini new file mode 100644 index 000000000..ba091468e --- /dev/null +++ b/tools/lint_diff.ini @@ -0,0 +1,5 @@ +[pycodestyle] +max_line_length = 79 +statistics = True +ignore = E121,E122,E123,E125,E126,E127,E128,E226,E251,E265,E266,E302,E402,E712,E721,E731,E741,W291,W293,W391,W503,W504 +exclude = numpy/__config__.py diff --git a/tools/linter.py b/tools/linter.py new file mode 100644 index 000000000..f0c2b7927 --- /dev/null +++ b/tools/linter.py @@ -0,0 +1,52 @@ +import os +import sys +import subprocess +from argparse import ArgumentParser +from git import Repo + +BASE_BRANCH = 'master' +CONFIG = os.path.join( + os.path.abspath(os.path.dirname(__file__)), + 'lint_diff.ini', +) + + +class DiffLinter: + def __init__(self, branch): + self.branch = branch + self.repo = Repo('.') + + def get_branch_diff(self): + commit = self.repo.merge_base(BASE_BRANCH, self.branch)[0] + diff = self.repo.git.diff(commit, self.branch, '*.py') + return diff + + def run_pycodestyle(self, diff): + """ + Original Author: Josh Wilson (@person142) + Source: https://github.com/scipy/scipy/blob/master/tools/lint_diff.py + Run pycodestyle on the given diff. + """ + res = subprocess.run( + ['pycodestyle', '--diff', '--config', CONFIG], + input=diff, + stdout=subprocess.PIPE, + encoding='utf-8', + ) + return res.returncode, res.stdout + + def run_lint(self): + diff = self.get_branch_diff() + retcode, errors = self.run_pycodestyle(diff) + + errors and print(errors) + + sys.exit(retcode) + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument("--branch", type=str, default='master', + help="The branch to diff against") + args = parser.parse_args() + + DiffLinter(args.branch).run_lint() |