Skip to content

Commit 163167e

Browse files
committed
fix formatting and add logging to pinned browsers script
1 parent 27d4f16 commit 163167e

File tree

1 file changed

+93
-48
lines changed

1 file changed

+93
-48
lines changed

scripts/pinned_browsers.py

Lines changed: 93 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,86 @@
33
import hashlib
44
import json
55
import os
6-
import urllib3
6+
import sys
7+
from pathlib import Path
78

9+
import urllib3
810
from packaging.version import parse
9-
from pathlib import Path
1011

1112
# Find the current stable versions of each browser we
1213
# support and the sha256 of these. That's useful for
1314
# updating `//common:repositories.bzl`
1415

1516
http = urllib3.PoolManager()
1617

18+
1719
def calculate_hash(url):
20+
print("Calculate hash for %s" % url, file=sys.stderr)
1821
h = hashlib.sha256()
19-
r = http.request('GET', url, preload_content=False)
22+
r = http.request("GET", url, preload_content=False)
2023
for b in iter(lambda: r.read(4096), b""):
2124
h.update(b)
2225
return h.hexdigest()
2326

27+
2428
def get_chrome_milestone():
2529
channel = os.getenv("CHROME_CHANNEL", "Stable")
26-
r = http.request('GET', f'https://chromiumdash.appspot.com/fetch_releases?channel={channel}&num=1&platform=Mac,Linux')
30+
r = http.request(
31+
"GET", f"https://chromiumdash.appspot.com/fetch_releases?channel={channel}&num=1&platform=Mac,Linux"
32+
)
2733
all_versions = json.loads(r.data)
2834
# use the same milestone for all chrome releases, so pick the lowest
2935
milestone = min([version["milestone"] for version in all_versions if version["milestone"]])
30-
r = http.request('GET', 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json')
36+
r = http.request(
37+
"GET", "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
38+
)
3139
versions = json.loads(r.data)["versions"]
3240

3341
return sorted(
34-
filter(lambda v: v['version'].split('.')[0] == str(milestone), versions),
35-
key=lambda v: parse(v['version'])
42+
filter(lambda v: v["version"].split(".")[0] == str(milestone), versions), key=lambda v: parse(v["version"])
3643
)[-1]
3744

45+
3846
def chromedriver(selected_version):
3947
content = ""
4048

41-
selected_version = get_chrome_milestone()
42-
4349
drivers = selected_version["downloads"]["chromedriver"]
4450

4551
linux = [d["url"] for d in drivers if d["platform"] == "linux64"][0]
4652
sha = calculate_hash(linux)
4753

48-
content = content + """
49-
http_archive(
54+
content = (
55+
content
56+
+ """ http_archive(
5057
name = "linux_chromedriver",
5158
url = "%s",
5259
sha256 = "%s",
5360
strip_prefix = "chromedriver-linux64",
5461
build_file_content = "exports_files([\\"chromedriver\\"])",
5562
)
56-
""" % (linux, sha)
63+
"""
64+
% (linux, sha)
65+
)
5766

5867
mac = [d["url"] for d in drivers if d["platform"] == "mac-x64"][0]
5968
sha = calculate_hash(mac)
60-
content = content + """
69+
content = (
70+
content
71+
+ """
6172
http_archive(
6273
name = "mac_chromedriver",
6374
url = "%s",
6475
sha256 = "%s",
6576
strip_prefix = "chromedriver-mac-x64",
6677
build_file_content = "exports_files([\\"chromedriver\\"])",
6778
)
68-
""" % (mac, sha)
79+
"""
80+
% (mac, sha)
81+
)
6982

7083
return content
7184

85+
7286
def chrome(selected_version):
7387
chrome_downloads = selected_version["downloads"]["chrome"]
7488

@@ -93,13 +107,15 @@ def chrome(selected_version):
93107
\"\"\",
94108
)
95109
96-
""" % (linux, sha)
110+
""" % (
111+
linux,
112+
sha,
113+
)
97114

98115
mac = [d["url"] for d in chrome_downloads if d["platform"] == "mac-x64"][0]
99116
sha = calculate_hash(mac)
100117

101-
content += """
102-
http_archive(
118+
content += """ http_archive(
103119
name = "mac_chrome",
104120
url = "%s",
105121
sha256 = "%s",
@@ -111,12 +127,16 @@ def chrome(selected_version):
111127
build_file_content = "exports_files([\\"Chrome.app\\"])",
112128
)
113129
114-
""" % (mac, sha)
130+
""" % (
131+
mac,
132+
sha,
133+
)
115134

116135
return content
117136

137+
118138
def edge():
119-
r = http.request('GET', 'https://edgeupdates.microsoft.com/api/products')
139+
r = http.request("GET", "https://edgeupdates.microsoft.com/api/products")
120140
all_data = json.loads(r.data)
121141

122142
edge = None
@@ -145,71 +165,91 @@ def edge():
145165
},
146166
build_file_content = "exports_files([\\"Edge.app\\"])",
147167
)
148-
""" % (edge, hash.lower(), version)
168+
""" % (
169+
edge,
170+
hash.lower(),
171+
version,
172+
)
149173

150174
return ""
151175

