From 7a073c27cae5f94d0d23d518390362b77e3af62b Mon Sep 17 00:00:00 2001 From: loonghao Date: Sat, 11 May 2024 14:01:55 +0800 Subject: [PATCH] feat: Split all nox sessions Signed-off-by: loonghao --- nox_actions/__init__.py | 0 nox_actions/lint.py | 17 ++++++ nox_actions/release.py | 114 ++++++++++++++++++++++++++++++++++++++++ nox_actions/run_maya.py | 88 +++++++++++++++++++++++++++++++ nox_actions/run_test.py | 16 ++++++ nox_actions/utils.py | 10 ++++ 6 files changed, 245 insertions(+) create mode 100644 nox_actions/__init__.py create mode 100644 nox_actions/lint.py create mode 100644 nox_actions/release.py create mode 100644 nox_actions/run_maya.py create mode 100644 nox_actions/run_test.py create mode 100644 nox_actions/utils.py diff --git a/nox_actions/__init__.py b/nox_actions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nox_actions/lint.py b/nox_actions/lint.py new file mode 100644 index 0000000..76837e0 --- /dev/null +++ b/nox_actions/lint.py @@ -0,0 +1,17 @@ +# Import third-party modules +import nox +from nox_actions.utils import PACKAGE_NAME + + +def lint(session: nox.Session) -> None: + session.install("wemake-python-styleguide", "isort", "ruff") + session.run("flake8", PACKAGE_NAME) + session.run("isort", "--check-only", PACKAGE_NAME) + session.run("ruff", "check") + + +def lint_fix(session: nox.Session) -> None: + session.install("isort", "ruff", "pre-commit") + session.run("ruff", "check", "--fix") + session.run("isort", ".") + session.run("pre-commit", "run", "--all-files") diff --git a/nox_actions/release.py b/nox_actions/release.py new file mode 100644 index 0000000..4ac1843 --- /dev/null +++ b/nox_actions/release.py @@ -0,0 +1,114 @@ +# Import built-in modules +import argparse +import os +from pathlib import Path +import shutil +from typing import Iterator +from typing import Tuple +import zipfile + +# Import third-party modules +import nox +from nox_actions.utils import PACKAGE_NAME +from nox_actions.utils import ROOT + + +def make_install_zip(session: nox.Session) -> None: + temp_dir = os.path.join(ROOT, ".zip") + build_root = os.path.join(temp_dir, "maya_umbrella") + script_dir = os.path.join(build_root, "scripts") + shutil.rmtree(temp_dir, ignore_errors=True) + bat_template = """ +@echo off +SET "batPath=%~dp0" +SET "modContent=+ maya_umbrella {version} %batPath%" +SET "modFilePath=%~dp0maya_umbrella.mod" +echo %modContent% > "%modFilePath%" +xcopy "%~dp0maya_umbrella.mod" "%USERPROFILE%\\documents\\maya\\modules\\" /y +del /f "%~dp0maya_umbrella.mod" +pause +""" + parser = argparse.ArgumentParser(prog="nox -s make-zip") + parser.add_argument("--version", default="0.5.0", help="Version to use for the zip file") + args = parser.parse_args(session.posargs) + version = str(args.version) + print(f"make zip to current version: {version}") + + shutil.copytree(os.path.join(ROOT, "maya_umbrella"), + os.path.join(script_dir, "maya_umbrella")) + with open(os.path.join(build_root, "install.bat"), "w") as f: + f.write(bat_template.format(version=version)) + + shutil.copy2(os.path.join(ROOT, "maya", "userSetup.py"), + os.path.join(script_dir, "userSetup.py")) + + zip_file = os.path.join(temp_dir, f"{PACKAGE_NAME}-{version}.zip") + with zipfile.ZipFile(zip_file, "w") as zip_obj: + for root, _, files in os.walk(build_root): + for file in files: + zip_obj.write(os.path.join(root, file), + os.path.relpath(os.path.join(root, file), + os.path.join(build_root, "."))) + print("Saving to {zipfile}".format(zipfile=zip_file)) + + +# https://github.com/pypa/pip/blob/main/noxfile.py#L185C1-L250C59@nox.session +def vendoring(session: nox.Session) -> None: + session.install("vendoring~=1.2.0") + + parser = argparse.ArgumentParser(prog="nox -s vendoring") + parser.add_argument("--upgrade-all", action="store_true") + parser.add_argument("--upgrade", action="append", default=[]) + parser.add_argument("--skip", action="append", default=[]) + args = parser.parse_args(session.posargs) + + if not (args.upgrade or args.upgrade_all): + session.run("vendoring", "sync", "-v") + return + + def pinned_requirements(path: Path) -> Iterator[Tuple[str, str]]: + for line in path.read_text().splitlines(keepends=False): + one, sep, two = line.partition("==") + if not sep: + continue + name = one.strip() + version = two.split("#", 1)[0].strip() + if name and version: + yield name, version + + vendor_txt = Path("maya_umbrella/_vendor/vendor.txt") + for name, old_version in pinned_requirements(vendor_txt): + if name in args.skip: + continue + if args.upgrade and name not in args.upgrade: + continue + + # update requirements.txt + session.run("vendoring", "update", ".", name) + + # get the updated version + new_version = old_version + for inner_name, inner_version in pinned_requirements(vendor_txt): + if inner_name == name: + # this is a dedicated assignment, to make lint happy + new_version = inner_version + break + else: + session.error(f"Could not find {name} in {vendor_txt}") + + # check if the version changed. + if new_version == old_version: + continue # no change, nothing more to do here. + + # synchronize the contents + session.run("vendoring", "sync", ".") + + # Determine the correct message + message = f"Upgrade {name} to {new_version}" + + # Write our news fragment + news_file = Path("news") / (name + ".vendor.rst") + news_file.write_text(message + "\n") # "\n" appeases end-of-line-fixer + + # Commit the changes + # release.commit_file(session, ".", message=message) diff --git a/nox_actions/run_maya.py b/nox_actions/run_maya.py new file mode 100644 index 0000000..15f5aa2 --- /dev/null +++ b/nox_actions/run_maya.py @@ -0,0 +1,88 @@ +# Import built-in modules +import argparse +import os +import sys + +# Import third-party modules +import nox +from nox_actions.utils import PACKAGE_NAME +from nox_actions.utils import ROOT +from nox_actions.utils import _assemble_env_paths +import requests + + +# Ensure maya_umbrella is importable. +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +# Import local modules +from maya_umbrella.filesystem import get_maya_install_root + + +def run_maya(session: nox.Session): + parser = argparse.ArgumentParser(prog="nox -s maya") + parser.add_argument("maya_version", type=int) + parser.add_argument("--test", action="store_true", default=False) + parser.add_argument("--standalone", action="store_true", default=False) + parser.add_argument("--install-root", type=str, default=None) + parser.add_argument("--pattern", type=str) + args = parser.parse_args(session.posargs) + maya_version = str(args.maya_version) + session.install("requests") + maya_root = get_maya_install_root(maya_version) + standalone_runner = os.path.join(ROOT, "run_maya_standalone.py") + if maya_root: + maya_bin_root = os.path.join(maya_root, "bin") + maya_exe_root = os.path.join(maya_bin_root, "maya.exe") + mayapy = os.path.join(maya_bin_root, "mayapy.exe") + if args.test: + test_runner = os.path.join(ROOT, "tests", "_test_runner.py") + temp_dir = os.path.join(session._runner.envdir, "test", "site-packages") + pip_py_name = "get-pip.py" + dev_dir = os.path.join(session._runner.envdir, "dev") + get_pip_py = os.path.join(dev_dir, pip_py_name) + for path in (temp_dir, dev_dir): + os.makedirs(path, exist_ok=True) + get_pip_url = "https://bootstrap.pypa.io/get-pip.py" + if args.maya_version <= 2020: + get_pip_url = "https://bootstrap.pypa.io/pip/2.7/get-pip.py" + r = requests.get(get_pip_url) # create HTTP response object + with open(get_pip_py, "wb") as f: + f.write(r.content) + session.run_install(mayapy, get_pip_py) + session.run_install( + mayapy, + "-m", + "pip", + "install", + "--ignore-installed", + "pytest", + "pytest-cov", + "pytest-mock", + "--target", + temp_dir, + ) + test_root = os.path.join(ROOT, "tests") + session.run( + mayapy, + test_runner, + f"--cov={PACKAGE_NAME}", + f"--rootdir={test_root}", + env={"PYTHONPATH": f"{ROOT};{temp_dir}"}, + ) + + elif args.standalone: + session.run( + mayapy, + standalone_runner, + args.pattern, + env={"PYTHONPATH": ROOT}, + ) + else: + # Launch maya + print(_assemble_env_paths(ROOT, os.path.join(ROOT, "maya"))) + session.run( + maya_exe_root, + env={ + "PYTHONPATH": _assemble_env_paths(ROOT, os.path.join(ROOT, "maya")), + "MAYA_UMBRELLA_LOG_LEVEL": "DEBUG", + }, + ) diff --git a/nox_actions/run_test.py b/nox_actions/run_test.py new file mode 100644 index 0000000..8ff97a0 --- /dev/null +++ b/nox_actions/run_test.py @@ -0,0 +1,16 @@ +# Import built-in modules +import os + +# Import third-party modules +import nox +from nox_actions.utils import PACKAGE_NAME +from nox_actions.utils import ROOT + + +def pytest(session: nox.Session) -> None: + session.install("pytest", "pytest_cov", "pytest_mock") + test_root = os.path.join(ROOT, "tests") + session.run("pytest", f"--cov={PACKAGE_NAME}", + "--cov-report=xml:coverage.xml", + f"--rootdir={test_root}", + env={"PYTHONPATH": ROOT}, ) diff --git a/nox_actions/utils.py b/nox_actions/utils.py new file mode 100644 index 0000000..9f9e189 --- /dev/null +++ b/nox_actions/utils.py @@ -0,0 +1,10 @@ +# Import built-in modules +import os + + +PACKAGE_NAME = "maya_umbrella" +ROOT = os.path.dirname(os.path.dirname(__file__)) + + +def _assemble_env_paths(*paths): + return ";".join(paths)