Skip to content

Commit 8625f7b

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 8625f7b

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
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: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@
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 three format parameters are assumed to be the organisation, the
15+
# repository names, and the branch/tag name, 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={}",
19+
# https://raw.githubusercontent.com/{org}/{repo}/{branch}/{path}
20+
"github:": "raw.githubusercontent.com/{}/{}/{}/{p}",
21+
# https://gitlab.com/{org}/{repo}/-/raw/{branch}/{path}
22+
"gitlab:": "gitlab.com/{}/{}/-/raw/{}/{p}",
23+
}
24+
25+
_ALLOWED_MIP_URL_PREFIXES = const(("http://", "https://", "codeberg:", "github:", "gitlab:"))
1426

1527

1628
# This implements os.makedirs(os.dirname(path))
@@ -62,31 +74,13 @@ def _check_exists(path, short_hash):
6274

6375

6476
def _rewrite_url(url, branch=None):
65-
if not branch:
66-
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:])
77+
for provider, url_format in _HOSTS.items():
78+
if not url.startswith(provider):
79+
continue
80+
components = url[len(provider) :].split("/")
81+
# Add https:// prefix to final URL.
82+
return _ALLOWED_MIP_URL_PREFIXES[1] + url_format.format(
83+
components[0], components[1], branch or "HEAD", p="/".join(components[2:])
9084
)
9185
return url
9286

@@ -130,7 +124,7 @@ def _install_json(package_json_url, index, target, version, mpy):
130124
base_url = package_json_url.rpartition("/")[0]
131125
for target_path, url in package_json.get("urls", ()):
132126
fs_target_path = target + "/" + target_path
133-
is_full_url = any(url.startswith(p) for p in allowed_mip_url_prefixes)
127+
is_full_url = any(url.startswith(p) for p in _ALLOWED_MIP_URL_PREFIXES)
134128
if base_url and not is_full_url:
135129
url = f"{base_url}/{url}" # Relative URLs
136130
if not _download_file(_rewrite_url(url, version), fs_target_path):
@@ -143,7 +137,7 @@ def _install_json(package_json_url, index, target, version, mpy):
143137

144138

145139
def _install_package(package, index, target, version, mpy):
146-
if any(package.startswith(p) for p in allowed_mip_url_prefixes):
140+
if any(package.startswith(p) for p in _ALLOWED_MIP_URL_PREFIXES):
147141
if package.endswith(".py") or package.endswith(".mpy"):
148142
print("Downloading {} to {}".format(package, target))
149143
return _download_file(

0 commit comments

Comments
 (0)