blob: 8e7c2440d71b7c1983f8d1d3f514723401b421bc [file] [log] [blame]
Andrew Grievee6c6bb382021-04-27 21:47:081#!/usr/bin/env python3
Nate Fischerac07b2622020-10-01 20:20:142#
3# Copyright 2020 The Chromium Authors. All rights reserved.
4# 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
15
16
17class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
18 # Ex. 'const base::Feature kConstantName{"StringNameOfTheFeature", ...};'
19 # would parse as:
20 # ExtractConstantName() -> 'ConstantName'
21 # ExtractValue() -> '"StringNameOfTheFeature"'
22 FEATURE_RE = re.compile(r'\s*const (?:base::)?Feature\s+k(\w+)\s*(?:=\s*)?{')
23 VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
24
25 def ExtractConstantName(self, line):
26 match = FeatureParserDelegate.FEATURE_RE.match(line)
27 return match.group(1) if match else None
28
29 def ExtractValue(self, line):
30 match = FeatureParserDelegate.VALUE_RE.search(line)
31 return match.group(1) if match else None
32
33 def CreateJavaConstant(self, name, value, comments):
34 return java_cpp_utils.JavaString(name, value, comments)
35
36
37def _GenerateOutput(template, source_paths, template_path, features):
38 description_template = """
39 // This following string constants were inserted by
40 // {SCRIPT_NAME}
41 // From
42 // {SOURCE_PATHS}
43 // Into
44 // {TEMPLATE_PATH}
45
46"""
47 values = {
48 'SCRIPT_NAME': java_cpp_utils.GetScriptName(),
49 'SOURCE_PATHS': ',\n // '.join(source_paths),
50 'TEMPLATE_PATH': template_path,
51 }
52 description = description_template.format(**values)
53 native_features = '\n\n'.join(x.Format() for x in features)
54
55 values = {
56 'NATIVE_FEATURES': description + native_features,
57 }
58 return template.format(**values)
59
60
61def _ParseFeatureFile(path):
62 with open(path) as f:
63 feature_file_parser = java_cpp_utils.CppConstantParser(
64 FeatureParserDelegate(), f.readlines())
65 return feature_file_parser.Parse()
66
67
68def _Generate(source_paths, template_path):
69 with open(template_path) as f:
70 lines = f.readlines()
71
72 template = ''.join(lines)
73 package, class_name = java_cpp_utils.ParseTemplateFile(lines)
74 output_path = java_cpp_utils.GetJavaFilePath(package, class_name)
75
76 features = []
77 for source_path in source_paths:
78 features.extend(_ParseFeatureFile(source_path))
79
80 output = _GenerateOutput(template, source_paths, template_path, features)
81 return output, output_path
82
83
84def _Main(argv):
85 parser = argparse.ArgumentParser()
86
87 parser.add_argument('--srcjar',
88 required=True,
89 help='The path at which to generate the .srcjar file')
90
91 parser.add_argument('--template',
92 required=True,
93 help='The template file with which to generate the Java '
94 'class. Must have "{NATIVE_FEATURES}" somewhere in '
95 'the template.')
96
97 parser.add_argument('inputs',
98 nargs='+',
99 help='Input file(s)',
100 metavar='INPUTFILE')
101 args = parser.parse_args(argv)
102
103 with build_utils.AtomicOutput(args.srcjar) as f:
104 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar:
105 data, path = _Generate(args.inputs, args.template)
106 build_utils.AddToZipHermetic(srcjar, path, data=data)
107
108
109if __name__ == '__main__':
110 _Main(sys.argv[1:])