# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import os
import pathlib
import re
import subprocess
import sys
from datetime import date
from pathlib import Path
from textwrap import indent

import requests
from coverage.config import CoverageConfig

import hypothesistooling as tools
import hypothesistooling.projects.hypothesispython as hp
from hypothesistooling import installers as install, releasemanagement as rm
from hypothesistooling.scripts import pip_tool

TASKS = {}
BUILD_FILES = tuple(
    os.path.join(tools.ROOT, f)
    for f in ["tooling", "requirements", ".github", "hypothesis-python/tox.ini"]
)
TODAY = date.today().isoformat()


def task(if_changed=()):
    if not isinstance(if_changed, tuple):
        if_changed = (if_changed,)

    def accept(fn):
        def wrapped(*args, **kwargs):
            if if_changed and tools.IS_PULL_REQUEST:
                if not tools.has_changes(if_changed + BUILD_FILES):
                    changed = ", ".join(map(str, if_changed))
                    print(f"Skipping task due to no changes in {changed}")
                    return
            fn(*args, **kwargs)

        wrapped.__name__ = fn.__name__
        name = fn.__name__.replace("_", "-")
        if name != "<lambda>":
            TASKS[name] = wrapped
        return wrapped

    return accept


@task()
def check_installed():
    """No-op task that can be used to test for a successful install (so we
    don't fail to run if a previous install failed midway)."""


def codespell(*files):
    pip_tool(
        "codespell",
        "--check-hidden",
        "--check-filenames",
        "--ignore-words=./tooling/codespell-ignore.txt",
        # passing a custom --dictionary disables the default dictionary by default.
        # Add it back in with --dictionary=-.
        "--dictionary=-",
        "--dictionary=./tooling/codespell-dict.txt",
        "--skip=__pycache__,.mypy_cache,.venv,.git,tlds-alpha-by-domain.txt",
        *files,
    )


@task()
def lint():
    pip_tool("ruff", "check", ".")
    codespell(*(p for p in tools.all_files() if not p.name.endswith("by-domain.txt")))

    failed = False

    matches = subprocess.run(
        r"git grep -En '@(dataclasses\.)?dataclass\(.*\)' "
        "| grep -Ev 'frozen=.*slots=|slots=.*frozen='",
        shell=True,
        capture_output=True,
        text=True,
    ).stdout
    if matches:
        print("\nAll dataclass decorators must pass slots= and frozen= arguments:")
        print(indent(matches, "    "))
        failed = True

    matches = subprocess.run(
        r"git grep -nP '\b(the|as|a|to|because|user|test|about|from|only)\s+\1\b'",
        shell=True,
        capture_output=True,
        text=True,
    ).stdout
    if matches:
        print("\nFound duplicate words:")
        print(indent(matches, "    "))
        failed = True

    if failed:
        sys.exit(1)


def do_release(package):
    if not package.has_release():
        print(f"No release for {package.__name__}")
        return

    os.chdir(package.BASE_DIR)

    print("Updating changelog and version")
    package.update_changelog_and_version()

    print("Committing changes")
    rm.commit_pending_release(package)

    print("Building distribution")
    package.build_distribution()

    print("Looks good to release!")

    tag_name = package.tag_name()

    print(f"Creating tag {tag_name}")
    tools.create_tag(tag_name)
    tools.push_tag(tag_name)

    print("Uploading distribution")
    package.upload_distribution()


@task()
def deploy():
    HEAD = tools.hash_for_name("HEAD")
    MASTER = tools.hash_for_name("origin/master")

    print("Current head:  ", HEAD)
    print("Current master:", MASTER)

    if not tools.is_ancestor(HEAD, MASTER):
        print("Not deploying due to not being on master")
        sys.exit(0)

    if "TWINE_PASSWORD" not in os.environ:
        print("Running without access to secure variables, so no deployment")
        sys.exit(0)

    tools.configure_git()

    for project in tools.all_projects():
        do_release(project)

    sys.exit(0)


