|
| 1 | +"""Script to deploy the latest version to github pages using mike""" |
| 2 | +import logging |
| 3 | +import subprocess |
| 4 | +from typing import List, Union |
| 5 | + |
| 6 | +from packaging import version |
| 7 | + |
| 8 | +logger = logging.getLogger(__file__) |
| 9 | + |
| 10 | + |
| 11 | +def run_cmd(cmd: str) -> List[str]: |
| 12 | + "Run a command in a subprocess." |
| 13 | + return subprocess.check_output(cmd.split()).decode("utf-8").splitlines() |
| 14 | + |
| 15 | + |
| 16 | +def get_current_version() -> version.Version: |
| 17 | + "Get current version specified in pyproject.toml." |
| 18 | + for line in run_cmd("poetry version"): |
| 19 | + if line.startswith("norfair"): |
| 20 | + return version.parse(line.split()[-1]) |
| 21 | + raise Exception("Could not read current version from toml") |
| 22 | + |
| 23 | + |
| 24 | +def get_latest_version() -> Union[version.Version, None]: |
| 25 | + "Get the deployed version number tagged as `latest` in mike." |
| 26 | + for line in run_cmd("mike list"): |
| 27 | + if line.endswith("[latest]"): |
| 28 | + return version.parse(line.split()[0]) |
| 29 | + logger.warning("Could not read the latest version deployed") |
| 30 | + |
| 31 | + |
| 32 | +def truncate_version(v: version.Version): |
| 33 | + "Truncates a version as `major.minor`" |
| 34 | + return version.parse(f"{v.major}.{v.minor}") |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + logging.basicConfig() |
| 39 | + |
| 40 | + current_version = get_current_version() |
| 41 | + latest_version = get_latest_version() |
| 42 | + |
| 43 | + target_version = truncate_version(current_version) |
| 44 | + alias = "" |
| 45 | + |
| 46 | + if latest_version is None or current_version >= latest_version: |
| 47 | + alias = "latest" |
| 48 | + |
| 49 | + logger.debug(f"{latest_version=}") |
| 50 | + logger.debug(f"{current_version=}") |
| 51 | + logger.debug(f"{target_version=}") |
| 52 | + logger.debug(f"{alias=}") |
| 53 | + |
| 54 | + logger.info("Running mike...") |
| 55 | + |
| 56 | + logger.info("\n".join(run_cmd(f"mike deploy -u {target_version} {alias}"))) |
0 commit comments