forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateapilist.py
More file actions
231 lines (172 loc) · 7.3 KB
/
updateapilist.py
File metadata and controls
231 lines (172 loc) · 7.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script is used to synthesize generated parts of this library."""
import os
import requests
from typing import List, Optional
from dataclasses import dataclass
class MissingGithubToken(ValueError):
"""Raised when the GITHUB_TOKEN environment variable is not set"""
pass
RAW_CONTENT_BASE_URL = "https://raw.githubusercontent.com"
MONO_REPO_PATH_FORMAT = "googleapis/google-cloud-python/main/packages/{repo_slug}"
SPLIT_REPO_PATH_FORMAT = "{repo_slug}/main"
REPO_METADATA_FILENAME = ".repo-metadata.json"
# MONO_REPO defines the name of the mono repository for Python.
MONO_REPO = "googleapis/google-cloud-python"
# REPO_EXCLUSION lists the repositories that need to be excluded.
REPO_EXCLUSION = [
# core libraries
"googleapis/python-api-core",
"googleapis/python-cloud-core",
# proto only packages
"googleapis/python-api-common-protos",
# testing utilities
"googleapis/python-test-utils",
]
# PACKAGE_RESPONSE_KEY defines the package name in the response.
PACKAGE_RESPONSE_KEY = "name"
# REPO_RESPONSE_KEY defines the repository name in the response.
REPO_RESPONSE_KEY = "full_name"
# ARCHIVED_RESPONSE_KEY defines the repository archived status in the response.
ARCHIVED_RESPONSE_KEY = "archived"
# BASE_API defines the base API for Github.
BASE_API = "https://api.github.com"
class CloudClient:
repo: str = None
title: str = None
release_level: str = None
distribution_name: str = None
issue_tracker: str = None
def __init__(self, repo: dict):
self.repo = repo["repo"]
# For now, strip out "Google Cloud" to standardize the titles
self.title = repo["name_pretty"].replace("Google ", "").replace("Cloud ", "")
self.release_level = repo["release_level"]
self.distribution_name = repo["distribution_name"]
self.issue_tracker = repo.get("issue_tracker")
# For sorting, we want to sort by release level, then API pretty_name
def __lt__(self, other):
if self.release_level == other.release_level:
return self.title < other.title
return other.release_level < self.release_level
def __repr__(self):
return repr((self.release_level, self.title))
@dataclass
class Extractor:
path_format: str
response_key: str
def client_for_repo(self, repo_slug) -> Optional[CloudClient]:
path = self.path_format.format(repo_slug=repo_slug)
url = f"{RAW_CONTENT_BASE_URL}/{path}/{REPO_METADATA_FILENAME}"
response = requests.get(url)
if response.status_code != requests.codes.ok:
return
return CloudClient(response.json())
def get_clients_from_batch_response(self, response_json) -> List[CloudClient]:
return [self.client_for_repo(repo[self.response_key]) for repo in response_json if allowed_repo(repo)]
def replace_content_in_readme(content_rows: List[str]) -> None:
START_MARKER = ".. API_TABLE_START"
END_MARKER = ".. API_TABLE_END"
newlines = []
repl_open = False
with open("README.rst", "r") as f:
for line in f:
if not repl_open:
newlines.append(line)
if line.startswith(START_MARKER):
repl_open = True
newlines = newlines + content_rows
elif line.startswith(END_MARKER):
newlines.append("\n")
newlines.append(line)
repl_open = False
with open("README.rst", "w") as f:
for line in newlines:
f.write(line)
def client_row(client: CloudClient) -> str:
pypi_badge = f""".. |PyPI-{client.distribution_name}| image:: https://img.shields.io/pypi/v/{client.distribution_name}.svg
:target: https://pypi.org/project/{client.distribution_name}\n"""
url = f"https://github.com/{client.repo}"
if client.repo == MONO_REPO:
url += f"/tree/main/packages/{client.distribution_name}"
content_row = [
f" * - `{client.title} <{url}>`_\n",
f" - " + client.release_level + "\n",
f" - |PyPI-{client.distribution_name}|\n",
]
if client.issue_tracker:
content_row.append(f" - `API Issues <{client.issue_tracker}>`_\n")
return (content_row, pypi_badge)
def generate_table_contents(clients: List[CloudClient]) -> List[str]:
content_rows = [
"\n",
".. list-table::\n",
" :header-rows: 1\n",
"\n",
" * - Client\n",
" - Release Level\n",
" - Version\n",
" - API Issue Tracker\n",
]
pypi_links = ["\n"]
for client in clients:
content_row, pypi_link = client_row(client)
content_rows += content_row
pypi_links.append(pypi_link)
return content_rows + pypi_links
def allowed_repo(repo) -> bool:
return REPO_RESPONSE_KEY not in repo or (
repo[REPO_RESPONSE_KEY].startswith("googleapis/python-")
and repo[REPO_RESPONSE_KEY] not in REPO_EXCLUSION
and not repo[ARCHIVED_RESPONSE_KEY]
)
def mono_repo_clients(token: str) -> List[CloudClient]:
# all mono repo clients
url = f"{BASE_API}/repos/{MONO_REPO}/contents/packages"
headers = {'Authorization': f'token {token}'}
response = requests.get(url=url, headers=headers)
mono_repo_extractor = Extractor(path_format=MONO_REPO_PATH_FORMAT, response_key=PACKAGE_RESPONSE_KEY)
return mono_repo_extractor.get_clients_from_batch_response(response.json())
def split_repo_clients(token: str) -> List[CloudClient]:
first_request = True
while first_request or 'next' in response.links:
if first_request:
url = f"{BASE_API}/search/repositories?page=1"
first_request = False
else:
url = response.links['next']['url']
headers = {'Authorization': f'token {token}'}
params = {'per_page': 100, "q": "python- in:name org:googleapis"}
response = requests.get(url=url, params=params, headers=headers)
repositories = response.json().get("items", [])
if len(repositories) == 0:
break
split_repo_extractor = Extractor(path_format=SPLIT_REPO_PATH_FORMAT, response_key=REPO_RESPONSE_KEY)
return split_repo_extractor.get_clients_from_batch_response(repositories)
def get_token():
if 'GITHUB_TOKEN' not in os.environ:
raise MissingGithubToken("Please include a GITHUB_TOKEN env var.")
token = os.environ['GITHUB_TOKEN']
return token
def all_clients() -> List[CloudClient]:
clients = []
token = get_token()
clients.extend(split_repo_clients(token))
clients.extend(mono_repo_clients(token))
# remove empty clients
return [client for client in clients if client]
clients = sorted(all_clients())
table_contents = generate_table_contents(clients)
replace_content_in_readme(table_contents)