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 { |
Doug Anderson | 830216f | 2011-05-02 10:08:37 -0700 | [diff] [blame] | 42 | "chromiumos/third_party/kernel": [_run_checkpatch], |
| 43 | "chromiumos/third_party/kernel-next": [_run_checkpatch], |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | def _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 Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 55 | for commit in _get_commits(): |
| 56 | for hook in hooks[project]: |
| 57 | hook(project, commit) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 58 | os.chdir(pwd) |
| 59 | |
| 60 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 61 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 62 | def main(project_list, **kwargs): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 63 | hooks = _setup_project_hooks() |
| 64 | for project in project_list: |
| 65 | _run_project_hooks(project, hooks) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 66 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 67 | if __name__ == '__main__': |
| 68 | main() |