mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-15 00:44:44 +00:00
remove encoding/decoding of staged files
This commit is contained in:
parent
b8bd62dbc2
commit
06256daf3a
1 changed files with 4 additions and 23 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
import re
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import stat
|
import stat
|
||||||
|
|
@ -53,22 +52,11 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
|
||||||
# Copy staged versions to temporary directory
|
# Copy staged versions to temporary directory
|
||||||
tmpdir = mkdtemp()
|
tmpdir = mkdtemp()
|
||||||
files_to_check = []
|
files_to_check = []
|
||||||
coding_pattern = r'coding[=:]\s*([-\w.]+)'
|
|
||||||
try:
|
try:
|
||||||
for file_ in files_modified:
|
for file_ in files_modified:
|
||||||
# try to detect encoding of python file according to PEP 0263
|
|
||||||
with open(file_) as fh:
|
|
||||||
for i_ in xrange(2):
|
|
||||||
line = fh.readline()
|
|
||||||
match = re.search(coding_pattern, line)
|
|
||||||
if match:
|
|
||||||
coding = match.groups()[0]
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
coding = None
|
|
||||||
# get the staged version of the file
|
# get the staged version of the file
|
||||||
gitcmd_getstaged = ["git", "show", ":%s" % file_]
|
gitcmd_getstaged = ["git", "show", ":%s" % file_]
|
||||||
_, out, _ = run(gitcmd_getstaged, raw_output=True, coding=coding)
|
_, out, _ = run(gitcmd_getstaged, raw_output=True, decode=False)
|
||||||
# write the staged version to temp dir with its full path to
|
# write the staged version to temp dir with its full path to
|
||||||
# avoid overwriting files with the same name
|
# avoid overwriting files with the same name
|
||||||
dirname, filename = os.path.split(os.path.abspath(file_))
|
dirname, filename = os.path.split(os.path.abspath(file_))
|
||||||
|
|
@ -78,9 +66,6 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
|
||||||
if not os.path.isdir(dirname):
|
if not os.path.isdir(dirname):
|
||||||
os.makedirs(dirname)
|
os.makedirs(dirname)
|
||||||
filename = os.path.join(dirname, filename)
|
filename = os.path.join(dirname, filename)
|
||||||
# encode for output if we know the encoding
|
|
||||||
if coding:
|
|
||||||
out = out.encode(coding)
|
|
||||||
# write staged version of file to temporary directory
|
# write staged version of file to temporary directory
|
||||||
with open(filename, "wb") as fh:
|
with open(filename, "wb") as fh:
|
||||||
fh.write(out)
|
fh.write(out)
|
||||||
|
|
@ -121,7 +106,7 @@ def hg_hook(ui, repo, **kwargs):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def run(command, raw_output=False, coding=None):
|
def run(command, raw_output=False, decode=True):
|
||||||
if isinstance(command, basestring):
|
if isinstance(command, basestring):
|
||||||
command = command.split()
|
command = command.split()
|
||||||
p = Popen(command, stdout=PIPE, stderr=PIPE)
|
p = Popen(command, stdout=PIPE, stderr=PIPE)
|
||||||
|
|
@ -131,14 +116,10 @@ def run(command, raw_output=False, coding=None):
|
||||||
# string objects. This is simply less mysterious than using b'.py' in the
|
# string objects. This is simply less mysterious than using b'.py' in the
|
||||||
# endswith method. That should work but might still fail horribly.
|
# endswith method. That should work but might still fail horribly.
|
||||||
if hasattr(stdout, 'decode'):
|
if hasattr(stdout, 'decode'):
|
||||||
if coding:
|
if decode:
|
||||||
stdout = stdout.decode(coding)
|
|
||||||
else:
|
|
||||||
stdout = stdout.decode()
|
stdout = stdout.decode()
|
||||||
if hasattr(stderr, 'decode'):
|
if hasattr(stderr, 'decode'):
|
||||||
if coding:
|
if decode:
|
||||||
stderr = stderr.decode(coding)
|
|
||||||
else:
|
|
||||||
stderr = stderr.decode()
|
stderr = stderr.decode()
|
||||||
if not raw_output:
|
if not raw_output:
|
||||||
stdout = [line.strip() for line in stdout.splitlines()]
|
stdout = [line.strip() for line in stdout.splitlines()]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue