ยปCore Development>Code coverage>Lib/packaging/pypi/mirrors.py

Python code coverage for Lib/packaging/pypi/mirrors.py

#countcontent
1n/a"""Utilities related to the mirror infrastructure defined in PEP 381."""
2n/a
3n/afrom string import ascii_lowercase
4n/aimport socket
5n/a
6n/aDEFAULT_MIRROR_URL = "last.pypi.python.org"
7n/a
8n/a
9n/adef get_mirrors(hostname=None):
10n/a """Return the list of mirrors from the last record found on the DNS
11n/a entry::
12n/a
13n/a >>> from packaging.pypi.mirrors import get_mirrors
14n/a >>> get_mirrors()
15n/a ['a.pypi.python.org', 'b.pypi.python.org', 'c.pypi.python.org',
16n/a 'd.pypi.python.org']
17n/a
18n/a """
19n/a if hostname is None:
20n/a hostname = DEFAULT_MIRROR_URL
21n/a
22n/a # return the last mirror registered on PyPI.
23n/a try:
24n/a hostname = socket.gethostbyname_ex(hostname)[0]
25n/a except socket.gaierror:
26n/a return []
27n/a end_letter = hostname.split(".", 1)
28n/a
29n/a # determine the list from the last one.
30n/a return ["%s.%s" % (s, end_letter[1]) for s in string_range(end_letter[0])]
31n/a
32n/a
33n/adef string_range(last):
34n/a """Compute the range of string between "a" and last.
35n/a
36n/a This works for simple "a to z" lists, but also for "a to zz" lists.
37n/a """
38n/a for k in range(len(last)):
39n/a for x in product(ascii_lowercase, repeat=(k + 1)):
40n/a result = ''.join(x)
41n/a yield result
42n/a if result == last:
43n/a return
44n/a
45n/a
46n/adef product(*args, **kwds):
47n/a pools = [tuple(arg) for arg in args] * kwds.get('repeat', 1)
48n/a result = [[]]
49n/a for pool in pools:
50n/a result = [x + [y] for x in result for y in pool]
51n/a for prod in result:
52n/a yield tuple(prod)