def _pytest(session: nox.Session, *py_flags: str) -> None: try: os.remove(".coverage") except: # Ignore errors pass session.run("python", *py_flags, "-m", "pytest", *FLAGS, *session.posargs, config.TEST_PACKAGE)
def reformat_code(session: nox.Session) -> None: """Remove trailing whitespace in source, run isort and then run black code formatter.""" session.install("-r", "dev-requirements.txt") remove_trailing_whitespaces() session.run("isort", *REFORMATING_PATHS) session.run("black", *REFORMATING_PATHS)
def verify_types(session: nox.Session) -> None: """Verify the "type completeness" of types exported by the library using Pyright.""" session.install("-r", "dev-requirements.txt") session.install(".") # session.env["PYRIGHT_PYTHON_GLOBAL_NODE"] = "off" session.env["PYRIGHT_PYTHON_FORCE_VERSION"] = config.PYRIGHT_VERSION session.run("python", "-m", "pyright", "--version") session.run("python", "-m", "pyright", "--verifytypes", "hikari", "--ignoreexternal")
def _pytest(session: nox.Session, *py_flags: str) -> None: if "--skip-coverage" in session.posargs: session.posargs.remove("--skip-coverage") flags = RUN_FLAGS else: flags = [*RUN_FLAGS, *COVERAGE_FLAGS] session.run("python", *py_flags, "-m", "pytest", *flags, *session.posargs, config.TEST_PACKAGE)
def reformat_code(session: nox.Session) -> None: """Remove trailing whitespace in source, run isort, codespell and then run black code formatter.""" session.install("-r", "dev-requirements.txt") remove_trailing_whitespaces(session) session.run("isort", *config.PYTHON_REFORMATTING_PATHS) session.run("black", *config.PYTHON_REFORMATTING_PATHS) session.run("codespell", "-w", *config.FULL_REFORMATTING_PATHS)
def mypy(session: nox.Session) -> None: """Perform static type analysis on Python source code.""" session.install("-r", "requirements.txt", "-r", "dev-requirements.txt") _generate_stubs(session) session.run("mypy", "-p", config.MAIN_PACKAGE, "--config", config.MYPY_INI) session.run("mypy", "-p", config.EXAMPLE_SCRIPTS, "--config", config.MYPY_INI)
def reformat_code(session: nox.Session) -> None: """Remove trailing whitespace in source, run isort and then run black code formatter.""" remove_trailing_whitespaces() # isort session.install(f"isort=={ISORT_VERSION}") session.run("isort", *REFORMATING_PATHS) # black session.install(f"black=={BLACK_VERSION}") session.run("black", *REFORMATING_PATHS)
def flake8(session: nox.Session) -> None: """Run code linting, SAST, and analysis.""" session.install("-r", "requirements.txt", "-r", "flake8-requirements.txt") session.run( "flake8", "--statistics", "--show-source", "--benchmark", "--tee", config.MAIN_PACKAGE, config.TEST_PACKAGE, )
def flake8_html(session: nox.Session) -> None: """Run code linting, SAST, and analysis and generate an HTML report.""" session.install("-r", "requirements.txt", "-r", "flake8-requirements.txt") session.run( "flake8", "--format=html", f"--htmldir={config.FLAKE8_REPORT}", "--statistics", "--show-source", "--benchmark", "--tee", config.MAIN_PACKAGE, config.TEST_PACKAGE, )
def _generate_stubs(session: nox.Session) -> None: session.run("stubgen", *STUBGEN_GENERATE, "-o", ".", "--include-private", "--no-import") stub_paths = [path + "i" for path in STUBGEN_GENERATE] session.run("isort", *stub_paths) session.run("black", *stub_paths) for stub_path in stub_paths: with open(stub_path, "r") as fp: content = fp.read() with open(stub_path, "w") as fp: fp.write("# DO NOT MANUALLY EDIT THIS FILE!\n") fp.write("# This file was automatically generated by `nox -s generate-stubs`\n\n") fp.write(content)
def pdoc3(session: nox.Session) -> None: """Generate documentation with pdoc.""" session.install("-r", "requirements.txt", "-r", "dev-requirements.txt") session.env["PDOC3_GENERATING"] = "1" session.run( "python", "docs/patched_pdoc.py", config.MAIN_PACKAGE, "--html", "--output-dir", config.ARTIFACT_DIRECTORY, "--template-dir", config.DOCUMENTATION_DIRECTORY, "--force", ) shutil.copyfile( os.path.join(config.DOCUMENTATION_DIRECTORY, config.LOGO_SOURCE), os.path.join(config.ARTIFACT_DIRECTORY, config.LOGO_SOURCE), )
def pages(session: nox.Session) -> None: """Generate website pages.""" if not os.path.exists(config.ARTIFACT_DIRECTORY): os.mkdir(config.ARTIFACT_DIRECTORY) # Static print("Copying static objects...") copy_from_in(config.PAGES_DIRECTORY, config.ARTIFACT_DIRECTORY) # Documentation session.install("-r", "requirements.txt", "-r", "dev-requirements.txt") session.env["PDOC3_GENERATING"] = "1" print("Building documentation...") session.run( "python", "docs/patched_pdoc.py", config.MAIN_PACKAGE, "--html", "--output-dir", config.ARTIFACT_DIRECTORY, "--template-dir", config.DOCUMENTATION_DIRECTORY, "--force", ) # Rename `hikari` into `documentation` # print("Renaming output dir...") # print(f"{config.ARTIFACT_DIRECTORY}/{config.MAIN_PACKAGE} -> {config.ARTIFACT_DIRECTORY}/documentation") # shutil.rmtree(f"{config.ARTIFACT_DIRECTORY}/documentation", ignore_errors=True) # shutil.move(f"{config.ARTIFACT_DIRECTORY}/{config.MAIN_PACKAGE}", f"{config.ARTIFACT_DIRECTORY}/documentation") # Pre-generated indexes if shutil.which("npm") is None: message = "'npm' not installed, can't prebuild index" if "CI" in os.environ: session.error(message) session.skip(message) print("Prebuilding index...") session.run("npm", "install", "[email protected]", external=True) session.run( "node", "scripts/prebuild_index.js", f"{config.ARTIFACT_DIRECTORY}/hikari/index.json", f"{config.ARTIFACT_DIRECTORY}/hikari/prebuilt_index.json", external=True, )
def coveralls(session: nox.Session) -> None: """Run coveralls. This has little effect outside TravisCI.""" session.install("-U", "python-coveralls") session.run("coveralls")
def _pytest(session: nox.Session, *py_flags: str) -> None: shutil.rmtree(".coverage", ignore_errors=True) session.run("python", *py_flags, "-m", "pytest", *FLAGS, *session.posargs, config.TEST_PACKAGE)
def safety(session: nox.Session) -> None: """Perform dependency scanning.""" # Temporary addition to avoid safety erroring due to https://github.com/pypa/pip/pull/9827 session.install("--upgrade", "pip") session.install("-r", "requirements.txt", "-r", "dev-requirements.txt") session.run("safety", "check", "--full-report")
def verify_types(session: nox.Session) -> None: """Verify the "type completeness" of types exported by the library using Pyright.""" session.install("-r", "dev-requirements.txt") session.install(".") session.run("python", "-m", "pyright", "--verifytypes", config.MAIN_PACKAGE, "--ignoreexternal")
def codespell(session: nox.Session) -> None: """Run codespell to check for spelling mistakes.""" session.install("-r", "dev-requirements.txt") session.run("codespell", *config.FULL_REFORMATTING_PATHS)
def safety(session: nox.Session) -> None: """Perform dependency scanning.""" session.install("-r", "requirements.txt", "-r", "dev-requirements.txt") session.run("safety", "check", "--full-report")
def _generate_stubs(session: nox.Session) -> None: session.run("stubgen", *STUBGEN_GENERATE, "-o", ".", "--include-private", "--no-import")
def twemoji_test(session: nox.Session): """Brute-force test all possible Twemoji mappings for Discord unicode emojis.""" session.install("-U", "-e", ".", "requests") session.run("python", "scripts/test_twemoji_mapping.py")
def slotscheck(session: nox.Session) -> None: """Check for common slotting mistakes.""" session.install(".", "-r", "dev-requirements.txt") session.run("slotscheck", "-m", config.MAIN_PACKAGE)