diff --git a/utilities/batch_generate_apis.py b/utilities/batch_generate_apis.py index f7cec97424c9..ec4e706ac450 100644 --- a/utilities/batch_generate_apis.py +++ b/utilities/batch_generate_apis.py @@ -31,12 +31,11 @@ def run_gapic_gen(googleapis): - def generate(artman_yaml): + def generate(artman_yaml, artifact_name=generate_api.JAVA_GAPIC): generate_api.run_generate_api(os.path.join(googleapis, artman_yaml), - generate_api.JAVA_GAPIC) + artifact_name) - # TODO Needs to have java_proto called instead of java_grpc - #generate('google/datastore/artman_datastore.yaml') + generate('google/datastore/artman_datastore.yaml', generate_api.JAVA_PROTO) generate('google/cloud/automl/artman_automl_v1beta1.yaml') generate('google/cloud/bigquery/datatransfer/artman_bigquerydatatransfer.yaml') diff --git a/utilities/generate_api.py b/utilities/generate_api.py index fc195fe21458..8ea63024fb07 100644 --- a/utilities/generate_api.py +++ b/utilities/generate_api.py @@ -15,9 +15,9 @@ # Instructions: # # Find the artman config file the describes the API you want to generate a client for. -# Specifiy the artman ARTIFACT_TYPE to generate, e.g. "java_gapic" +# Specifiy the artman ARTIFACT_NAME to generate, e.g. "java_gapic" # -# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE ARTIFACT_TYPE +# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE ARTIFACT_NAME import argparse import io @@ -35,19 +35,21 @@ 'spanner-admin-database': 'google-cloud-spanner' } +JAVA_PROTO="java_proto" +JAVA_GRPC="java_grpc" JAVA_GAPIC="java_gapic" JAVA_DISCOGAPIC="java_discogapic" -def run_generate_api(config_path, artifact_type, noisy=False): +def run_generate_api(config_path, artifact_name, noisy=False): """ Generate an API client library. :param config_path: (str) Path to directory containing artman config file. - :param artifact_type: (str) artman target, e.g "java_gapic". + :param artifact_name: (str) artman target, e.g "java_gapic". :param noisy: (bool) if console output should be verbose. """ api_repo_index = config_path.rfind('/google/') - if artifact_type == JAVA_DISCOGAPIC: + if artifact_name == JAVA_DISCOGAPIC: api_repo_index = config_path.rfind('/gapic/') if api_repo_index == -1: raise ValueError('Didn\'t find the API repo in config file path; need absolute path to the artman config file.') @@ -60,7 +62,7 @@ def run_generate_api(config_path, artifact_type, noisy=False): subprocess.check_call( ['artman', '--config', api_dir, '--local', '--root-dir', root_dir] - + extra_options + ['generate', artifact_type]) + + extra_options + ['generate', artifact_name, '--aspect', 'CODE']) with io.open(config_path, encoding='UTF-8') as config_file: artman_config_data = yaml.load(config_file, Loader=yaml.Loader) @@ -74,48 +76,44 @@ def run_generate_api(config_path, artifact_type, noisy=False): grpc_dirname = 'grpc-{}'.format(api_full_name) gapic_dirname = 'gapic-{}'.format(api_full_name) + generating_gapic = artifact_name == JAVA_GAPIC or artifact_name == JAVA_DISCOGAPIC + generating_grpc = generating_gapic or artifact_name == JAVA_GRPC + gapic_dir = os.path.join('artman-genfiles', 'java', gapic_dirname) - if not os.path.exists(gapic_dir): + if generating_gapic and not os.path.exists(gapic_dir): raise ValueError('generated gapic dir doesn\'t exist: {}'.format(gapic_dir)) - if artifact_type != JAVA_DISCOGAPIC: + if artifact_name != JAVA_DISCOGAPIC: proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname) grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname) if not os.path.exists(proto_dir): raise ValueError('generated proto dir doesn\'t exist: {}'.format(proto_dir)) - if not os.path.exists(grpc_dir): + if generating_grpc and not os.path.exists(grpc_dir): raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir)) target_proto_dir = os.path.join('google-api-grpc', proto_dirname) - target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname) - if os.path.exists(target_proto_dir): - print('{} already exists, removing & replacing it.'.format(target_proto_dir)) - if os.path.exists(target_grpc_dir): - print('{} already exists, removing & replacing it.'.format(target_grpc_dir)) - - print('-- ignore any pathspec errors that follow') - - if os.path.exists(target_proto_dir): - shutil.rmtree(target_proto_dir) - shutil.copytree(proto_dir, target_proto_dir) - os.remove(os.path.join(target_proto_dir, 'LICENSE')) - os.remove(os.path.join(target_proto_dir, 'build.gradle')) - subprocess.call(['git', 'checkout', os.path.join(target_proto_dir, 'pom.xml')]) - - if os.path.exists(target_grpc_dir): - shutil.rmtree(target_grpc_dir) - shutil.copytree(grpc_dir, target_grpc_dir) - os.remove(os.path.join(target_grpc_dir, 'LICENSE')) - os.remove(os.path.join(target_grpc_dir, 'build.gradle')) - subprocess.call(['git', 'checkout', os.path.join(target_grpc_dir, 'pom.xml')]) - - api_unversioned_name = '{}-{}'.format(org_name, api_name) - if api_name in dir_overrides: - api_unversioned_name = dir_overrides[api_name] - - target_gapic_dir = os.path.join('google-cloud-clients', api_unversioned_name) - dir_util.copy_tree(os.path.join(gapic_dir, 'src'), os.path.join(target_gapic_dir, 'src')) + target_proto_code_dir = os.path.join(target_proto_dir, 'src') + if os.path.exists(target_proto_code_dir): + print('{} already exists, removing & replacing it.'.format(target_proto_code_dir)) + shutil.rmtree(target_proto_code_dir) + dir_util.copy_tree(proto_dir, target_proto_dir) + + if generating_grpc: + target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname) + target_grpc_code_dir = os.path.join(target_grpc_dir, 'src') + if os.path.exists(target_grpc_code_dir): + print('{} already exists, removing & replacing it.'.format(target_grpc_code_dir)) + shutil.rmtree(target_grpc_code_dir) + dir_util.copy_tree(grpc_dir, target_grpc_dir) + + if generating_gapic: + api_unversioned_name = '{}-{}'.format(org_name, api_name) + if api_name in dir_overrides: + api_unversioned_name = dir_overrides[api_name] + + target_gapic_dir = os.path.join('google-cloud-clients', api_unversioned_name) + dir_util.copy_tree(os.path.join(gapic_dir, 'src'), os.path.join(target_gapic_dir, 'src')) if noisy: print('**** REMAINING MANUAL WORK: *****') @@ -134,13 +132,13 @@ def run_generate_api(config_path, artifact_type, noisy=False): def main(): parser = argparse.ArgumentParser(description='Regenerate a single API.') parser.add_argument('config_file', help='The artman config file for the API') - parser.add_argument('artifact_type', help='The artman artifact type', + parser.add_argument('artifact_name', help='The artman artifact type', default="java_gapic") parser.add_argument('--quiet', action="store_true", default=False, help='Don\'t print informational instructions') args = parser.parse_args() - run_generate_api(args.config_file, args.artifact_type, not args.quiet) + run_generate_api(args.config_file, args.artifact_name, not args.quiet) if __name__ == '__main__': main()