Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GCE and discogapic to batch gen script #3441

Merged
merged 12 commits into from
Jul 11, 2018
27 changes: 21 additions & 6 deletions utilities/batch_generate_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@

# Instructions:
#
# Check out the googleapis repo somewhere locally (e.g. from
# https://github.com/googleapis/googleapis):
# Check out the googleapis and discovery-artifact-manager repo somewhere locally
# (e.g. from https://github.com/googleapis/googleapis):
#
# $ git checkout https://github.com/googleapis/googleapis.git
# $ git checkout https://github.com/googleapis/discovery-artifact-manager

This comment was marked as spam.

This comment was marked as spam.

#
# Run this script:
#
# $ python utilities/batch_generate_apis.py PATH_TO_GOOGLEAPIS
# $ python utilities/batch_generate_apis.py PATH_TO_GOOGLEAPIS PATH_TO_DISCOVERY_ARTIFACT_MANAGER

import argparse
import os

import generate_api


def run(googleapis):
def run_gapic_gen(googleapis):
def generate(artman_yaml):
generate_api.run_generate_api(os.path.join(googleapis, artman_yaml))
generate_api.run_generate_api(os.path.join(googleapis, artman_yaml), "java_gapic")

# TODO Needs to have java_proto called instead of java_grpc
#generate('google/datastore/artman_datastore.yaml')
Expand Down Expand Up @@ -73,13 +74,27 @@ def generate(artman_yaml):
generate('google/cloud/websecurityscanner/artman_websecurityscanner_v1alpha.yaml')


def run_discogapic_gen(discovery_repo):
def generate(artman_yaml):
# Run java_discogapic task. No proto or grpc libraries are generated.
generate_api.run_generate_api(os.path.join(discovery_repo, artman_yaml),
"java_discogapic",
root_dir=discovery_repo,
has_proto_libs=False)

This comment was marked as spam.

This comment was marked as spam.


generate('gapic/google/compute/artman_compute.yaml')


def main():
# TODO Make the docker image the default, add --local option
parser = argparse.ArgumentParser(description='Batch generate all APIs.')
parser.add_argument('googleapis', help='The path to the googleapis repo')
parser.add_argument('discovery', help='The path to the discovery-artifact-manager repo')

This comment was marked as spam.

This comment was marked as spam.

args = parser.parse_args()

run(args.googleapis)
run_gapic_gen(args.googleapis)
run_discogapic_gen(args.discovery)


if __name__ == '__main__':
main()
89 changes: 55 additions & 34 deletions utilities/generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# Find the artman config file the describes the API you want to generate a client for.
#
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE
# $ python utilities/generate_api.py PATH_TO_ARTMAN_CONFIG_FILE java_gapic

import argparse
import io
Expand All @@ -36,18 +36,33 @@
}


def run_generate_api(config_path, noisy=False):
def run_generate_api(config_path, artifact_type, root_dir=None, has_proto_libs=True, 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 root_dir: (str) Path to the directory containing the API definitions.
:param has_proto_libs: (bool) if the generated proto and grpc libraries
should replace the existing ones
:param noisy: (bool) if console output should be verbose.

"""
googleapis_index = config_path.rfind('/google/')
if googleapis_index == -1:
raise ValueError('Didn\'t find /googleapis/ in config file path; need absolute path to the artman config file.')
root_dir = config_path[0:googleapis_index]
api_dir = config_path[googleapis_index+1:]
if has_proto_libs:
root_dir = config_path[0:googleapis_index]
api_dir = config_path[googleapis_index+1:]
else:
api_dir = config_path

extra_options = []
if noisy:
extra_options = ['-v']

subprocess.check_call(['artman', '--config', api_dir, '--local', '--root-dir', root_dir] + extra_options + ['generate', 'java_gapic'])
print("api_dir: "+ api_dir)
print("root_dir: "+ root_dir)

This comment was marked as spam.

This comment was marked as spam.

subprocess.check_call(['artman', '--config', api_dir, '--local', '--root-dir', root_dir] + extra_options + ['generate', artifact_type])

with io.open(config_path, encoding='UTF-8') as config_file:
artman_config_data = yaml.load(config_file, Loader=yaml.Loader)
Expand All @@ -60,38 +75,42 @@ def run_generate_api(config_path, noisy=False):
proto_dirname = 'proto-{}'.format(api_full_name)
grpc_dirname = 'grpc-{}'.format(api_full_name)
gapic_dirname = 'gapic-{}'.format(api_full_name)
proto_dir = os.path.join('artman-genfiles', 'java', proto_dirname)
grpc_dir = os.path.join('artman-genfiles', 'java', grpc_dirname)

gapic_dir = os.path.join('artman-genfiles', 'java', gapic_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):
raise ValueError('generated grpc dir doesn\'t exist: {}'.format(grpc_dir))
if not os.path.exists(gapic_dir):
raise ValueError('generated gapic dir doesn\'t exist: {}'.format(gapic_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')])
if has_proto_libs:
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):
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:
Expand All @@ -113,11 +132,13 @@ def run_generate_api(config_path, 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',
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, not args.quiet)
run_generate_api(args.config_file, args.artifact_type, not args.quiet)

if __name__ == '__main__':
main()