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
6476def _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
145139def _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