run_intents: refuse Blender user directories that are a person's own, or that Blender is not using
The guard only checked that the three variables were non-empty. tools/private_dirs.py now refuses a variable set inside %APPDATA%/Blender Foundation, ~/.config/blender or the macOS folder, and a Blender whose own answer for CONFIG, EXTENSIONS or SCRIPTS is not under the variable. (Soul finding run-intents-guard-checks-non-empty, PR 22.) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
0921bec89b
commit
64ffc14e0a
2 changed files with 51 additions and 5 deletions
44
surfaces/blender/tools/private_dirs.py
Normal file
44
surfaces/blender/tools/private_dirs.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
"""Whether a Blender about to be driven by a script has user directories of its own. Stdlib only.
|
||||
|
||||
A script that enables add-ons or saves preferences in a person's own Blender folders changes her Blender. The
|
||||
three variables below point Blender at other folders; BLENDER_USER_RESOURCE_PATH does not (Blender 5.3 ignores it).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Callable, Mapping
|
||||
|
||||
VARIABLES = {"BLENDER_USER_CONFIG": "CONFIG", "BLENDER_USER_EXTENSIONS": "EXTENSIONS", "BLENDER_USER_SCRIPTS": "SCRIPTS"}
|
||||
|
||||
|
||||
def own_blender_roots(environ: Mapping[str, str], home: str) -> list[Path]:
|
||||
"""Where a person's own Blender keeps its folders on Windows, Linux and macOS, for this environment."""
|
||||
roots = [Path(home) / ".config" / "blender", Path(home) / "Library" / "Application Support" / "Blender"]
|
||||
for variable in ("APPDATA", "XDG_CONFIG_HOME"):
|
||||
if environ.get(variable):
|
||||
roots.append(Path(environ[variable]) / ("Blender Foundation" if variable == "APPDATA" else "blender"))
|
||||
return roots
|
||||
|
||||
|
||||
def _inside(path: Path, root: Path) -> bool:
|
||||
a, b = os.path.normcase(os.path.abspath(path)), os.path.normcase(os.path.abspath(root))
|
||||
return a == b or a.startswith(b + os.sep)
|
||||
|
||||
|
||||
def refusal(environ: Mapping[str, str], home: str, resource: Callable[[str], str] | None = None) -> str | None:
|
||||
"""None when the three variables are set to folders outside every own-Blender root, and, given Blender's
|
||||
own answer (resource('CONFIG') and so on, bpy.utils.user_resource), Blender is really using them; otherwise
|
||||
the sentence to refuse with."""
|
||||
for variable, kind in VARIABLES.items():
|
||||
value = environ.get(variable)
|
||||
if not value:
|
||||
return f"{variable} is not set: run in private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories"
|
||||
for root in own_blender_roots(environ, home):
|
||||
if _inside(Path(value), root):
|
||||
return f"{variable} is {value}, inside a person's own Blender folder ({root}): point it at a scratch directory"
|
||||
if resource is not None:
|
||||
used = resource(kind)
|
||||
if not used or not _inside(Path(used), Path(value)):
|
||||
return f"Blender uses {used or 'no folder'} for {kind}, not {variable} ({value}): refusing to touch it"
|
||||
return None
|
||||
|
|
@ -8,8 +8,8 @@ rows are checked as in a live session, and each gets a receipt. No CultMesh node
|
|||
written to <receipts.json> (default: the recipe's name with .receipts.json) and nowhere else, so nothing here
|
||||
shows in a person's Agents panel. This script is the human of its own Blender: it enables the host add-ons the
|
||||
recipe names, reloads host actions once, and opens and saves the .blend. It refuses to start unless
|
||||
BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS are set, so it never reads or writes a
|
||||
person's own Blender folders (docs/install.md).
|
||||
BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS are set to folders outside a person's own
|
||||
Blender folders and Blender is really using them (tools/private_dirs.py), so it never reads or writes hers.
|
||||
|
||||
The recipe:
|
||||
|
||||
|
|
@ -41,9 +41,11 @@ def fail(message: str) -> None:
|
|||
|
||||
|
||||
def main() -> int:
|
||||
for name in ("BLENDER_USER_CONFIG", "BLENDER_USER_EXTENSIONS", "BLENDER_USER_SCRIPTS"):
|
||||
if not os.environ.get(name):
|
||||
fail(f"{name} is not set: run in private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories")
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
import private_dirs
|
||||
refused = private_dirs.refusal(os.environ, os.path.expanduser("~"), lambda kind: bpy.utils.user_resource(kind))
|
||||
if refused:
|
||||
fail(refused)
|
||||
arguments = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
|
||||
if not arguments:
|
||||
fail("usage: blender --background --factory-startup --python run_intents.py -- <recipe.json> [<receipts.json>]")
|
||||
|
|
|
|||
Loading…
Reference in a new issue