blob: d2b9d41be4551cd346bceec4e473c12fab8ce5b5 [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 {
Doug Anderson830216f2011-05-02 10:08:37 -070042 "chromiumos/third_party/kernel": [_run_checkpatch],
43 "chromiumos/third_party/kernel-next": [_run_checkpatch],
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070044 }
45
46def _run_project_hooks(project, hooks):
47 """For each project run its project specific hook from the hooks dictionary"""
48 cmd = ['repo', 'forall', project, '-c', 'pwd']
49 proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
50 proj_dir = proj_dir.strip()
51 if project in hooks:
52 pwd = os.getcwd()
53 # hooks assume they are run from the root of the project
54 os.chdir(proj_dir)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070055 for commit in _get_commits():
56 for hook in hooks[project]:
57 hook(project, commit)
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070058 os.chdir(pwd)
59
60# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070061
Anush Elangovan63afad72011-03-23 00:41:27 -070062def main(project_list, **kwargs):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070063 hooks = _setup_project_hooks()
64 for project in project_list:
65 _run_project_hooks(project, hooks)
Anush Elangovan63afad72011-03-23 00:41:27 -070066
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070067if __name__ == '__main__':
68 main()