# See https://www.linuxfoundation.org/blog/copyright-notices-in-open-source-software-projects/
HEADER = """
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
""".strip()

# this pattern is copied from shed
# https://github.com/Zac-HD/shed/blob/6471da71c5b5cc443443ef5ed072799db275e7c0/src/shed/__init__.py#L297
rst_pattern = re.compile(
    r"(?P<before>"
    r"^(?P<indent> *)\.\. "
    r"(?P<block>jupyter-execute::|"
    r"invisible-code-block: python|"  # magic rst comment for Sybil doctests
    r"(code|code-block|sourcecode|ipython):: (python|py|sage|python3|py3|numpy))\n"
    r"((?P=indent) +:.*\n)*"
    r"\n*"
    r")"
    r"(?P<code>(^((?P=indent) +.*)?\n)+)",
    flags=re.MULTILINE,
)


def remove_consecutive_newlines_in_rst(path: Path):
    # replace 2+ empty lines in `.. code-block:: python` blocks with just one empty
    # line
    content = path.read_text()
    processed_content = rst_pattern.sub(
        lambda m: m["before"] + re.sub(r"\n{3,}", "\n\n", m["code"]), content
    )
    if processed_content != content:
        path.write_text(processed_content)


@task()
def format():
    changed = tools.modified_files()

    format_all = os.environ.get("FORMAT_ALL", "").lower() == "true"
    if "requirements/tools.txt" in changed:
        # We've changed the tools, which includes a lot of our formatting
        # logic, so we need to rerun formatters.
        format_all = True

    paths = tools.all_files() if format_all else changed

    doc_paths_to_format = [p for p in sorted(paths) if p.suffix in {".rst", ".md"}]
    paths_to_format = [p for p in sorted(paths) if p.suffix == ".py"]

    if not (paths_to_format or doc_paths_to_format):
        return

    # .coveragerc lists several regex patterns to treat as nocover pragmas, and
    # we want to find (and delete) cases where # pragma: no cover is redundant.
    def warn(msg):
        raise Exception(msg)

    config = CoverageConfig()
    config.from_file(os.path.join(hp.BASE_DIR, ".coveragerc"), warn=warn, our_file=True)
    pattern = "|".join(l for l in config.exclude_list if "pragma" not in l)
    unused_pragma_pattern = re.compile(f"(({pattern}).*)  # pragma: no (branch|cover)")
    last_header_line = HEADER.splitlines()[-1].rstrip()

    for p in paths_to_format:
        lines = []
        with open(p, encoding="utf-8") as fp:
            shebang = None
            first = True
            in_header = True
            for l in fp:
                if first:
                    first = False
                    if l[:2] == "#!":
                        shebang = l
                        continue
                elif in_header and l.rstrip() == last_header_line:
                    in_header = False
                    lines = []
                else:
                    lines.append(unused_pragma_pattern.sub(r"\1", l))

        source = "".join(lines).strip()
        with open(p, "w", encoding="utf-8") as fp:
            if shebang is not None:
                fp.write(shebang)
                fp.write("\n")
            fp.write(HEADER)
            if source:
                fp.write("\n\n")
                fp.write(source)
            fp.write("\n")

    codespell("--write-changes", *paths_to_format, *doc_paths_to_format)
    pip_tool("ruff", "check", "--fix-only", ".")
    pip_tool("shed", "--py310-plus", *paths_to_format, *doc_paths_to_format)

    for p in doc_paths_to_format:
        remove_consecutive_newlines_in_rst(p)


VALID_STARTS = (HEADER.split()[0], "#!/usr/bin/env python")


