Skip to content

Commit 07d570b

Browse files
committed
chore: Add script for testing urls
1 parent 63681df commit 07d570b

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

scripts/test_urls.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from datetime import datetime
2+
import json
3+
from pathlib import Path
4+
import sys
5+
from typing import Any
6+
7+
import requests
8+
9+
color_green = '\033[92m'
10+
color_red = '\033[91m'
11+
color_reset = '\033[0m'
12+
13+
class JSONWithCommentsDecoder(json.JSONDecoder):
14+
def __init__(self, **kw):
15+
super().__init__(**kw)
16+
17+
def decode(self, s: str) -> Any:
18+
s = '\n'.join(l for l in s.split('\n') if not l.lstrip(' ').startswith('//'))
19+
return super().decode(s)
20+
21+
if __name__ == '__main__':
22+
check_success = True
23+
with open('bin/resources.json', 'r') as f:
24+
raw_resources = json.load(f, cls=JSONWithCommentsDecoder)
25+
26+
# Order the resources by date
27+
resources = sorted(raw_resources, key=lambda r: datetime.strptime(r["modified_on"], "%Y-%m-%d"))
28+
29+
if resources != raw_resources:
30+
print("Resources were not ordered by date, dumping them back to the file...")
31+
# Write the resources back to the file, to keep the order
32+
with open('bin/resources.json', 'w') as f:
33+
json.dump(resources, f, indent=4)
34+
35+
for resource in resources:
36+
if not resource['resource_urls']["official"]:
37+
continue
38+
39+
print(f"--------------- {resource['version']} ---------------")
40+
print("Official: ", end="", flush=True)
41+
42+
response = requests.head(resource['resource_urls']["official"], allow_redirects=True)
43+
content_type = response.headers.get('Content-Type', "")
44+
45+
if resource['resource_urls']["mirrored"] is not None:
46+
binary_path = Path(resource['resource_urls']["mirrored"]).resolve()
47+
else:
48+
binary_path = None
49+
50+
remote_online = True
51+
52+
if response.status_code != 200:
53+
print(f"{color_red}KO{color_reset} (Jython version {resource['version']} not available online)")
54+
remote_online = False
55+
check_success = False
56+
57+
if content_type not in ['application/java-archive', 'application/octet-stream']:
58+
print(f"{color_red}KO{color_reset} (Unknown content type {content_type} for Jython version {resource['version']})")
59+
remote_online = False
60+
check_success = False
61+
62+
if remote_online:
63+
print(f"{color_green}OK{color_reset}", flush=True)
64+
65+
if binary_path is not None:
66+
print("Mirrored: ", end="", flush=True)
67+
if binary_path.exists():
68+
print(f"{color_green}OK{color_reset}", flush=True)
69+
check_success = True
70+
71+
elif remote_online:
72+
try:
73+
with requests.get(resource['resource_urls']["official"], allow_redirects=True, stream=True) as r:
74+
r.raise_for_status()
75+
76+
file_extension = resource['resource_urls']["official"].split('.')[-1]
77+
with open(resource['resource_urls']["mirrored"], 'wb') as f:
78+
for chunk in r.iter_content(chunk_size=8192):
79+
f.write(chunk)
80+
except Exception as e:
81+
print(f"{color_red}KO{color_reset} (cannot download due to {e})", flush=True)
82+
check_success = False
83+
else:
84+
print(f"{color_green}OK{color_reset} (version {resource['version']} downloaded)")
85+
check_success = True
86+
else:
87+
print(f"{color_red}KO{color_reset} (cannot download due to resource not available online)", flush=True)
88+
check_success = False
89+
print("")
90+
91+
sys.exit(0 if check_success else 1)

0 commit comments

Comments
 (0)