blob: 96fde4f4c98ff212effe8b0e61b31b5261aabdce [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
15def _get_diff():
16 """Returns the diff for this project"""
17
18 # TODO(msb) iterate over each commit
19 cmd = ['git', 'show', 'HEAD']
20 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
21
22# Hooks
23
24def _run_checkpatch(project):
25 """Runs checkpatch.pl on the given project"""
26 hooks_dir = _get_hooks_dir()
27 cmd = ['%s/checkpatch.pl' % hooks_dir, '-']
28 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
29 output = p.communicate(_get_diff())[0]
30 if p.returncode:
31 raise Exception('checkpatch.pl errors/warnings\n\n' + output)
32
33# Base
34
35def _setup_project_hooks():
36 """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]"""
37 return {
38 "chromiumos/third_party/kernel": [_run_checkpatch]
39 }
40
41def _run_project_hooks(project, hooks):
42 """For each project run its project specific hook from the hooks dictionary"""
43 cmd = ['repo', 'forall', project, '-c', 'pwd']
44 proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
45 proj_dir = proj_dir.strip()
46 if project in hooks:
47 pwd = os.getcwd()
48 # hooks assume they are run from the root of the project
49 os.chdir(proj_dir)
50 for hook in hooks[project]:
51 hook(project)
52 os.chdir(pwd)
53
54# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070055
Anush Elangovan63afad72011-03-23 00:41:27 -070056def main(project_list, **kwargs):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070057 hooks = _setup_project_hooks()
58 for project in project_list:
59 _run_project_hooks(project, hooks)
Anush Elangovan63afad72011-03-23 00:41:27 -070060
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -070061if __name__ == '__main__':
62 main()