@task()
def check_format():
    format()
    n = max(map(len, VALID_STARTS))
    bad = False
    for p in tools.all_files():
        if p.suffix != ".py":
            continue
        with open(p, encoding="utf-8") as fp:
            start = fp.read(n)
            if not any(start.startswith(s) for s in VALID_STARTS):
                print(f"{p} has incorrect start {start!r}", file=sys.stderr)
                bad = True
    assert not bad
    try:
        check_not_changed()
    except Exception:
        box_width = 50
        inner_width = box_width - 2
        content_width = inner_width - 2
        msg1 = "Note: code differed after formatting."
        msg2 = "To fix this, run:"
        msg3 = "    ./build.sh format"

        lines = [
            "",
            "    " + "*" * box_width,
            "    *" + " " * inner_width + "*",
            "    *  " + msg1 + " " * (content_width - len(msg1)) + "*",
            "    *" + " " * inner_width + "*",
            "    *  " + msg2 + " " * (content_width - len(msg2)) + "*",
            "    *" + " " * inner_width + "*",
            "    *  " + msg3 + " " * (content_width - len(msg3)) + "*",
            "    *" + " " * inner_width + "*",
            "    " + "*" * box_width,
            "",
        ]
        print("\n".join(lines), file=sys.stderr)
        raise


def check_not_changed():
    subprocess.check_call(["git", "diff", "--exit-code"])


@task()
def compile_requirements(*, upgrade=False):
    if upgrade:
        extra = ["--upgrade", "--rebuild"]
    else:
        extra = []

    for f in Path("requirements").glob("*.in"):
        out_file = f.with_suffix(".txt")
        pip_tool(
            "pip-compile",
            "--allow-unsafe",  # future default, not actually unsafe
            "--resolver=backtracking",  # new pip resolver, default in pip-compile 7+
            *extra,
            str(f),
            "hypothesis-python/pyproject.toml",
            "--output-file",
            str(out_file),
            cwd=tools.ROOT,
            env={
                "CUSTOM_COMPILE_COMMAND": "./build.sh upgrade-requirements",
                **os.environ,
            },
        )
        # Check that we haven't added anything to output files without adding to inputs
        out_pkgs = out_file.read_text(encoding="utf-8")
        for p in f.read_text(encoding="utf-8").splitlines():
            p = p.lower().replace("_", "-")
            if re.fullmatch(r"[a-z-]+", p):
                assert p + "==" in out_pkgs, f"Package `{p}` deleted from {out_file}"
        out_file.write_text(out_pkgs.replace(f"{tools.ROOT}/", ""))


def update_python_versions():
    install.ensure_python(PYTHONS[ci_version])
    where = os.path.expanduser("~/.cache/hypothesis-build-runtimes/pyenv/")
    subprocess.run(
        "git fetch && git reset --hard origin/master",
        cwd=where,
        shell=True,
        capture_output=True,
    )
    cmd = "bin/pyenv install --list"
    result = subprocess.run(
        cmd, shell=True, stdout=subprocess.PIPE, cwd=where
    ).stdout.decode()
    # pyenv reports available versions in chronological order, so we keep the newest
    # *unless* our current ends with a digit (is stable) and the candidate does not.
    # (plus some special cases for the `t` suffix for free-threading builds)
    stable = re.compile(r".*3\.\d+.\d+t?$")
    min_minor_version = re.search(
        r'requires-python = ">= ?3.(\d+)"',
        Path("hypothesis-python/pyproject.toml").read_text(encoding="utf-8"),
    ).group(1)
    best = {}
    for line in map(str.strip, result.splitlines()):
        if m := re.match(r"(?:pypy)?3\.(?:9|\d\dt?)", line):
            key = m.group()
            curr = best.get(key, line)
            if (
                (stable.match(line) or not stable.match(curr))
                and not (line.endswith("-dev") and not curr.endswith("-dev"))
                and int(key.split(".")[-1].rstrip("t")) >= int(min_minor_version)
                and key.endswith("t") == line.endswith(("t", "t-dev"))
            ):
                best[key] = line

    if best == PYTHONS:
        return

    # Write the new mapping back to this file
    thisfile = pathlib.Path(__file__)
    before = thisfile.read_text(encoding="utf-8")
    after = re.sub(r"\nPYTHONS = \{[^{}]+\}", f"\nPYTHONS = {best}", before)
    thisfile.write_text(after, encoding="utf-8")
    pip_tool("shed", str(thisfile))

    # Automatically sync ci_version with the version in build.sh
    build_sh = tools.ROOT / "build.sh"
    sh_before = build_sh.read_text(encoding="utf-8")
    sh_after = re.sub(r"3\.\d\d?\.\d\d?", best[ci_version], sh_before)
    if sh_before != sh_after:
        build_sh.unlink()  # so bash doesn't reload a modified file
        build_sh.write_text(sh_after, encoding="utf-8")
        build_sh.chmod(0o755)


