Skip to content

Commit b3e9a4c

Browse files
Using --aspect CODE from artman; supporting java_proto (#3507)
1 parent 89433fc commit b3e9a4c

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

utilities/batch_generate_apis.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131

3232

3333
def run_gapic_gen(googleapis):
34-
def generate(artman_yaml):
34+
def generate(artman_yaml, artifact_name=generate_api.JAVA_GAPIC):
3535
generate_api.run_generate_api(os.path.join(googleapis, artman_yaml),
36-
generate_api.JAVA_GAPIC)
36+
artifact_name)
3737

38-
# TODO Needs to have java_proto called instead of java_grpc
39-
#generate('google/datastore/artman_datastore.yaml')
38+
generate('google/datastore/artman_datastore.yaml', generate_api.JAVA_PROTO)
4039

4140
generate('google/cloud/automl/artman_automl_v1beta1.yaml')
4241
generate('google/cloud/bigquery/datatransfer/artman_bigquerydatatransfer.yaml')

utilities/generate_api.py

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +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"
18+
# Specifiy the artman ARTIFACT_NAME to generate, e.g. "java_gapic"
1919
#
20-
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE ARTIFACT_TYPE
20+
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE ARTIFACT_NAME
2121

2222
import argparse
2323
import io
@@ -35,19 +35,21 @@
3535
'spanner-admin-database': 'google-cloud-spanner'
3636
}
3737

38+
JAVA_PROTO="java_proto"
39+
JAVA_GRPC="java_grpc"
3840
JAVA_GAPIC="java_gapic"
3941
JAVA_DISCOGAPIC="java_discogapic"
4042

41-
def run_generate_api(config_path, artifact_type, noisy=False):
43+
def run_generate_api(config_path, artifact_name, noisy=False):
4244
""" Generate an API client library.
4345
4446
:param config_path: (str) Path to directory containing artman config file.
45-
:param artifact_type: (str) artman target, e.g "java_gapic".
47+
:param artifact_name: (str) artman target, e.g "java_gapic".
4648
:param noisy: (bool) if console output should be verbose.
4749
4850
"""
4951
api_repo_index = config_path.rfind('/google/')
50-
if artifact_type == JAVA_DISCOGAPIC:
52+
if artifact_name == JAVA_DISCOGAPIC:
5153
api_repo_index = config_path.rfind('/gapic/')
5254
if api_repo_index == -1:
5355
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):
6062

6163
subprocess.check_call(
6264
['artman', '--config', api_dir, '--local', '--root-dir', root_dir]
63-
+ extra_options + ['generate', artifact_type])
65+
+ extra_options + ['generate', artifact_name, '--aspect', 'CODE'])
6466

6567
with io.open(config_path, encoding='UTF-8') as config_file:
6668
artman_config_data = yaml.load(config_file, Loader=yaml.Loader)
@@ -74,48 +76,44 @@ def run_generate_api(config_path, artifact_type, noisy=False):
7476
grpc_dirname = 'grpc-{}'.format(api_full_name)
7577
gapic_dirname = 'gapic-{}'.format(api_full_name)
7678

79+
generating_gapic = artifact_name == JAVA_GAPIC or artifact_name == JAVA_DISCOGAPIC
80+
generating_grpc = generating_gapic or artifact_name == JAVA_GRPC
81+
7782
gapic_dir = os.path.join('artman-genfiles', 'java', gapic_dirname)
78-
if not os.path.exists(gapic_dir):
83+
if generating_gapic and not os.path.exists(gapic_dir):
7984
raise ValueError('generated gapic dir doesn\'t exist: {}'.format(gapic_dir))
8085

81-
if artifact_type != JAVA_DISCOGAPIC:
86+
if artifact_name != JAVA_DISCOGAPIC:
8287
proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname)
8388
grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname)
8489

8590
if not os.path.exists(proto_dir):
8691
raise ValueError('generated proto dir doesn\'t exist: {}'.format(proto_dir))
87-
if not os.path.exists(grpc_dir):
92+
if generating_grpc and not os.path.exists(grpc_dir):
8893
raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir))
8994

