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