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 | |
| 15 | def _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 | |
| 24 | def _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 | |
| 35 | def _setup_project_hooks(): |
| 36 | """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]""" |
| 37 | return { |
| 38 | "chromiumos/third_party/kernel": [_run_checkpatch] |
| 39 | } |
| 40 | |
| 41 | def _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 Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 55 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 56 | def main(project_list, **kwargs): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame^] | 57 | hooks = _setup_project_hooks() |
| 58 | for project in project_list: |
| 59 | _run_project_hooks(project, hooks) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 60 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 61 | if __name__ == '__main__': |
| 62 | main() |