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
2122import argparse
2223import io
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):
113131def 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
122142if __name__ == '__main__' :
123143 main ()
0 commit comments