Brian Ryner | 3ff8bea | 2024-01-02 00:32:13 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 2 | # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 6 | """Python wrapper for various Go tools. |
| 7 | |
| 8 | Intended to be used from a PRESUBMIT check. |
| 9 | """ |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 10 | |
| 11 | import os |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 15 | WORKSPACE_ROOT = os.path.dirname(os.path.abspath(__file__)) |
| 16 | |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 17 | |
Takuto Ikuta | 875c274 | 2019-02-28 16:38:50 | [diff] [blame] | 18 | def group_by_dir(filestream): |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 19 | """Generates directories of the files in the given filestream. |
| 20 | |
| 21 | If the filestream has paths in sorted order, then directories |
| 22 | shouldn't be repeated. |
Vadim Shtayura | 5c22e8e | 2021-07-13 21:19:59 | [diff] [blame] | 23 | |
| 24 | Skips files that reside in a "testdata" directory. |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 25 | """ |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 26 | prefix = None |
| 27 | group = [] |
| 28 | for fname in filestream: |
| 29 | dirname = os.path.dirname(fname) |
Vadim Shtayura | 5c22e8e | 2021-07-13 21:19:59 | [diff] [blame] | 30 | if '/testdata/' in os.path.normpath(dirname): |
| 31 | continue |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 32 | if dirname != prefix: |
| 33 | if group: |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 34 | yield prefix |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 35 | prefix = dirname |
| 36 | group = [] |
| 37 | group.append(fname) |
| 38 | if group: |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 39 | yield prefix |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 40 | |
| 41 | |
Takuto Ikuta | 875c274 | 2019-02-28 16:38:50 | [diff] [blame] | 42 | def mk_checker(*tool_name): |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 43 | """Creates and returns a "main" function. |
| 44 | |
| 45 | The returned function invokes the tool and returns the retcode-style result. |
| 46 | """ |
| 47 | tool_cmd = list(tool_name) |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 48 | |
| 49 | def _inner(_verbose, filestream): |
Stephen Martinis | 2f07162 | 2016-08-19 18:00:13 | [diff] [blame] | 50 | found_errs = [] |
| 51 | retcode = 0 |
Robert Iannucci | 2c2344f | 2018-12-11 00:32:41 | [diff] [blame] | 52 | |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 53 | for dpath in group_by_dir(filestream): |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 54 | proc = subprocess.Popen( |
Vadim Shtayura | 33fc806 | 2021-07-14 22:55:39 | [diff] [blame] | 55 | tool_cmd + ['.'], |
| 56 | cwd=dpath, |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 57 | stdout=subprocess.PIPE, |
Takuto Ikuta | f0ac901 | 2022-06-24 01:42:11 | [diff] [blame] | 58 | stderr=subprocess.STDOUT, |
| 59 | universal_newlines=True) |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 60 | out = proc.communicate()[0].strip() |
Vadim Shtayura | 33fc806 | 2021-07-14 22:55:39 | [diff] [blame] | 61 | if proc.returncode and 'build constraints exclude all Go files' in out: |
| 62 | continue |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 63 | if out or proc.returncode: |
Stephen Martinis | 2f07162 | 2016-08-19 18:00:13 | [diff] [blame] | 64 | found_errs.append(out or 'Unrecognized error') |
| 65 | retcode = proc.returncode or 1 |
| 66 | |
| 67 | for err in found_errs: |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 68 | print(err) |
Stephen Martinis | 2f07162 | 2016-08-19 18:00:13 | [diff] [blame] | 69 | return retcode |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 70 | return _inner |
| 71 | |
| 72 | |
| 73 | def gofmt_main(verbose, filestream): |
Quinten Yearsley | 110d612 | 2019-05-28 22:10:49 | [diff] [blame] | 74 | """Reads list of paths from stdin. |
| 75 | |
| 76 | Expects go toolset to be in PATH (use ./env.py to set this up). |
| 77 | """ |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 78 | def check_file(path): |
| 79 | proc = subprocess.Popen( |
vadimsh | cd7ed9f | 2015-10-08 19:13:48 | [diff] [blame] | 80 | ['gofmt', '-s', '-d', path], |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 81 | stdout=subprocess.PIPE, |
| 82 | stderr=subprocess.PIPE) |
| 83 | out, err = proc.communicate() |
| 84 | if proc.returncode: |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 85 | print(err, end=' ') |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 86 | return False |
| 87 | if out: |
| 88 | if verbose: |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 89 | print(out, end=' ') |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 90 | return False |
| 91 | return True |
| 92 | |
| 93 | bad = [] |
| 94 | for fpath in filestream: |
| 95 | if not check_file(fpath): |
| 96 | bad.append(fpath) |
| 97 | if bad: |
| 98 | root = WORKSPACE_ROOT.rstrip(os.sep) + os.sep |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 99 | print('Badly formated Go files:') |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 100 | for p in bad: |
| 101 | if p.startswith(root): |
| 102 | p = p[len(root):] |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 103 | print(' %s' % p) |
| 104 | print() |
| 105 | print('Consider running \'gofmt -s -w /path/to/infra\'') |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 106 | return 0 if not bad else 1 |
| 107 | |
| 108 | |
| 109 | def show_help(): |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 110 | print('Usage: check.py [--verbose] <tool>') |
| 111 | print('List of file paths to operate is read from stdin.') |
| 112 | print('Available tools:') |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 113 | for x in TOOL_FUNC: |
Takuto Ikuta | 54517f4 | 2021-03-10 06:04:49 | [diff] [blame] | 114 | print(" *", x) |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 115 | sys.exit(1) |
| 116 | |
| 117 | |
| 118 | def main(args): |
| 119 | if len(args) < 1 or args[0] not in TOOL_FUNC: |
| 120 | show_help() |
| 121 | |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 122 | verbose = '--verbose' in args |
| 123 | |
| 124 | def filestream(): |
| 125 | for path in sys.stdin: |
| 126 | path = path.rstrip() |
Vadim Shtayura | fe4263b | 2018-11-12 21:17:31 | [diff] [blame] | 127 | if path: |
| 128 | yield path |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 129 | |
| 130 | return TOOL_FUNC[args[0]](verbose, filestream()) |
| 131 | |
| 132 | |
| 133 | TOOL_FUNC = { |
Gregory Nisbet | 4a5aedc | 2022-05-10 03:24:19 | [diff] [blame] | 134 | # TODO(gregorynisbet): tricium copies around proto messages. |
| 135 | # Disable copylock analysis for now. |
| 136 | "govet": mk_checker("go", "vet", "-copylocks=false"), |
iannucci | 20f4180 | 2015-06-10 00:01:56 | [diff] [blame] | 137 | "gofmt": gofmt_main, |
| 138 | } |
| 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | sys.exit(main(sys.argv[1:])) |