Skip to content

Commit 4ee3038

Browse files
andreamlinyihanzhen
authored andcommitted
Add GCE and discogapic to batch gen script (googleapis#3441)
1 parent 6d60d15 commit 4ee3038

File tree

2 files changed

+77
-43
lines changed

2 files changed

+77
-43
lines changed

utilities/batch_generate_apis.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@
1414

1515
# Instructions:
1616
#
17-
# Check out the googleapis repo somewhere locally (e.g. from
18-
# https://github.com/googleapis/googleapis):
17+
# Check out the googleapis and discovery-artifact-manager repo somewhere locally
18+
# (e.g. from https://github.com/googleapis/googleapis):
1919
#
2020
# $ git checkout https://github.com/googleapis/googleapis.git
21+
# $ git checkout https://github.com/googleapis/discovery-artifact-manager.git
2122
#
2223
# Run this script:
2324
#
24-
# $ python utilities/batch_generate_apis.py PATH_TO_GOOGLEAPIS
25+
# $ python utilities/batch_generate_apis.py PATH_TO_GOOGLEAPIS PATH_TO_DISCOVERY_ARTIFACT_MANAGER
2526

2627
import argparse
2728
import os
2829

2930
import generate_api
3031

3132

32-
def run(googleapis):
33+
def run_gapic_gen(googleapis):
3334
def generate(artman_yaml):
34-
generate_api.run_generate_api(os.path.join(googleapis, artman_yaml))
35+
generate_api.run_generate_api(os.path.join(googleapis, artman_yaml),
36+
generate_api.JAVA_GAPIC)
3537

3638
# TODO Needs to have java_proto called instead of java_grpc
3739
#generate('google/datastore/artman_datastore.yaml')
@@ -73,13 +75,25 @@ def generate(artman_yaml):
7375
generate('google/cloud/websecurityscanner/artman_websecurityscanner_v1alpha.yaml')
7476

7577

78+
def run_discogapic_gen(discovery_repo):
79+
def generate(artman_yaml):
80+
# Run java_discogapic task. No proto or grpc libraries are generated.
81+
generate_api.run_generate_api(os.path.join(discovery_repo, artman_yaml),
82+
generate_api.JAVA_DISCOGAPIC)
83+
84+
generate('gapic/google/compute/artman_compute.yaml')
85+
86+
7687
def main():
7788
# TODO Make the docker image the default, add --local option
7889
parser = argparse.ArgumentParser(description='Batch generate all APIs.')
7990
parser.add_argument('googleapis', help='The path to the googleapis repo')
91+
parser.add_argument('discovery_repo',
92+
help='The path to the discovery-artifact-manager repo')
8093
args = parser.parse_args()
8194

82-
run(args.googleapis)
95+
run_gapic_gen(args.googleapis)
96+
run_discogapic_gen(args.discovery_repo)
8397

8498
if __name__ == '__main__':
8599
main()

utilities/generate_api.py

+57-37
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
# Instructions:
1616
#
1717
# Find the artman config file the describes the API you want to generate a client for.
18+
# Specifiy the artman ARTIFACT_TYPE to generate, e.g. "java_gapic"
1819
#
19-
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE
20+
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE ARTIFACT_TYPE
2021

2122
import argparse
2223
import io
@@ -35,19 +36,32 @@
3536
'spanner-admin-database': 'google-cloud-spanner'
3637
}
3738

39+
JAVA_GAPIC="java_gapic"
40+
JAVA_DISCOGAPIC="java_discogapic"
3841

39-
def run_generate_api(config_path, noisy=False):
40-
googleapis_index = config_path.rfind('/google/')
41-
if googleapis_index == -1:
42-
raise ValueError('Didn\'t find /googleapis/ in config file path; need absolute path to the artman config file.')
43-
root_dir = config_path[0:googleapis_index]
44-
api_dir = config_path[googleapis_index+1:]
42+
def run_generate_api(config_path, artifact_type, noisy=False):
43+
""" Generate an API client library.
44+
45+
:param config_path: (str) Path to directory containing artman config file.
46+
:param artifact_type: (str) artman target, e.g "java_gapic".
47+
:param noisy: (bool) if console output should be verbose.
48+
49+
"""
50+
api_repo_index = config_path.rfind('/google/')
51+
if artifact_type == JAVA_DISCOGAPIC:
52+
api_repo_index = config_path.rfind('/gapic/')
53+
if api_repo_index == -1:
54+
raise ValueError('Didn\'t find the API repo in config file path; need absolute path to the artman config file.')
55+
root_dir = config_path[0:api_repo_index]
56+
api_dir = config_path[api_repo_index+1:]
4557

4658
extra_options = []
4759
if noisy:
4860
extra_options = ['-v']
4961

50-
subprocess.check_call(['artman', '--config', api_dir, '--local', '--root-dir', root_dir] + extra_options + ['generate', 'java_gapic'])
62+
subprocess.check_call(
63+
['artman', '--config', api_dir, '--local', '--root-dir', root_dir]
64+
+ extra_options + ['generate', artifact_type])
5165

