Download:
child 56:438d96a2042f
parent 54:53e3c39fff13
55:da06db1fadcd
Anton Shestakov <av6@dwimlabs.net>, Thu, 23 Jun 2016 15:21:20 +0800
checker: don't hardcode linter config, load it from files

1 файлов изменено, 47 вставок(+), 9 удалений(-) [+]
checker.py file | annotate | diff | comparison | revisions
--- a/checker.py Thu Jun 23 15:06:21 2016 +0800
+++ b/checker.py Thu Jun 23 15:21:20 2016 +0800
@@ -11,6 +11,9 @@
import yaml
+rel = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x))
+
+
def run_ignore_codes(fn, args, codes):
try:
return fn(args)
@@ -46,6 +49,20 @@
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S+00:00')
+def read_linter_config(name, path=rel('linters')):
+ try:
+ fd = open(os.path.join(path, '{}.yml'.format(name)))
+ except Exception as e:
+ print("# C&O couldn't load linter config: {}".format(e))
+ return None
+ with fd:
+ try:
+ return yaml.safe_load(fd)
+ except Exception as e:
+ print("# C&O couldn't parse linter config: {}".format(e))
+ return None
+
+
def execute(config):
ok = True
@@ -77,20 +94,38 @@
if ok:
print('# C&O task: setup')
+ linter_config = {}
- venv = '../venv'
- run(['virtualenv', venv])
- pip = os.path.join(venv, 'bin', 'pip')
for linter in config['linters']:
- if 'pip' in linter:
- if not run([pip, 'install'] + linter['pip']):
+ if 'name' not in linter:
+ continue
+
+ name = linter['name']
+ if name in linter_config:
+ continue
+
+ lc = read_linter_config(name)
+ if lc is None or 'exec' not in lc:
+ continue
+
+ linter_config[name] = lc
+
+ for item in lc.get('setup', []):
+ if not run(item):
ok = False
break
+ if 'version' in lc and not run(lc['exec'] + lc['version']):
+ ok = False
+ break
+
if ok:
print('# C&O task: checks')
for linter in config['linters']:
- if 'include' not in linter:
+ if 'name' not in linter or 'include' not in linter:
+ continue
+
+ if linter['name'] not in linter_config:
continue
cmd = ['hg', 'files']
@@ -103,10 +138,13 @@
ok = False
break
+ lc = linter_config[linter['name']]
+
for f in files.splitlines():
- if 'pip' in linter:
- cmd = [os.path.join(venv, 'bin', linter['cmd'])]
- if not run(cmd + linter.get('flags', []) + [f], ignore_codes=(1,)):
+ cmd = lc['exec']
+ flags = lc.get('flags', []) + linter.get('flags', [])
+ pf = lc.get('post_flags', []) + linter.get('post_flags', [])
+ if not run(cmd + flags + [f] + pf, ignore_codes=(1,)):
ok = False
break