|
| 1 | +# NOX: Noxfile checks |
| 2 | +## NOXxxx: noxfile checks |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import ast |
| 7 | +import dataclasses |
| 8 | +import re |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +from .._compat import tomllib |
| 12 | +from .._compat.importlib.resources.abc import Traversable |
| 13 | +from . import mk_url |
| 14 | + |
| 15 | +REGEX = re.compile( |
| 16 | + r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$" |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@dataclasses.dataclass(frozen=True, kw_only=True, slots=True, eq=False) |
| 21 | +class NoxfileInfo: |
| 22 | + module: ast.Module |
| 23 | + shebang: str |
| 24 | + script: dict[str, Any] |
| 25 | + |
| 26 | + __hash__ = None # type: ignore[assignment] |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def from_str(cls, content: str) -> NoxfileInfo: |
| 30 | + module = ast.parse(content, filename="noxfile.py") |
| 31 | + shebang_match = re.match(r"^#!.*\n", content) |
| 32 | + shebang = shebang_match.group(0).strip() if shebang_match else "" |
| 33 | + script = _load_script_block(content) |
| 34 | + return cls(module=module, shebang=shebang, script=script) |
| 35 | + |
| 36 | + def __eq__(self, other: object) -> bool: |
| 37 | + if not isinstance(other, NoxfileInfo): |
| 38 | + return NotImplemented |
| 39 | + |
| 40 | + ast_equal = ast.dump(self.module, include_attributes=True) == ast.dump( |
| 41 | + other.module, include_attributes=True |
| 42 | + ) |
| 43 | + return ( |
| 44 | + self.shebang == other.shebang and self.script == other.script and ast_equal |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def _load_script_block(content: str, /) -> dict[str, Any]: |
| 49 | + name = "script" |
| 50 | + matches = list(filter(lambda m: m.group("type") == name, REGEX.finditer(content))) |
| 51 | + |
| 52 | + if not matches: |
| 53 | + return {} |
| 54 | + |
| 55 | + if len(matches) > 1: |
| 56 | + msg = f"Multiple {name} blocks found" |
| 57 | + raise ValueError(msg) |
| 58 | + |
| 59 | + content = "".join( |
| 60 | + line[2:] if line.startswith("# ") else line[1:] |
| 61 | + for line in matches[0].group("content").splitlines(keepends=True) |
| 62 | + ) |
| 63 | + return tomllib.loads(content) |
| 64 | + |
| 65 | + |
| 66 | +def noxfile(root: Traversable) -> NoxfileInfo | None: |
| 67 | + """ |
| 68 | + Returns the shabang line (or empty string if missing), the noxfile script block, and the AST of the noxfile.py. |
| 69 | + Returns None if noxfile.py is not present. |
| 70 | + """ |
| 71 | + |
| 72 | + noxfile_path = root.joinpath("noxfile.py") |
| 73 | + if not noxfile_path.is_file(): |
| 74 | + return None |
| 75 | + |
| 76 | + with noxfile_path.open("r", encoding="utf-8") as f: |
| 77 | + return NoxfileInfo.from_str(f.read()) |
| 78 | + |
| 79 | + |
| 80 | +class Noxfile: |
| 81 | + family = "noxfile" |
| 82 | + requires = {"PY007"} |
| 83 | + url = mk_url("tasks") |
| 84 | + |
| 85 | + |
| 86 | +class NOX101(Noxfile): |
| 87 | + "Sets minimum nox version" |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def check(noxfile: NoxfileInfo | None) -> bool | None: |
| 91 | + """Set a minimum nox version: |
| 92 | +
|
| 93 | + ```python |
| 94 | + nox.needs_version = "2025.10.14" |
| 95 | + ``` |
| 96 | + """ |
| 97 | + |
| 98 | + if noxfile is None: |
| 99 | + return None |
| 100 | + |
| 101 | + for statement in noxfile.module.body: |
| 102 | + if isinstance(statement, ast.Assign): |
| 103 | + for target in statement.targets: |
| 104 | + match target: |
| 105 | + case ast.Attribute( |
| 106 | + value=ast.Name(id="nox"), attr="needs_version" |
| 107 | + ): |
| 108 | + return True |
| 109 | + |
| 110 | + return False |
| 111 | + |
| 112 | + |
| 113 | +class NOX102(Noxfile): |
| 114 | + "Sets venv backend" |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def check(noxfile: NoxfileInfo | None | None) -> bool | None: |
| 118 | + """ |
| 119 | + The default venv backend should be set, ideally to `uv|virtualenv`: |
| 120 | +
|
| 121 | + ```python |
| 122 | + nox.options.default_venv_backend = "uv|virtualenv" |
| 123 | + ``` |
| 124 | + """ |
| 125 | + if noxfile is None: |
| 126 | + return None |
| 127 | + |
| 128 | + for statement in noxfile.module.body: |
| 129 | + if isinstance(statement, ast.Assign): |
| 130 | + for target in statement.targets: |
| 131 | + match target: |
| 132 | + case ast.Attribute( |
| 133 | + value=ast.Attribute( |
| 134 | + value=ast.Name(id="nox"), attr="options" |
| 135 | + ), |
| 136 | + attr="default_venv_backend", |
| 137 | + ): |
| 138 | + return True |
| 139 | + |
| 140 | + return False |
| 141 | + |
| 142 | + |
| 143 | +class NOX103(Noxfile): |
| 144 | + "Set default per session instead of session list" |
| 145 | + |
| 146 | + @staticmethod |
| 147 | + def check(noxfile: NoxfileInfo | None) -> bool | None: |
| 148 | + """ |
| 149 | + You should use `default=` in each session instead of setting a global list. |
| 150 | + """ |
| 151 | + if noxfile is None: |
| 152 | + return None |
| 153 | + |
| 154 | + for statement in noxfile.module.body: |
| 155 | + if isinstance(statement, ast.Assign): |
| 156 | + for target in statement.targets: |
| 157 | + match target: |
| 158 | + case ast.Attribute( |
| 159 | + value=ast.Attribute( |
| 160 | + value=ast.Name(id="nox"), attr="options" |
| 161 | + ), |
| 162 | + attr="sessions", |
| 163 | + ): |
| 164 | + return False |
| 165 | + |
| 166 | + return True |
| 167 | + |
| 168 | + |
| 169 | +class NOX201(Noxfile): |
| 170 | + "Set a script block with dependencies in your noxfile" |
| 171 | + |
| 172 | + @staticmethod |
| 173 | + def check(noxfile: NoxfileInfo | None) -> bool | None: |
| 174 | + """ |
| 175 | + You should have a script block with nox in it, for example: |
| 176 | +
|
| 177 | + ```toml |
| 178 | + # /// script |
| 179 | + dependencies = ["nox"] |
| 180 | + # /// |
| 181 | + ``` |
| 182 | + """ |
| 183 | + if noxfile is None: |
| 184 | + return None |
| 185 | + match noxfile.script: |
| 186 | + case {"dependencies": list()}: |
| 187 | + return True |
| 188 | + return False |
| 189 | + |
| 190 | + |
| 191 | +class NOX202(Noxfile): |
| 192 | + "Has a shebang line" |
| 193 | + |
| 194 | + @staticmethod |
| 195 | + def check(noxfile: NoxfileInfo | None) -> bool | None: |
| 196 | + """ |
| 197 | + You should have a shabang line at the top of your noxfile.py, for example: |
| 198 | +
|
| 199 | + ```python |
| 200 | + #!/usr/bin/env -S uv run --script |
| 201 | + ``` |
| 202 | + """ |
| 203 | + if noxfile is None: |
| 204 | + return None |
| 205 | + return bool(noxfile.shebang) |
| 206 | + |
| 207 | + |
| 208 | +class NOX203(Noxfile): |
| 209 | + "Provide a main block to run nox" |
| 210 | + |
| 211 | + @staticmethod |
| 212 | + def check(noxfile: NoxfileInfo | None) -> bool | None: |
| 213 | + """ |
| 214 | + You should have a main block at the bottom of your noxfile.py, for example: |
| 215 | +
|
| 216 | + ```python |
| 217 | + if __name__ == "__main__": |
| 218 | + nox.main() |
| 219 | + ``` |
| 220 | + """ |
| 221 | + if noxfile is None: |
| 222 | + return None |
| 223 | + |
| 224 | + for statement in noxfile.module.body: |
| 225 | + if isinstance(statement, ast.If): |
| 226 | + match statement.test: |
| 227 | + case ast.Compare( |
| 228 | + left=ast.Name(id="__name__"), |
| 229 | + ops=[ast.Eq()], |
| 230 | + comparators=[ast.Constant(value="__main__")], |
| 231 | + ): |
| 232 | + return True |
| 233 | + |
| 234 | + return False |
| 235 | + |
| 236 | + |
| 237 | +def repo_review_checks( |
| 238 | + list_all: bool = True, noxfile: tuple[str, dict[str, Any], ast.Module] | None = None |
| 239 | +) -> dict[str, Noxfile]: |
| 240 | + if not list_all and noxfile is None: |
| 241 | + return {} |
| 242 | + return {p.__name__: p() for p in Noxfile.__subclasses__()} |
0 commit comments