9095
target_proto_dir = os.path.join('google-api-grpc', proto_dirname)
91-
target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname)
92-
if os.path.exists(target_proto_dir):
93-
print('{} already exists, removing & replacing it.'.format(target_proto_dir))
94-
if os.path.exists(target_grpc_dir):
95-
print('{} already exists, removing & replacing it.'.format(target_grpc_dir))
96-
97-
print('-- ignore any pathspec errors that follow')
98-
99-
if os.path.exists(target_proto_dir):
100-
shutil.rmtree(target_proto_dir)
101-
shutil.copytree(proto_dir, target_proto_dir)
102-
os.remove(os.path.join(target_proto_dir, 'LICENSE'))
103-
os.remove(os.path.join(target_proto_dir, 'build.gradle'))
104-
subprocess.call(['git', 'checkout', os.path.join(target_proto_dir, 'pom.xml')])
105-
106-
if os.path.exists(target_grpc_dir):
107-
shutil.rmtree(target_grpc_dir)
108-
shutil.copytree(grpc_dir, target_grpc_dir)
109-
os.remove(os.path.join(target_grpc_dir, 'LICENSE'))
110-
os.remove(os.path.join(target_grpc_dir, 'build.gradle'))
111-
subprocess.call(['git', 'checkout', os.path.join(target_grpc_dir, 'pom.xml')])
112-
113-
api_unversioned_name = '{}-{}'.format(org_name, api_name)
114-
if api_name in dir_overrides:
115-
api_unversioned_name = dir_overrides[api_name]
116-
117-
target_gapic_dir = os.path.join('google-cloud-clients', api_unversioned_name)
118-
dir_util.copy_tree(os.path.join(gapic_dir, 'src'), os.path.join(target_gapic_dir, 'src'))
96+
target_proto_code_dir = os.path.join(target_proto_dir, 'src')
97+
if os.path.exists(target_proto_code_dir):
98+
print('{} already exists, removing & replacing it.'.format(target_proto_code_dir))
99+
shutil.rmtree(target_proto_code_dir)
100+
dir_util.copy_tree(proto_dir, target_proto_dir)
101+
102+
if generating_grpc:
103+
target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname)
104+
target_grpc_code_dir = os.path.join(target_grpc_dir, 'src')
105+
if os.path.exists(target_grpc_code_dir):
106+
print('{} already exists, removing & replacing it.'.format(target_grpc_code_dir))
107+
shutil.rmtree(target_grpc_code_dir)
108+
dir_util.copy_tree(grpc_dir, target_grpc_dir)
109+
110+
if generating_gapic:
111+
api_unversioned_name = '{}-{}'.format(org_name, api_name)
112+
if api_name in dir_overrides:
113+
api_unversioned_name = dir_overrides[api_name]
114+
115+
target_gapic_dir = os.path.join('google-cloud-clients', api_unversioned_name)
116+
dir_util.copy_tree(os.path.join(gapic_dir, 'src'), os.path.join(target_gapic_dir, 'src'))
119117

120118
if noisy:
121119
print('**** REMAINING MANUAL WORK: *****')
@@ -134,13 +132,13 @@ def run_generate_api(config_path, artifact_type, noisy=False):
134132
def main():
135133
parser = argparse.ArgumentParser(description='Regenerate a single API.')
136134
parser.add_argument('config_file', help='The artman config file for the API')
137-
parser.add_argument('artifact_type', help='The artman artifact type',
135+
parser.add_argument('artifact_name', help='The artman artifact type',
138136
default="java_gapic")
139137
parser.add_argument('--quiet', action="store_true", default=False,
140138
help='Don\'t print informational instructions')
141139
args = parser.parse_args()
142140

143-
run_generate_api(args.config_file, args.artifact_type, not args.quiet)
141+
run_generate_api(args.config_file, args.artifact_name, not args.quiet)
144142

145143
if __name__ == '__main__':
146144
main()

0 commit comments

Comments
 (0)