Skip to content

Commit 85fabcd

Browse files
committed
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 <a.gatti@frob.it>
1 parent 8380c7b commit 85fabcd

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

micropython/mip/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
metadata(version="0.4.2", description="On-device package installer for network-capable boards")
1+
metadata(version="0.5.0", description="On-device package installer for network-capable boards")
22

33
require("requests")
44

micropython/mip/mip/__init__.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010
_PACKAGE_INDEX = const("https://micropython.org/pi/v2")
1111
_CHUNK_SIZE = const(128)
1212

13-
allowed_mip_url_prefixes = ("http://", "https://", "github:", "gitlab:")
13+
# Since all URLs are accessed via HTTPS, the URL scheme has to be omitted here.
14+
# The first two format parameters are assumed to be the organisation and the
15+
# repository names, in this order.
16+
_HOSTS = {
17+
# https://codeberg.org/api/v1/repos/{org}/{repo}/raw/{path}?ref={branch}
18+
"codeberg:": "codeberg.org/api/v1/repos/{}/{}/raw/{p}?ref={b}",
19+
# https://raw.githubusercontent.com/{org}/{repo}/{branch}/{path}
20+
"github:": "raw.githubusercontent.com/{}/{}/{b}/{p}",
21+
# https://gitlab.com/{org}/{repo}/-/raw/{branch}/{path}
22+
"gitlab:": "gitlab.com/{}/{}/-/raw/{b}/{p}",
23+
}
24+
25+
# ruff: noqa: RUF005 - tuple construction with list destructuring is not supported.
26+
allowed_mip_url_prefixes = tuple(["http://", "https://"] + list(_HOSTS.keys()))
1427

1528

1629
# This implements os.makedirs(os.dirname(path))
@@ -64,29 +77,13 @@ def _check_exists(path, short_hash):
6477
def _rewrite_url(url, branch=None):
6578
if not branch:
6679
branch = "HEAD"
67-
if url.startswith("github:"):
68-
url = url[7:].split("/")
69-
url = (
70-
"https://raw.githubusercontent.com/"
71-
+ url[0]
72-
+ "/"
73-
+ url[1]
74-
+ "/"
75-
+ branch
76-
+ "/"
77-
+ "/".join(url[2:])
78-
)
79-
elif url.startswith("gitlab:"):
80-
url = url[7:].split("/")
81-
url = (
82-
"https://gitlab.com/"
83-
+ url[0]
84-
+ "/"
85-
+ url[1]
86-
+ "/-/raw/"
87-
+ branch
88-
+ "/"
89-
+ "/".join(url[2:])
80+
for provider, url_format in _HOSTS.items():
81+
if not url.startswith(provider):
82+
continue
83+
components = url[len(provider) :].split("/")
84+
# Add https:// prefix to final URL.
85+
return allowed_mip_url_prefixes[1] + url_format.format(
86+
components[0], components[1], b=branch, p="/".join(components[2:])
9087
)
9188
return url
9289

0 commit comments

Comments
 (0)