git-linter/git_lint/__main__.py

130 lines
3.5 KiB
Python
Raw Normal View History

2016-09-28 21:26:06 +00:00
#!/usr/bin/env python
2016-09-28 22:22:34 +00:00
from __future__ import print_function
2016-09-28 21:26:06 +00:00
from .options import OPTIONS
from .option_handler import cleanup_options
2016-09-28 21:57:02 +00:00
from .reporters import print_report, print_help, print_linters
2016-09-28 21:26:06 +00:00
from .git_lint import load_config, run_linters, git_base
from getopt import GetoptError
2016-09-30 18:15:10 +00:00
import os.path
2016-09-28 21:57:02 +00:00
import sys
2016-09-30 18:15:10 +00:00
import time
watchdog = False
try:
import watchdog
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
except Exception as e:
pass
2016-09-28 21:26:06 +00:00
import gettext
_ = gettext.gettext
2016-09-28 21:57:02 +00:00
NAME = 'git-lint'
VERSION = '0.0.4'
2016-09-30 18:15:10 +00:00
def remove_unavailable_options(options):
failures = [] + ((watchdog == False and ['monitor']) or [])
return filter(lambda i: i[1] not in failures, options)
def monitor(options, config, filenames):
observer = watchdog.observers.Observer()
skip = ['\.git/']
def run_monitor_linters():
(results,
unlintable_filenames,
cant_lint_filenames,
broken_linter_names,
unfindable_filenames) = run_linters(options, config, filenames)
print_report(results,
unlintable_filenames,
cant_lint_filenames,
broken_linter_names,
unfindable_filenames,
options)
class LintMonitor(RegexMatchingEventHandler):
def __init__(self):
super(LintMonitor, self).__init__(ignore_regexes=skip)
def on_created(self, event):
run_monitor_linters()
def on_modified(self, event):
run_monitor_linters()
observer.schedule(LintMonitor(), git_base, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
2016-09-28 21:57:02 +00:00
def main():
2016-09-28 21:26:06 +00:00
if git_base is None:
sys.exit(_('A git repository was not found.'))
2016-09-30 18:15:10 +00:00
initial_options = remove_unavailable_options(OPTIONS)
(options, filenames, excluded_commands) = cleanup_options(initial_options, sys.argv)
2016-09-28 21:26:06 +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:
config = load_config(options, git_base)
if 'help' in options:
2016-09-30 18:15:10 +00:00
print_help(initial_options, NAME)
2016-09-28 21:26:06 +00:00
return 0
if 'version' in options:
2016-09-28 21:57:02 +00:00
from .reporters import print_version
2016-09-28 21:26:06 +00:00
print_version(NAME, VERSION)
return 0
if 'linters' in options:
2016-09-28 21:57:02 +00:00
from .git_lint import get_linter_status
2016-09-28 21:26:06 +00:00
working_linter_names, broken_linter_names = get_linter_status(config)
2016-09-28 21:57:02 +00:00
print_linters(config, broken_linter_names)
2016-09-28 21:26:06 +00:00
return 0
2016-09-30 18:15:10 +00:00
if 'monitor' in options:
return monitor(options, config, filenames)
2016-09-28 21:26:06 +00:00
(results,
unlintable_filenames,
cant_lint_filenames,
broken_linter_names,
unfindable_filenames) = run_linters(options, config, filenames)
2016-09-28 21:26:06 +00:00
print_report(results,
unlintable_filenames,
cant_lint_filenames,
broken_linter_names,
unfindable_filenames,
options)
2016-09-28 21:26:06 +00:00
if not len(results):
return 0
2016-09-28 21:26:06 +00:00
return max([i[2] for i in results if len(i)])
except GetoptError as err:
2016-09-30 18:15:10 +00:00
print_help(initial_options)
2016-09-28 21:26:06 +00:00
return 1
2016-09-28 21:26:06 +00:00
if __name__ == '__main__':
import sys
2016-09-28 21:57:02 +00:00
sys.exit(main())