|
| 1 | +""" |
| 2 | +Diff based code style checker with flake8 |
| 3 | +
|
| 4 | +This code come from: |
| 5 | +https://github.com/scipy/scipy/blob/main/tools/lint_diff.py |
| 6 | +
|
| 7 | +Scipy's licence: https://github.com/scipy/scipy/blob/main/LICENSE.txt |
| 8 | +Copyright (c) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers. |
| 9 | +All rights reserved. |
| 10 | +
|
| 11 | +Redistribution and use in source and binary forms, with or without |
| 12 | +modification, are permitted provided that the following conditions |
| 13 | +are met: |
| 14 | +
|
| 15 | +1. Redistributions of source code must retain the above copyright |
| 16 | + notice, this list of conditions and the following disclaimer. |
| 17 | +
|
| 18 | +2. Redistributions in binary form must reproduce the above |
| 19 | + copyright notice, this list of conditions and the following |
| 20 | + disclaimer in the documentation and/or other materials provided |
| 21 | + with the distribution. |
| 22 | +
|
| 23 | +3. Neither the name of the copyright holder nor the names of its |
| 24 | + contributors may be used to endorse or promote products derived |
| 25 | + from this software without specific prior written permission. |
| 26 | +
|
| 27 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | +""" |
| 39 | +import conftest |
| 40 | +import subprocess |
| 41 | + |
| 42 | + |
| 43 | +def rev_list(branch, num_commits): |
| 44 | + """List commits in reverse chronological order. |
| 45 | + Only the first `num_commits` are shown. |
| 46 | + """ |
| 47 | + res = subprocess.run( |
| 48 | + [ |
| 49 | + 'git', |
| 50 | + 'rev-list', |
| 51 | + '--max-count', |
| 52 | + f'{num_commits}', |
| 53 | + '--first-parent', |
| 54 | + branch |
| 55 | + ], |
| 56 | + stdout=subprocess.PIPE, |
| 57 | + encoding='utf-8', |
| 58 | + ) |
| 59 | + res.check_returncode() |
| 60 | + return res.stdout.rstrip('\n').split('\n') |
| 61 | + |
| 62 | + |
| 63 | +def find_branch_point(branch): |
| 64 | + """Find when the current branch split off from the given branch. |
| 65 | + It is based off of this Stackoverflow post: |
| 66 | + https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git#4991675 |
| 67 | + """ |
| 68 | + branch_commits = rev_list('HEAD', 1000) |
| 69 | + main_commits = set(rev_list(branch, 1000)) |
| 70 | + for branch_commit in branch_commits: |
| 71 | + if branch_commit in main_commits: |
| 72 | + return branch_commit |
| 73 | + |
| 74 | + # If a branch split off over 1000 commits ago we will fail to find |
| 75 | + # the ancestor. |
| 76 | + raise RuntimeError( |
| 77 | + 'Failed to find a common ancestor in the last 1000 commits') |
| 78 | + |
| 79 | + |
| 80 | +def find_diff(sha): |
| 81 | + """Find the diff since the given sha.""" |
| 82 | + files = ['*.py'] |
| 83 | + res = subprocess.run( |
| 84 | + ['git', 'diff', '--unified=0', sha, '--'] + files, |
| 85 | + stdout=subprocess.PIPE, |
| 86 | + encoding='utf-8' |
| 87 | + ) |
| 88 | + res.check_returncode() |
| 89 | + return res.stdout |
| 90 | + |
| 91 | + |
| 92 | +def run_flake8(diff): |
| 93 | + """Run flake8 on the given diff.""" |
| 94 | + res = subprocess.run( |
| 95 | + ['flake8', '--diff'], |
| 96 | + input=diff, |
| 97 | + stdout=subprocess.PIPE, |
| 98 | + encoding='utf-8', |
| 99 | + ) |
| 100 | + return res.returncode, res.stdout |
| 101 | + |
| 102 | + |
| 103 | +def test(): |
| 104 | + branch_commit = find_branch_point("origin/master") |
| 105 | + diff = find_diff(branch_commit) |
| 106 | + rc, errors = run_flake8(diff) |
| 107 | + if errors: |
| 108 | + print(errors) |
| 109 | + else: |
| 110 | + print("No lint errors found.") |
| 111 | + assert rc == 0 |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + conftest.run_this_test(__file__) |
0 commit comments