Skip to content

Commit 922e5bc

Browse files
committed
Updated MSYS2 API endpoints to expose more types of installers and archives for download
1 parent c8f62ce commit 922e5bc

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

src/murfey/server/api/bootstrap.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def parse_cygwin_request(request_path: str):
256256
# Variables used by the MSYS2 functions below
257257
msys2_url = "https://repo.msys2.org"
258258
msys2_setup_file = "msys2-x86_64-latest.exe"
259+
msys2_file_ext = (".exe", ".sig", ".tar.xz", "tar.zst")
259260
valid_envs = (
260261
# Tuple of systems and supported libraries/compilers/architectures within
261262
(
@@ -289,15 +290,19 @@ def parse_cygwin_request(request_path: str):
289290
)
290291

291292

292-
@msys2.get("/setup-x86_64.exe", response_class=Response)
293-
def get_msys2_setup():
293+
@msys2.get("/distrib/{setup_file}", response_class=Response)
294+
def get_msys2_setup(setup_file: str):
294295
"""
295296
Obtain and pass through an MSYS2 installer from an official source.
296297
This is used during client bootstrapping, and can download and install the
297298
MSYS2 distribution that then remains on the client machines.
298299
"""
299300

300-
installer = requests.get(f"{msys2_url}/distrib/{msys2_setup_file}")
301+
# Allow only '.exe', 'tar.xz', 'tar.zst', or '.sig' files
302+
if not any(setup_file.endswith(suffix) for suffix in (msys2_file_ext)):
303+
raise ValueError(f"{setup_file!r} is not a valid executable")
304+
305+
installer = requests.get(f"{msys2_url}/distrib/{setup_file}")
301306
return Response(
302307
content=installer.content,
303308
media_type=installer.headers.get("Content-Type"),
@@ -314,24 +319,6 @@ def get_msys2_main_index(
314319
from the main MSYS2 repository.
315320
"""
316321

317-
def get_msys2_setup_html():
318-
"""
319-
Returns the HTML line for the latest MSYS2 installer for Windows from an official
320-
source.
321-
"""
322-
url = f"{msys2_url}/distrib"
323-
index = requests.get(url)
324-
content: bytes = index.content
325-
content_text: str = content.decode("latin1")
326-
327-
for line in content_text.splitlines():
328-
if line.startswith("<a href="):
329-
if f'"{msys2_setup_file}"' in line:
330-
return line
331-
else:
332-
pass
333-
return None
334-
335322
def _rewrite_url(match):
336323
"""
337324
Use regular expression matching to rewrite the package URLs and point them
@@ -358,35 +345,15 @@ def _rewrite_url(match):
358345
content_text_list = []
359346
for line in content_text.splitlines():
360347
if line.startswith("<a href"):
361-
# Mirror only lines related to MSYS2 environments
362-
if any(env[0] in line for env in valid_envs):
348+
# Mirror only lines related to MSYS2 environments or the distribution folder
349+
if any(env[0] in line for env in valid_envs) or "distrib" in line:
363350
line_new = re.sub(
364351
'^<a href="([^">]*)">([^<]*)</a>', # Regex search criteria
365352
_rewrite_url, # Function to apply search criteria to
366353
line,
367354
)
368355
content_text_list.append(line_new)
369356

370-
# Replace the "distrib/" hyperlink with one to the setup file
371-
elif "distrib" in line:
372-
# Set up URL to be requested on the Murfey server
373-
mirror_file_name = "setup-x86_64.exe"
374-
setup_url = f"{base_path}/{mirror_file_name}"
375-
376-
# Get request from the "distrib" page and rewrite it
377-
setup_html = get_msys2_setup_html()
378-
if setup_html is None:
379-
# Skip showing the setup file link if it fails to find it
380-
continue
381-
382-
line_new = " ".join( # Adjust spaces to align columns
383-
re.sub(
384-
'^<a href="([^">]*)">([^"<]*)</a>',
385-
f'<a href="{setup_url}">{mirror_file_name}</a>',
386-
setup_html,
387-
).split(" ", 1)
388-
)
389-
content_text_list.append(line_new)
390357
# Other URLs don't need to be mirrored
391358
else:
392359
pass
@@ -430,8 +397,8 @@ def _rewrite_url(match):
430397
path = request.url.path.strip("/")
431398
base_path = f"{base_url}/{path}"
432399

433-
# Validate provided system
434-
if any(system in env[0] for env in valid_envs) is False:
400+
# Validate provided system; use this endpoint to display 'distrib' folder too
401+
if not (any(system in env[0] for env in valid_envs) or system == "distrib"):
435402
raise ValueError(f"{system!r} is not a valid msys2 environment")
436403

437404
# Construct URL to main MSYS repo and get response
@@ -444,6 +411,11 @@ def _rewrite_url(match):
444411
content_text_list = []
445412
for line in content_text.splitlines():
446413
if line.startswith("<a href="):
414+
# Skip non-executable files when querying 'distrib' repo
415+
if system == "distrib":
416+
if not any(ext in line for ext in msys2_file_ext):
417+
continue
418+
447419
# Rewrite URL to point explicitly to current server
448420
line_new = re.sub(
449421
'^<a href="([^">]*)">([^<]*)</a>', # Regex search criteria

0 commit comments

Comments
 (0)