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