Anton Shestakov <av6@dwimlabs.net>, Thu, 23 Jun 2016 14:58:09 +0800
checker: try and catch exceptions from execute() and somewhat finish the job
Exceptions coming from execute() are programming errors in checker.py, but jobs
still need to tell that they are finished (and failed) to be processed by
incoming.py.
checker.py
Permissions: -rwxr-xr-x
from __future__ import print_function from argparse import ArgumentParser, FileType from datetime import datetime from shutil import rmtree from subprocess import check_call, check_output, CalledProcessError from tempfile import mkdtemp 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)) print('# C&O job failed') return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S+00:00') print('# C&O job started: {}'.format(now())) tmp = mkdtemp(prefix='candolint.') print('# C&O task: clone') print('# C&O project URL: {}'.format(config['url'])) if ok and not run(['hg', 'clone', config['url'], source]): print('$ cd {}'.format(source)) if ok and not run(['hg', 'sum']): template = (r'# C&O commit: {rev}:{node} {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') if not run(['hg', 'log', '-r', '.', '-T', template], silent=True): print('# C&O task: setup') run(['virtualenv', venv]) pip = os.path.join(venv, 'bin', 'pip') for linter in config['linters']: if not run([pip, 'install'] + linter['pip']): print('# C&O task: checks') for linter in config['linters']: if 'cmd' not in linter or 'files' not in linter: cmd = ['hg', 'files', 'set:' + ' or '.join(linter['files'])] files = run(cmd, silent=True, get_output=True, ignore_codes=(1,)) for f in files.splitlines(): cmd = [os.path.join(venv, 'bin', linter['cmd'])] if not run(cmd + linter.get('flags', []) + [f], ignore_codes=(1,)): print('# C&O task: cleanup') print('# C&O job finished: {}'.format(now())) print('# C&O job failed (exception not caught)') print('# C&O job finished: {}'.format(now())) parser = ArgumentParser() parser.add_argument('config', help='configuration file (YAML)', type=FileType('r')) args = parser.parse_args() config = yaml.safe_load(args.config) if __name__ == '__main__':