Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1 | # 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 | |
| 5 | import os |
| 6 | import subprocess |
| 7 | |
| 8 | # Helpers |
| 9 | |
| 10 | def _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 Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame^] | 15 | def _get_diff(commit): |
| 16 | """Returns the diff for this commit""" |
| 17 | cmd = ['git', 'show', commit] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 18 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 19 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame^] | 20 | def _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 Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 26 | # Hooks |
| 27 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame^] | 28 | def _run_checkpatch(project, commit): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 29 | """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 Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame^] | 33 | output = p.communicate(_get_diff(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 34 | if p.returncode: |
| 35 | raise Exception('checkpatch.pl errors/warnings\n\n' + output) |
| 36 | |
| 37 | # Base |
| 38 | |
| 39 | def _setup_project_hooks(): |
| 40 | """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]""" |
| 41 | return { |
| 42 | "chromiumos/third_party/kernel": [_run_checkpatch] |
| 43 | } |
| 44 | |
| 45 | def _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 Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame^] | 54 | for commit in _get_commits(): |
| 55 | for hook in hooks[project]: |
| 56 | hook(project, commit) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 57 | os.chdir(pwd) |
| 58 | |
| 59 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 60 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 61 | def main(project_list, **kwargs): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 62 | hooks = _setup_project_hooks() |
| 63 | for project in project_list: |
| 64 | _run_project_hooks(project, hooks) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 65 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 66 | if __name__ == '__main__': |
| 67 | main() |