"""Diff based code style checker with flake8This code come from:https://github.com/scipy/scipy/blob/main/tools/lint_diff.pyScipy's licence: https://github.com/scipy/scipy/blob/main/LICENSE.txtCopyright (c) 2001-2002 Enthought, Inc. 2003-2022, SciPy Developers.All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the abovecopyright notice, this list of conditions and the followingdisclaimer in the documentation and/or other materials providedwith the distribution.3. Neither the name of the copyright holder nor the names of itscontributors may be used to endorse or promote products derivedfrom this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""import conftestimport subprocessdef rev_list(branch, num_commits):"""List commits in reverse chronological order.Only the first `num_commits` are shown."""res = subprocess.run(['git','rev-list','--max-count',f'{num_commits}','--first-parent',branch],stdout=subprocess.PIPE,encoding='utf-8',)res.check_returncode()return res.stdout.rstrip('\n').split('\n')def find_branch_point(branch):"""Find when the current branch split off from the given branch.It is based off of this Stackoverflow post:https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git#4991675"""branch_commits = rev_list('HEAD', 1000)main_commits = set(rev_list(branch, 1000))for branch_commit in branch_commits:if branch_commit in main_commits:return branch_commit# If a branch split off over 1000 commits ago we will fail to find# the ancestor.raise RuntimeError('Failed to find a common ancestor in the last 1000 commits')def find_diff(sha):"""Find the diff since the given sha."""files = ['*.py']res = subprocess.run(['git', 'diff', '--unified=0', sha, '--'] + files,stdout=subprocess.PIPE,encoding='utf-8')res.check_returncode()return res.stdoutdef run_flake8(diff):"""Run flake8 on the given diff."""res = subprocess.run(['flake8', '--diff'],input=diff,stdout=subprocess.PIPE,encoding='utf-8',)return res.returncode, res.stdoutdef test():branch_commit = find_branch_point("origin/master")diff = find_diff(branch_commit)rc, errors = run_flake8(diff)if errors:print(errors)else:print("No lint errors found.")assert rc == 0if __name__ == '__main__':conftest.run_this_test(__file__)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。