176+
152177
def edgedriver():
153-
r = http.request('GET', 'https://msedgedriver.azureedge.net/LATEST_STABLE')
154-
v = r.data.decode('utf-16').strip()
178+
r = http.request("GET", "https://msedgedriver.azureedge.net/LATEST_STABLE")
179+
v = r.data.decode("utf-16").strip()
155180

156181
content = ""
157182

158183
linux = "https://msedgedriver.azureedge.net/%s/edgedriver_linux64.zip" % v
159184
sha = calculate_hash(linux)
160-
content = content + """
185+
content = (
186+
content
187+
+ """
161188
http_archive(
162189
name = "linux_edgedriver",
163190
url = "%s",
164191
sha256 = "%s",
165192
build_file_content = "exports_files([\\"msedgedriver\\"])",
166193
)
167-
""" % (linux, sha)
194+
"""
195+
% (linux, sha)
196+
)
168197

169198
mac = "https://msedgedriver.azureedge.net/%s/edgedriver_mac64.zip" % v
170199
sha = calculate_hash(mac)
171-
content = content + """
200+
content = (
201+
content
202+
+ """
172203
http_archive(
173204
name = "mac_edgedriver",
174205
url = "%s",
175206
sha256 = "%s",
176207
build_file_content = "exports_files([\\"msedgedriver\\"])",
177208
)
178-
""" % (mac, sha)
209+
"""
210+
% (mac, sha)
211+
)
179212
return content
180213

214+
181215
def geckodriver():
182216
content = ""
183217

184-
r = http.request('GET', 'https://api.github.com/repos/mozilla/geckodriver/releases/latest')
185-
for a in json.loads(r.data)['assets']:
186-
if a['name'].endswith('-linux64.tar.gz'):
187-
url = a['browser_download_url']
218+
r = http.request("GET", "https://api.github.com/repos/mozilla/geckodriver/releases/latest")
219+
for a in json.loads(r.data)["assets"]:
220+
if a["name"].endswith("-linux64.tar.gz"):
221+
url = a["browser_download_url"]
188222
sha = calculate_hash(url)
189-
content = content + \
190-
"""
191-
http_archive(
223+
content = (
224+
content
225+
+ """ http_archive(
192226
name = "linux_geckodriver",
193227
url = "%s",
194228
sha256 = "%s",
195229
build_file_content = "exports_files([\\"geckodriver\\"])",
196230
)
197-
""" % (url, sha)
231+
"""
232+
% (url, sha)
233+
)
198234

199-
if a['name'].endswith('-macos.tar.gz'):
200-
url = a['browser_download_url']
235+
if a["name"].endswith("-macos.tar.gz"):
236+
url = a["browser_download_url"]
201237
sha = calculate_hash(url)
202-
content = content + \
203-
"""
238+
content = (
239+
content
240+
+ """
204241
http_archive(
205242
name = "mac_geckodriver",
206243
url = "%s",
207244
sha256 = "%s",
208245
build_file_content = "exports_files([\\"geckodriver\\"])",
209246
)
210-
""" % (url, sha)
247+
"""
248+
% (url, sha)
249+
)
211250
return content
212251

252+
213253
def firefox():
214254
firefox_versions = json.loads(firefox_version_data())
215255

@@ -247,8 +287,9 @@ def firefox_mac(version):
247287
def print_firefox(version, workspace_name, sha_linux, sha_mac):
248288
content = ""
249289

250-
content = content + f"""
251-
http_archive(
290+
content = (
291+
content
292+
+ f""" http_archive(
252293
name = "linux_{workspace_name}firefox",
253294
url = "{firefox_linux(version)}",
254295
sha256 = "{sha_linux}",
@@ -266,22 +307,25 @@ def print_firefox(version, workspace_name, sha_linux, sha_mac):
266307
)
267308
268309
"""
310+
)
269311

270-
content = content + f"""
271-
dmg_archive(
312+
content = (
313+
content
314+
+ f""" dmg_archive(
272315
name = "mac_{workspace_name}firefox",
273316
url = "{firefox_mac(version)}",
274317
sha256 = "{sha_mac}",
275318
build_file_content = "exports_files([\\"Firefox.app\\"])",
276319
)
277320
278321
"""
322+
)
279323

280324
return content
281325

282-
if __name__ == '__main__':
283-
content = """
284-
# This file has been generated using `bazel run scripts:pinned_browsers`
326+
327+
if __name__ == "__main__":
328+
content = """# This file has been generated using `bazel run scripts:pinned_browsers`
285329
286330
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
287331
load("//common/private:dmg_archive.bzl", "dmg_archive")
@@ -290,6 +334,7 @@ def print_firefox(version, workspace_name, sha_linux, sha_mac):
290334
291335
def pin_browsers():
292336
local_drivers()
337+
293338
"""
294339
content = content + firefox()
295340
content = content + geckodriver()
@@ -300,7 +345,7 @@ def pin_browsers():
300345
content = content + chromedriver(chrome_milestone)
301346

302347
current_script_dir = Path(os.path.realpath(__file__)).parent
303-
target_file_path = current_script_dir.parent / 'common/repositories.bzl'
348+
target_file_path = current_script_dir.parent / "common/repositories.bzl"
304349

305-
with open(target_file_path, 'w') as file:
350+
with open(target_file_path, "w") as file:
306351
file.write(content)

0 commit comments

Comments
 (0)