blob: 934f4660fd47132d512b9a0d1ae03689596d295b [file] [log] [blame]
Mandeep Singh Bainese9cd41e2010-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
13
14_EBUILD_FILES = (
15 r".*\.ebuild",
16)
17
18def _IsCrosWorkonEbuild(ebuild_contents):
19 return ebuild_contents.count('inherit cros-workon') > 0
20
21def Check9999Updated(input_api, output_api, source_file_filter=None):
22 """Checks that the 9999 ebuild was also modified."""
23 output = []
24 inconsistent = []
25 missing_9999 = set()
26 for f in input_api.AffectedSourceFiles(source_file_filter):
27 ebuild_contents = f.NewContents()
28 # only look at non-9999
29 if f.LocalPath().endswith('-9999.ebuild'):
30 continue
31 if _IsCrosWorkonEbuild(ebuild_contents):
32 dir = os.path.dirname(f.AbsoluteLocalPath())
33 ebuild = os.path.basename(dir)
34 devebuild_path = os.path.join(dir, ebuild + '-9999.ebuild')
35 # check if 9999 ebuild exists
36 if not os.path.isfile(devebuild_path):
37 missing_9999.add(ebuild)
38 continue
39 diff = difflib.ndiff(ebuild_contents,
40 open(devebuild_path).read().splitlines())
41 for line in diff:
42 if line.startswith('+') or line.startswith('-'):
43 # ignore empty-lines
44 if len(line) == 2:
45 continue
46 if not (line[2:].startswith('KEYWORDS=') or
47 line[2:].startswith('CROS_WORKON_COMMIT=')):
48 inconsistent.append(f.LocalPath())
49
50 if missing_9999:
51 output.append(output_api.PresubmitPromptWarning(
52 'Missing 9999 for these cros-workon ebuilds:', items=missing_9999))
53 if inconsistent:
54 output.append(output_api.PresubmitPromptWarning(
55 'Following ebuilds are inconsistent with 9999:', items=inconsistent))
56 return output
57
58def CheckChange(input_api, output_api, committing):
59 ebuilds = lambda x: input_api.FilterSourceFile(x, white_list=_EBUILD_FILES)
60 results = []
61 results += Check9999Updated(input_api, output_api,
62 source_file_filter=ebuilds)
63 return results
64
65def CheckChangeOnUpload(input_api, output_api):
66 return CheckChange(input_api, output_api, False)
67
68def CheckChangeOnCommit(input_api, output_api):
69 return CheckChange(input_api, output_api, True)