-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathconfigure_release_please.py
More file actions
174 lines (137 loc) · 6.27 KB
/
configure_release_please.py
File metadata and controls
174 lines (137 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import json
from pathlib import Path
from typing import Union, Dict, List, Tuple
import re
SCRIPT_DIR = Path(__file__).resolve().parent
ROOT_DIR = Path(SCRIPT_DIR / ".." / "..").resolve()
PACKAGES_DIR = ROOT_DIR / "packages"
PACKAGES_WITHOUT_GAPIC_VERSION = [
"google-cloud-access-context-manager",
"google-cloud-audit-log",
"googleapis-common-protos",
"grpc-google-iam-v1",
]
def get_version_for_package(version_path: Path) -> Tuple[int]:
"""
Given a `version_path` to a `gapic_version.py` file,
return Tuple<int> which contains the version.
Args:
version_path(pathlib.Path): Path to the gapic_version.py file
Returns:
Tuple[int] in the format (<major>, <minor>, <patch>)
"""
VERSION_REGEX = r"__version__\s=\s\"(?P<major_version>\d+)\.(?P<minor_version>\d+)\.(?P<patch_version>\d+)\""
match = re.search(VERSION_REGEX, version_path.read_text())
if match is None:
raise Exception("Could not detect version")
major_version = int(match.group("major_version"))
minor_version = int(match.group("minor_version"))
patch_version = int(match.group("patch_version"))
if any(elem is None for elem in [major_version, minor_version, patch_version]):
raise Exception("could not detect version")
return (major_version, minor_version, patch_version)
def get_packages_with_owlbot_yaml(packages_dir: Path = PACKAGES_DIR) -> List[Path]:
"""
Walks through all API packages in the specified `packages_dir` path.
Args:
packages_dir(pathlib.Path): Path to the directory which contains packages.
Returns:
List[pathlib.Path] where each entry corresponds to a package within the
specified `packages_dir`, which has a corresponding .OwlBot.yaml file.
"""
if not Path(packages_dir).exists():
raise FileNotFoundError(f"Directory {packages_dir} not found")
return [obj.parents[0].resolve() for obj in packages_dir.rglob("**/.OwlBot.yaml")]
def configure_release_please_manifest(
package_dirs: List[Path], root_dir: Path = ROOT_DIR
) -> None:
"""
This method updates the `.release-please-manifest.json` file in the directory
`root_dir`. It removes entries that are not in `package_dirs` and updates
versions for both existing and new packages.
Args:
package_dirs(List[pathlib.Path]): A list of Paths, one for each package in the
`packages/` folder whose entry will be updated in the release-please manifest.
root_dir(pathlib.Path): The directory to update the `.release-please-manifest.json`
Returns:
None
"""
release_please_manifest = root_dir / ".release-please-manifest.json"
with open(release_please_manifest, "r") as f:
manifest_json = json.load(f)
expected_package_paths = {
f"packages/{package_dir.name}" for package_dir in package_dirs
}
keys_to_remove = []
for key in manifest_json.keys():
if key not in expected_package_paths:
keys_to_remove.append(key)
for key in keys_to_remove:
print(f"Removed stale entry from manifest: {key}")
del manifest_json[key]
for package_dir in package_dirs:
if f"packages/{package_dir.name}" not in manifest_json:
manifest_json[f"packages/{package_dir.name}"] = "0.0.0"
if not package_supports_gapic_version(package_dir):
continue
gapic_version_file = next(package_dir.rglob("**/gapic_version.py"), None)
if gapic_version_file is None:
raise Exception("Failed to find gapic_version.py")
version = get_version_for_package(gapic_version_file)
# check the version in gapic_version.py and update if newer than the default which is
# 0.0.0 or 0.1.0.
if version != (0, 0, 0) and version != (0, 1, 0):
manifest_json[
f"packages/{package_dir.name}"
] = f"{version[0]}.{version[1]}.{version[2]}"
with open(release_please_manifest, "w") as f:
json.dump(manifest_json, f, indent=4, sort_keys=True)
f.write("\n")
def package_supports_gapic_version(package_dir: str) -> bool:
"""Returns True if the given package directory is expected to have gapic_version.py"""
for package in PACKAGES_WITHOUT_GAPIC_VERSION:
if package_dir == Path(PACKAGES_DIR / package):
return False
return True
def configure_release_please_config(
package_dirs: List[Path], root_dir: Path = ROOT_DIR
) -> None:
"""
This method updates the `release-please-config.json` file in the directory
`root_dir`. If `root_dir` is not provided, `google-cloud-python` will be used as the root.
Args:
package_dirs(List[pathlib.Path]): A list of Paths, one for each package in
the `packages/` folder whose entry will be updated in the release-please config.
root_dir(pathlib.Path): The directory to update the `release-please-config.json`
Returns:
None
"""
release_please_config = root_dir / "release-please-config.json"
config_json = {"packages": {}}
for package_dir in package_dirs:
extra_files: List[Union[str, Dict[str, str]]] = [
str(file.relative_to(package_dir))
for file in sorted(package_dir.rglob("**/gapic_version.py"))
]
if len(extra_files) < 1 and package_supports_gapic_version(package_dir):
raise Exception("Failed to find gapic_version.py")
for json_file in sorted(package_dir.glob("samples/**/*.json")):
sample_json = {}
sample_json["jsonpath"] = "$.clientLibrary.version"
sample_json["path"] = str(json_file.relative_to(package_dir))
sample_json["type"] = "json"
extra_files.append(sample_json)
config_json["packages"][f"packages/{package_dir.name}"] = {
"component": f"{package_dir.name}",
"release-type": "python",
"extra-files": extra_files,
"bump-minor-pre-major": True,
"bump-patch-for-minor-pre-major": True,
}
with open(release_please_config, "w") as f:
json.dump(config_json, f, indent=4, sort_keys=True)
f.write("\n")
if __name__ == "__main__":
owlbot_dirs = get_packages_with_owlbot_yaml()
configure_release_please_manifest(owlbot_dirs)
configure_release_please_config(owlbot_dirs)