Skip to content

Commit 0b92e26

Browse files
committed
micropython/mip/mip/__init__.py: 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@branch". Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 8380c7b commit 0b92e26

1 file changed

Lines changed: 20 additions & 24 deletions

File tree

micropython/mip/mip/__init__.py

Lines changed: 20 additions & 24 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+
# URL components must appear in the same order to avoid having to embed
14+
# component names in the format string themselves. Since all URLs are
15+
# accessed via HTTPS, the URL scheme has to be omitted here.
16+
_PROVIDERS = {
17+
# https://codeberg.org/{org}/{repo}/raw/branch/{branch}/{path}
18+
"codeberg:": "codeberg.org/{}/{}/raw/branch/{}/{}",
19+
# https://raw.githubusercontent.com/{org}/{repo}/{branch}/{path}
20+
"github:": "raw.githubusercontent.com/{}/{}/{}/{}",
21+
# https://gitlab.com/{org}/{repo}/-/raw/{branch}/{path}
22+
"gitlab:": "gitlab.com/{}/{}/-/raw/{}/{}",
23+
}
24+
25+
allowed_mip_url_prefixes = tuple(["http://", "https://"] + list(_PROVIDERS.keys()))
1426

1527

1628
# This implements os.makedirs(os.dirname(path))
@@ -64,29 +76,13 @@ def _check_exists(path, short_hash):
6476
def _rewrite_url(url, branch=None):
6577
if not branch:
6678
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:])
79+
for provider, url_format in _PROVIDERS.items():
80+
if not url.startswith(provider):
81+
continue
82+
components = url[len(provider) :].split("/")
83+
# Add https:// prefix to final URL.
84+
return allowed_mip_url_prefixes[1] + url_format.format(
85+
components[0], components[1], branch, "/".join(components[2:])
9086
)
9187
return url
9288

0 commit comments

Comments
 (0)