blob: 16cf35408d6de476af13ed5c7b152b218def852f [file] [log] [blame]
LaMont Jonesd3e2cc72020-06-04 18:41:42 -06001# Copyright 2020 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"""Presubmit checks."""
5
6import json
7
Jeremy Bettis2d5c01b2021-05-04 15:57:40 -06008USE_PYTHON3 = True
9
LaMont Jonesd3e2cc72020-06-04 18:41:42 -060010
11def VerifyJson(input_api, output_api):
12 """Verify that the json files contain valid JSON."""
13
14 def json_file_filter(f):
15 return f.LocalPath().endswith('.json')
16
17 results = []
18 for affected in input_api.AffectedFiles(
19 include_deletes=False, file_filter=json_file_filter):
20 name = affected.LocalPath()
21 with open(name) as f:
22 try:
23 json.load(f)
24 except Exception as e:
25 err = output_api.PresubmitError(
26 'Bad JSON', items=[name], long_text=str(e))
27 results.append(err)
28
29 return results
30
31
Greg Edelstondd80aa12020-10-14 12:31:52 -060032def VerifyConsolidated(input_api, output_api):
33 """Verify that re-running consolidate.py creates no diff."""
34 results = []
35 script_basename = 'consolidate.py'
36 script_fullpath = input_api.os_path.join(input_api.PresubmitLocalPath(),
37 script_basename)
38 json_basename = 'CONSOLIDATED.json'
39 json_fullpath = input_api.os_path.join(input_api.PresubmitLocalPath(),
40 json_basename)
41 tmp_json = input_api.os_path.join('/tmp', json_basename)
42 if not input_api.os_path.isfile(json_fullpath):
43 msg = 'Error: %s not found. Please run %s and try again.' % (
44 json_basename, script_basename)
45 results.append(output_api.PresubmitError(msg))
46 elif input_api.subprocess.call([script_fullpath, '-o', tmp_json],
47 stdout=input_api.subprocess.PIPE,
48 stderr=input_api.subprocess.PIPE):
49 msg = 'Error: %s failed. Please fix and try again.' % script_basename
50 results.append(output_api.PresubmitError(msg))
51 elif not input_api.os_path.isfile(tmp_json):
52 msg = 'Error: %s did not produce output. Please fix and try again.' % (
53 script_basename)
54 results.append(output_api.PresubmitError(msg))
55 elif input_api.subprocess.call(['diff', json_fullpath, tmp_json],
56 stdout=input_api.subprocess.PIPE,
57 stderr=input_api.subprocess.PIPE):
58 msg = ('Error: Running %s produced a diff. Please run the script, '
59 'amend your changes, and try again.') % script_basename
60 results.append(output_api.PresubmitError(msg))
Greg Edelston62931ec2020-10-27 09:52:16 -060061 elif input_api.subprocess.call(['git',
62 'diff',
63 '--exit-code',
64 json_fullpath],
65 stdout=input_api.subprocess.PIPE,
66 stderr=input_api.subprocess.PIPE):
67 msg = ('Error: %s has unstaged changes. Please amend your commit and '
68 'try again.') % json_fullpath
69 results.append(output_api.PresubmitError(msg))
70 elif input_api.subprocess.call(['git',
71 'diff',
72 '--exit-code',
73 '--staged',
74 json_fullpath],
75 stdout=input_api.subprocess.PIPE,
76 stderr=input_api.subprocess.PIPE):
77 msg = ('Error: %s has uncommitted changes. Please commit your staged '
78 'changes and try again.') % json_fullpath
79 results.append(output_api.PresubmitError(msg))
Greg Edelstondd80aa12020-10-14 12:31:52 -060080 if input_api.os_path.isfile(tmp_json):
81 input_api.subprocess.call(['rm', '-f', tmp_json],
82 stdout=input_api.subprocess.PIPE,
83 stderr=input_api.subprocess.PIPE)
84 return results
85
86
LaMont Jonesd3e2cc72020-06-04 18:41:42 -060087def CommonCheck(input_api, output_api):
88 results = []
89 results.extend(VerifyJson(input_api, output_api))
Greg Edelstondd80aa12020-10-14 12:31:52 -060090 results.extend(VerifyConsolidated(input_api, output_api))
Greg Edelstonfa649212020-10-13 14:40:38 -060091 results.extend(input_api.canned_checks.RunUnitTests(
92 input_api, output_api,
93 [input_api.os_path.join(
94 input_api.PresubmitLocalPath(), 'consolidate_unittest.py')],
95 run_on_python2=False))
LaMont Jonesd3e2cc72020-06-04 18:41:42 -060096 return results
97
98
99def CheckChangeOnCommit(input_api, output_api):
100 results = []
101 results.extend(CommonCheck(input_api, output_api))
102 return results
103
104
105def CheckChangeOnUpload(input_api, output_api):
106 results = []
107 results.extend(CommonCheck(input_api, output_api))
108 return results