|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import json |
| 18 | +from pathlib import Path |
| 19 | +from typing import List |
| 20 | +import yaml |
| 21 | + |
| 22 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 23 | +ROOT_DIR = Path(SCRIPT_DIR / ".." / "..").resolve() |
| 24 | +PACKAGES_DIR = ROOT_DIR / "packages" |
| 25 | +PACKAGES_TO_ONBOARD_YAML = SCRIPT_DIR / "packages_to_onboard.yaml" |
| 26 | +LIBRARIAN_DIR = ROOT_DIR / ".librarian" |
| 27 | +LIBRARIAN_YAML = LIBRARIAN_DIR / "state.yaml" |
| 28 | +RELEASE_PLEASE_MANIFEST_JSON = ROOT_DIR / ".release-please-manifest.json" |
| 29 | +GAPIC_METADATA_JSON = "gapic_metadata.json" |
| 30 | + |
| 31 | + |
| 32 | +def configure_state_yaml(package_dirs: List[Path]) -> None: |
| 33 | + """ |
| 34 | + This method updates the `state.yaml` file in the directory |
| 35 | + `.librarian`. |
| 36 | +
|
| 37 | + Args: |
| 38 | + package_dirs(List[pathlib.Path]): A list of Paths, one for each package in the |
| 39 | + `packages/` folder whose entry will be updated in the `state.yaml`. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + None |
| 43 | + """ |
| 44 | + |
| 45 | + state_dict = {} |
| 46 | + |
| 47 | + release_please_manifest = {} |
| 48 | + with open(RELEASE_PLEASE_MANIFEST_JSON, "r") as release_please_manifest_json_file: |
| 49 | + release_please_manifest = json.load(release_please_manifest_json_file) |
| 50 | + |
| 51 | + packages_to_onboard = {} |
| 52 | + with open(PACKAGES_TO_ONBOARD_YAML, "r") as packages_to_onboard_yaml_file: |
| 53 | + packages_to_onboard = yaml.safe_load(packages_to_onboard_yaml_file) |
| 54 | + |
| 55 | + state_dict["image"] = "gapic-generator-python:latest" |
| 56 | + state_dict["libraries"] = [] |
| 57 | + for package_name in packages_to_onboard["packages_to_onboard"]: |
| 58 | + package_path = Path(PACKAGES_DIR / package_name).resolve() |
| 59 | + api_paths = [] |
| 60 | + for individual_metadata_file in package_path.rglob(f"**/{GAPIC_METADATA_JSON}"): |
| 61 | + with open(individual_metadata_file, "r") as gapic_metadata_json_file: |
| 62 | + gapic_metadata = json.load(gapic_metadata_json_file) |
| 63 | + api_paths.extend( |
| 64 | + [ |
| 65 | + { |
| 66 | + "path": gapic_metadata["protoPackage"].replace(".", "/"), |
| 67 | + "service_config": "", |
| 68 | + } |
| 69 | + ] |
| 70 | + ) |
| 71 | + state_dict["libraries"].append( |
| 72 | + { |
| 73 | + "id": package_name, |
| 74 | + "version": release_please_manifest[f"packages/{package_name}"], |
| 75 | + "last_generated_commit": "97a83d76a09a7f6dcab43675c87bdfeb5bcf1cb5", |
| 76 | + "apis": api_paths, |
| 77 | + "sourcePaths": [f"packages/{package_name}"], |
| 78 | + "preserve_regex": "", |
| 79 | + "remove_regex": "", |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + with open(LIBRARIAN_YAML, "w") as f: |
| 84 | + yaml.dump(state_dict, f) |
| 85 | + |
| 86 | + |
| 87 | +def get_all_packages(packages_dir: Path = PACKAGES_DIR) -> List[Path]: |
| 88 | + """ |
| 89 | + Walks through all API packages in the specified `packages_dir` path. |
| 90 | +
|
| 91 | + Args: |
| 92 | + packages_dir(pathlib.Path): Path to the directory which contains packages. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + List[pathlib.Path] where each entry corresponds to a package within the |
| 96 | + specified `packages_dir`. |
| 97 | + """ |
| 98 | + if not Path(packages_dir).exists(): |
| 99 | + raise FileNotFoundError(f"Directory {packages_dir} not found") |
| 100 | + return [obj.parents[0].resolve() for obj in packages_dir.rglob("**/.OwlBot.yaml")] |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + package_dirs = get_all_packages() |
| 105 | + configure_state_yaml(package_dirs) |
0 commit comments