DJANGO_VERSIONS = {
    "4.2": "4.2.30",
    "5.2": "5.2.13",
    "6.0": "6.0.4",
}


def update_django_versions():
    # https://endoflife.date/django makes it easier to track these
    releases = requests.get("https://endoflife.date/api/django.json").json()
    versions = {r["cycle"]: r["latest"] for r in releases[::-1] if TODAY <= r["eol"]}

    if versions == DJANGO_VERSIONS:
        return

    # Write the new mapping back to this file
    thisfile = pathlib.Path(__file__)
    before = thisfile.read_text(encoding="utf-8")
    after = re.sub(
        r"DJANGO_VERSIONS = \{[^{}]+\}",
        "DJANGO_VERSIONS = " + repr(versions).replace("}", ",}"),
        before,
    )
    thisfile.write_text(after, encoding="utf-8")
    pip_tool("shed", str(thisfile))

    # Update the minimum version in pyproject.toml
    pyproject_toml = hp.BASE_DIR / "pyproject.toml"
    content = re.sub(
        r"django>=\d+\.\d+",
        f"django>={min(versions, key=float)}",
        pyproject_toml.read_text(encoding="utf-8"),
    )
    pyproject_toml.write_text(content, encoding="utf-8")

    # Automatically sync ci_version with the version in build.sh
    tox_ini = hp.BASE_DIR / "tox.ini"
    content = tox_ini.read_text(encoding="utf-8")
    print(versions)
    for short, full in versions.items():
        content = re.sub(
            rf"django=={short}(\.\d+)?",
            rf"django=={full}",
            content,
        )
    tox_ini.write_text(content, encoding="utf-8")


def update_pyodide_versions():

    def version_tuple(v: str) -> tuple[int, int, int]:
        return tuple(int(x) for x in v.split("."))  # type: ignore

    vers_re = r"(\d+\.\d+\.\d+)"
    all_pyodide_build_versions = re.findall(
        f"pyodide_build-{vers_re}-py3-none-any.whl",  # excludes pre-releases
        requests.get("https://pypi.org/simple/pyodide-build/").text,
    )
    pyodide_build_version = max(
        # Don't just pick the most recent version; find the highest stable version.
        set(all_pyodide_build_versions),
        key=version_tuple,
    )

    cross_build_environments_url = "https://raw.githubusercontent.com/pyodide/pyodide/refs/heads/main/pyodide-cross-build-environments.json"
    cross_build_environments_data = requests.get(cross_build_environments_url).json()

    # Find the latest stable release for the Pyodide runtime/xbuildenv that is compatible
    # with the pyodide-build version we found
    stable_releases = [
        release
        for release in cross_build_environments_data["releases"].values()
        if re.fullmatch(vers_re, release["version"])
    ]

    compatible_releases = []
    for release in stable_releases:  # sufficiently large values
        min_build_version = release.get("min_pyodide_build_version", "0.0.0")
        max_build_version = release.get("max_pyodide_build_version", "999.999.999")

        # Perform version comparisons to avoid getting an incompatible pyodide-build version
        # with the Pyodide runtime
        if (
            version_tuple(min_build_version)
            <= version_tuple(pyodide_build_version)
            <= version_tuple(max_build_version)
        ):
            compatible_releases.append(release)

    if not compatible_releases:
        raise RuntimeError(
            f"No compatible Pyodide release found for pyodide-build {pyodide_build_version}"
        )

    pyodide_release = max(
        compatible_releases,
        key=lambda release: version_tuple(release["version"]),
    )

    pyodide_version = pyodide_release["version"]
    python_version = pyodide_release["python_version"]

    ci_file = tools.ROOT / ".github/workflows/main.yml"
    config = ci_file.read_text(encoding="utf-8")
    for name, var in [
        ("PYODIDE", pyodide_version),
        ("PYODIDE_BUILD", pyodide_build_version),
        ("PYTHON", python_version),
    ]:
        config = re.sub(f"{name}_VERSION: {vers_re}", f"{name}_VERSION: {var}", config)
    ci_file.write_text(config, encoding="utf-8")


