Download:
child 99:b0bcc3b9dc46
parent 97:4bd6c0a6ba6d
98:c922c588315e
Anton Shestakov <av6@dwimlabs.net>, Mon, 04 Jul 2016 16:35:41 +0800
checker: wrap execute() in another function to ease exiting on failures

1 файлов изменено, 92 вставок(+), 95 удалений(-) [+]
checker.py file | annotate | diff | comparison | revisions
--- a/checker.py Mon Jul 04 16:04:14 2016 +0800
+++ b/checker.py Mon Jul 04 16:35:41 2016 +0800
@@ -77,106 +77,103 @@
print("# C&O checker doesn't support {} yet.".format(config['scm']))
ok = False
- if not ok:
- print('# C&O job failed')
-
return ok
-def execute(config):
+def execute(tmp, config):
+ if not prevalidate(config):
+ return False
+
+ os.chdir(tmp)
+ source = './source'
+
+ print('# C&O task: clone')
+ print('# C&O project URL: {}'.format(config['url']))
+ os.environ['HGPLAIN'] = '1'
+
+ if not run(['hg', 'clone', config['url'], source]):
+ return False
+
+ print('$ cd {}'.format(source))
+ os.chdir(source)
+
+ if not run(['hg', 'sum']):
+ return False
+
+ 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')
+ if not run(['hg', 'log', '-r', '.', '-T', template], silent=True):
+ return False
+
+ print('# C&O task: setup')
+
+ to_install = [l['name'] for l in config.get('linters', []) if 'name' in l]
+ print('# C&O linters to install: {}'.format(' '.join(to_install)))
+
+ linter_config = {}
+
+ for linter in config.get('linters', []):
+ 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):
+ return False
+
+ if 'version' in lc and not run(lc['exec'] + lc['version']):
+ return False
+
+ 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:
+ continue
+
+ if linter['name'] not in linter_config:
+ continue
+
+ cmd = ['hg', 'files']
+ for pat in linter['include']:
+ cmd.extend(('-I', pat))
+ for pat in linter.get('exclude', []):
+ cmd.extend(('-X', pat))
+ files = run(cmd, silent=True, get_output=True, ignore_codes=(1,))
+ if files is False:
+ return False
+
+ lc = linter_config[linter['name']]
+
+ for f in files.splitlines():
+ cmd = lc['exec']
+ 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):
+ return False
+
+ return True
+
+
+def wrapper(config):
print('# C&O job started: {}'.format(now()))
tmp = mkdtemp(prefix='candolint.')
- os.chdir(tmp)
- source = './source'
- ok = prevalidate(config)
-
- if ok:
- print('# C&O task: clone')
- print('# C&O project URL: {}'.format(config['url']))
- os.environ['HGPLAIN'] = '1'
-
- if ok and not run(['hg', 'clone', config['url'], source]):
- ok = False
-
- if ok:
- print('$ cd {}'.format(source))
- os.chdir(source)
-
- if ok and not run(['hg', 'sum']):
- ok = False
-
- if ok:
- 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')
- if not run(['hg', 'log', '-r', '.', '-T', template], silent=True):
- ok = False
-
- if ok:
- print('# C&O task: setup')
-
- to_install = [l['name'] for l in config.get('linters', []) if 'name' in l]
- print('# C&O linters to install: {}'.format(' '.join(to_install)))
-
- linter_config = {}
-
- for linter in config.get('linters', []):
- 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
-
- print('# C&O linters installed: {}'.format(' '.join(linter_config.keys())))
-
- if ok:
- print('# C&O task: checks')
- for linter in config.get('linters', []):
- if 'name' not in linter or 'include' not in linter:
- continue
-
- if linter['name'] not in linter_config:
- continue
-
- cmd = ['hg', 'files']
- for pat in linter['include']:
- cmd.extend(('-I', pat))
- for pat in linter.get('exclude', []):
- cmd.extend(('-X', pat))
- files = run(cmd, silent=True, get_output=True, ignore_codes=(1,))
- if files is False:
- ok = False
- break
-
- lc = linter_config[linter['name']]
-
- for f in files.splitlines():
- cmd = lc['exec']
- 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):
- ok = False
- break
+ if not execute(tmp, config):
+ print('# C&O job failed')
print('# C&O task: cleanup')
rmtree(tmp)
@@ -196,7 +193,7 @@
args = parser.parse_args()
config = yaml.safe_load(args.config)
try:
- execute(config)
+ wrapper(config)
except:
bad_exit()