|
| 1 | +from configparser import ConfigParser |
| 2 | +from glob import glob |
| 3 | +from os import environ, makedirs |
| 4 | +from os.path import dirname |
| 5 | +from shutil import copy, rmtree |
| 6 | +from subprocess import CalledProcessError, run |
| 7 | +from sys import platform |
| 8 | +from tempfile import TemporaryDirectory |
| 9 | +import venv |
| 10 | +from sys import stdout, stderr |
| 11 | + |
| 12 | + |
| 13 | +# TODO: 32-bit Windows? |
| 14 | + |
| 15 | + |
| 16 | +LINUX = platform == "linux" |
| 17 | +MACOS = platform == "darwin" |
| 18 | + |
| 19 | +PYENV_INSTALL_ARGS = "--skip-existing" if LINUX or MACOS else "--quiet --skip-existing" |
| 20 | + |
| 21 | +WHEELS = {"3.5.4", "3.6.1", "3.7.0", "3.8.0"} |
| 22 | +DEFAULTS = { |
| 23 | + "python": "3.8.0", |
| 24 | + "test": "pip install --requirement requirements.txt && pytest", |
| 25 | + "test-if": 'true', |
| 26 | + "upload": "pip install --upgrade twine && twine upload --skip-existing dist/*", |
| 27 | + "upload-if": 'test "$TRAVIS_TAG"', |
| 28 | + "wheels": " ".join(sorted(WHEELS)), |
| 29 | + "windows": '32 64' |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +def _shell(command: str, *, version: str = "", echo: bool = False) -> None: |
| 34 | + if echo: |
| 35 | + print(f"\033[1m{command}\033[0m") |
| 36 | + run(f'pyenv install {PYENV_INSTALL_ARGS} {version or "3.8.0"}', check=True, shell=True) |
| 37 | + # with open(".exec.sh", "w") as file: |
| 38 | + # file.write(command) |
| 39 | + if LINUX or MACOS: |
| 40 | + run(f"bash -c 'eval \"$(pyenv init -)\" && pyenv shell {version or '3.8.0'} && python -m venv .env{version} && source .env{version}/bin/activate && {command}'", check=True, shell=True) |
| 41 | + else: |
| 42 | + run(f'pyenv global {version or "3.8.0"}', check=True, shell=True) |
| 43 | + run(f'pyenv exec python -m venv .env{version}', check=True, shell=True) |
| 44 | + # makedirs(f'.env{version}/bin', exist_ok=True) |
| 45 | + # try: |
| 46 | + # copy(f"{dirname(venv.__file__)}/scripts/common/activate", f'.env{version}/bin') |
| 47 | + # except OSError: |
| 48 | + # pass |
| 49 | + run(f'bash -c "source .env{version}/Scripts/activate && {command}"', check=True, shell=True) |
| 50 | + stderr.flush() |
| 51 | + stdout.flush() |
| 52 | + |
| 53 | + |
| 54 | +def main() -> None: |
| 55 | + parser = ConfigParser() |
| 56 | + parser.read("setup.cfg") |
| 57 | + config = DEFAULTS.copy() |
| 58 | + if "wheel" in parser: |
| 59 | + config.update(parser["wheel"]) |
| 60 | + if unknown := config.keys() - DEFAULTS.keys(): |
| 61 | + raise RuntimeError( |
| 62 | + f"Unknown config option{'' if len(unknown) == 1 else 's'}: {'/'.join(sorted(unknown))}! Expected {'/'.join(sorted(DEFAULTS))}." |
| 63 | + ) |
| 64 | + wheels = set(config["wheels"].split()) |
| 65 | + if not LINUX and not MACOS: |
| 66 | + windows = set(map(int, config['windows'].split())) |
| 67 | + if 64 in windows: |
| 68 | + for wheel in tuple(wheels): |
| 69 | + wheels.add(f'{wheel}-amd64') |
| 70 | + if 32 not in windows: |
| 71 | + for wheel in tuple(wheels): |
| 72 | + if not wheel.endswith('-amd64'): |
| 73 | + wheels.remove(wheel) |
| 74 | + # if unknown := WHEELS < wheels: |
| 75 | + # raise RuntimeError( |
| 76 | + # f"Unknown wheel version{'' if len(unknown) == 1 else 's'}: {'/'.join(sorted(unknown))}! Expected {'/'.join(sorted(WHEELS))}." |
| 77 | + # ) |
| 78 | + # config["wheels"] = [f"{wheel}.0" for wheel in sorted(wheels)] |
| 79 | + config["wheels"] = sorted(wheels) |
| 80 | + rmtree("dist", ignore_errors=True) |
| 81 | + _shell("python -m pip install --upgrade pip") |
| 82 | + _shell("pip install --upgrade setuptools") |
| 83 | + _shell("python setup.py sdist --dist-dir=dist") |
| 84 | + with TemporaryDirectory() as dist: |
| 85 | + for version in config["wheels"]: |
| 86 | + _shell("python -m pip install --upgrade pip", version=version) |
| 87 | + if config["test"]: |
| 88 | + _shell("python setup.py develop", version=version) |
| 89 | + banner = f"Test: Python {version}" |
| 90 | + line = "=" * len(banner) |
| 91 | + print(f"\n{line}\n{banner}\n{line}\n") |
| 92 | + try: |
| 93 | + _shell(config["test-if"], version=version, echo=True) |
| 94 | + except CalledProcessError: |
| 95 | + pass |
| 96 | + else: |
| 97 | + _shell(config["test"], version=version, echo=True) |
| 98 | + finally: |
| 99 | + print(f"\n{line}\n") |
| 100 | + _shell("python setup.py develop --uninstall", version=version) |
| 101 | + _shell("pip install --upgrade setuptools wheel", version=version) |
| 102 | + if MACOS: |
| 103 | + # We lie for macOS, and say our 10.9 64-bit build is a 10.6 |
| 104 | + # 32/64-bit one. This is because pip is conservative in what |
| 105 | + # wheels it will use, but Python installations are EXTREMELY |
| 106 | + # liberal in their macOS support. A typical user may be running |
| 107 | + # a 32/64 Python built for 10.6. In reality, we shouldn't worry |
| 108 | + # about supporting 32-bit Snow Leopard. |
| 109 | + _shell( |
| 110 | + f"python setup.py bdist_wheel --dist-dir={dist} --plat-name=macosx_10_6_intel", version=version |
| 111 | + ) |
| 112 | + elif LINUX: |
| 113 | + _shell(f"python setup.py bdist_wheel --dist-dir={dist}", version=version) |
| 114 | + else: |
| 115 | + _shell(f"python setup.py bdist_wheel --dist-dir=dist", version=version) |
| 116 | + wheels = glob(f"{dist}/*") |
| 117 | + if LINUX: |
| 118 | + _shell("pip install --upgrade auditwheel") |
| 119 | + for wheel in wheels: |
| 120 | + _shell(f"auditwheel repair --wheel-dir=dist {wheel}") |
| 121 | + elif MACOS: |
| 122 | + _shell("pip install --upgrade delocate") |
| 123 | + for wheel in wheels: |
| 124 | + _shell(f"delocate-wheel --wheel-dir=dist --require-archs=intel {wheel}") |
| 125 | + # else: |
| 126 | + # for wheel in wheels: |
| 127 | + # copy(wheel, "dist") |
| 128 | + if config["upload"]: |
| 129 | + banner = "Upload" |
| 130 | + line = "=" * len(banner) |
| 131 | + print(f"\n{line}\n{banner}\n{line}\n") |
| 132 | + try: |
| 133 | + _shell(config["upload-if"], version=config["python"], echo=True) |
| 134 | + except CalledProcessError: |
| 135 | + pass |
| 136 | + else: |
| 137 | + _shell(config["upload"], version=config["python"], echo=True) |
| 138 | + finally: |
| 139 | + print(f"\n{line}\n") |
| 140 | + # _shell("python setup.py develop --uninstall", version=config["python"], ) |
| 141 | + |
| 142 | + print(*sorted(glob("dist/*")), sep="\n") # XXX |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments