Skip to content

Commit a28bc8d

Browse files
Creating generate_api.py (#3389)
1 parent e26920d commit a28bc8d

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

utilities/generate_api.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
import io
17+
import os
18+
import shutil
19+
import subprocess
20+
21+
from distutils import dir_util
22+
from ruamel import yaml
23+
24+
def run(config_path):
25+
api_dir_index = config_path.find('/google/')
26+
if api_dir_index == -1:
27+
raise ValueError('Didn\'t find /google/ in config file path; need absolute path to the artman config file.')
28+
root_dir = config_path[0:api_dir_index]
29+
api_dir = config_path[api_dir_index+1:]
30+
31+
subprocess.check_call(['artman', '--config', api_dir, '--local', '-v', '--root-dir', root_dir, 'generate', 'java_gapic'])
32+
33+
with io.open(config_path, encoding='UTF-8') as config_file:
34+
artman_config_data = yaml.load(config_file, Loader=yaml.Loader)
35+
36+
api_name = artman_config_data['common']['api_name']
37+
api_version = artman_config_data['common']['api_version']
38+
org_name = artman_config_data['common']['organization_name']
39+
40+
api_full_name = '{}-{}-{}'.format(org_name, api_name, api_version)
41+
proto_dirname = 'proto-{}'.format(api_full_name)
42+
grpc_dirname = 'grpc-{}'.format(api_full_name)
43+
gapic_dirname = 'gapic-{}'.format(api_full_name)
44+
proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname)
45+
grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname)
46+
gapic_dir = os.path.join('artman-genfiles', 'java', gapic_dirname)
47+
print(proto_dir)
48+
print(grpc_dir)
49+
print(gapic_dir)
50+
if not os.path.exists(proto_dir):
51+
raise ValueError('generated proto dir doesn\'t exist: {}'.format(proto_dir))
52+
if not os.path.exists(grpc_dir):
53+
raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir))
54+
if not os.path.exists(gapic_dir):
55+
raise ValueError('generated gapic dir doesn\'t exist: {}'.format(gapic_dir))
56+
57+
target_proto_dir = os.path.join('google-api-grpc', proto_dirname)
58+
target_grpc_dir = os.path.join('google-api-grpc', grpc_dirname)
59+
if os.path.exists(target_proto_dir):
60+
print('{} already exists, removing & replacing it.'.format(target_proto_dir))
61+
if os.path.exists(target_grpc_dir):
62+
print('{} already exists, removing & replacing it.'.format(target_grpc_dir))
63+
64+
print('-- ignore any pathspec errors that follow')
65+
66+
if os.path.exists(target_proto_dir):
67+
shutil.rmtree(target_proto_dir)
68+
shutil.copytree(proto_dir, target_proto_dir)
69+
os.remove(os.path.join(target_proto_dir, 'LICENSE'))
70+
os.remove(os.path.join(target_proto_dir, 'build.gradle'))
71+
subprocess.call(['git', 'checkout', os.path.join(target_proto_dir, 'pom.xml')])
72+
73+
if os.path.exists(target_grpc_dir):
74+
shutil.rmtree(target_grpc_dir)
75+
shutil.copytree(grpc_dir, target_grpc_dir)
76+
os.remove(os.path.join(target_grpc_dir, 'LICENSE'))
77+
os.remove(os.path.join(target_grpc_dir, 'build.gradle'))
78+
subprocess.call(['git', 'checkout', os.path.join(target_grpc_dir, 'pom.xml')])
79+
80+
api_unversioned_name = '{}-{}'.format(org_name, api_name)
81+
target_gapic_dir = os.path.join('google-cloud-clients', api_unversioned_name)
82+
dir_util.copy_tree(os.path.join(gapic_dir, 'src'), os.path.join(target_gapic_dir, 'src'))
83+
84+
print('**** REMAINING MANUAL WORK: *****')
85+
print('This script doesn\'t set up new clients. If this is a new client, you need to:')
86+
print('1. Add the new proto and grpc modules into google-api-grpc/pom.xml')
87+
print('3. Copy an existing client pom.xml to the new client directory, update its text')
88+
print('4. Copy an existing client README.md to the new client directory, update its text')
89+
print('5. Add the new proto, grpc, and client modules into versions.txt')
90+
print('6. Add the new proto, grpc, and client modules into google-cloud-bom/pom.xml')
91+
print('7. Run `utilities/replace_versions.py` to update the module versions')
92+
93+
def main():
94+
parser = argparse.ArgumentParser(description='Regenerate a single API.')
95+
parser.add_argument('config_file', help='The artman config file for the API')
96+
args = parser.parse_args()
97+
98+
run(args.config_file)
99+
100+
if __name__ == '__main__':
101+
main()

0 commit comments

Comments
 (0)