def update_vendored_files():
    vendor = pathlib.Path(hp.PYTHON_SRC) / "hypothesis" / "vendor"

    # Turns out that as well as adding new gTLDs, IANA can *terminate* old ones
    url = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt"
    fname = vendor / url.split("/")[-1]
    new = requests.get(url).content
    # If only the timestamp in the header comment has changed, skip the update.
    if fname.read_bytes().splitlines()[1:] != new.splitlines()[1:]:
        fname.write_bytes(new)

    # Always require the most recent version of tzdata - we don't need to worry about
    # pre-releases because tzdata is a 'latest data' package  (unlike pyodide-build).
    # Our crosshair extra is research-grade, so we require latest versions there too.
    pyproject_toml = pathlib.Path(hp.BASE_DIR, "pyproject.toml")
    new = pyproject_toml.read_text(encoding="utf-8")
    for pkgname in ("tzdata", "crosshair-tool", "hypothesis-crosshair"):
        pkg_url = f"https://pypi.org/pypi/{pkgname}/json"
        pkg_version = requests.get(pkg_url).json()["info"]["version"]
        new = re.sub(rf"{pkgname}>=([a-z0-9.]+)", f"{pkgname}>={pkg_version}", new)
    pyproject_toml.write_text(new, encoding="utf-8")


def has_diff(file_or_directory):
    diff = ["git", "diff", "--no-patch", "--exit-code", "--", file_or_directory]
    return subprocess.call(diff) != 0


@task()
def upgrade_requirements():
    update_vendored_files()
    compile_requirements(upgrade=True)
    subprocess.call(["./build.sh", "format"], cwd=tools.ROOT)  # exits 1 if changed
    if has_diff(hp.PYTHON_SRC) and not os.path.isfile(hp.RELEASE_FILE):
        msg = hp.get_autoupdate_message(domainlist_changed=has_diff(hp.DOMAINS_LIST))
        with open(hp.RELEASE_FILE, mode="w", encoding="utf-8") as f:
            f.write(f"RELEASE_TYPE: patch\n\n{msg}")
    update_python_versions()
    update_pyodide_versions()
    update_django_versions()
    subprocess.call(["git", "add", "."], cwd=tools.ROOT)


@task()
def check_requirements():
    compile_requirements(upgrade=False)


@task(if_changed=hp.HYPOTHESIS_PYTHON)
def documentation():
    try:
        if hp.has_release():
            hp.update_changelog_and_version()
        hp.build_docs()
    finally:
        subprocess.check_call(
            ["git", "checkout", "docs/changelog.rst", "src/hypothesis/version.py"],
            cwd=hp.HYPOTHESIS_PYTHON,
        )


@task()
def website():
    subprocess.call([sys.executable, "-m", "pelican"], cwd=tools.ROOT / "website")


@task()
def live_website():
    subprocess.call(
        [sys.executable, "-m", "pelican", "--autoreload", "--listen"],
        cwd=tools.ROOT / "website",
    )


@task()
def live_docs():
    pip_tool(
        "sphinx-autobuild",
        "docs",
        "docs/_build/html",
        "--watch",
        "src",
        "--open-browser",
        cwd=hp.HYPOTHESIS_PYTHON,
    )


