-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.py
28 lines (21 loc) · 907 Bytes
/
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
import re
import socket
def extract_version_triple(version_str: str) -> str:
match = re.search(r"\d+\.\d+\.\d+", version_str)
if not match:
raise ValueError("Unable to parse version number")
return match.group()
def is_minimum_version(system_version: str, minimum_version: str) -> bool:
system_version_as_tuple = tuple(map(int, system_version.split(".")))
minimum_version_as_tuple = tuple(map(int, minimum_version.split(".")))
return system_version_as_tuple >= minimum_version_as_tuple
def is_network_available(host: str = "8.8.8.8", port: int = 53, timeout: float = 3.0) -> bool:
"""
Check if internet is available by trying to establish a socket connection.
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except OSError:
return False