Skip to content

Commit c43cf6f

Browse files
Downloading Binaries (UBC-Thunderbots#3575)
* downloading * new * full * trimmed * format * tweaks * le nit * f * only specific os * 5 * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 79b4a9f commit c43cf6f

2 files changed

Lines changed: 91 additions & 4 deletions

File tree

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,105 @@
1-
# TODO: #3559
1+
import requests
2+
from pathlib import Path
3+
from software.logger.logger import create_logger
4+
import zipfile
5+
import tarfile
6+
import shutil
7+
from software.thunderscope.constants import RuntimeManagerConstants
8+
import platform
9+
10+
logger = create_logger(__name__)
11+
12+
213
class RuntimeInstaller:
314
"""Delegate class for handling runtime installation and remote interfacing"""
415

516
def __init__(self):
6-
pass
17+
self.download_urls = []
718

819
def fetch_remote_runtimes(self) -> list[str]:
920
"""Requests a list of available runtimes from the remote. This is an expensive operation
1021
and should only be called when necessary.
1122
:return: A unique list of names for available runtimes
1223
"""
13-
return []
24+
url = RuntimeManagerConstants.INSTALL_URL
25+
headers = {"Accept": "application/vnd.github+json"}
26+
27+
response = requests.get(url, headers=headers)
28+
logger.warning(response)
29+
30+
releases = response.json()
31+
32+
PREFIX = RuntimeManagerConstants.DOWNLOAD_PREFIX
33+
34+
# I'm going to assume you are trying to reload the assets so reset the download_urls
35+
if len(self.download_urls) != 0:
36+
self.download_urls = []
37+
download_names = []
38+
os_name = platform.system()
39+
40+
for release in releases:
41+
tag = release.get("tag_name", "")
42+
43+
# ✅ Only keep tags that include the OS name
44+
if os_name not in tag:
45+
continue
46+
for asset in release.get("assets", []):
47+
url = asset["browser_download_url"]
48+
if "fullsystem" not in asset_name:
49+
continue
50+
self.download_urls.append(url)
51+
trimmed = url.removeprefix(PREFIX)
52+
download_names.append(trimmed)
53+
54+
return download_names[:5]
1455

1556
def install_runtime(self, version: str) -> None:
1657
"""Installs the runtime of the specified version or throws an error upon failure.
1758
Ensures that the runtime is compatible with the current platform
1859
:param version: Version of the runtime hosted on the remote to install
1960
"""
20-
pass
61+
url = RuntimeManagerConstants.INSTALL_URL
62+
headers = {"Accept": "application/vnd.github+json"}
63+
64+
response = requests.get(url, headers=headers)
65+
logger.warning(response)
66+
67+
releases = response.json()
68+
69+
TARGET_SUFFIX = version
70+
71+
selected_asset = None
72+
73+
for download_url in self.download_urls:
74+
if download_url.endswith(TARGET_SUFFIX):
75+
selected_asset = download_url
76+
77+
if selected_asset:
78+
url = selected_asset
79+
filename = Path(url).name
80+
81+
target_dir = Path(RuntimeManagerConstants.EXTERNAL_RUNTIMES_PATH)
82+
tmp_path = Path("/tmp") / filename
83+
84+
with requests.get(url, stream=True) as r:
85+
r.raise_for_status()
86+
with open(tmp_path, "wb") as f:
87+
for chunk in r.iter_content(chunk_size=8192):
88+
if chunk:
89+
f.write(chunk)
90+
91+
if filename.endswith((".tar.gz", ".tgz")):
92+
with tarfile.open(tmp_path, "r:*") as tar:
93+
tar.extractall(path=target_dir)
94+
95+
elif filename.endswith(".zip"):
96+
with zipfile.ZipFile(tmp_path, "r") as zipf:
97+
zipf.extractall(path=target_dir)
98+
99+
else:
100+
dest = target_dir / filename
101+
shutil.copy2(tmp_path, dest)
102+
dest.chmod(0o755) # make executable (common for runtimes)
103+
104+
if not selected_asset:
105+
logger.warning("Can't find binary")

src/software/thunderscope/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,5 @@ class RuntimeManagerConstants:
397397
DEFAULT_BINARY_NAME = "localhost"
398398
EXTERNAL_RUNTIMES_PATH = "/opt/tbotspython/external_runtimes"
399399
RUNTIME_CONFIG_PATH = f"{EXTERNAL_RUNTIMES_PATH}/runtime_config.toml"
400+
INSTALL_URL = "https://api.github.com/repos/UBC-Thunderbots/Software/releases"
401+
DOWNLOAD_PREFIX = "https://github.com/UBC-Thunderbots/Software/releases/download/"

0 commit comments

Comments
 (0)