Ruff: reformat tools (part 3/3: perf .. win). Preserve yapf style to the best capacity. LSC doc: https://docs.google.com/document/d/1P6AE9aeKuFkxPAYInYxLFIpDgpiCAT-S_BQkNU0lb3s/edit?tab=t.0 Onboarded using the following command: `~/cr/depot_tools/ruff_chromium format .` NO_IFTTT=reformatting only. BYPASS_RECITATION_REASON=reformatting only. Bug: 40874143 Change-Id: I971d8d3ad1a0dee3998a9eaa25c0617c6a6a6964 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8254624 Commit-Queue: Junji Watanabe <jwata@google.com> Owners-Override: Alex Ovsienko <ovsienko@google.com> SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com> Commit-Queue: Alex Ovsienko <ovsienko@google.com> Auto-Submit: Alex Ovsienko <ovsienko@google.com> Reviewed-by: Junji Watanabe <jwata@google.com> Cr-Commit-Position: refs/heads/main@{#1680337} NOKEYCHECK=True GitOrigin-RevId: 54e9909c3189a77bc8d353662628e4c5d6686290
diff --git a/protoc-gen-ts_proto.py b/protoc-gen-ts_proto.py index a048ff1..ae3b2ed 100755 --- a/protoc-gen-ts_proto.py +++ b/protoc-gen-ts_proto.py
@@ -23,4 +23,4 @@ if __name__ == '__main__': - main(sys.argv[1:]) \ No newline at end of file + main(sys.argv[1:])
diff --git a/protoc_convert.py b/protoc_convert.py index 4c730ce..887581f 100644 --- a/protoc_convert.py +++ b/protoc_convert.py
@@ -10,20 +10,28 @@ import argparse import subprocess + def Main(): parser = argparse.ArgumentParser() parser.add_argument('--protoc', help='Path to protoc compiler.') - parser.add_argument('--infile', required=True, - help='Path to input file that will be used as stdin.') - parser.add_argument('--outfile', required=True, - help='Path to output file that will be used as stdout.') + parser.add_argument( + '--infile', + required=True, + help='Path to input file that will be used as stdin.', + ) + parser.add_argument( + '--outfile', + required=True, + help='Path to output file that will be used as stdout.', + ) args, passthrough_args = parser.parse_known_args() stdin = open(args.infile, 'r') stdout = open(args.outfile, 'w') - subprocess.check_call([args.protoc] + passthrough_args, stdin=stdin, - stdout=stdout) + subprocess.check_call( + [args.protoc] + passthrough_args, stdin=stdin, stdout=stdout + ) if __name__ == '__main__':
diff --git a/protoc_wrapper.py b/protoc_wrapper.py index 1ff0472..a2ff59f 100755 --- a/protoc_wrapper.py +++ b/protoc_wrapper.py
@@ -40,14 +40,17 @@ def VerifyProtoNames(protos): for filename in protos: if "-" in filename: - raise RuntimeError("Proto file names must not contain hyphens " - "(see http://crbug.com/386125 for more information).") + raise RuntimeError( + "Proto file names must not contain hyphens " + "(see http://crbug.com/386125 for more information)." + ) def StripProtoExtension(filename): if not filename.endswith(".proto"): - raise RuntimeError("Invalid proto filename extension: " - "{0} .".format(filename)) + raise RuntimeError( + "Invalid proto filename extension: {0} .".format(filename) + ) return filename.rsplit(".", 1)[0] @@ -61,8 +64,11 @@ modified = False for i, line in enumerate(itertools.islice(lines, 50)): if "@bufbuild/protobuf/" in line: - lines[i] = re.sub(r"'@bufbuild\/protobuf\/(\w+)'", - r"'/@bufbuild/protobuf/\1/index.js'", line) + lines[i] = re.sub( + r"'@bufbuild\/protobuf\/(\w+)'", + r"'/@bufbuild/protobuf/\1/index.js'", + line, + ) modified = True if modified: f.seek(0) @@ -88,8 +94,9 @@ contents.append(extra_statement) if not include_point_found: - raise RuntimeError("Include point not found in header: " - "{0} .".format(filename)) + raise RuntimeError( + "Include point not found in header: {0} .".format(filename) + ) with open(filename, "w") as f: for line in contents: @@ -98,58 +105,78 @@ def main(argv): parser = argparse.ArgumentParser() - parser.add_argument("--protoc", required=True, - help="Relative path to compiler.") - - parser.add_argument("--proto-in-dir", required=True, - help="Base directory with source protos.") - parser.add_argument("--cc-out-dir", - help="Output directory for standard C++ generator.") - parser.add_argument("--py-out-dir", - help="Output directory for standard Python generator.") - parser.add_argument("--js-out-dir", - help="Output directory for standard JS generator.") - parser.add_argument("--protoc-gen-js", - help="Relative path to javascript compiler.") - parser.add_argument("--ts-out-dir", - help="Output directory for standard TS generator.") - parser.add_argument("--protoc-gen-ts", - help="Relative path to typescript compiler.") - - parser.add_argument("--plugin-out-dir", - help="Output directory for custom generator plugin.") - - parser.add_argument('--enable-kythe-annotations', action='store_true', - help='Enable generation of Kythe kzip, used for ' - 'codesearch.') - parser.add_argument("--plugin", - help="Relative path to custom generator plugin.") - parser.add_argument("--plugin-options", - help="Custom generator plugin options.") - parser.add_argument("--cc-options", - help="Standard C++ generator options.") - parser.add_argument("--include", - help="Name of include to insert into generated headers.") - parser.add_argument("--import-dir", action="append", default=[], - help="Extra import directory for protos, can be repeated." - ) - parser.add_argument("--descriptor-set-out", - help="Path to write a descriptor.") parser.add_argument( - "--descriptor-set-dependency-file", - help="Path to write the dependency file for descriptor set.") + "--protoc", required=True, help="Relative path to compiler." + ) + + parser.add_argument( + "--proto-in-dir", required=True, help="Base directory with source protos." + ) + parser.add_argument( + "--cc-out-dir", help="Output directory for standard C++ generator." + ) + parser.add_argument( + "--py-out-dir", help="Output directory for standard Python generator." + ) + parser.add_argument( + "--js-out-dir", help="Output directory for standard JS generator." + ) + parser.add_argument( + "--protoc-gen-js", help="Relative path to javascript compiler." + ) + parser.add_argument( + "--ts-out-dir", help="Output directory for standard TS generator." + ) + parser.add_argument( + "--protoc-gen-ts", help="Relative path to typescript compiler." + ) + + parser.add_argument( + "--plugin-out-dir", help="Output directory for custom generator plugin." + ) + + parser.add_argument( + '--enable-kythe-annotations', + action='store_true', + help='Enable generation of Kythe kzip, used for codesearch.', + ) + parser.add_argument( + "--plugin", help="Relative path to custom generator plugin." + ) + parser.add_argument( + "--plugin-options", help="Custom generator plugin options." + ) + parser.add_argument("--cc-options", help="Standard C++ generator options.") + parser.add_argument( + "--include", help="Name of include to insert into generated headers." + ) + parser.add_argument( + "--import-dir", + action="append", + default=[], + help="Extra import directory for protos, can be repeated.", + ) + parser.add_argument( + "--descriptor-set-out", help="Path to write a descriptor." + ) + parser.add_argument( + "--descriptor-set-dependency-file", + help="Path to write the dependency file for descriptor set.", + ) # The meaning of this flag is flipped compared to the corresponding protoc # flag due to this script previously passing --include_imports. Removing the # --include_imports is likely to have unintended consequences. parser.add_argument( - "--exclude-imports", - help="Do not include imported files into generated descriptor.", - action="store_true", - default=False) + "--exclude-imports", + help="Do not include imported files into generated descriptor.", + action="store_true", + default=False, + ) parser.add_argument('--fatal_warnings', action='store_true') - parser.add_argument("protos", nargs="+", - help="Input protobuf definition file(s).") + parser.add_argument( + "protos", nargs="+", help="Input protobuf definition file(s)." + ) options = parser.parse_args(argv) @@ -169,17 +196,16 @@ if options.js_out_dir: protoc_cmd += [ - "--js_out", - "one_output_file_per_input_file,binary:" + options.js_out_dir, - "--plugin=protoc-gen-js=" + os.path.realpath(options.protoc_gen_js), + "--js_out", + "one_output_file_per_input_file,binary:" + options.js_out_dir, + "--plugin=protoc-gen-js=" + os.path.realpath(options.protoc_gen_js), ] if options.ts_out_dir: protoc_cmd += [ - "--ts_proto_out=" + options.ts_out_dir, - "--ts_proto_opt=env=browser,esModuleInterop=true,importSuffix=.js", - "--ts_proto_opt=useOptionals=all", - "--plugin=protoc-gen-ts_proto=" + - os.path.realpath(options.protoc_gen_ts), + "--ts_proto_out=" + options.ts_out_dir, + "--ts_proto_opt=env=browser,esModuleInterop=true,importSuffix=.js", + "--ts_proto_opt=useOptionals=all", + "--plugin=protoc-gen-ts_proto=" + os.path.realpath(options.protoc_gen_ts), ] for filename in protos: stripped_name = StripProtoExtension(filename) @@ -189,10 +215,13 @@ cc_out_dir = options.cc_out_dir cc_options_list = [] if options.enable_kythe_annotations: - cc_options_list.extend([ - 'annotate_headers', 'annotation_pragma_name=kythe_metadata', - 'annotation_guard_name=KYTHE_IS_RUNNING' - ]) + cc_options_list.extend( + [ + 'annotate_headers', + 'annotation_pragma_name=kythe_metadata', + 'annotation_guard_name=KYTHE_IS_RUNNING', + ] + ) # cc_options will likely have trailing colon so needs to be inserted at the # end. @@ -208,8 +237,10 @@ if options.plugin_out_dir: plugin_options = FormatGeneratorOptions(options.plugin_options) protoc_cmd += [ - "--plugin", "protoc-gen-plugin=" + os.path.relpath(options.plugin), - "--plugin_out", plugin_options + options.plugin_out_dir + "--plugin", + "protoc-gen-plugin=" + os.path.relpath(options.plugin), + "--plugin_out", + plugin_options + options.plugin_out_dir, ] protoc_cmd += ["--proto_path", proto_dir] @@ -244,8 +275,9 @@ error_number = "0x%08X" % (ret + (1 << 32)) else: error_number = "%d" % ret - raise RuntimeError("Protoc has returned non-zero status: " - "{0}".format(error_number)) + raise RuntimeError( + "Protoc has returned non-zero status: {0}".format(error_number) + ) if dependency_file_data: with open(options.descriptor_set_dependency_file, 'w') as f:
diff --git a/protoc_wrapper_test.py b/protoc_wrapper_test.py index c133486..5e54978 100755 --- a/protoc_wrapper_test.py +++ b/protoc_wrapper_test.py
@@ -24,84 +24,164 @@ @mock.patch('subprocess.call', return_value=0) def test_minimal_arguments(self, mock_call): protoc_wrapper.main( - ['--proto-in-dir', './', '--protoc', '/foo/protoc', 'foo.proto']) + ['--proto-in-dir', './', '--protoc', '/foo/protoc', 'foo.proto'] + ) mock_call.assert_called_once_with( - ['/foo/protoc', '--proto_path', '.', './foo.proto']) + ['/foo/protoc', '--proto_path', '.', './foo.proto'] + ) @mock.patch('subprocess.call', return_value=0) def test_kythe_no_out(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--enable-kythe-annotation', '--protoc', - '/foo/protoc', 'foo.proto' - ]) + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--enable-kythe-annotation', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) mock_call.assert_called_once_with( - ['/foo/protoc', '--proto_path', '.', './foo.proto']) + ['/foo/protoc', '--proto_path', '.', './foo.proto'] + ) @mock.patch('subprocess.call', return_value=0) def test_kythe_cpp_out_no_options(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--enable-kythe-annotation', '--cc-out-dir', - './bar', '--protoc', '/foo/protoc', 'foo.proto' - ]) - mock_call.assert_called_once_with([ - '/foo/protoc', '--cpp_out', + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--enable-kythe-annotation', + '--cc-out-dir', + './bar', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) + mock_call.assert_called_once_with( + [ + '/foo/protoc', + '--cpp_out', 'annotate_headers,annotation_pragma_name=kythe_metadata,annotation_guard_name=KYTHE_IS_RUNNING:./bar', - '--proto_path', '.', './foo.proto' - ]) + '--proto_path', + '.', + './foo.proto', + ] + ) @mock.patch('subprocess.call', return_value=0) def test_kythe_cpp_out_with_options(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--enable-kythe-annotation', '--cc-options', - 'foo=bar:', '--cc-out-dir', './bar', '--protoc', '/foo/protoc', - 'foo.proto' - ]) - mock_call.assert_called_once_with([ - '/foo/protoc', '--cpp_out', + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--enable-kythe-annotation', + '--cc-options', + 'foo=bar:', + '--cc-out-dir', + './bar', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) + mock_call.assert_called_once_with( + [ + '/foo/protoc', + '--cpp_out', 'annotate_headers,annotation_pragma_name=kythe_metadata,annotation_guard_name=KYTHE_IS_RUNNING,foo=bar:./bar', - '--proto_path', '.', './foo.proto' - ]) + '--proto_path', + '.', + './foo.proto', + ] + ) @mock.patch('subprocess.call', return_value=0) def test_kythe_cpp_out_with_options_no_colon(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--enable-kythe-annotation', '--cc-options', - 'foo=bar', '--cc-out-dir', './bar', '--protoc', '/foo/protoc', - 'foo.proto' - ]) - mock_call.assert_called_once_with([ - '/foo/protoc', '--cpp_out', + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--enable-kythe-annotation', + '--cc-options', + 'foo=bar', + '--cc-out-dir', + './bar', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) + mock_call.assert_called_once_with( + [ + '/foo/protoc', + '--cpp_out', 'annotate_headers,annotation_pragma_name=kythe_metadata,annotation_guard_name=KYTHE_IS_RUNNING,foo=bar:./bar', - '--proto_path', '.', './foo.proto' - ]) + '--proto_path', + '.', + './foo.proto', + ] + ) @mock.patch('subprocess.call', return_value=0) def test_cpp_out_with_options_no_colon(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--cc-options', 'foo=bar:', '--cc-out-dir', - './bar', '--protoc', '/foo/protoc', 'foo.proto' - ]) - mock_call.assert_called_once_with([ - '/foo/protoc', '--cpp_out', 'foo=bar:./bar', '--proto_path', '.', - './foo.proto' - ]) + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--cc-options', + 'foo=bar:', + '--cc-out-dir', + './bar', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) + mock_call.assert_called_once_with( + [ + '/foo/protoc', + '--cpp_out', + 'foo=bar:./bar', + '--proto_path', + '.', + './foo.proto', + ] + ) @mock.patch('subprocess.call', return_value=0) def test_ts_out_with_options_no_colon(self, mock_call): - protoc_wrapper.main([ - '--proto-in-dir', './', '--ts-out-dir', './bar', '--protoc-gen-ts', - '/foo/protoc-gen-ts_proto', '--protoc', '/foo/protoc', 'foo.proto' - ]) - mock_call.assert_called_once_with([ - '/foo/protoc', '--ts_proto_out=./bar', + protoc_wrapper.main( + [ + '--proto-in-dir', + './', + '--ts-out-dir', + './bar', + '--protoc-gen-ts', + '/foo/protoc-gen-ts_proto', + '--protoc', + '/foo/protoc', + 'foo.proto', + ] + ) + mock_call.assert_called_once_with( + [ + '/foo/protoc', + '--ts_proto_out=./bar', '--ts_proto_opt=env=browser,esModuleInterop=true,importSuffix=.js', '--ts_proto_opt=useOptionals=all', - '--plugin=protoc-gen-ts_proto=/foo/protoc-gen-ts_proto', '--proto_path', - '.', './foo.proto' - ]) + '--plugin=protoc-gen-ts_proto=/foo/protoc-gen-ts_proto', + '--proto_path', + '.', + './foo.proto', + ] + ) if __name__ == '__main__': logging.basicConfig( - level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) + level=logging.DEBUG if '-v' in sys.argv else logging.ERROR + ) unittest.main()