| 21 | |
| 22 | |
| 23 | class DiffLinter: |
| 24 | def __init__(self, branch): |
| 25 | self.branch = branch |
| 26 | self.repo = Repo('.') |
| 27 | self.head = self.repo.head.commit |
| 28 | |
| 29 | def get_branch_diff(self, uncommitted = False): |
| 30 | """ |
| 31 | Determine the first common ancestor commit. |
| 32 | Find diff between branch and FCA commit. |
| 33 | Note: if `uncommitted` is set, check only |
| 34 | uncommitted changes |
| 35 | """ |
| 36 | try: |
| 37 | commit = self.repo.merge_base(self.branch, self.head)[0] |
| 38 | except exc.GitCommandError: |
| 39 | print(f"Branch with name `{self.branch}` does not exist") |
| 40 | sys.exit(1) |
| 41 | |
| 42 | exclude = [f':(exclude){i}' for i in EXCLUDE] |
| 43 | if uncommitted: |
| 44 | diff = self.repo.git.diff( |
| 45 | self.head, '--unified=0', '***.py', *exclude |
| 46 | ) |
| 47 | else: |
| 48 | diff = self.repo.git.diff( |
| 49 | commit, self.head, '--unified=0', '***.py', *exclude |
| 50 | ) |
| 51 | return diff |
| 52 | |
| 53 | def run_pycodestyle(self, diff): |
| 54 | """ |
| 55 | Original Author: Josh Wilson (@person142) |
| 56 | Source: |
| 57 | https://github.com/scipy/scipy/blob/main/tools/lint_diff.py |
| 58 | Run pycodestyle on the given diff. |
| 59 | """ |
| 60 | res = subprocess.run( |
| 61 | ['pycodestyle', '--diff', '--config', CONFIG], |
| 62 | input=diff, |
| 63 | stdout=subprocess.PIPE, |
| 64 | encoding='utf-8', |
| 65 | ) |
| 66 | return res.returncode, res.stdout |
| 67 | |
| 68 | def run_lint(self, uncommitted): |
| 69 | diff = self.get_branch_diff(uncommitted) |
| 70 | retcode, errors = self.run_pycodestyle(diff) |
| 71 | |
| 72 | errors and print(errors) |
| 73 | |
| 74 | sys.exit(retcode) |
| 75 | |
| 76 | |
| 77 | if __name__ == '__main__': |
no outgoing calls