OLD | NEW |
(Empty) | |
| 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 |
| 9 import argparse |
| 10 import os |
| 11 import string |
| 12 import sys |
| 13 import zipfile |
| 14 |
| 15 sys.path.append( |
| 16 os.path.abspath(os.path.join(sys.path[0], '../../../google_apis'))) |
| 17 import google_api_keys |
| 18 |
| 19 sys.path.append(os.path.abspath(os.path.join( |
| 20 os.path.dirname(__file__), os.pardir))) |
| 21 from pylib import constants |
| 22 |
| 23 |
| 24 PACKAGE = 'org.chromium.chrome' |
| 25 CLASSNAME = 'GoogleAPIKeys' |
| 26 HERMETIC_TIMESTAMP = (2001, 1, 1, 0, 0, 0) |
| 27 HERMETIC_FILE_ATTR = (0644 << 16L) |
| 28 |
| 29 |
| 30 def GetScriptName(): |
| 31 return os.path.relpath(__file__, constants.DIR_SOURCE_ROOT) |
| 32 |
| 33 |
| 34 def 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 |
| 45 package ${PACKAGE}; |
| 46 |
| 47 public 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 |
| 73 def _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 |
| 81 def _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 zipinfo = zipfile.ZipInfo(filename=path, |
| 88 date_time=HERMETIC_TIMESTAMP) |
| 89 zipinfo.external_attr = HERMETIC_FILE_ATTR |
| 90 srcjar.writestr(zipinfo, GenerateOutput(constant_definition)) |
| 91 |
| 92 |
| 93 def _DoMain(argv): |
| 94 parser = argparse.ArgumentParser() |
| 95 parser.add_argument("--out", help="Path for java output.") |
| 96 parser.add_argument("--srcjar", help="Path for srcjar output.") |
| 97 options = parser.parse_args(argv) |
| 98 if not options.out and not options.srcjar: |
| 99 parser.print_help() |
| 100 sys.exit(-1) |
| 101 |
| 102 values = {} |
| 103 values['GOOGLE_API_KEY'] = google_api_keys.GetAPIKey() |
| 104 values['GOOGLE_API_KEY_REMOTING'] = google_api_keys.GetAPIKeyRemoting() |
| 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 |
| 127 if __name__ == '__main__': |
| 128 _DoMain(sys.argv[1:]) |
| 129 |
OLD | NEW |