blob: 2034c12628e298fa709a44f19f42a30bab3d9c61 [file] [log] [blame]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import subprocess
7
8# Helpers
9
10def _get_hooks_dir():
11 """Returns the absolute path to the repohooks directory"""
12 cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd']
13 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()
14
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070015def _get_diff(commit):
16 """Returns the diff for this commit"""
17 cmd = ['git', 'show', commit]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070018 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
19
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070020def _get_commits():
21 """Returns a list of commits for this review"""
22 cmd = ['git', 'log', 'm/master...', '--format=%H']
23 commits = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
24 return commits.split()
25
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070026# Hooks
27
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070028def _run_checkpatch(project, commit):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070029 """Runs checkpatch.pl on the given project"""
30 hooks_dir = _get_hooks_dir()
31 cmd = ['%s/checkpatch.pl' % hooks_dir, '-']
32 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070033 output = p.communicate(_get_diff(commit))[0]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070034 if p.returncode:
35 raise Exception('checkpatch.pl errors/warnings\n\n' + output)
36
37# Base
38
39def _setup_project_hooks():
40 """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]"""
41 return {
42 "chromiumos/third_party/kernel": [_run_checkpatch]
43 }
44
45def _run_project_hooks(project, hooks):
46 """For each project run its project specific hook from the hooks dictionary"""
47 cmd = ['repo', 'forall', project, '-c', 'pwd']
48 proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
49 proj_dir = proj_dir.strip()
50 if project in hooks:
51 pwd = os.getcwd()
52 # hooks assume they are run from the root of the project
53 os.chdir(proj_dir)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070054 for commit in _get_commits():
55 for hook in hooks[project]:
56 hook(project, commit)
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070057 os.chdir(pwd)
58
59# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070060
Anush Elangovan63afad72011-03-23 00:41:27 -070061def main(project_list, **kwargs):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070062 hooks = _setup_project_hooks()
63 for project in project_list:
64 _run_project_hooks(project, hooks)
Anush Elangovan63afad72011-03-23 00:41:27 -070065
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070066if __name__ == '__main__':
67 main()