-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
79 lines (66 loc) · 2.22 KB
/
utils.py
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
import signal
from typing import Optional
from genisolist import process_ini, gen_from_sections
import requests
import logging
import os
import json
import functools
from pathlib import Path
logger = logging.getLogger(__name__)
CONFIG_FOLDER = Path(os.path.dirname(__file__)) / "config"
class AlarmTimeoutException(Exception):
pass
class BadRequestException(Exception):
pass
def alarm_handler(signum, frame):
raise AlarmTimeoutException
# return None when exception caught
def get_resp_with_timeout(
url: str, timeout: int = 20, logger: logging.Logger = None
) -> Optional[requests.Response]:
if logger is None:
logger = logging.getLogger()
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(timeout)
try:
resp = requests.get(url)
if resp.status_code != 200:
raise BadRequestException
return resp
except AlarmTimeoutException:
logger.error(f"requesting {url} timed out")
except BadRequestException:
logger.error(f"failed to retrieve data from {url}")
except Exception as e:
logger.error('unknown exception caught: "{}".'.format(str(e)))
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
return None
# Currently within one run, we expect to download cname.json only once.
@functools.cache
def get_mirrorz_cname():
cname_path = CONFIG_FOLDER / "cname.json"
# download cname
try:
req = requests.get("https://mirrorz.org/static/json/cname.json", timeout=10)
req.raise_for_status()
cname = req.json()
with open(cname_path, "w") as f:
json.dump(cname, f)
except:
# website req timeout, use local cname
try:
logger.warn("Failed to fetch mirrorz cname.json, try using local one")
with open(cname_path) as f:
cname = json.load(f)
except:
logger.warn("Failed to load mirrorz cname.json")
cname = {}
return cname
# Used by multiple generators, to directly use reference impl without modification
def get_isolist():
genisolist_inifile = CONFIG_FOLDER / "genisolist.ini"
sections = process_ini(genisolist_inifile)
return gen_from_sections(sections)