|
3 | 3 | import io |
4 | 4 | import os |
5 | 5 | import pathlib |
6 | | -import requests |
| 6 | +import urllib.parse |
7 | 7 | import yaml |
8 | 8 |
|
9 | 9 | import magic |
@@ -131,28 +131,40 @@ def _load_cmdline_cache(self, cache_config_path: pathlib.Path) -> Dict: |
131 | 131 | return mirror_cfg |
132 | 132 |
|
133 | 133 | def _check_mirrors(self): |
134 | | - """Validate the mirror config entries.""" |
| 134 | + """Validate that each mirror url is well-formed. |
| 135 | +
|
| 136 | + Only the format of the url is checked: no attempt is made to connect to |
| 137 | + remote mirrors, because a valid-but-unreachable url would otherwise |
| 138 | + block until the network request times out. |
| 139 | + """ |
135 | 140 |
|
136 | 141 | for name, mirror in self.mirrors.items(): |
137 | 142 | url = mirror["url"] |
| 143 | + |
138 | 144 | if url.startswith("file://"): |
139 | | - # verify that the root path exists |
140 | | - path = pathlib.Path(os.path.expandvars(url)) |
| 145 | + # local mirror: verify that the root path is an existing directory |
| 146 | + path = pathlib.Path(os.path.expandvars(url[len("file://") :])) |
141 | 147 | if not path.is_absolute(): |
142 | 148 | raise MirrorError(f"The mirror path '{path}' for mirror '{name}' is not absolute") |
143 | 149 | if not path.is_dir(): |
144 | 150 | raise MirrorError(f"The mirror path '{path}' for mirror '{name}' is not a directory") |
145 | 151 |
|
146 | 152 | mirror["url"] = path |
| 153 | + continue |
147 | 154 |
|
148 | | - elif url.startswith("https://"): |
149 | | - try: |
150 | | - requests.request(url=url, method="HEAD") |
151 | | - except requests.exceptions.RequestException as err: |
| 155 | + parsed = urllib.parse.urlparse(url) |
| 156 | + |
| 157 | + if not parsed.scheme: |
| 158 | + # a bare path is accepted if absolute (e.g. the legacy command line cache) |
| 159 | + if not pathlib.Path(url).is_absolute(): |
152 | 160 | raise MirrorError( |
153 | | - f"Could not reach the mirror url '{url}'. " |
154 | | - f"Check the url listed in mirrors.yaml in system config. \n{err}" |
| 161 | + f"The mirror url '{url}' for mirror '{name}' is not a valid url or absolute path" |
155 | 162 | ) |
| 163 | + continue |
| 164 | + |
| 165 | + # a remote mirror: require a well-formed url with both a scheme and a host |
| 166 | + if not parsed.netloc: |
| 167 | + raise MirrorError(f"The mirror url '{url}' for mirror '{name}' is not a valid url") |
156 | 168 |
|
157 | 169 | @property |
158 | 170 | def keys(self): |
|
0 commit comments