"""Provide executable sessions for project automation."""

import os

import nox

from nox_poetry import Session, session

locations = "quendor", "tests", "noxfile.py"

nox.options.stop_on_first_error = True
nox.options.error_on_external_run = True
nox.options.sessions = "formatting", "linting", "typecheck", "safety", "test"

if os.path.exists("quendor.py"):
    os.remove("quendor.py")


@session
def test(session: Session) -> Session:
    """Run the test suite (using pytest)."""

    args = session.posargs or ["--cov"]
    session.install(".")
    session.install("pytest", "pytest-cov", "expects")
    session.run("pytest", *args)


@session
def formatting(session: Session) -> Session:
    """Run formatter (using black)."""

    args = session.posargs or locations
    session.install("black")
    session.run("black", *args)


@session
def typecheck(session: Session) -> Session:
    """Run type checking (using mypy)."""

    args = session.posargs or locations
    session.install("types-requests", "types-termcolor", "types-colorama")
    session.install("mypy")
    session.run("mypy", *args)


@session
def linting(session: Session) -> Session:
    """Run linting checks (using flake8)."""

    args = session.posargs or locations

    session.install(
        "autoflake",
        "flake8",
        "flake8-html",
        "flake8-2020",
        "flake8-annotations",
        "flake8-annotations-complexity",
        "flake8-annotations-coverage",
        "flake8-bandit",
        "flake8-black",
        "flake8-broken-line",
        "flake8-bugbear",
        "flake8-builtins",
        "flake8-coding",
        "flake8-cognitive-complexity",
        "flake8-commas",
        "flake8-comprehensions",
        "flake8-docstrings",
        "flake8-eradicate",
        "flake8-expression-complexity",
        "flake8-functions",
        "flake8-import-order",
        "flake8-multiline-containers",
        "flake8-mutable",
        "flake8-printf-formatting",
        "flake8-pytest-style",
        "flake8-quotes",
        "flake8-return",
        "flake8-simplify",
        "flake8-string-format",
        "flake8-use-fstring",
        "flake8-variables-names",
        "pep8-naming",
    )
    session.run(
        "autoflake",
        "--recursive",
        "--in-place",
        "--remove-unused-variables",
        "quendor/",
    )
    session.run("flake8", *args)


@session
def lintreport(session: Session) -> Session:
    """Generate linting HTML report."""

    list_locations = list(locations)
    list_locations.insert(0, "--format=html")

    session.posargs = tuple(list_locations)

    linting(session)


@session
def safety(session: Session) -> Session:
    """Run security checks (using safety)."""

    session.install("safety", ".")
    session.run("safety", "check")


@session
def standalone(session: Session) -> Session:
    """Build standalone executable."""

    session.run("python", "build.py")


@session
def clean(session: Session) -> Session:
    """Clean up generated files and directories."""

    session.run(
        "python",
        "-c",
        "import os; os.remove('.coverage') if os.path.exists('.coverage') else None",  # noqa: B950 E501
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./dist', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./htmlcov', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./htmllint', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.mypy_cache', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.pytest_cache', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]') if not str(p).startswith('.nox')]",  # noqa: B950 E501
    )

    session.run(
        "python",
        "-c",
        "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__') if not str(p).startswith('.nox')]",  # noqa: B950 E501
    )


@session
def clean_nox_vm(session: Session) -> Session:
    """Clean up generated Nox virtual environments."""

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.nox', ignore_errors=True)",
    )

"""Provide executable sessions for project automation."""

import os

import nox

from nox_poetry import Session, session

locations = "quendor", "tests", "noxfile.py"

nox.options.stop_on_first_error = True
nox.options.error_on_external_run = True
nox.options.sessions = "formatting", "linting", "typecheck", "safety", "test"

if os.path.exists("quendor.py"):
    os.remove("quendor.py")


@session
def test(session: Session) -> Session:
    """Run the test suite (using pytest)."""

    args = session.posargs or ["--cov"]
    session.install(".")
    session.install("pytest", "pytest-cov", "expects")
    session.run("pytest", *args)


@session
def formatting(session: Session) -> Session:
    """Run formatter (using black)."""

    args = session.posargs or locations
    session.install("black")
    session.run("black", *args)


@session
def typecheck(session: Session) -> Session:
    """Run type checking (using mypy)."""

    args = session.posargs or locations
    session.install("types-requests", "types-termcolor", "types-colorama")
    session.install("mypy")
    session.run("mypy", *args)


@session
def linting(session: Session) -> Session:
    """Run linting checks (using flake8)."""

    args = session.posargs or locations

    session.install(
        "autoflake",
        "flake8",
        "flake8-html",
        "flake8-2020",
        "flake8-annotations",
        "flake8-annotations-complexity",
        "flake8-annotations-coverage",
        "flake8-bandit",
        "flake8-black",
        "flake8-broken-line",
        "flake8-bugbear",
        "flake8-builtins",
        "flake8-coding",
        "flake8-cognitive-complexity",
        "flake8-commas",
        "flake8-comprehensions",
        "flake8-docstrings",
        "flake8-eradicate",
        "flake8-expression-complexity",
        "flake8-functions",
        "flake8-import-order",
        "flake8-multiline-containers",
        "flake8-mutable",
        "flake8-printf-formatting",
        "flake8-pytest-style",
        "flake8-quotes",
        "flake8-return",
        "flake8-simplify",
        "flake8-string-format",
        "flake8-use-fstring",
        "flake8-variables-names",
        "pep8-naming",
    )
    session.run(
        "autoflake",
        "--recursive",
        "--in-place",
        "--remove-unused-variables",
        "quendor/",
    )
    session.run("flake8", *args)


@session
def lintreport(session: Session) -> Session:
    """Generate linting HTML report."""

    list_locations = list(locations)
    list_locations.insert(0, "--format=html")

    session.posargs = tuple(list_locations)

    linting(session)


@session
def safety(session: Session) -> Session:
    """Run security checks (using safety)."""

    session.install("safety", ".")
    session.run("safety", "check")


@session
def standalone(session: Session) -> Session:
    """Build standalone executable."""

    session.run("python", "build.py")


@session
def clean(session: Session) -> Session:
    """Clean up generated files and directories."""

    session.run(
        "python",
        "-c",
        "import os; os.remove('.coverage') if os.path.exists('.coverage') else None",  # noqa: B950 E501
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./dist', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./htmlcov', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./htmllint', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.mypy_cache', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.pytest_cache', ignore_errors=True)",
    )

    session.run(
        "python",
        "-c",
        "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]') if not str(p).startswith('.nox')]",  # noqa: B950 E501
    )

    session.run(
        "python",
        "-c",
        "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__') if not str(p).startswith('.nox')]",  # noqa: B950 E501
    )


@session
def clean_nox_vm(session: Session) -> Session:
    """Clean up generated Nox virtual environments."""

    session.run(
        "python",
        "-c",
        "import shutil; shutil.rmtree('./.nox', ignore_errors=True)",
    )

Provide executable sessions for project automation. Run the test suite (using pytest). Run formatter (using black). Run type checking (using mypy). Run linting checks (using flake8). Generate linting HTML report. Run security checks (using safety). Build standalone executable. Clean up generated files and directories. # noqa: B950 E501 # noqa: B950 E501 # noqa: B950 E501 Clean up generated Nox virtual environments.