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