mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-09 22:34:17 +00:00
make git_hook check staged versions of files instead of local version
copies all staged versions of files to check in a temporary folder and runs checks on this folder.
This commit is contained in:
parent
762bb68968
commit
74c6958e57
1 changed files with 37 additions and 7 deletions
|
|
@ -4,6 +4,8 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import stat
|
import stat
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
import shutil
|
||||||
|
from tempfile import mkdtemp
|
||||||
try:
|
try:
|
||||||
# The 'demandimport' breaks pyflakes and flake8._pyflakes
|
# The 'demandimport' breaks pyflakes and flake8._pyflakes
|
||||||
from mercurial import demandimport
|
from mercurial import demandimport
|
||||||
|
|
@ -42,12 +44,36 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
|
||||||
|
|
||||||
# Returns the exit code, list of files modified, list of error messages
|
# Returns the exit code, list of files modified, list of error messages
|
||||||
_, files_modified, _ = run(gitcmd)
|
_, files_modified, _ = run(gitcmd)
|
||||||
|
files_modified = [f for f in files_modified if f.endswith('.py')]
|
||||||
|
|
||||||
# Run the checks
|
|
||||||
flake8_style = get_style_guide(
|
flake8_style = get_style_guide(
|
||||||
config_file=DEFAULT_CONFIG, ignore=ignore, max_complexity=complexity)
|
config_file=DEFAULT_CONFIG, ignore=ignore, max_complexity=complexity)
|
||||||
report = flake8_style.check_files([f for f in files_modified if
|
|
||||||
f.endswith('.py')])
|
# Copy staged versions to temporary directory
|
||||||
|
tmpdir = mkdtemp()
|
||||||
|
files_to_check = []
|
||||||
|
try:
|
||||||
|
for file_ in files_modified:
|
||||||
|
# get the staged version of the file
|
||||||
|
gitcmd_getstaged = ["git", "show", ":%s" % file_]
|
||||||
|
_, out, _ = run(gitcmd_getstaged, raw_output=True)
|
||||||
|
# write the staged version to temp dir with its full path to
|
||||||
|
# avoid overwriting files with the same name
|
||||||
|
dirname, filename = os.path.split(os.path.abspath(file_))
|
||||||
|
prefix = os.path.commonprefix([dirname, tmpdir])
|
||||||
|
print dirname, tmpdir, prefix
|
||||||
|
dirname = os.path.relpath(dirname, start=prefix)
|
||||||
|
dirname = os.path.join(tmpdir, dirname)
|
||||||
|
if not os.path.isdir(dirname):
|
||||||
|
os.makedirs(dirname)
|
||||||
|
filename = os.path.join(dirname, filename)
|
||||||
|
with open(filename, "wb") as fh:
|
||||||
|
fh.write(out)
|
||||||
|
files_to_check.append(filename)
|
||||||
|
# Run the checks
|
||||||
|
report = flake8_style.check_files(files_to_check)
|
||||||
|
finally:
|
||||||
|
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||||
|
|
||||||
if strict:
|
if strict:
|
||||||
return report.total_errors
|
return report.total_errors
|
||||||
|
|
@ -79,8 +105,10 @@ def hg_hook(ui, repo, **kwargs):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def run(command):
|
def run(command, raw_output=False):
|
||||||
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
|
if isinstance(command, basestring):
|
||||||
|
command = command.split()
|
||||||
|
p = Popen(command, stdout=PIPE, stderr=PIPE)
|
||||||
(stdout, stderr) = p.communicate()
|
(stdout, stderr) = p.communicate()
|
||||||
# On python 3, subprocess.Popen returns bytes objects which expect
|
# On python 3, subprocess.Popen returns bytes objects which expect
|
||||||
# endswith to be given a bytes object or a tuple of bytes but not native
|
# endswith to be given a bytes object or a tuple of bytes but not native
|
||||||
|
|
@ -90,8 +118,10 @@ def run(command):
|
||||||
stdout = stdout.decode()
|
stdout = stdout.decode()
|
||||||
if hasattr(stderr, 'decode'):
|
if hasattr(stderr, 'decode'):
|
||||||
stderr = stderr.decode()
|
stderr = stderr.decode()
|
||||||
return (p.returncode, [line.strip() for line in stdout.splitlines()],
|
if not raw_output:
|
||||||
[line.strip() for line in stderr.splitlines()])
|
stdout = [line.strip() for line in stdout.splitlines()]
|
||||||
|
stderr = [line.strip() for line in stderr.splitlines()]
|
||||||
|
return (p.returncode, stdout, stderr)
|
||||||
|
|
||||||
|
|
||||||
def _get_files(repo, **kwargs):
|
def _get_files(repo, **kwargs):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue