Andrew Grieve | e6c6bb38 | 2021-04-27 21:47:08 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 2 | # |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 3 | # Copyright 2020 The Chromium Authors |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import argparse |
| 8 | import os |
| 9 | import re |
| 10 | import sys |
| 11 | import zipfile |
| 12 | |
| 13 | from util import build_utils |
| 14 | from util import java_cpp_utils |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 15 | import action_helpers # build_utils adds //build to sys.path. |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 16 | import zip_helpers |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate): |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 20 | # Ex. 'BASE_FEATURE(kConstantName, "StringNameOfTheFeature", ...);' |
| 21 | # would parse as: |
| 22 | # ExtractConstantName() -> 'ConstantName' |
| 23 | # ExtractValue() -> '"StringNameOfTheFeature"' |
| 24 | FEATURE_RE = re.compile(r'BASE_FEATURE\(k([^,]+),') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 25 | VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,') |
| 26 | |
| 27 | def ExtractConstantName(self, line): |
| 28 | match = FeatureParserDelegate.FEATURE_RE.match(line) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 29 | return match.group(1) if match else None |
| 30 | |
| 31 | def ExtractValue(self, line): |
| 32 | match = FeatureParserDelegate.VALUE_RE.search(line) |
| 33 | return match.group(1) if match else None |
| 34 | |
| 35 | def CreateJavaConstant(self, name, value, comments): |
| 36 | return java_cpp_utils.JavaString(name, value, comments) |
| 37 | |
| 38 | |
| 39 | def _GenerateOutput(template, source_paths, template_path, features): |
| 40 | description_template = """ |
| 41 | // This following string constants were inserted by |
| 42 | // {SCRIPT_NAME} |
| 43 | // From |
| 44 | // {SOURCE_PATHS} |
| 45 | // Into |
| 46 | // {TEMPLATE_PATH} |
| 47 | |
| 48 | """ |
| 49 | values = { |
| 50 | 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), |
| 51 | 'SOURCE_PATHS': ',\n // '.join(source_paths), |
| 52 | 'TEMPLATE_PATH': template_path, |
| 53 | } |
| 54 | description = description_template.format(**values) |
| 55 | native_features = '\n\n'.join(x.Format() for x in features) |
| 56 | |
| 57 | values = { |
| 58 | 'NATIVE_FEATURES': description + native_features, |
| 59 | } |
| 60 | return template.format(**values) |
| 61 | |
| 62 | |
| 63 | def _ParseFeatureFile(path): |
| 64 | with open(path) as f: |
| 65 | feature_file_parser = java_cpp_utils.CppConstantParser( |
| 66 | FeatureParserDelegate(), f.readlines()) |
| 67 | return feature_file_parser.Parse() |
| 68 | |
| 69 | |
| 70 | def _Generate(source_paths, template_path): |
| 71 | with open(template_path) as f: |
| 72 | lines = f.readlines() |
| 73 | |
| 74 | template = ''.join(lines) |
| 75 | package, class_name = java_cpp_utils.ParseTemplateFile(lines) |
| 76 | output_path = java_cpp_utils.GetJavaFilePath(package, class_name) |
| 77 | |
| 78 | features = [] |
| 79 | for source_path in source_paths: |
| 80 | features.extend(_ParseFeatureFile(source_path)) |
| 81 | |
| 82 | output = _GenerateOutput(template, source_paths, template_path, features) |
| 83 | return output, output_path |
| 84 | |
| 85 | |
| 86 | def _Main(argv): |
| 87 | parser = argparse.ArgumentParser() |
| 88 | |
| 89 | parser.add_argument('--srcjar', |
| 90 | required=True, |
| 91 | help='The path at which to generate the .srcjar file') |
| 92 | |
| 93 | parser.add_argument('--template', |
| 94 | required=True, |
| 95 | help='The template file with which to generate the Java ' |
| 96 | 'class. Must have "{NATIVE_FEATURES}" somewhere in ' |
| 97 | 'the template.') |
| 98 | |
| 99 | parser.add_argument('inputs', |
| 100 | nargs='+', |
| 101 | help='Input file(s)', |
| 102 | metavar='INPUTFILE') |
| 103 | args = parser.parse_args(argv) |
| 104 | |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 105 | with action_helpers.atomic_output(args.srcjar) as f: |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 106 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar: |
| 107 | data, path = _Generate(args.inputs, args.template) |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 108 | zip_helpers.add_to_zip_hermetic(srcjar, path, data=data) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | _Main(sys.argv[1:]) |