1 # Copyright (c) 2012 The Native Client 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.
5 # Documentation on PRESUBMIT.py can be found at:
6 # http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 from __future__ import print_function
16 # List of directories to not apply presubmit project checks, relative
17 # to the NaCl top directory
18 EXCLUDE_PROJECT_CHECKS = [
19 # The following contain test data (including automatically generated),
20 # and do not follow our conventions.
21 'src/trusted/validator_ragel/testdata/32/',
22 'src/trusted/validator_ragel/testdata/64/',
23 'src/trusted/validator_x86/testdata/32/',
24 'src/trusted/validator_x86/testdata/64/',
25 'src/trusted/validator/x86/decoder/generator/testdata/32/',
26 'src/trusted/validator/x86/decoder/generator/testdata/64/',
27 # The following directories contains automatically generated source,
28 # which may not follow our conventions.
29 'src/trusted/validator_x86/gen/',
30 'src/trusted/validator/x86/decoder/gen/',
31 'src/trusted/validator/x86/decoder/generator/gen/',
32 'src/trusted/validator/x86/ncval_seg_sfi/gen/',
33 'src/trusted/validator_arm/gen/',
34 'src/trusted/validator_ragel/gen/',
35 # The following files contain code from outside native_client (e.g. newlib)
36 # or are known the contain style violations.
37 'src/trusted/service_runtime/include/sys/',
38 'src/include/win/port_win.h',
39 'src/trusted/service_runtime/include/machine/_types.h'
42 NACL_TOP_DIR = os.getcwd()
43 while not os.path.isfile(os.path.join(NACL_TOP_DIR, 'PRESUBMIT.py')):
44 NACL_TOP_DIR = os.path.dirname(NACL_TOP_DIR)
45 assert len(NACL_TOP_DIR) >= 3, "Could not find NaClTopDir"
48 def _CommonChecks(input_api, output_api):
49 """Checks for both upload and commit."""
52 results.extend(input_api.canned_checks.PanProjectChecks(
53 input_api, output_api, project_name='Native Client',
54 excluded_paths=tuple(EXCLUDE_PROJECT_CHECKS)))
56 # The commit queue assumes PRESUBMIT.py is standalone.
57 # TODO(bradnelson): Migrate code_hygiene to a common location so that
58 # it can be used by the commit queue.
59 old_sys_path = list(sys.path)
61 sys.path.append(os.path.join(NACL_TOP_DIR, 'tools'))
62 sys.path.append(os.path.join(NACL_TOP_DIR, 'build'))
65 sys.path = old_sys_path
68 affected_files = input_api.AffectedFiles(include_deletes=False)
69 exclude_dirs = [ NACL_TOP_DIR + '/' + x for x in EXCLUDE_PROJECT_CHECKS ]
70 for filename in affected_files:
71 filename = filename.AbsoluteLocalPath()
72 if filename in exclude_dirs:
74 if not IsFileInDirectories(filename, exclude_dirs):
75 errors, warnings = code_hygiene.CheckFile(filename, False)
77 results.append(output_api.PresubmitError(e, items=errors[e]))
79 results.append(output_api.PresubmitPromptWarning(w, items=warnings[w]))
84 def IsFileInDirectories(f, dirs):
85 """ Returns true if f is in list of directories"""
87 if d is os.path.commonprefix([f, d]):
92 def CheckChangeOnUpload(input_api, output_api):
93 """Verifies all changes in all files.
95 input_api: the limited set of input modules allowed in presubmit.
96 output_api: the limited set of output modules allowed in presubmit.
99 report.extend(_CommonChecks(input_api, output_api))
103 def CheckChangeOnCommit(input_api, output_api):
104 """Verifies all changes in all files and verifies that the
105 tree is open and can accept a commit.
107 input_api: the limited set of input modules allowed in presubmit.
108 output_api: the limited set of output modules allowed in presubmit.
111 report.extend(_CommonChecks(input_api, output_api))