def run_tox(task, version, *args):
    python = install.python_executable(version)

    # Create a version of the name that tox will pick up for the correct
    # interpreter alias.
    linked_version = os.path.basename(python) + ALIASES[version]
    try:
        os.symlink(python, linked_version)
    except FileExistsError:
        pass

    env = dict(os.environ)
    python = install.python_executable(version)

    env["PATH"] = os.path.dirname(python) + ":" + env["PATH"]
    # Set environment variable for tox to use in basepython substitution
    if version.startswith("pypy"):
        # For PyPy, use the version name from e.g. "pypy3.11-7.3.20"
        # to match tox's environment name inference.
        env["TOX_PYTHON_VERSION"] = version.split("-")[0]  # "pypy3.11"
    else:
        env["TOX_PYTHON_VERSION"] = ALIASES[version]  # "python3.12"
    print(env["PATH"])

    pip_tool("tox", "-e", task, *args, env=env, cwd=hp.HYPOTHESIS_PYTHON)


# update_python_versions(), above, keeps the contents of this dict up to date.
# When a version is added or removed, manually update the env lists in tox.ini and
# workflows/main.yml, and the `Programming Language ::` specifiers in pyproject.toml
PYTHONS = {
    "3.10": "3.10.20",
    "3.11": "3.11.15",
    "3.12": "3.12.13",
    "3.13": "3.13.13",
    "3.13t": "3.13t-dev",
    "3.14": "3.14.4",
    "3.14t": "3.14t-dev",
    "3.15": "3.15.0a8",
    "3.15t": "3.15t-dev",
    "pypy3.10": "pypy3.10-7.3.19",
    "pypy3.11": "pypy3.11-7.3.20",
}
ci_version = "3.14"  # Keep this in sync with GH Actions main.yml and .readthedocs.yml

python_tests = task(
    if_changed=(
        hp.PYTHON_SRC,
        hp.PYTHON_TESTS,
        hp.HYPOTHESIS_PYTHON / "pyproject.toml",
        tools.ROOT / "tooling",
        hp.HYPOTHESIS_PYTHON / "scripts",
    )
)


# ALIASES are the executable names for each Python version
ALIASES = {}
for key, version in PYTHONS.items():
    if key.startswith("pypy"):
        ALIASES[version] = "pypy3"
        name = key.replace(".", "")
    else:
        ALIASES[version] = f"python{key}"
        name = f"py3{key[2:]}"
    TASKS[f"check-{name}"] = python_tests(
        lambda n=f"{name}-full", v=version, *args: run_tox(n, v, *args)
    )
    for subtask in (
        "brief",
        "full",
        "cover",
        "rest",
        "nocover",
        "niche",
        "custom",
    ):
        TASKS[f"check-{name}-{subtask}"] = python_tests(
            lambda n=f"{name}-{subtask}", v=version, *args: run_tox(n, v, *args)
        )


@python_tests
def check_py310_pyjion(*args):
    run_tox("py310-pyjion", PYTHONS["3.10"], *args)


@task()
def tox(*args):
    if len(args) < 2:
        print("Usage: ./build.sh tox TOX_ENV PY_VERSION [tox args]")
        sys.exit(1)
    run_tox(*args)


def standard_tox_task(name, py=ci_version):
    TASKS["check-" + name] = python_tests(
        lambda *args: run_tox(name, PYTHONS.get(py, py), *args)
    )


standard_tox_task("py311-pytest62", py="3.11")  # hits "ast.Str is deprecated" in 3.12+
standard_tox_task("pytest74")
standard_tox_task("pytest84")
standard_tox_task("pytest9")

dj_version = max(ci_version, "3.12")
for n in DJANGO_VERSIONS:
    standard_tox_task(f"django{n.replace('.', '')}", py=dj_version)
# we also test no-contrib on the latest django version
standard_tox_task("django-nocontrib", py=dj_version)

# test each pandas version with the latest python version they support
standard_tox_task("py310-pandas11", py="3.10")
standard_tox_task("py310-pandas12", py="3.10")
standard_tox_task("py310-pandas13", py="3.10")
standard_tox_task("py310-pandas14", py="3.10")
standard_tox_task("py311-pandas15", py="3.11")
standard_tox_task("py311-pandas20", py="3.11")
standard_tox_task("py312-pandas21", py="3.12")
standard_tox_task("py313-pandas22", py="3.13")

