blob: 10639a54895f19262e3e5bdfede3b6b9ba977609 [file] [log] [blame]
Andrew Grievee6c6bb382021-04-27 21:47:081#!/usr/bin/env python3
Nate Fischerac07b2622020-10-01 20:20:142#
Avi Drissman73a09d12022-09-08 20:33:383# Copyright 2020 The Chromium Authors
Nate Fischerac07b2622020-10-01 20:20:144# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import re
10import sys
11import zipfile
12
13from util import build_utils
14from util import java_cpp_utils
Andrew Grieve4bc50e2d2023-03-31 02:25:5715import action_helpers # build_utils adds //build to sys.path.
Andrew Grieve3dec3462023-03-31 20:31:2916import zip_helpers
Nate Fischerac07b2622020-10-01 20:20:1417
18
19class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
Daniel Chengdc644a12022-09-19 23:21:3720 # 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 Fischerac07b2622020-10-01 20:20:1425 VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
26
27 def ExtractConstantName(self, line):
28 match = FeatureParserDelegate.FEATURE_RE.match(line)
Nate Fischerac07b2622020-10-01 20:20:1429 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
39def _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
63def _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
70def _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
86def _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 Grieve4bc50e2d2023-03-31 02:25:57105 with action_helpers.atomic_output(args.srcjar) as f:
Nate Fischerac07b2622020-10-01 20:20:14106 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar:
107 data, path = _Generate(args.inputs, args.template)
Andrew Grieve3dec3462023-03-31 20:31:29108 zip_helpers.add_to_zip_hermetic(srcjar, path, data=data)
Nate Fischerac07b2622020-10-01 20:20:14109
110
111if __name__ == '__main__':
112 _Main(sys.argv[1:])