xref: /aosp_15_r20/external/cronet/build/android/gyp/java_google_api_keys.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2#
3# Copyright 2015 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
7# Generates a Java file with API keys.
8
9import argparse
10import os
11import string
12import sys
13import zipfile
14
15from util import build_utils
16import zip_helpers
17
18sys.path.append(
19    os.path.abspath(os.path.join(sys.path[0], '../../../google_apis')))
20import google_api_keys
21
22
23PACKAGE = 'org.chromium.chrome'
24CLASSNAME = 'GoogleAPIKeys'
25
26
27def GetScriptName():
28  return os.path.relpath(__file__, build_utils.DIR_SOURCE_ROOT)
29
30
31def GenerateOutput(constant_definitions):
32  template = string.Template("""
33// Copyright 2015 The Chromium Authors
34// Use of this source code is governed by a BSD-style license that can be
35// found in the LICENSE file.
36
37// This file is autogenerated by
38//     ${SCRIPT_NAME}
39// From
40//     ${SOURCE_PATH}
41
42package ${PACKAGE};
43
44public class ${CLASS_NAME} {
45${CONSTANT_ENTRIES}
46}
47""")
48
49  constant_template = string.Template(
50      '  public static final String ${NAME} = "${VALUE}";')
51  constant_entries_list = []
52  for constant_name, constant_value in constant_definitions.items():
53    values = {
54        'NAME': constant_name,
55        'VALUE': constant_value,
56    }
57    constant_entries_list.append(constant_template.substitute(values))
58  constant_entries_string = '\n'.join(constant_entries_list)
59
60  values = {
61      'CLASS_NAME': CLASSNAME,
62      'CONSTANT_ENTRIES': constant_entries_string,
63      'PACKAGE': PACKAGE,
64      'SCRIPT_NAME': GetScriptName(),
65      'SOURCE_PATH': 'google_api_keys/google_api_keys.h',
66  }
67  return template.substitute(values)
68
69
70def _DoWriteJavaOutput(output_path, constant_definition):
71  folder = os.path.dirname(output_path)
72  if folder and not os.path.exists(folder):
73    os.makedirs(folder)
74  with open(output_path, 'w') as out_file:
75    out_file.write(GenerateOutput(constant_definition))
76
77
78def _DoWriteJarOutput(output_path, constant_definition):
79  folder = os.path.dirname(output_path)
80  if folder and not os.path.exists(folder):
81    os.makedirs(folder)
82  with zipfile.ZipFile(output_path, 'w') as srcjar:
83    path = '%s/%s' % (PACKAGE.replace('.', '/'), CLASSNAME + '.java')
84    data = GenerateOutput(constant_definition)
85    zip_helpers.add_to_zip_hermetic(srcjar, path, data=data)
86
87
88def _DoMain(argv):
89  parser = argparse.ArgumentParser()
90  parser.add_argument("--out", help="Path for java output.")
91  parser.add_argument("--srcjar", help="Path for srcjar output.")
92  options = parser.parse_args(argv)
93  if not options.out and not options.srcjar:
94    parser.print_help()
95    sys.exit(-1)
96
97  values = {}
98  values['GOOGLE_API_KEY'] = google_api_keys.GetAPIKey()
99  values['GOOGLE_API_KEY_ANDROID_NON_STABLE'] = (
100      google_api_keys.GetAPIKeyAndroidNonStable())
101  values['GOOGLE_CLIENT_ID_MAIN'] = google_api_keys.GetClientID('MAIN')
102  values['GOOGLE_CLIENT_SECRET_MAIN'] = google_api_keys.GetClientSecret('MAIN')
103  values['GOOGLE_CLIENT_ID_REMOTING'] = google_api_keys.GetClientID('REMOTING')
104  values['GOOGLE_CLIENT_SECRET_REMOTING'] = google_api_keys.GetClientSecret(
105      'REMOTING')
106  values['GOOGLE_CLIENT_ID_REMOTING_HOST'] = google_api_keys.GetClientID(
107      'REMOTING_HOST')
108  values['GOOGLE_CLIENT_SECRET_REMOTING_HOST'] = (google_api_keys.
109      GetClientSecret('REMOTING_HOST'))
110
111  if options.out:
112    _DoWriteJavaOutput(options.out, values)
113  if options.srcjar:
114    _DoWriteJarOutput(options.srcjar, values)
115
116
117if __name__ == '__main__':
118  _DoMain(sys.argv[1:])
119