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