Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gitlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@

import docopt
import termcolor
if sys.platform == "win32":
import colorama
colorama.init()
import yaml

import gitlint.git as git
Expand Down
7 changes: 3 additions & 4 deletions gitlint/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def modified_files(root, tracked_only=False, commit=None):
status_lines = subprocess.check_output([
'git', 'status', '--porcelain', '--untracked-files=all',
'--ignore-submodules=all'
]).decode('utf-8').split(os.linesep)
]).decode('utf-8').splitlines()

modes = ['M ', ' M', 'A ', 'AM', 'MM']
if not tracked_only:
Expand All @@ -93,7 +93,7 @@ def _modified_files_with_commit(root, commit):
status_lines = subprocess.check_output([
'git', 'diff-tree', '-r', '--root', '--no-commit-id', '--name-status',
commit
]).decode('utf-8').split(os.linesep)
]).decode('utf-8').splitlines()

modified_file_status = utils.filter_lines(
status_lines,
Expand Down Expand Up @@ -132,8 +132,7 @@ def modified_lines(filename, extra_data, commit=None):

# Split as bytes, as the output may have some non unicode characters.
blame_lines = subprocess.check_output(
['git', 'blame', '--porcelain', filename]).split(
os.linesep.encode('utf-8'))
['git', 'blame', '--porcelain', filename]).splitlines()
modified_line_numbers = utils.filter_lines(
blame_lines, commit + br' (?P<line>\d+) (\d+)', groups=('line', ))

Expand Down
7 changes: 3 additions & 4 deletions gitlint/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def modified_files(root, tracked_only=False, commit=None):
command.append('--change=%s' % commit)

# Convert to unicode and split
status_lines = subprocess.check_output(command).decode('utf-8').split(
os.linesep)
status_lines = subprocess.check_output(command).decode(
'utf-8').splitlines()

modes = ['M', 'A']
if not tracked_only:
Expand Down Expand Up @@ -105,8 +105,7 @@ def modified_lines(filename, extra_data, commit=None):
command.append(filename)

# Split as bytes, as the output may have some non unicode characters.
diff_lines = subprocess.check_output(command).split(
os.linesep.encode('utf-8'))
diff_lines = subprocess.check_output(command).splitlines()
diff_line_numbers = utils.filter_lines(
diff_lines,
br'@@ -\d+,\d+ \+(?P<start_line>\d+),(?P<lines>\d+) @@',
Expand Down
2 changes: 1 addition & 1 deletion gitlint/linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def lint_command(name, program, arguments, filter_regex, filename, lines):
output = output.decode('utf-8')
utils.save_output_in_cache(name, filename, output)

output_lines = output.split(os.linesep)
output_lines = output.splitlines()

if lines is None:
lines_regex = r'\d+'
Expand Down
86 changes: 69 additions & 17 deletions gitlint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,72 @@
import pathlib2 as pathlib


# copy from python3, shutil.which
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which version of python was used? Which commit?

Probably is better to depend on a backported version of which like. https://pypi.org/project/backports.shutil_which/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience, I will improve this PR later。

def which(cmd, mode=os.F_OK | os.X_OK, path=None):
"""Given a command, mode, and a PATH string, return the path which
conforms to the given mode on the PATH, or None if there is no such
file.

`mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
of os.environ.get("PATH"), or can be overridden with a custom search
path.

"""

# Check that a given file can be accessed with the correct mode.
# Additionally check that `file` is not a directory, as on Windows
# directories pass the os.access check.
def _access_check(filename, mode):
return (os.path.exists(filename) and os.access(filename, mode)
and not os.path.isdir(filename))

# If we're given a path with a directory part, look it up directly rather
# than referring to PATH directories. This includes checking relative to the
# current directory, e.g. ./script
if os.path.dirname(cmd):
if _access_check(cmd, mode):
return cmd
return None

if path is None:
path = os.environ.get("PATH", os.defpath)
if not path:
return None
path = path.split(os.pathsep)

import sys
if sys.platform == "win32":
# The current directory takes precedence on Windows.
if os.curdir not in path:
path.insert(0, os.curdir)

# PATHEXT is necessary to check on Windows.
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
# See if the given file matches any of the expected path extensions.
# This will allow us to short circuit when given "python.exe".
# If it does match, only test that one, otherwise we have to try
# others.
if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
files = [cmd]
else:
files = [cmd + ext for ext in pathext]
else:
# On other platforms you don't have things like PATHEXT to tell you
# what file suffixes are executable, so just pass on cmd as-is.
files = [cmd]

seen = set()
for dirname in path:
normdir = os.path.normcase(dirname)
if not normdir in seen:
seen.add(normdir)
for thefile in files:
name = os.path.join(dirname, thefile)
if _access_check(name, mode):
return name
return None


def filter_lines(lines, filter_regex, groups=None):
"""Filters out the lines not matching the pattern.

Expand All @@ -43,22 +109,6 @@ def filter_lines(lines, filter_regex, groups=None):
yield tuple(matched_groups.get(group) for group in groups)


# TODO(skreft): add test
def which(program):
"""Returns a list of paths where the program is found."""
if (os.path.isabs(program) and os.path.isfile(program)
and os.access(program, os.X_OK)):
return [program]

candidates = []
locations = os.environ.get("PATH").split(os.pathsep)
for location in locations:
candidate = os.path.join(location, program)
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
candidates.append(candidate)
return candidates


def programs_not_in_path(programs):
"""Returns all the programs that are not found in the PATH."""
return [program for program in programs if not which(program)]
Expand All @@ -74,7 +124,9 @@ def _open_for_write(filename):

def _get_cache_filename(name, filename):
"""Returns the cache location for filename and linter name."""
filename = os.path.abspath(filename)[1:]
filename = os.path.abspath(filename)
filename = os.path.splitdrive(filename)[1]
filename = filename.lstrip(os.path.sep)
home_folder = os.path.expanduser('~')
base_cache_dir = os.path.join(home_folder, '.git-lint', 'cache')

Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
termcolor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Termcolor and colorama are not dev dependencies. But as said above, colorama should only be required for windows.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be removed an instead add the dependencies to the setup file. But colorama should only be required for windows.

See https://hynek.me/articles/conditional-python-dependencies/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i will change them to use setup.py, declaring-platform-specific-dependencies

colorama
nose
docopt
pyfakefs
pyyaml
pathlib2
2 changes: 1 addition & 1 deletion test/unittest/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ def test_which_absolute_path(self):
self.fs.create_file(filename)
os.chmod(filename, 0o755)

self.assertEqual([filename], utils.which(filename))
self.assertEqual(filename, utils.which(filename))