LaMont Jones | d3e2cc7 | 2020-06-04 18:41:42 -0600 | [diff] [blame] | 1 | # 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 | |
| 6 | import json |
| 7 | |
Jeremy Bettis | 2d5c01b | 2021-05-04 15:57:40 -0600 | [diff] [blame] | 8 | USE_PYTHON3 = True |
| 9 | |
LaMont Jones | d3e2cc7 | 2020-06-04 18:41:42 -0600 | [diff] [blame] | 10 | |
| 11 | def 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 Edelston | dd80aa1 | 2020-10-14 12:31:52 -0600 | [diff] [blame] | 32 | def 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 Edelston | 62931ec | 2020-10-27 09:52:16 -0600 | [diff] [blame] | 61 | 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 Edelston | dd80aa1 | 2020-10-14 12:31:52 -0600 | [diff] [blame] | 80 | 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 Jones | d3e2cc7 | 2020-06-04 18:41:42 -0600 | [diff] [blame] | 87 | def CommonCheck(input_api, output_api): |
| 88 | results = [] |
| 89 | results.extend(VerifyJson(input_api, output_api)) |
Greg Edelston | dd80aa1 | 2020-10-14 12:31:52 -0600 | [diff] [blame] | 90 | results.extend(VerifyConsolidated(input_api, output_api)) |
Greg Edelston | fa64921 | 2020-10-13 14:40:38 -0600 | [diff] [blame] | 91 | 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 Jones | d3e2cc7 | 2020-06-04 18:41:42 -0600 | [diff] [blame] | 96 | return results |
| 97 | |
| 98 | |
| 99 | def CheckChangeOnCommit(input_api, output_api): |
| 100 | results = [] |
| 101 | results.extend(CommonCheck(input_api, output_api)) |
| 102 | return results |
| 103 | |
| 104 | |
| 105 | def CheckChangeOnUpload(input_api, output_api): |
| 106 | results = [] |
| 107 | results.extend(CommonCheck(input_api, output_api)) |
| 108 | return results |