blob: 34810a98ff37726fda9d2f79be23e2d78c86580e [file] [log] [blame]
Mandeep Singh Baines67b70ef52010-07-26 17:26:44 -07001# 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
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into gcl and git cl.
9"""
10
11import difflib
12import os
Mandeep Singh Bainesd19a4ff2010-07-27 14:59:26 -070013import re
Mandeep Singh Baines67b70ef52010-07-26 17:26:44 -070014
15_EBUILD_FILES = (
16 r".*\.ebuild",
17)
18
19def _IsCrosWorkonEbuild(ebuild_contents):
Mandeep Singh Bainesd19a4ff2010-07-27 14:59:26 -070020 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 Baines67b70ef52010-07-26 17:26:44 -070025
26def Check9999Updated(input_api, output_api, source_file_filter=None):
27 """Checks that the 9999 ebuild was also modified."""
28 output = []
Mandeep Singh Baines521c5a62010-07-28 07:46:01 -070029 inconsistent = set()
Mandeep Singh Baines67b70ef52010-07-26 17:26:44 -070030 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 Baines521c5a62010-07-28 07:46:01 -070053 inconsistent.add(f.LocalPath())
Mandeep Singh Baines67b70ef52010-07-26 17:26:44 -070054
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
63def 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
70def CheckChangeOnUpload(input_api, output_api):
71 return CheckChange(input_api, output_api, False)
72
73def CheckChangeOnCommit(input_api, output_api):
74 return CheckChange(input_api, output_api, True)