git-linter/git_lint/git_lint.py

638 lines
22 KiB
Python
Raw Normal View History

from __future__ import print_function
from functools import reduce
from collections import namedtuple
2016-09-23 22:27:15 +00:00
import getopt
import gettext
2016-09-23 20:11:06 +00:00
import operator
2016-09-23 22:27:15 +00:00
import os
2016-09-23 20:11:06 +00:00
import re
2016-09-23 22:27:15 +00:00
import subprocess
2016-09-23 20:11:06 +00:00
import sys
2016-09-26 18:44:31 +00:00
import pprint
try:
import configparser
except ImportError as e:
import ConfigParser as configparser
2016-09-23 22:27:15 +00:00
2016-09-23 20:11:06 +00:00
_ = gettext.gettext
2016-09-26 18:44:31 +00:00
VERSION = '0.0.4'
NAME = 'git-lint'
OPTIONS_LIST = [
('o', 'only', True,
_('A comma-separated list of only those linters to run'), ['exclude']),
('x', 'exclude', True,
_('A comma-separated list of linters to skip'), []),
('l', 'linters', False,
_('Show the list of configured linters'), []),
('b', 'base', False,
_('Check all changed files in the repository, not just those in the current directory.'), []),
('a', 'all', False,
_('Scan all files in the repository, not just those that have changed.'), []),
('e', 'every', False,
_('Short for -b -a: scan everything'), []),
('w', 'workspace', False,
_('Scan the workspace'), ['staging']),
('s', 'staging', False,
_('Scan the staging area (useful for pre-commit).'), []),
('g', 'changes', False,
_("Report lint failures only for diff'd sections"), ['complete']),
('p', 'complete', False,
_('Report lint failures for all files'), []),
2016-09-26 21:58:43 +00:00
('t', 'bylinter', False,
_('Group the reports by linter first as they appear in the config file [default]'), []),
('f', 'byfile', False,
_('Group the reports by file first'), []),
2016-09-26 18:44:31 +00:00
('d', 'dryrun', False,
_('Dry run - report what would be done, but do not run linters'), []),
('c', 'config', True,
_('Path to config file'), []),
('h', 'help', False,
_('This help message'), []),
('v', 'version', False,
_('Version information'), [])
]
2016-09-26 21:58:43 +00:00
# ___ _ _ _
# / __|___ _ __ _ __ __ _ _ _ __| | | | (_)_ _ ___
# | (__/ _ \ ' \| ' \/ _` | ' \/ _` | | |__| | ' \/ -_)
# \___\___/_|_|_|_|_|_\__,_|_||_\__,_| |____|_|_||_\___|
2016-09-26 21:58:43 +00:00
#
2016-09-26 18:44:31 +00:00
# This was a lot shorter and smarter in Hy...
def make_rational_options(optlist, args):
# OptionTupleList -> (getOptOptions -> dictionaryOfOptions)
def make_options_rationalizer(optlist):
"""Takes a list of option tuples, and returns a function that takes
the output of getopt and reduces it to the longopt key and
associated values as a dictionary.
"""
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
def make_opt_assoc(prefix, pos):
def associater(acc, it):
acc[(prefix + it[pos])] = it[1]
return acc
return associater
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
short_opt_assoc = make_opt_assoc('-', 0)
long_opt_assoc = make_opt_assoc('--', 1)
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
def make_full_set(acc, i):
return long_opt_assoc(short_opt_assoc(acc, i), i)
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
fullset = reduce(make_full_set, optlist, {})
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
def rationalizer(acc, it):
acc[fullset[it[0]]] = it[1]
return acc
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
return rationalizer
# (OptionTupleList, dictionaryOfOptions) -> (dictionaryOfOptions, excludedOptions)
def remove_conflicted_options(optlist, request):
"""Takes our list of option tuples, and a cleaned copy of what was
requested from getopt, and returns a copy of the request
without any options that are marked as superseded, along with
the list of superseded options
"""
def get_excluded_keys(memo, opt):
return memo + ((len(opt) > 4 and opt[4]) or [])
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
keys = request.keys()
marked = [option for option in optlist if option[1] in keys]
exclude = reduce(get_excluded_keys, marked, [])
excluded = [key for key in keys if key in exclude]
cleaned = {key: request[key] for key in keys
if key not in excluded}
return (cleaned, excluded)
2016-09-26 21:58:43 +00:00
2016-09-26 18:44:31 +00:00
def shortoptstogo(i):
return i[0] + ((i[2] and ':') or '')
2016-09-26 18:44:31 +00:00
def longoptstogo(i):
return i[1] + ((i[2] and '=') or '')
2016-09-26 18:44:31 +00:00
optstringsshort = ''.join([shortoptstogo(opt) for opt in optlist])
optstringslong = [longoptstogo(opt) for opt in optlist]
(options, filenames) = getopt.getopt(args[1:], optstringsshort,
optstringslong)
# Turns what getopt returns into something more human-readable
rationalize_options = make_options_rationalizer(optlist)
# Remove any options that are superseded by others.
(retoptions, excluded) = remove_conflicted_options(
optlist, reduce(rationalize_options, options, {}))
return (retoptions, filenames, excluded)
2016-09-26 21:58:43 +00:00
# ___ __ _ ___ _
# / __|___ _ _ / _(_)__ _ | _ \___ __ _ __| |___ _ _
# | (__/ _ \ ' \| _| / _` | | / -_) _` / _` / -_) '_|
2016-09-26 21:58:43 +00:00
# \___\___/_||_|_| |_\__, | |_|_\___\__,_\__,_\___|_|
# |___/
def find_config_file(options, base):
""" Returns the configuration file from a prioritized list of locations.
Locations are prioritized as:
1. From the command line. Fail if specified but not found
2. The repository's root directory, as the file .git-lint
3. The repository's root directory, as the file .git-lint/config
4. The user's home directory, as file .git-lint
5. The user's home directory, as the file .git-lint/config
2016-09-26 21:58:43 +00:00
If no configuration file is found, this is an error.
"""
if 'config' in options:
config = options['config']
configpath = os.path.abspath(config)
if not os.path.isfile(configpath):
sys.exit(_('Configuration file not found: {}\n').format(config))
return configpath
home = os.path.join(os.environ.get('HOME'))
possibles = (os.path.join(base, '.git-lint'),
os.path.join(base, '.git-lint/config'),
os.path.join(home, '.git-lint'),
os.path.join(home, '.git-lint/config'))
matches = [p for p in possibles if os.path.isfile(p)]
if len(matches) == 0:
sys.exit(_('No configuration file found'))
return matches[0]
# (commandLineDictionary, repositoryLocation) -> (configurationDictionary | exit)
def get_config(options, base):
"""Loads the git-lint configuration file.
Returns the configuration file as a dictionary of dictionaries.
Performs substitutions as specified in the SafeConfigParser
specification; the only one performed currently is the 'repodir'
will be replaced with the base directory of the repository.
Combined with the option to specify the .git-lint configuration as
a directory, this allows users to keep per-project configuration
files for specific linters.
"""
Linter = namedtuple('Linter', ['name', 'linter'])
path = find_config_file(options, base)
configloader = configparser.SafeConfigParser()
configloader.read(path)
configloader.set('DEFAULT', 'repodir', base)
return [Linter(section, {k: v for (k, v) in configloader.items(section)})
for section in configloader.sections()]
2016-09-26 21:58:43 +00:00
# ___ _ _
# / __(_) |_
# | (_ | | _|
# \___|_|\__|
2016-09-26 21:58:43 +00:00
#
2016-09-23 20:11:06 +00:00
def get_git_response_raw(cmd):
fullcmd = (['git'] + cmd)
process = subprocess.Popen(fullcmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
2016-09-23 22:27:15 +00:00
(out, err) = process.communicate()
return (out, err, process.returncode)
2016-09-23 20:11:06 +00:00
def get_git_response(cmd):
2016-09-23 22:27:15 +00:00
(out, error, returncode) = get_git_response_raw(cmd)
return out
2016-09-23 20:11:06 +00:00
def split_git_response(cmd):
2016-09-23 22:27:15 +00:00
(out, error, returncode) = get_git_response_raw(cmd)
return out.splitlines()
2016-09-23 20:11:06 +00:00
def run_git_command(cmd):
2016-09-23 22:27:15 +00:00
fullcmd = (['git'] + cmd)
return subprocess.call(fullcmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
2016-09-26 21:58:43 +00:00
universal_newlines=True)
2016-09-23 20:11:06 +00:00
def get_shell_response(fullcmd):
process = subprocess.Popen(fullcmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True)
2016-09-23 22:27:15 +00:00
(out, err) = process.communicate()
return (out, err, process.returncode)
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
def get_git_base():
(out, error, returncode) = get_git_response_raw(
['rev-parse', '--show-toplevel'])
return (returncode == 0 and out.rstrip()) or None
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
def get_git_head():
empty_repository_hash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
(out, err, returncode) = get_git_response_raw(
['rev-parse', '--verify HEAD'])
return ((err and empty_repository_hash) or 'HEAD')
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
git_base = get_git_base()
git_head = get_git_head()
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
# _ _ _ _ _ _ _ _
# | | | | |_(_) (_) |_(_)___ ___
# | |_| | _| | | | _| / -_|_-<
# \___/ \__|_|_|_|\__|_\___/__/
2016-09-26 21:58:43 +00:00
#
2016-09-23 20:11:06 +00:00
def base_file_cleaner(files):
return [file.replace(git_base + '/', '', 1) for file in files]
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
def make_match_filter_matcher(extensions):
2016-09-26 21:58:43 +00:00
trimmed = [s.strip() for s in reduce(operator.add,
[ex.split(',') for ex in extensions], [])]
cleaned = [re.sub(r'^\.', '', s) for s in trimmed]
2016-09-23 22:27:15 +00:00
return re.compile(r'\.' + '|'.join(cleaned) + r'$')
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
def make_match_filter(config):
matcher = make_match_filter_matcher([v.linter.get('match', '') for v in config])
2016-09-23 22:27:15 +00:00
def match_filter(path):
return matcher.search(path)
2016-09-23 22:27:15 +00:00
return match_filter
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
# ICK. Mutation, references, and hidden assignment.
def group_by(iterable, field_id):
results = []
keys = {}
for obj in iterable:
key = obj[field_id]
if key in keys:
keys[key].append(obj)
continue
keys[key] = [obj]
results.append((key, keys[key]))
return results
# ___ _ _ _ _ _
# / __| |_ ___ __| |__ | (_)_ _| |_ ___ _ _ ___
# | (__| ' \/ -_) _| / / | | | ' \ _/ -_) '_(_-<
# \___|_||_\___\__|_\_\ |_|_|_||_\__\___|_| /__/
2016-09-26 21:58:43 +00:00
#
2016-09-23 20:11:06 +00:00
def executable_exists(script, label):
2016-09-23 22:27:15 +00:00
if not len(script):
sys.exit(
_('Syntax error in command configuration for {} ').format(label))
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
scriptname = script.split(' ').pop(0)
if not len(scriptname):
sys.exit(
_('Syntax error in command configuration for {} ').format(label))
2016-09-23 22:27:15 +00:00
def is_executable(path):
2016-09-23 22:27:15 +00:00
return os.path.exists(path) and os.access(path, os.X_OK)
if scriptname.startswith('/'):
return (is_executable(scriptname) and scriptname) or None
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
possibles = [path for path in
[os.path.join(path, scriptname)
for path in os.environ.get('PATH').split(':')]
2016-09-23 22:27:15 +00:00
if is_executable(path)]
2016-09-26 21:58:43 +00:00
return (len(possibles) and possibles.pop(0)) or False
2016-09-23 22:27:15 +00:00
def get_working_linter_names(config):
return [i.name for i in config
if executable_exists(i.linter['command'], i.name)]
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
def get_linter_status(config):
working_linter_names = get_working_linter_names(config)
broken_linter_names = (set([i.name for i in config]) - set(working_linter_names))
return working_linter_names, broken_linter_names
2016-09-23 20:11:06 +00:00
def print_linters(config):
2016-09-23 22:27:15 +00:00
print(_('Currently supported linters:'))
2016-09-26 21:58:43 +00:00
working_linter_names, broken_linter_names = get_linter_status(config)
for linter in config:
print('{:<14} {}'.format(linter.name,
2016-09-26 21:58:43 +00:00
((linter.name in broken_linter_names and
_('(WARNING: executable not found)') or
linter.linter.get('comment', '')))))
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
# ___ _ _ _ _ __ __ _ _
# / __|___| |_ | (_)__| |_ ___ / _| / _(_) |___ ___
# | (_ / -_) _| | | (_-< _| / _ \ _| | _| | / -_|_-<
# \___\___|\__| |_|_/__/\__| \___/_| |_| |_|_\___/__/
2016-09-26 21:58:43 +00:00
#
2016-09-23 20:11:06 +00:00
2016-09-26 18:44:31 +00:00
def get_filelist(cmdline, extras):
""" Returns the list of files against which we'll run the linters. """
2016-09-23 20:11:06 +00:00
def base_file_filter(files):
""" Return the full path for all files """
return [os.path.join(git_base, file) for file in files]
2016-09-23 20:11:06 +00:00
def cwd_file_filter(files):
""" Return the full path for only those files in the cwd and down """
gitcwd = os.path.join(os.path.relpath(os.getcwd(), git_base), '')
return base_file_filter([file for file in files
if file.startswith(gitcwd)])
2016-09-23 20:11:06 +00:00
def check_for_conflicts(filesets):
""" Scan list of porcelain files for merge conflic state. """
MERGE_CONFLICT_PAIRS = set(['DD', 'DU', 'AU', 'AA', 'UD', 'UA', 'UU'])
status_pairs = set(['' + f[0] + f[1] for f in filesets])
if len(status_pairs & MERGE_CONFLICT_PAIRS):
sys.exit(
_('Current repository contains merge conflicts. Linters will not be run.'))
return filesets
2016-09-23 20:11:06 +00:00
def remove_submodules(files):
""" Remove all submodules from the list of files git-lint cares about. """
2016-09-23 20:11:06 +00:00
fixer_re = re.compile('^(\\.\\.\\/)+')
submodules = split_git_response(['submodule', 'status'])
submodule_names = [fixer_re.sub('', submodule.split(' ')[2])
for submodule in submodules]
return [file for file in files if (file not in submodule_names)]
2016-09-23 20:11:06 +00:00
def get_porcelain_status():
""" Return the status of all files in the system. """
cmd = ['status', '-z', '--porcelain',
'--untracked-files=all', '--ignore-submodules=all']
stream = [entry for entry in get_git_response(cmd).split(u'\x00')
if len(entry) > 0]
2016-09-23 20:11:06 +00:00
def parse_stream(acc, stream):
"""Parse the list of files. T
2016-09-23 20:11:06 +00:00
The list is null-terminated, but is not columnar. If
there's an 'R' in the index state, it means the file was
renamed and the old name is added as a column, so it's a
special case as we accumulate the list of files.
"""
2016-09-23 20:11:06 +00:00
if len(stream) == 0:
return acc
entry = stream.pop(0)
(index, workspace, filename) = (entry[0], entry[1], entry[3:])
if index == 'R':
stream.pop(0)
return parse_stream(acc + [(index, workspace, filename)], stream)
2016-09-23 20:11:06 +00:00
return check_for_conflicts(parse_stream([], stream))
def staging_list():
""" Return the list of files added or modified to the stage """
2016-09-23 20:11:06 +00:00
return [filename for (index, workspace, filename) in get_porcelain_status()
if index in ['A', 'M']]
def working_list():
2016-09-26 21:58:43 +00:00
""" Return the list of files that have been modified in the workspace.
Includes the '?' to include files that git is not currently tracking.
"""
return [filename for (index, workspace, filename) in get_porcelain_status()
if workspace in ['A', 'M', '?']]
def all_list():
""" Return all the files git is currently tracking for this repository. """
cmd = ['ls-tree', '--name-only', '--full-tree', '-r', '-z', git_head]
return [file for file in get_git_response(cmd).split(u'\x00')
if len(file) > 0]
2016-09-26 18:44:31 +00:00
if len(extras):
cwd = os.path.abspath(os.getcwd())
extras_fullpathed = set([os.path.abspath(os.path.join(cwd, f)) for f in extras])
2016-09-26 18:44:31 +00:00
not_found = set([f for f in extras_fullpathed if not os.path.isfile(f)])
return ([os.path.relpath(f, cwd) for f in (extras_fullpathed - not_found)], not_found)
2016-09-26 21:58:43 +00:00
2016-09-23 22:27:15 +00:00
working_directory_trans = cwd_file_filter
2016-09-26 18:44:31 +00:00
if 'base' in cmdline or 'every' in cmdline:
2016-09-23 22:27:15 +00:00
working_directory_trans = base_file_filter
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
file_list_generator = working_list
2016-09-26 21:58:43 +00:00
if 'all' in cmdline:
file_list_generator = all_list
2016-09-26 21:58:43 +00:00
if 'staging' in cmdline:
2016-09-23 22:27:15 +00:00
file_list_generator = staging_list
2016-09-23 20:11:06 +00:00
return (working_directory_trans(remove_submodules(file_list_generator())), [])
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
# ___ _ _
# / __| |_ __ _ __ _(_)_ _ __ _ __ __ ___ _ __ _ _ __ _ __ ___ _ _
# \__ \ _/ _` / _` | | ' \/ _` | \ V V / '_/ _` | '_ \ '_ \/ -_) '_|
2016-09-26 21:58:43 +00:00
# |___/\__\__,_\__, |_|_||_\__, | \_/\_/|_| \__,_| .__/ .__/\___|_|
# |___/ |___/ |_| |_|
2016-09-26 18:46:47 +00:00
def pick_stash_runner(cmdline):
"""Choose a runner.
This is the operation that will run the linters. It exists to
provide a way to stash the repository, then restore it when
complete. If possible, it attempts to restore the access and
modification times of the file in order to comfort IDEs that are
constantly monitoring file times.
"""
def staging_wrapper(run_linters):
2016-09-23 20:11:06 +00:00
def time_gather(f):
2016-09-23 22:27:15 +00:00
stats = os.stat(f)
return (f, (stats.atime, stats.mtime))
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
times = [time_gather(file) for file in files]
run_git_command(['stash', '--keep-index'])
2016-09-23 20:11:06 +00:00
2016-09-23 22:27:15 +00:00
results = run_linters()
run_git_command(['reset', '--hard'])
run_git_command(['stash', 'pop', '--quiet', '--index'])
2016-09-23 22:27:15 +00:00
for (filename, timepair) in times:
os.utime(filename, timepair)
return results
def workspace_wrapper(run_linters):
return run_linters()
return ('staging' in cmdline and staging_wrapper) or workspace_wrapper
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
# ___ _ _ _
# | _ \_ _ _ _ | (_)_ _| |_ _ __ __ _ ______
# | / || | ' \ | | | ' \ _| | '_ \/ _` (_-<_-<
# |_|_\\_,_|_||_| |_|_|_||_\__| | .__/\__,_/__/__/
# |_|
2016-09-23 20:11:06 +00:00
def run_external_linter(filename, linter, linter_name):
2016-09-23 20:11:06 +00:00
"""Run one linter against one file.
2016-09-23 20:11:06 +00:00
If the result matches the error condition specified in the
configuration file, return the error code and messages, either
return nothing.
"""
2016-09-23 20:11:06 +00:00
def encode_shell_messages(prefix, messages):
return ['{}{}'.format(prefix, line)
for line in messages.splitlines()]
cmd = linter['command'] + ' "' + filename + '"'
(out, err, returncode) = get_shell_response(cmd)
failed = ((out and (linter.get('condition', 'error') == 'output')) or err or (not (returncode == 0)))
2016-09-26 21:58:43 +00:00
trimmed_filename = filename.replace(git_base + '/', '', 1)
if not failed:
2016-09-26 21:58:43 +00:00
return (trimmed_filename, linter_name, 0, [])
2016-09-26 21:58:43 +00:00
prefix = ((linter.get('print', 'false').strip().lower() != 'true') and ' ') or ' {}: '.format(trimmed_filename)
output = base_file_cleaner(encode_shell_messages(prefix, out) + ((err and encode_shell_messages(prefix, err)) or []))
return (trimmed_filename, linter_name, (returncode or 1), output)
2016-09-23 20:11:06 +00:00
def run_one_linter(linter, filenames):
""" Runs one linter against a set of files
Creates a match filter for the linter, extract the files to be
linted, and runs the linter against each file, returning the
result as a list of successes and failures. Failures have a
return code and the output of the lint process.
"""
2016-09-26 21:58:43 +00:00
match_filter = make_match_filter([linter])
files = set([file for file in filenames if match_filter(file)])
2016-09-26 21:58:43 +00:00
return [run_external_linter(file, linter.linter, linter.name) for file in files]
2016-09-23 20:11:06 +00:00
def build_lint_runner(linters, filenames):
""" Returns a function to run a set of linters against a set of filenames
2016-09-23 20:11:06 +00:00
This returns a function because it's going to be wrapped in a
runner to better handle stashing and restoring a staged commit.
"""
def lint_runner():
return reduce(operator.add,
2016-09-26 21:58:43 +00:00
[run_one_linter(linter, filenames) for linter in linters], [])
return lint_runner
2016-09-23 20:11:06 +00:00
# __ __ _
# | \/ |__ _(_)_ _
# | |\/| / _` | | ' \
# |_| |_\__,_|_|_||_|
#
2016-09-23 20:11:06 +00:00
2016-09-26 21:58:43 +00:00
def print_report(results, config):
sort_position = 1
grouping = 'Linter: {}'
if 'byfile' in config:
sort_position = 0
grouping = 'Filename: {}'
grouped_results = group_by(results, sort_position)
for group in grouped_results:
print(grouping.format(group[0]))
for (filename, lintername, returncode, text) in group[1]:
print("\n".join(text))
print("")
2016-09-23 20:11:06 +00:00
2016-09-26 18:44:31 +00:00
def run_gitlint(cmdline, config, extras):
2016-09-23 20:11:06 +00:00
def build_config_subset(keys):
""" Returns a subset of the configuration, with only those linters mentioned in keys """
return [item for item in config if item.name in keys]
""" Runs the requested linters """
2016-09-26 21:58:43 +00:00
all_filenames, unfindable_filenames = get_filelist(cmdline, extras)
stash_runner = pick_stash_runner(cmdline)
2016-09-26 21:58:43 +00:00
is_lintable = make_match_filter(config)
2016-09-26 21:58:43 +00:00
lintable_filenames = set([filename for filename in all_filenames
if is_lintable(filename)])
unlintable_filenames = set(all_filenames) - lintable_filenames
2016-09-26 21:58:43 +00:00
working_linter_names, broken_linter_names = get_linter_status(config)
cant_lint_filter = make_match_filter(build_config_subset(
broken_linter_names))
cant_lint_filenames = [filename for filename in lintable_filenames
if cant_lint_filter(filename)]
2016-09-26 18:46:47 +00:00
2016-09-26 21:58:43 +00:00
lint_runner = build_lint_runner(
build_config_subset(working_linter_names), sorted(lintable_filenames))
2016-09-26 18:44:31 +00:00
results = stash_runner(lint_runner)
2016-09-26 18:44:31 +00:00
2016-09-26 21:58:43 +00:00
print_report(results, cmdline)
return max([i[2] for i in results if len(i)])
2016-09-23 20:11:06 +00:00
def print_help(options_list, name):
print(_('Usage: {} [options] [filenames]').format(name))
for item in options_list:
2016-09-26 18:44:31 +00:00
print(' -{:<1} --{:<12} {}'.format(item[0], item[1], item[3]))
return sys.exit()
def print_version(name, version):
print('{} {} Copyright (c) 2009, 2016 Kennth M. "Elf" Sternberg'.format(name, version))
2016-09-26 18:44:31 +00:00
2016-09-23 20:11:06 +00:00
def main(*args):
if git_base is None:
2016-09-26 18:44:31 +00:00
sys.exit(_('A git repository was not found.'))
(cmdline, filenames, excluded_commands) = make_rational_options(OPTIONS_LIST, args)
2016-09-26 18:44:31 +00:00
if len(excluded_commands) > 0:
print(_('These command line options were ignored due to option precedence.'))
for exc in excluded_commands:
print("\t{}".format(exc))
try:
2016-09-26 18:44:31 +00:00
config = get_config(cmdline, git_base)
if 'help' in cmdline:
print_help(OPTIONS_LIST, NAME)
return 0
2016-09-26 18:44:31 +00:00
if 'version' in cmdline:
print_version(NAME, VERSION)
return 0
2016-09-26 18:44:31 +00:00
if 'linters' in cmdline:
print_linters(config)
return 0
2016-09-26 18:44:31 +00:00
return run_gitlint(cmdline, config, filenames)
except getopt.GetoptError as err:
2016-09-26 18:44:31 +00:00
print_help(OPTIONS_LIST)
return 1
2016-09-23 20:11:06 +00:00
if __name__ == '__main__':
2016-09-23 20:11:06 +00:00
import sys
sys.exit(main(*sys.argv))