1#!/user/bin/env python 2# 3# Copyright 2019 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 StringParserDelegate(java_cpp_utils.CppConstantParser.Delegate): 18 STRING_RE = re.compile(r'\s*const char k(.*)\[\]\s*=') 19 VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*;') 20 21 def ExtractConstantName(self, line): 22 match = StringParserDelegate.STRING_RE.match(line) 23 return match.group(1) if match else None 24 25 def ExtractValue(self, line): 26 match = StringParserDelegate.VALUE_RE.search(line) 27 return match.group(1) if match else None 28 29 def CreateJavaConstant(self, name, value, comments): 30 return java_cpp_utils.JavaString(name, value, comments) 31 32 33def _GenerateOutput(template, source_paths, template_path, strings): 34 description_template = """ 35 // This following string constants were inserted by 36 // {SCRIPT_NAME} 37 // From 38 // {SOURCE_PATHS} 39 // Into 40 // {TEMPLATE_PATH} 41 42""" 43 values = { 44 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), 45 'SOURCE_PATHS': ',\n // '.join(source_paths), 46 'TEMPLATE_PATH': template_path, 47 } 48 description = description_template.format(**values) 49 native_strings = '\n\n'.join(x.Format() for x in strings) 50 51 values = { 52 'NATIVE_STRINGS': description + native_strings, 53 } 54 return template.format(**values) 55 56 57def _ParseStringFile(path): 58 with open(path) as f: 59 string_file_parser = java_cpp_utils.CppConstantParser( 60 StringParserDelegate(), f.readlines()) 61 return string_file_parser.Parse() 62 63 64def _Generate(source_paths, template_path): 65 with open(template_path) as f: 66 lines = f.readlines() 67 68 template = ''.join(lines) 69 package, class_name = java_cpp_utils.ParseTemplateFile(lines) 70 output_path = java_cpp_utils.GetJavaFilePath(package, class_name) 71 strings = [] 72 for source_path in source_paths: 73 strings.extend(_ParseStringFile(source_path)) 74 75 output = _GenerateOutput(template, source_paths, template_path, strings) 76 return output, output_path 77 78 79def _Main(argv): 80 parser = argparse.ArgumentParser() 81 82 parser.add_argument('--srcjar', 83 required=True, 84 help='The path at which to generate the .srcjar file') 85 86 parser.add_argument('--template', 87 required=True, 88 help='The template file with which to generate the Java ' 89 'class. Must have "{NATIVE_STRINGS}" somewhere in ' 90 'the template.') 91 92 parser.add_argument( 93 'inputs', nargs='+', help='Input file(s)', metavar='INPUTFILE') 94 args = parser.parse_args(argv) 95 96 with build_utils.AtomicOutput(args.srcjar) as f: 97 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar: 98 data, path = _Generate(args.inputs, args.template) 99 build_utils.AddToZipHermetic(srcjar, path, data=data) 100 101 102if __name__ == '__main__': 103 _Main(sys.argv[1:]) 104