|
| 1 | +""" |
| 2 | +This script allows generation of libraries that are composed of more than one |
| 3 | +service version. It is achieved by calling `generate_library.sh` without |
| 4 | +postprocessing for all service versions and then calling |
| 5 | +postprocess_library.sh at the end, once all libraries are ready. |
| 6 | +
|
| 7 | +Prerequisites |
| 8 | +- Needs a folder named `output` in current working directory. This folder |
| 9 | +is automatically detected by `generate_library.sh` and this script ensures it |
| 10 | +contains the necessary folders and files, specifically: |
| 11 | + - A "google" folder found in the googleapis/googleapis repository |
| 12 | + - A "grafeas" folder found in the googleapis/googleapis repository |
| 13 | +Note: googleapis repo is found in https://github.com/googleapis/googleapis. |
| 14 | +""" |
| 15 | + |
| 16 | +import click |
| 17 | +import utilities as util |
| 18 | +import os |
| 19 | +import sys |
| 20 | +import subprocess |
| 21 | +import json |
| 22 | +from model.GenerationConfig import GenerationConfig |
| 23 | +from model.LibraryConfig import LibraryConfig |
| 24 | +from model.ClientInputs import parse as parse_build_file |
| 25 | + |
| 26 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 27 | + |
| 28 | +""" |
| 29 | +Main function in charge of generating libraries composed of more than one |
| 30 | +service or service version. |
| 31 | +Arguments |
| 32 | + - config: a GenerationConfig object representing a parsed configuration |
| 33 | + yaml |
| 34 | + - library: a LibraryConfig object contained inside config, passed here for |
| 35 | + convenience and to prevent all libraries to be processed |
| 36 | + - enable_postprocessing: true if postprocessing should be done on the generated |
| 37 | + libraries |
| 38 | + - repository_path: path to the repository where the generated files will be |
| 39 | + sent. If not specified, it will default to the one defined in the configuration yaml |
| 40 | + and will be downloaded. The versions file will be inferred from this folder |
| 41 | +""" |
| 42 | +def generate_composed_library( |
| 43 | + config: GenerationConfig, |
| 44 | + library: LibraryConfig, |
| 45 | + repository_path: str, |
| 46 | + enable_postprocessing: bool = True, |
| 47 | +) -> None: |
| 48 | + output_folder = util.sh_util('get_output_folder') |
| 49 | + |
| 50 | + print(f'output_folder: {output_folder}') |
| 51 | + print('library: ', library) |
| 52 | + os.makedirs(output_folder, exist_ok=True) |
| 53 | + |
| 54 | + googleapis_commitish = config.googleapis_commitish |
| 55 | + if library.googleapis_commitish is not None: |
| 56 | + googleapis_commitish = library.googleapis_commitish |
| 57 | + print('using library-specific googleapis commitish: ' + googleapis_commitish) |
| 58 | + else: |
| 59 | + print('using common googleapis_commitish') |
| 60 | + |
| 61 | + print('removing old googleapis folders and files') |
| 62 | + util.delete_if_exists(f'{output_folder}/google') |
| 63 | + util.delete_if_exists(f'{output_folder}/grafeas') |
| 64 | + |
| 65 | + print('downloading googleapis') |
| 66 | + util.sh_util(f'download_googleapis_files_and_folders "{output_folder}" "{googleapis_commitish}"') |
| 67 | + |
| 68 | + is_monorepo = len(config.libraries) > 1 |
| 69 | + |
| 70 | + base_arguments = [] |
| 71 | + base_arguments += util.create_argument('gapic_generator_version', config) |
| 72 | + base_arguments += util.create_argument('grpc_version', config) |
| 73 | + base_arguments += util.create_argument('protobuf_version', config) |
| 74 | + |
| 75 | + library_name = f'java-{library.api_shortname}' |
| 76 | + library_path = None |
| 77 | + |
| 78 | + versions_file = '' |
| 79 | + if is_monorepo: |
| 80 | + print('this is a monorepo library') |
| 81 | + destination_path = config.destination_path + '/' + library_name |
| 82 | + library_folder = destination_path.split('/')[-1] |
| 83 | + if repository_path is None: |
| 84 | + print(f'sparse_cloning monorepo with {library_name}') |
| 85 | + repository_path = f'{output_folder}/{config.destination_path}' |
| 86 | + clone_out = util.sh_util(f'sparse_clone "https://github.com/googleapis/{MONOREPO_NAME}.git" "{library_folder} google-cloud-pom-parent google-cloud-jar-parent versions.txt .github"', cwd=output_folder) |
| 87 | + print(clone_out) |
| 88 | + library_path = f'{repository_path}/{library_name}' |
| 89 | + versions_file = f'{repository_path}/versions.txt' |
| 90 | + else: |
| 91 | + print('this is a HW library') |
| 92 | + destination_path = library_name |
| 93 | + if repository_path is None: |
| 94 | + repository_path = f'{output_folder}/{destination_path}' |
| 95 | + util.delete_if_exists(f'{output_folder}/{destination_path}') |
| 96 | + clone_out = util.sh_util(f'git clone "https://github.com/googleapis/{destination_path}.git"', cwd=output_folder) |
| 97 | + print(clone_out) |
| 98 | + library_path = f'{repository_path}' |
| 99 | + versions_file = f'{repository_path}/versions.txt' |
| 100 | + |
| 101 | + owlbot_cli_source_folder = util.sh_util('mktemp -d') |
| 102 | + for gapic in library.gapic_configs: |
| 103 | + |
| 104 | + effective_arguments = list(base_arguments) |
| 105 | + effective_arguments += util.create_argument('proto_path', gapic) |
| 106 | + |
| 107 | + build_file_folder = f'{output_folder}/{gapic.proto_path}' |
| 108 | + print(f'build_file_folder: {build_file_folder}') |
| 109 | + client_inputs = parse_build_file(build_file_folder, gapic.proto_path) |
| 110 | + effective_arguments += [ |
| 111 | + '--proto_only', client_inputs.proto_only, |
| 112 | + '--gapic_additional_protos', client_inputs.additional_protos, |
| 113 | + '--transport', client_inputs.transport, |
| 114 | + '--rest_numeric_enums', client_inputs.rest_numeric_enum, |
| 115 | + '--gapic_yaml', client_inputs.gapic_yaml, |
| 116 | + '--service_config', client_inputs.service_config, |
| 117 | + '--service_yaml', client_inputs.service_yaml, |
| 118 | + '--include_samples', client_inputs.include_samples, |
| 119 | + ] |
| 120 | + service_version = gapic.proto_path.split('/')[-1] |
| 121 | + temp_destination_path = f'java-{library.api_shortname}-{service_version}' |
| 122 | + effective_arguments += [ '--destination_path', temp_destination_path ] |
| 123 | + print('arguments: ') |
| 124 | + print(effective_arguments) |
| 125 | + print(f'Generating library from {gapic.proto_path} to {destination_path}...') |
| 126 | + util.run_process_and_print_output(['bash', '-x', f'{script_dir}/generate_library.sh', |
| 127 | + *effective_arguments], 'Library generation') |
| 128 | + |
| 129 | + |
| 130 | + if enable_postprocessing: |
| 131 | + util.sh_util(f'build_owlbot_cli_source_folder "{library_path}"' |
| 132 | + + f' "{owlbot_cli_source_folder}" "{output_folder}/{temp_destination_path}"' |
| 133 | + + f' "{gapic.proto_path}"', |
| 134 | + cwd=output_folder) |
| 135 | + |
| 136 | + if enable_postprocessing: |
| 137 | + # call postprocess library |
| 138 | + util.run_process_and_print_output([f'{script_dir}/postprocess_library.sh', |
| 139 | + f'{library_path}', '', versions_file, owlbot_cli_source_folder, |
| 140 | + config.owlbot_cli_image, config.synthtool_commitish, str(is_monorepo).lower()], 'Library postprocessing') |
| 141 | + |
0 commit comments