#!/usr/bin/env python
import os
import sys
from contextlib import contextmanager
from subprocess import check_call

_dname = os.path.dirname

REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))


@contextmanager
def cd(path):
    """Change directory while inside context manager."""
    cwd = os.getcwd()
    try:
        os.chdir(path)
        yield
    finally:
        os.chdir(cwd)


def run(command):
    return check_call(command, shell=True)


if __name__ == "__main__":
    with cd(REPO_ROOT):
        if sys.version_info[:2] >= (3, 12):
            # Python 3.12+ no longer includes setuptools by default.

            # Setuptools 71+ now prefers already installed versions
            # of packaging _and_ broke the API for packaging<22.0.
            # We'll pin to match what's in requirements-dev.txt.
            run(
                "pip install setuptools==78.1.1 packaging==24.1"
            )

        run("pip install -r requirements-dev-lock.txt")
