|
10 | 10 | _PACKAGE_INDEX = const("https://micropython.org/pi/v2") |
11 | 11 | _CHUNK_SIZE = const(128) |
12 | 12 |
|
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 | +# ruff: noqa: RUF005 - tuple construction with list destructuring is not supported. |
| 26 | +allowed_mip_url_prefixes = tuple(["http://", "https://"] + list(_PROVIDERS.keys())) |
14 | 27 |
|
15 | 28 |
|
16 | 29 | # This implements os.makedirs(os.dirname(path)) |
@@ -64,29 +77,13 @@ def _check_exists(path, short_hash): |
64 | 77 | def _rewrite_url(url, branch=None): |
65 | 78 | if not branch: |
66 | 79 | 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 _PROVIDERS.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], branch, "/".join(components[2:]) |
90 | 87 | ) |
91 | 88 | return url |
92 | 89 |
|
|
0 commit comments