• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2015 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
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
16
17sys.path.append(
18    os.path.abspath(os.path.join(sys.path[0], '../../../google_apis')))
19import google_api_keys
20
21sys.path.append(os.path.abspath(os.path.join(
22    os.path.dirname(__file__), os.pardir)))
23from pylib.constants import host_paths
24
25
26PACKAGE = 'org.chromium.chrome'
27CLASSNAME = 'GoogleAPIKeys'
28
29
30def GetScriptName():
31  return os.path.relpath(__file__, host_paths.DIR_SOURCE_ROOT)
32
33
34def GenerateOutput(constant_definitions):
35  template = string.Template("""
36// Copyright 2015 The Chromium Authors. All rights reserved.
37// Use of this source code is governed by a BSD-style license that can be
38// found in the LICENSE file.
39
40// This file is autogenerated by
41//     ${SCRIPT_NAME}
42// From
43//     ${SOURCE_PATH}
44
45package ${PACKAGE};
46
47public class ${CLASS_NAME} {
48${CONSTANT_ENTRIES}
49}
50""")
51
52  constant_template = string.Template(
53      '  public static final String ${NAME} = "${VALUE}";')
54  constant_entries_list = []
55  for constant_name, constant_value in constant_definitions.iteritems():
56    values = {
57        'NAME': constant_name,
58        'VALUE': constant_value,
59    }
60    constant_entries_list.append(constant_template.substitute(values))
61  constant_entries_string = '\n'.join(constant_entries_list)
62
63  values = {
64      'CLASS_NAME': CLASSNAME,
65      'CONSTANT_ENTRIES': constant_entries_string,
66      'PACKAGE': PACKAGE,
67      'SCRIPT_NAME': GetScriptName(),
68      'SOURCE_PATH': 'google_api_keys/google_api_keys.h',
69  }
70  return template.substitute(values)
71
72
73def _DoWriteJavaOutput(output_path, constant_definition):
74  folder = os.path.dirname(output_path)
75  if folder and not os.path.exists(folder):
76    os.makedirs(folder)
77  with open(output_path, 'w') as out_file:
78    out_file.write(GenerateOutput(constant_definition))
79
80
81def _DoWriteJarOutput(output_path, constant_definition):
82  folder = os.path.dirname(output_path)
83  if folder and not os.path.exists(folder):
84    os.makedirs(folder)
85  with zipfile.ZipFile(output_path, 'w') as srcjar:
86    path = '%s/%s' % (PACKAGE.replace('.', '/'), CLASSNAME + '.java')
87    data = GenerateOutput(constant_definition)
88    build_utils.AddToZipHermetic(srcjar, path, data=data)
89
90
91def _DoMain(argv):
92  parser = argparse.ArgumentParser()
93  parser.add_argument("--out", help="Path for java output.")
94  parser.add_argument("--srcjar", help="Path for srcjar output.")
95  options = parser.parse_args(argv)
96  if not options.out and not options.srcjar:
97    parser.print_help()
98    sys.exit(-1)
99
100  values = {}
101  values['GOOGLE_API_KEY'] = google_api_keys.GetAPIKey()
102  values['GOOGLE_API_KEY_REMOTING'] = google_api_keys.GetAPIKeyRemoting()
103  values['GOOGLE_API_KEY_PHYSICAL_WEB_TEST'] = (google_api_keys.
104      GetAPIKeyPhysicalWebTest())
105  values['GOOGLE_CLIENT_ID_MAIN'] = google_api_keys.GetClientID('MAIN')
106  values['GOOGLE_CLIENT_SECRET_MAIN'] = google_api_keys.GetClientSecret('MAIN')
107  values['GOOGLE_CLIENT_ID_CLOUD_PRINT'] = google_api_keys.GetClientID(
108      'CLOUD_PRINT')
109  values['GOOGLE_CLIENT_SECRET_CLOUD_PRINT'] = google_api_keys.GetClientSecret(
110      'CLOUD_PRINT')
111  values['GOOGLE_CLIENT_ID_REMOTING'] = google_api_keys.GetClientID('REMOTING')
112  values['GOOGLE_CLIENT_SECRET_REMOTING'] = google_api_keys.GetClientSecret(
113      'REMOTING')
114  values['GOOGLE_CLIENT_ID_REMOTING_HOST'] = google_api_keys.GetClientID(
115      'REMOTING_HOST')
116  values['GOOGLE_CLIENT_SECRET_REMOTING_HOST'] = (google_api_keys.
117      GetClientSecret('REMOTING_HOST'))
118  values['GOOGLE_CLIENT_ID_REMOTING_IDENTITY_API'] = (google_api_keys.
119      GetClientID('REMOTING_IDENTITY_API'))
120
121  if options.out:
122    _DoWriteJavaOutput(options.out, values)
123  if options.srcjar:
124    _DoWriteJarOutput(options.srcjar, values)
125
126
127if __name__ == '__main__':
128  _DoMain(sys.argv[1:])
129
130