Mandeep Singh Baines | e9cd41e | 2010-07-26 17:26:44 -0700 | [diff] [blame] | 1 | # Copyright (c) 2010 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 | """Top-level presubmit script for Chromium OS. |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into gcl and git cl. |
| 9 | """ |
| 10 | |
| 11 | import difflib |
| 12 | import os |
Mandeep Singh Baines | 57528e5 | 2010-07-27 14:59:26 -0700 | [diff] [blame] | 13 | import re |
Mandeep Singh Baines | e9cd41e | 2010-07-26 17:26:44 -0700 | [diff] [blame] | 14 | |
| 15 | _EBUILD_FILES = ( |
| 16 | r".*\.ebuild", |
| 17 | ) |
| 18 | |
| 19 | def _IsCrosWorkonEbuild(ebuild_contents): |
Mandeep Singh Baines | 57528e5 | 2010-07-27 14:59:26 -0700 | [diff] [blame] | 20 | pattern = re.compile('^ *inherit[-\._a-z0-9 ]*cros-workon') |
| 21 | for line in ebuild_contents: |
| 22 | if pattern.match(line): |
| 23 | return True |
| 24 | return False |
Mandeep Singh Baines | e9cd41e | 2010-07-26 17:26:44 -0700 | [diff] [blame] | 25 | |
| 26 | def Check9999Updated(input_api, output_api, source_file_filter=None): |
| 27 | """Checks that the 9999 ebuild was also modified.""" |
| 28 | output = [] |
Mandeep Singh Baines | 5a296bc | 2010-07-28 07:46:01 -0700 | [diff] [blame] | 29 | inconsistent = set() |
Mandeep Singh Baines | e9cd41e | 2010-07-26 17:26:44 -0700 | [diff] [blame] | 30 | missing_9999 = set() |
| 31 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 32 | ebuild_contents = f.NewContents() |
| 33 | # only look at non-9999 |
| 34 | if f.LocalPath().endswith('-9999.ebuild'): |
| 35 | continue |
| 36 | if _IsCrosWorkonEbuild(ebuild_contents): |
| 37 | dir = os.path.dirname(f.AbsoluteLocalPath()) |
| 38 | ebuild = os.path.basename(dir) |
| 39 | devebuild_path = os.path.join(dir, ebuild + '-9999.ebuild') |
| 40 | # check if 9999 ebuild exists |
| 41 | if not os.path.isfile(devebuild_path): |
| 42 | missing_9999.add(ebuild) |
| 43 | continue |
| 44 | diff = difflib.ndiff(ebuild_contents, |
| 45 | open(devebuild_path).read().splitlines()) |
| 46 | for line in diff: |
| 47 | if line.startswith('+') or line.startswith('-'): |
| 48 | # ignore empty-lines |
| 49 | if len(line) == 2: |
| 50 | continue |
| 51 | if not (line[2:].startswith('KEYWORDS=') or |
| 52 | line[2:].startswith('CROS_WORKON_COMMIT=')): |
Mandeep Singh Baines | 5a296bc | 2010-07-28 07:46:01 -0700 | [diff] [blame] | 53 | inconsistent.add(f.LocalPath()) |
Mandeep Singh Baines | e9cd41e | 2010-07-26 17:26:44 -0700 | [diff] [blame] | 54 | |
| 55 | if missing_9999: |
| 56 | output.append(output_api.PresubmitPromptWarning( |
| 57 | 'Missing 9999 for these cros-workon ebuilds:', items=missing_9999)) |
| 58 | if inconsistent: |
| 59 | output.append(output_api.PresubmitPromptWarning( |
| 60 | 'Following ebuilds are inconsistent with 9999:', items=inconsistent)) |
| 61 | return output |
| 62 | |
| 63 | def CheckChange(input_api, output_api, committing): |
| 64 | ebuilds = lambda x: input_api.FilterSourceFile(x, white_list=_EBUILD_FILES) |
| 65 | results = [] |
| 66 | results += Check9999Updated(input_api, output_api, |
| 67 | source_file_filter=ebuilds) |
| 68 | return results |
| 69 | |
| 70 | def CheckChangeOnUpload(input_api, output_api): |
| 71 | return CheckChange(input_api, output_api, False) |
| 72 | |
| 73 | def CheckChangeOnCommit(input_api, output_api): |
| 74 | return CheckChange(input_api, output_api, True) |