Skip to content

Commit 6ffdcf9

Browse files
committed
feat: set extra index for uv and pdm
1 parent 075bc52 commit 6ffdcf9

1 file changed

Lines changed: 70 additions & 17 deletions

File tree

pip_conf.py

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,23 +354,47 @@ def config_by_cmd(url, is_windows=False):
354354
_config_by_cmd(url, is_windows=is_windows)
355355

356356

357+
class ExtraIndex:
358+
caching = {} # type: dict[str, Optional[tuple[str, str]]]
359+
360+
def __init__(self, host):
361+
self._host = host
362+
363+
def get(self):
364+
# type: () -> Optional[tuple[str, str]]
365+
host = self._host
366+
if host in self.caching:
367+
return self.caching[host]
368+
value = self.caching[host] = self.get_extra_index(host)
369+
return value
370+
371+
@staticmethod
372+
def get_extra_index(host):
373+
# type: (str) -> Optional[tuple[str, str]]
374+
if host not in _hw_inner_source:
375+
return None
376+
extra_host = host.replace("mirrors", "socapi").replace("tools", "cloudbu")
377+
try:
378+
socket.gethostbyname(ensure_domain_name(extra_host))
379+
except socket.gaierror:
380+
print("Ignore {} as it's not pingable".format(extra_host))
381+
return None
382+
extra_index = _hw_inner_source.replace(host, extra_host)
383+
extra_index_url = INDEX_URL.replace("https", "http").format(extra_index)
384+
return extra_host, extra_index_url
385+
386+
357387
def _config_by_cmd(url, sudo=False, is_windows=False):
358388
# type: (str, bool, bool) -> int
359389
cmd = CONF_PIP + url
360390
if not url.startswith("https"):
361391
print("cmd = {}".format(repr(cmd)))
362392
host = parse_host(url)
363-
if host in _hw_inner_source:
364-
extra_host = host.replace("mirrors", "socapi").replace("tools", "cloudbu")
365-
try:
366-
socket.gethostbyname(ensure_domain_name(extra_host))
367-
except socket.gaierror:
368-
print("Ignore {} as it's not pingable".format(extra_host))
369-
else:
370-
extra_index = _hw_inner_source.replace(host, extra_host)
371-
extra_index_url = INDEX_URL.replace("https", "http").format(extra_index)
372-
cmd += " && pip config set global.extra-index-url " + extra_index_url
373-
host = '"{host} {extra_host}"'.format(host=host, extra_host=extra_host)
393+
extra_info = ExtraIndex(host).get()
394+
if extra_info is not None:
395+
extra_host, extra_index_url = extra_info
396+
cmd += " && pip config set global.extra-index-url " + extra_index_url
397+
host = '"{host} {extra_host}"'.format(host=host, extra_host=extra_host)
374398
cmd += " && pip config set global.trusted-host " + host
375399
if sudo:
376400
cmd = " && ".join("sudo " + i.strip() for i in cmd.split("&&"))
@@ -477,8 +501,22 @@ class PdmMirror:
477501
def set(url):
478502
# type: (str) -> int
479503
cmd = "pdm config pypi.url " + url
480-
if url.startswith("http:"):
504+
host = parse_host(url)
505+
trusted_hosts = [] # type: list[str]
506+
if url.startswith("https:"):
481507
cmd = "pdm config pypi.verify_ssl false && " + cmd
508+
else:
509+
trusted_hosts.append(host)
510+
extra_info = ExtraIndex(host).get()
511+
if extra_info is not None:
512+
extra_host, extra_index_url = extra_info
513+
cmd += " && pdm config pypi.extra.urls " + extra_index_url
514+
if extra_index_url.startswith("http:"):
515+
trusted_hosts.append(extra_host)
516+
if trusted_hosts:
517+
cmd += ' && pdm config pypi.trusted_hosts "{}"'.format(
518+
",".join(trusted_hosts)
519+
)
482520
return run_and_echo(cmd, dry="--dry" in sys.argv)
483521

484522

@@ -519,13 +557,28 @@ def check_installed(self):
519557

520558

521559
class UvMirror(Mirror):
522-
def build_content(self, url=None):
523-
# type: (Optional[str]) -> str
560+
@staticmethod
561+
def allow_insecure(url):
562+
# type: (str) -> str
563+
if url.startswith("https"):
564+
return ""
565+
return '\nallow-insecure-host=["{}"]'.format(parse_host(url))
566+
567+
def build_content(self, url=None, extra_index=None):
568+
# type: (Optional[str], Optional[str]) -> str
524569
if url is None:
525570
url = self.url
526-
text = '[[index]]\nurl = "{}"\ndefault = true'.format(url)
527-
if not url.startswith("https"):
528-
text += '\nallow-insecure-host=["{}"]'.format(parse_host(url))
571+
text = '[[index]]\nurl = "{}"\ndefault = true'.format(
572+
url
573+
) + self.allow_insecure(url)
574+
if extra_index is None:
575+
extra_info = ExtraIndex(parse_host(url)).get()
576+
if extra_info is not None:
577+
_, extra_index = extra_info
578+
if extra_index:
579+
text += '\n[[index]]\nurl = "{}"'.format(extra_index) + self.allow_insecure(
580+
extra_index
581+
)
529582
return text
530583

531584
def set(self):

0 commit comments

Comments
 (0)