for kind in ("cover", "nocover", "niche", "custom"):
    standard_tox_task(f"crosshair-{kind}")

for kind in ("rest", "nocover"):
    # Note, in CI these are executed on alternative platforms (e.g., windows)
    # directly in tox (and not via build.sh)
    standard_tox_task(f"alt-{kind}")

standard_tox_task("threading")
standard_tox_task("py310-oldestnumpy", py="3.10")
standard_tox_task("numpy-nightly", py="3.12")

standard_tox_task("coverage")
standard_tox_task("conjecture-coverage")
standard_tox_task("snapshots")


@task()
def check_quality(*args):
    run_tox("quality", PYTHONS[ci_version], *args)


@task(if_changed=(hp.PYTHON_SRC, os.path.join(hp.HYPOTHESIS_PYTHON, "examples")))
def check_examples3(*args):
    run_tox("examples3", PYTHONS[ci_version], *args)


@task()
def check_whole_repo_tests(*args):
    install.ensure_shellcheck()
    subprocess.check_call(
        [sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON]
    )

    if not args:
        args = ["-n", "auto", tools.REPO_TESTS / "whole_repo"]
    subprocess.check_call([sys.executable, "-m", "pytest", *args])


@task()
def check_documentation(*args):
    install.ensure_shellcheck()
    subprocess.check_call(
        [sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON]
    )

    if not args:
        args = ["-n", "auto", tools.REPO_TESTS / "documentation"]
    subprocess.check_call([sys.executable, "-m", "pytest", *args])


@task()
def check_types(*args):
    install.ensure_shellcheck()
    subprocess.check_call(
        [sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON]
    )

    if not args:
        args = ["-n", "auto", tools.REPO_TESTS / "types"]
    subprocess.check_call([sys.executable, "-m", "pytest", *args])


@task()
def check_types_api(*args):
    install.ensure_shellcheck()
    subprocess.check_call(
        [sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON]
    )

    if not args:
        ignore = ["--ignore", tools.REPO_TESTS / "types/test_hypothesis.py"]
        args = ["-n", "auto", tools.REPO_TESTS / "types"] + ignore
    subprocess.check_call([sys.executable, "-m", "pytest", *args])


@task()
def check_types_hypothesis(*args):
    install.ensure_shellcheck()
    subprocess.check_call(
        [sys.executable, "-m", "pip", "install", "--upgrade", hp.HYPOTHESIS_PYTHON]
    )

    if not args:
        testcase = "types/test_hypothesis.py"
        args = ["-n", "auto", tools.REPO_TESTS / testcase]
    subprocess.check_call([sys.executable, "-m", "pytest", *args])


@task()
def shell():
    import IPython

    IPython.start_ipython([])


@task()
def python(*args):
    os.execv(sys.executable, (sys.executable, *args))


@task()
def tasks():
    """Print a list of all task names supported by the build system."""
    for task_name in sorted(TASKS.keys()):
        print("    " + task_name)


if __name__ == "__main__":
    if "SNAKEPIT" not in os.environ:
        print(
            "This module should not be executed directly, but instead via "
            "build.sh (which sets up its environment)"
        )
        sys.exit(1)

    if len(sys.argv) > 1:
        task_to_run = sys.argv[1]
        args = sys.argv[2:]
    else:
        task_to_run = os.environ.get("TASK")
        args = ()

    if task_to_run is None:
        print(
            "No task specified. Either pass the task to run as an "
            "argument or as an environment variable TASK. "
            '(Use "./build.sh tasks" to list all supported task names.)'
        )
        sys.exit(1)

    if task_to_run not in TASKS:
        print(f"\nUnknown task {task_to_run!r}.  Available tasks are:")
        tasks()
        sys.exit(1)

    try:
        TASKS[task_to_run](*args)
    except subprocess.CalledProcessError as e:
        sys.exit(e.returncode)