5266
with io.open(config_path, encoding='UTF-8') as config_file:
5367
artman_config_data = yaml.load(config_file, Loader=yaml.Loader)
@@ -60,38 +74,42 @@ def run_generate_api(config_path, noisy=False):
6074
proto_dirname = 'proto-{}'.format(api_full_name)
6175
grpc_dirname = 'grpc-{}'.format(api_full_name)
6276
gapic_dirname = 'gapic-{}'.format(api_full_name)
63-
proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname)
64-
grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname)
77+
6578
gapic_dir = os.path.join('artman-genfiles', 'java', gapic_dirname)
66-
if not os.path.exists(proto_dir):
67-
raise ValueError('generated proto dir doesn\'t exist: {}'.format(proto_dir))
68-
if not os.path.exists(grpc_dir):
69-
raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir))
7079
if not os.path.exists(gapic_dir):
7180
raise ValueError('generated gapic dir doesn\'t exist: {}'.format(gapic_dir))
7281

73-
target_proto_dir = os.path.join('google-api-grpc', proto_dirname)
74-
target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname)
75-
if os.path.exists(target_proto_dir):
76-
print('{} already exists, removing & replacing it.'.format(target_proto_dir))
77-
if os.path.exists(target_grpc_dir):
78-
print('{} already exists, removing & replacing it.'.format(target_grpc_dir))
79-
80-
print('-- ignore any pathspec errors that follow')
81-
82-
if os.path.exists(target_proto_dir):
83-
shutil.rmtree(target_proto_dir)
84-
shutil.copytree(proto_dir, target_proto_dir)
85-
os.remove(os.path.join(target_proto_dir, 'LICENSE'))
86-
os.remove(os.path.join(target_proto_dir, 'build.gradle'))
87-
subprocess.call(['git', 'checkout', os.path.join(target_proto_dir, 'pom.xml')])
88-
89-
if os.path.exists(target_grpc_dir):
90-
shutil.rmtree(target_grpc_dir)
91-
shutil.copytree(grpc_dir, target_grpc_dir)
92-
os.remove(os.path.join(target_grpc_dir, 'LICENSE'))
93-
os.remove(os.path.join(target_grpc_dir, 'build.gradle'))
94-
subprocess.call(['git', 'checkout', os.path.join(target_grpc_dir, 'pom.xml')])
82+
if artifact_type != JAVA_DISCOGAPIC:
83+
proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname)
84+
grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname)
85+
86+
if not os.path.exists(proto_dir):
87+
raise ValueError('generated proto dir doesn\'t exist: {}'.format(proto_dir))
88+
if not os.path.exists(grpc_dir):
89+
raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir))
90+
91+
target_proto_dir = os.path.join('google-api-grpc', proto_dirname)
92+
target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname)
93+
if os.path.exists(target_proto_dir):
94+
print('{} already exists, removing & replacing it.'.format(target_proto_dir))
95+
if os.path.exists(target_grpc_dir):
96+
print('{} already exists, removing & replacing it.'.format(target_grpc_dir))
97+
98+
print('-- ignore any pathspec errors that follow')
99+
100+
if os.path.exists(target_proto_dir):
101+
shutil.rmtree(target_proto_dir)
102+
shutil.copytree(proto_dir, target_proto_dir)
103+
os.remove(os.path.join(target_proto_dir, 'LICENSE'))
104+
os.remove(os.path.join(target_proto_dir, 'build.gradle'))
105+
subprocess.call(['git', 'checkout', os.path.join(target_proto_dir, 'pom.xml')])
106+
107+
if os.path.exists(target_grpc_dir):
108+
shutil.rmtree(target_grpc_dir)
109+
shutil.copytree(grpc_dir, target_grpc_dir)
110+
os.remove(os.path.join(target_grpc_dir, 'LICENSE'))
111+
os.remove(os.path.join(target_grpc_dir, 'build.gradle'))
112+
subprocess.call(['git', 'checkout', os.path.join(target_grpc_dir, 'pom.xml')])
95113

96114
api_unversioned_name = '{}-{}'.format(org_name, api_name)
97115
if api_name in dir_overrides:
@@ -113,11 +131,13 @@ def run_generate_api(config_path, noisy=False):
113131
def main():
114132
parser = argparse.ArgumentParser(description='Regenerate a single API.')
115133
parser.add_argument('config_file', help='The artman config file for the API')
134+
parser.add_argument('artifact_type', help='The artman artifact type',
135+
default="java_gapic")
116136
parser.add_argument('--quiet', action="store_true", default=False,
117137
help='Don\'t print informational instructions')
118138
args = parser.parse_args()
119139

120-
run_generate_api(args.config_file, not args.quiet)
140+
run_generate_api(args.config_file, args.artifact_type, not args.quiet)
121141

122142
if __name__ == '__main__':
123143
main()

0 commit comments

Comments
 (0)