From 713d424fcf67e5ac37125143f1871463aebfb20b Mon Sep 17 00:00:00 2001 From: Alessandro Gatti Date: Wed, 25 Mar 2026 08:17:32 +0100 Subject: [PATCH] micropython/mip/mip: Add an alias for codeberg repos. This commit introduces an alias to access codeberg repos from within the mip embedded package manager. Right now packages hosted on codeberg could only be referenced by their full URL, unlike other packages hosted on either GitHub or GitLab. To make access those packages easier, now they can be referenced as "codeberg:org/repo" when used inside `mip.install`. Signed-off-by: Alessandro Gatti --- micropython/mip/manifest.py | 2 +- micropython/mip/mip/__init__.py | 45 +++++++++++++++------------------ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/micropython/mip/manifest.py b/micropython/mip/manifest.py index a1b340670..f577ea601 100644 --- a/micropython/mip/manifest.py +++ b/micropython/mip/manifest.py @@ -1,4 +1,4 @@ -metadata(version="0.4.2", description="On-device package installer for network-capable boards") +metadata(version="0.5.0", description="On-device package installer for network-capable boards") require("requests") diff --git a/micropython/mip/mip/__init__.py b/micropython/mip/mip/__init__.py index ab48393d6..63ff01bce 100644 --- a/micropython/mip/mip/__init__.py +++ b/micropython/mip/mip/__init__.py @@ -10,7 +10,20 @@ _PACKAGE_INDEX = const("https://micropython.org/pi/v2") _CHUNK_SIZE = const(128) -allowed_mip_url_prefixes = ("http://", "https://", "github:", "gitlab:") +# Since all URLs are accessed via HTTPS, the URL scheme has to be omitted here. +# The first two format parameters are assumed to be the organisation and the +# repository names, in this order. +_HOSTS = { + # https://codeberg.org/api/v1/repos/{org}/{repo}/raw/{path}?ref={branch} + "codeberg:": "codeberg.org/api/v1/repos/{}/{}/raw/{p}?ref={}", + # https://raw.githubusercontent.com/{org}/{repo}/{branch}/{path} + "github:": "raw.githubusercontent.com/{}/{}/{}/{p}", + # https://gitlab.com/{org}/{repo}/-/raw/{branch}/{path} + "gitlab:": "gitlab.com/{}/{}/-/raw/{}/{p}", +} + +# ruff: noqa: RUF005 - tuple construction with list destructuring is not supported. +allowed_mip_url_prefixes = tuple(["http://", "https://"] + list(_HOSTS.keys())) # This implements os.makedirs(os.dirname(path)) @@ -64,29 +77,13 @@ def _check_exists(path, short_hash): def _rewrite_url(url, branch=None): if not branch: branch = "HEAD" - if url.startswith("github:"): - url = url[7:].split("/") - url = ( - "https://raw.githubusercontent.com/" - + url[0] - + "/" - + url[1] - + "/" - + branch - + "/" - + "/".join(url[2:]) - ) - elif url.startswith("gitlab:"): - url = url[7:].split("/") - url = ( - "https://gitlab.com/" - + url[0] - + "/" - + url[1] - + "/-/raw/" - + branch - + "/" - + "/".join(url[2:]) + for provider, url_format in _HOSTS.items(): + if not url.startswith(provider): + continue + components = url[len(provider) :].split("/") + # Add https:// prefix to final URL. + return allowed_mip_url_prefixes[1] + url_format.format( + components[0], components[1], branch, p="/".join(components[2:]) ) return url