Anton Shestakov <av6@dwimlabs.net>, Sun, 17 Jul 2016 22:29:24 +0800
viewer: pass listen address to listen() as a positional argument
checker.py
Permissions: -rwxr-xr-x
from __future__ import print_function from argparse import ArgumentParser, FileType from fnmatch import fnmatchcase from shutil import rmtree from subprocess import check_call, check_output, CalledProcessError from tempfile import mkdtemp from candolint.utils import rel, timestamp def run_ignore_codes(fn, args, codes): except CalledProcessError as e: if e.returncode not in codes: def run(args, silent=False, get_output=False, ignore_codes=None): print('$ ' + ' '.join(args)) result = run_ignore_codes(fn, args, ignore_codes) print('# C&O error: {}'.format(e)) def read_linter_config(name, path=rel('linters')): fd = open(os.path.join(path, '{}.yml'.format(name))) print("# C&O couldn't load linter config: {}".format(e)) return yaml.safe_load(fd) print("# C&O couldn't parse linter config: {}".format(e)) print("# C&O config doesn't have 'url' defined.") print("# C&O config doesn't have 'scm' defined.") elif config['scm'] not in ('git', 'hg'): print("# C&O checker doesn't support {} yet.".format(config['scm'])) def git_clone(url, dest): return run(['git', 'clone', '--depth', '1', url, dest]) return run(['hg', 'clone', url, dest]) return run(['git', 'status']) return run(['hg', 'sum']) template = (r'# C&O commit: %H%n' r'# C&O commit ref names: %D%n' r'# C&O commit date: %ai%n' r'# C&O commit author: %aN%n' r'# C&O commit message: %s%n') return run(['git', 'show', '-q', '--format=format:' + template], silent=True) template = (r'# C&O commit: {rev}:{node}\n' r'# C&O commit branch: {branch}\n' r'# C&O commit date: {date|isodatesec}\n' r'# C&O commit author: {author|person}\n' r'# C&O commit message: {desc|firstline}\n') return run(['hg', 'log', '-r', '.', '-T', template], silent=True) def git_files(include, exclude): cmd = ['git', 'ls-files'] files = run(cmd, silent=True, get_output=True) files = files.splitlines() files = [f for f in files if not fnmatchcase(f, e)] def hg_files(include, exclude): files = run(cmd, silent=True, get_output=True, ignore_codes=(1,)) files = files.splitlines() def execute(tmp, config): if not prevalidate(config): print('# C&O task: clone') print('# C&O project URL: {}'.format(config['url'])) if config['scm'] == 'hg': os.environ['HGPLAIN'] = '1' if config['scm'] == 'git': if not git_clone(config['url'], source): elif config['scm'] == 'hg': if not hg_clone(config['url'], source): print('$ cd {}'.format(source)) if config['scm'] == 'git': elif config['scm'] == 'hg': if config['scm'] == 'git': elif config['scm'] == 'hg': print('# C&O task: setup') for linter in config.get('linters', []): if 'name' in linter and linter['name'] not in requested: requested.append(linter['name']) print('# C&O linters requested: {}'.format(' '.join(requested))) for linter in config.get('linters', []): if name in linter_config: lc = read_linter_config(name) if lc is None or 'exec' not in lc: for item in lc.get('setup', []): if 'version' in lc and not run(lc['exec'] + lc['version']): print('# C&O linters installed: {}'.format(' '.join(linter_config.keys()))) print('# C&O task: checks') for linter in config.get('linters', []): if 'name' not in linter or 'include' not in linter: if linter['name'] not in linter_config: if config['scm'] == 'git': files = git_files(linter['include'], linter.get('exclude', [])) elif config['scm'] == 'hg': files = hg_files(linter['include'], linter.get('exclude', [])) lc = linter_config[linter['name']] flags = lc.get('flags', []) + linter.get('flags', []) pf = lc.get('post_flags', []) + linter.get('post_flags', []) codes = lc.get('codes', (1,)) if not run(cmd + flags + [f] + pf, ignore_codes=codes): print('# C&O job started: {}'.format(timestamp())) tmp = mkdtemp(prefix='candolint.') if not execute(tmp, config): print('# C&O job failed') print('# C&O task: cleanup') print('# C&O job finished: {}'.format(timestamp())) print('# C&O job failed (exception not caught)') print('# C&O job finished: {}'.format(timestamp())) parser = ArgumentParser() helptext = 'project configuration file (YAML)' parser.add_argument('config', type=FileType('r'), help=helptext) args = parser.parse_args() config = yaml.safe_load(args.config) if __name__ == '__main__':