File size: 4,508 Bytes
7ec48b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""Build-time patch: strip the coding surfaces out of an extracted Blender tree.

    nocode-patch.py /opt/blender/5.0/scripts

Sid's rule for this task: "the coding tools have to be disabled" — the agent
must do everything through the modelling UI, the way the tutorial does. Blender
has no switch for that, so the image removes the Python execution paths:

  * the Python Console's operators (console.execute / autocomplete / ...) are
    Python-defined in bl_operators/console.py and back onto
    modules/_console_python.py + _console_shell.py + _bl_console_utils/ — all
    deleted, so a Console area (if one is ever opened) cannot run anything;
  * the Text Editor's "Run Script" (text.run_script is a C operator and cannot
    be removed) is stripped from the header button and the Text menu in
    bl_ui/space_text.py, and the startup guard (nocode_guard.py) removes its
    Alt+P keymap entry; with Developer Extras off, F3 menu search cannot reach
    an operator that is in no menu;
  * nocode_guard.py (installed into scripts/startup/) deletes the "Scripting"
    workspace on load and keeps converting any Console / Text Editor area back
    to the Info editor; Auto Run Python Scripts is off (userpref, plus -Y on
    the command line) so file-embedded scripts and Python drivers never run.

The script asserts every edit it makes; the docker build fails if the Blender
layout changes under it.
"""
import pathlib
import shutil
import sys

scripts = pathlib.Path(sys.argv[1])
assert (scripts / "startup" / "bl_ui").is_dir(), scripts

# 1. Python console: operators + backend.
# Blender 4.0 names these console_python.py / console_shell.py (no underscore,
# no _bl_console_utils package); 5.0 uses the underscored names. Remove whichever
# exist and insist the operator module and the python backend were found.
removed = []
for rel in [
    "startup/bl_operators/console.py",
    "modules/_console_python.py", "modules/console_python.py",
    "modules/_console_shell.py", "modules/console_shell.py",
]:
    p = scripts / rel
    if p.is_file():
        p.unlink()
        removed.append(rel)
assert "startup/bl_operators/console.py" in removed, "console operators not found"
assert any(r.endswith("console_python.py") for r in removed), "console_python backend not found"
p = scripts / "modules" / "_bl_console_utils"
if p.is_dir():
    shutil.rmtree(p)
    removed.append("modules/_bl_console_utils/")

# bl_operators/__init__.py imports the console module by name: drop it.
init = scripts / "startup" / "bl_operators" / "__init__.py"
src = init.read_text()
assert "console," in src or '"console"' in src or "console\n" in src, "bl_operators/__init__.py: console import not found"
lines = [l for l in src.splitlines(keepends=True) if l.strip() not in ("console,", '"console",')]
assert len(lines) == len(src.splitlines(keepends=True)) - 1, "expected exactly one console entry in bl_operators/__init__.py"
init.write_text("".join(lines))

# 2. Text editor: no "Run Script" button / menu entry.
st = scripts / "startup" / "bl_ui" / "space_text.py"
src = st.read_text()
n = src.count("text.run_script")
assert n >= 1, f"space_text.py: expected run_script references, found {n}"
out = []
for l in src.splitlines(keepends=True):
    if "text.run_script" in l:
        indent = l[: len(l) - len(l.lstrip())]
        out.append(f"{indent}pass  # nocode: run_script removed\n")
    else:
        out.append(l)
st.write_text("".join(out))
assert "text.run_script" not in st.read_text()

# 3. Console header: say why it is dead (the area can still be opened for a
#    moment before the guard converts it).
sc = scripts / "startup" / "bl_ui" / "space_console.py"
src = sc.read_text()
marker = "class CONSOLE_HT_header(Header):"
assert marker in src, "space_console.py: header class not found"
src = src.replace(
    "    def draw(self, context):\n        layout = self.layout\n",
    "    def draw(self, context):\n        layout = self.layout\n"
    "        layout.label(text=\"Python console is disabled in this environment\", icon='ERROR')\n",
    1,
)
assert "Python console is disabled" in src
sc.write_text(src)

# 4. Install the runtime guard.
guard_src = pathlib.Path(__file__).with_name("nocode_guard.py")
assert guard_src.is_file(), guard_src
shutil.copy(guard_src, scripts / "startup" / "nocode_guard.py")

print("nocode-patch: removed", ", ".join(removed), "; patched space_text.py, space_console.py, bl_operators/__init__.py; installed startup/nocode_guard.py")