xref: /aosp_15_r20/external/google-cloud-java/owl-bot-postprocessor/synthtool/languages/common.py (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1*55e87721SMatt Gilbride# Copyright 2018 Google LLC
2*55e87721SMatt Gilbride#
3*55e87721SMatt Gilbride# Licensed under the Apache License, Version 2.0 (the "License");
4*55e87721SMatt Gilbride# you may not use this file except in compliance with the License.
5*55e87721SMatt Gilbride# You may obtain a copy of the License at
6*55e87721SMatt Gilbride#
7*55e87721SMatt Gilbride#     https://www.apache.org/licenses/LICENSE-2.0
8*55e87721SMatt Gilbride#
9*55e87721SMatt Gilbride# Unless required by applicable law or agreed to in writing, software
10*55e87721SMatt Gilbride# distributed under the License is distributed on an "AS IS" BASIS,
11*55e87721SMatt Gilbride# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*55e87721SMatt Gilbride# See the License for the specific language governing permissions and
13*55e87721SMatt Gilbride# limitations under the License.
14*55e87721SMatt Gilbride
15*55e87721SMatt Gilbrideimport json
16*55e87721SMatt Gilbridefrom pathlib import Path
17*55e87721SMatt Gilbrideimport re
18*55e87721SMatt Gilbride
19*55e87721SMatt Gilbride
20*55e87721SMatt Gilbridedef update_library_version(version: str, root_dir: str):
21*55e87721SMatt Gilbride    """
22*55e87721SMatt Gilbride    Rewrites all metadata files in ./samples/generated to the version number argument
23*55e87721SMatt Gilbride
24*55e87721SMatt Gilbride    """
25*55e87721SMatt Gilbride    root_dir_path = Path(root_dir)
26*55e87721SMatt Gilbride
27*55e87721SMatt Gilbride    snippet_metadata_files = get_sample_metadata_files(root_dir_path)
28*55e87721SMatt Gilbride    for file in snippet_metadata_files:
29*55e87721SMatt Gilbride        with open(file, "r+") as f:
30*55e87721SMatt Gilbride            data = json.load(f)
31*55e87721SMatt Gilbride            data["clientLibrary"]["version"] = version
32*55e87721SMatt Gilbride            f.seek(0)
33*55e87721SMatt Gilbride            json.dump(data, f, indent=4)
34*55e87721SMatt Gilbride            f.truncate()
35*55e87721SMatt Gilbride
36*55e87721SMatt Gilbride
37*55e87721SMatt Gilbridedef get_sample_metadata_files(dir: Path, regex: str = r"snippet_metadata"):
38*55e87721SMatt Gilbride    """
39*55e87721SMatt Gilbride    Walks through samples/generated to find all snippet metadata files, appends them to a list
40*55e87721SMatt Gilbride
41*55e87721SMatt Gilbride    Returns:
42*55e87721SMatt Gilbride    A list of all metadata files.
43*55e87721SMatt Gilbride    """
44*55e87721SMatt Gilbride    metadata_files = []
45*55e87721SMatt Gilbride    for path_object in dir.glob("**/*"):
46*55e87721SMatt Gilbride        if path_object.is_file():
47*55e87721SMatt Gilbride            if re.search(regex, str(path_object)):
48*55e87721SMatt Gilbride                metadata_files.append(str(Path(path_object).resolve()))
49*55e87721SMatt Gilbride        if path_object.is_dir():
50*55e87721SMatt Gilbride            get_sample_metadata_files(path_object)
51*55e87721SMatt Gilbride
52*55e87721SMatt Gilbride    return metadata_files
53