|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from ast import literal_eval |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | +import httpx |
| 8 | +from utils import ROOT |
| 9 | + |
| 10 | +URL = 'https://raw.githubusercontent.com/ofek/pyapp/master/build.rs' |
| 11 | +OUTPUT_FILE = ROOT / 'src' / 'hatch' / 'python' / 'distributions.py' |
| 12 | +ARCHES = {('linux', 'x86'): 'i686', ('windows', 'x86_64'): 'amd64', ('windows', 'x86'): 'i386'} |
| 13 | + |
| 14 | +# system, architecture, ABI, variant |
| 15 | +MAX_IDENTIFIER_COMPONENTS = 4 |
| 16 | + |
| 17 | + |
| 18 | +def parse_distributions(contents: str, constant: str): |
| 19 | + match = re.search(f'^const {constant}.+?^];$', contents, flags=re.DOTALL | re.MULTILINE) |
| 20 | + if not match: |
| 21 | + message = f'Could not find {constant} in {URL}' |
| 22 | + raise ValueError(message) |
| 23 | + |
| 24 | + block = match.group(0).replace('",\n', '",') |
| 25 | + for line in block.splitlines()[1:-1]: |
| 26 | + line = line.strip() |
| 27 | + if not line or line.startswith('//'): |
| 28 | + continue |
| 29 | + |
| 30 | + identifier, *data, source = literal_eval(line[:-1]) |
| 31 | + os, arch = data[:2] |
| 32 | + if arch == 'powerpc64': |
| 33 | + arch = 'ppc64le' |
| 34 | + |
| 35 | + # Force everything to have a variant to maintain structure |
| 36 | + if len(data) != MAX_IDENTIFIER_COMPONENTS: |
| 37 | + data.append('') |
| 38 | + |
| 39 | + data[1] = ARCHES.get((os, arch), arch) |
| 40 | + yield identifier, tuple(data), source |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + response = httpx.get(URL) |
| 45 | + response.raise_for_status() |
| 46 | + |
| 47 | + contents = response.text |
| 48 | + distributions = defaultdict(list) |
| 49 | + ordering_data = defaultdict(dict) |
| 50 | + |
| 51 | + for i, distribution_type in enumerate(('DEFAULT_CPYTHON_DISTRIBUTIONS', 'DEFAULT_PYPY_DISTRIBUTIONS')): |
| 52 | + for identifier, data, source in parse_distributions(contents, distribution_type): |
| 53 | + ordering_data[i][identifier] = None |
| 54 | + distributions[identifier].append((data, source)) |
| 55 | + |
| 56 | + ordered = [identifier for identifiers in ordering_data.values() for identifier in reversed(identifiers)] |
| 57 | + output = [ |
| 58 | + 'from __future__ import annotations', |
| 59 | + '', |
| 60 | + '# fmt: off', |
| 61 | + 'ORDERED_DISTRIBUTIONS: tuple[str, ...] = (', |
| 62 | + ] |
| 63 | + for identifier in ordered: |
| 64 | + output.append(f' {identifier!r},') |
| 65 | + output.append(')') |
| 66 | + |
| 67 | + output.append('DISTRIBUTIONS: dict[str, dict[tuple[str, ...], str]] = {') |
| 68 | + for identifier, data in distributions.items(): |
| 69 | + output.append(f' {identifier!r}: {{') |
| 70 | + |
| 71 | + for d, source in data: |
| 72 | + output.append(f' {d!r}:') |
| 73 | + output.append(f' {source!r},') |
| 74 | + |
| 75 | + output.append(' },') |
| 76 | + |
| 77 | + output.append('}') |
| 78 | + output.append('') |
| 79 | + output = '\n'.join(output) |
| 80 | + |
| 81 | + with open(OUTPUT_FILE, 'w') as f: |
| 82 | + f.write(output) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + main() |
0 commit comments