asta-code-execution 2.0.0__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/PKG-INFO +2 -2
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/pyproject.toml +2 -2
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/process_backend.py +100 -51
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/LICENSE +0 -0
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/README.md +0 -0
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/__init__.py +0 -0
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/executor.py +0 -0
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/ipython_session.py +0 -0
- {asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/py.typed +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: asta-code-execution
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Add your description here
|
|
5
5
|
Author: Allen Institute for Artificial Intelligence
|
|
6
6
|
License-Expression: Apache-2.0
|
|
7
7
|
License-File: LICENSE
|
|
8
8
|
Requires-Dist: ipython>=9.9.0
|
|
9
|
-
Requires-Python: >=3.
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
11
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "asta-code-execution"
|
|
3
|
-
version = "2.0.
|
|
3
|
+
version = "2.0.1"
|
|
4
4
|
description = "Add your description here"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "Apache-2.0"
|
|
@@ -8,7 +8,7 @@ license-files = ["LICENSE"]
|
|
|
8
8
|
authors = [
|
|
9
9
|
{ name = "Allen Institute for Artificial Intelligence" }
|
|
10
10
|
]
|
|
11
|
-
requires-python = ">=3.
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
12
|
dependencies = [
|
|
13
13
|
"ipython>=9.9.0",
|
|
14
14
|
]
|
{asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/process_backend.py
RENAMED
|
@@ -9,6 +9,7 @@ import shutil
|
|
|
9
9
|
import subprocess
|
|
10
10
|
import sys
|
|
11
11
|
import tempfile
|
|
12
|
+
import threading
|
|
12
13
|
from collections.abc import Iterable
|
|
13
14
|
from pathlib import Path
|
|
14
15
|
from typing import Any
|
|
@@ -73,12 +74,60 @@ if __name__ == "__main__":
|
|
|
73
74
|
"""
|
|
74
75
|
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
_WINDOWS = os.name == "nt"
|
|
78
|
+
|
|
79
|
+
# Experiment code installs a package by running this command (a JSON list) with
|
|
80
|
+
# the package name appended. It is run without a shell, so it works the same on
|
|
81
|
+
# Windows.
|
|
82
|
+
INSTALL_COMMAND_ENV = "CODE_EXECUTION_INSTALL_COMMAND"
|
|
83
|
+
|
|
84
|
+
# Records what a sandbox venv was built with, so a venv shared by several
|
|
85
|
+
# backends is built once.
|
|
86
|
+
_SANDBOX_MARKER = ".code-execution-sandbox.json"
|
|
87
|
+
_SANDBOX_LOCK = threading.Lock()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _find_uv() -> str | None:
|
|
91
|
+
"""Return the path to ``uv`` if it is installed, else None.
|
|
92
|
+
|
|
93
|
+
``uv run`` and ``uv tool run`` set ``UV`` to the binary they were started
|
|
94
|
+
from, which finds uv even when it is not on PATH.
|
|
95
|
+
"""
|
|
96
|
+
uv = shutil.which("uv") or os.environ.get("UV")
|
|
97
|
+
return uv if uv and os.path.exists(uv) else None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _venv_bin(venv_path: Path) -> Path:
|
|
101
|
+
"""Return a venv's scripts directory: ``Scripts`` on Windows, ``bin`` elsewhere."""
|
|
102
|
+
return venv_path / ("Scripts" if _WINDOWS else "bin")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _venv_python(venv_path: Path) -> Path:
|
|
106
|
+
"""Return the path to a venv's Python interpreter."""
|
|
107
|
+
return _venv_bin(venv_path) / ("python.exe" if _WINDOWS else "python")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _pip_install(uv: str | None, python_bin: Path) -> list[str]:
|
|
111
|
+
"""Return the command that installs packages into the venv of ``python_bin``.
|
|
112
|
+
|
|
113
|
+
uv when it is installed, because it is much faster; otherwise the venv's own
|
|
114
|
+
pip, so the backend needs nothing beyond Python itself.
|
|
115
|
+
"""
|
|
116
|
+
if uv:
|
|
117
|
+
return [uv, "pip", "install", "--python", str(python_bin)]
|
|
118
|
+
return [str(python_bin), "-m", "pip", "install", "--disable-pip-version-check"]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _run(command: list[str]) -> None:
|
|
122
|
+
"""Run a setup command; on failure, raise with the command's own error output."""
|
|
123
|
+
# errors="replace": the output is in whatever encoding the tool chose, and a
|
|
124
|
+
# byte the locale codec rejects must not hide the real failure.
|
|
125
|
+
proc = subprocess.run(command, capture_output=True, text=True, errors="replace")
|
|
126
|
+
if proc.returncode != 0:
|
|
127
|
+
output = (proc.stderr or proc.stdout).strip()
|
|
128
|
+
raise RuntimeError(
|
|
129
|
+
f"Setting up the process backend's sandbox failed: {' '.join(command)}\n{output}"
|
|
130
|
+
)
|
|
82
131
|
|
|
83
132
|
|
|
84
133
|
# TODO: Transplant this implementation onto asta_sandbox.SandboxBase (making run_cell
|
|
@@ -92,7 +141,7 @@ class ProcessIPythonBackend:
|
|
|
92
141
|
Each backend instance lazily creates a *base sandbox venv* with a curated
|
|
93
142
|
set of packages (mirroring the Modal sandbox image). Individual
|
|
94
143
|
``run_cell`` invocations get a per-process temporary directory so that any
|
|
95
|
-
``
|
|
144
|
+
``pip install`` executed by experiment code does **not** mutate the
|
|
96
145
|
base venv.
|
|
97
146
|
"""
|
|
98
147
|
|
|
@@ -112,6 +161,7 @@ class ProcessIPythonBackend:
|
|
|
112
161
|
Path(sandbox_venv_path) if sandbox_venv_path else None
|
|
113
162
|
)
|
|
114
163
|
self._sandbox_ready = False
|
|
164
|
+
self._uv: str | None = None
|
|
115
165
|
self._owned_venv_dir: tempfile.TemporaryDirectory[str] | None = None
|
|
116
166
|
|
|
117
167
|
# -- lazy venv setup -----------------------------------------------------
|
|
@@ -120,7 +170,11 @@ class ProcessIPythonBackend:
|
|
|
120
170
|
"""Create the base sandbox venv if it doesn't already exist."""
|
|
121
171
|
if self._sandbox_ready and self._sandbox_venv_path is not None:
|
|
122
172
|
return self._sandbox_venv_path
|
|
173
|
+
# One build at a time: backends on several threads may share a venv.
|
|
174
|
+
with _SANDBOX_LOCK:
|
|
175
|
+
return self._build_sandbox()
|
|
123
176
|
|
|
177
|
+
def _build_sandbox(self) -> Path:
|
|
124
178
|
uv = _find_uv()
|
|
125
179
|
|
|
126
180
|
if self._sandbox_venv_path is None:
|
|
@@ -128,24 +182,7 @@ class ProcessIPythonBackend:
|
|
|
128
182
|
self._sandbox_venv_path = Path(self._owned_venv_dir.name)
|
|
129
183
|
|
|
130
184
|
venv_path = self._sandbox_venv_path
|
|
131
|
-
python_bin = venv_path
|
|
132
|
-
|
|
133
|
-
if not python_bin.exists():
|
|
134
|
-
subprocess.run(
|
|
135
|
-
[uv, "venv", str(venv_path), "--seed", "--python", sys.executable],
|
|
136
|
-
check=True,
|
|
137
|
-
capture_output=True,
|
|
138
|
-
text=True,
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
# Install curated packages
|
|
142
|
-
if self._packages:
|
|
143
|
-
subprocess.run(
|
|
144
|
-
[uv, "pip", "install", "--python", str(python_bin), *self._packages],
|
|
145
|
-
check=True,
|
|
146
|
-
capture_output=True,
|
|
147
|
-
text=True,
|
|
148
|
-
)
|
|
185
|
+
python_bin = _venv_python(venv_path)
|
|
149
186
|
|
|
150
187
|
# Install code_execution package so the sandbox runner can import it.
|
|
151
188
|
code_exec_pkg = Path(__file__).resolve().parent.parent.parent
|
|
@@ -156,13 +193,31 @@ class ProcessIPythonBackend:
|
|
|
156
193
|
# Running from pip-installed cli
|
|
157
194
|
version = importlib.metadata.version("asta-code-execution")
|
|
158
195
|
install_spec = [f"asta-code-execution=={version}"]
|
|
159
|
-
subprocess.run(
|
|
160
|
-
[uv, "pip", "install", "--python", str(python_bin), *install_spec],
|
|
161
|
-
check=True,
|
|
162
|
-
capture_output=True,
|
|
163
|
-
text=True,
|
|
164
|
-
)
|
|
165
196
|
|
|
197
|
+
# A venv another backend already built with the same contents is ready.
|
|
198
|
+
marker = venv_path / _SANDBOX_MARKER
|
|
199
|
+
built_with = json.dumps({"packages": self._packages, "code_execution": install_spec})
|
|
200
|
+
if python_bin.exists() and marker.is_file() and marker.read_text() == built_with:
|
|
201
|
+
self._uv = uv
|
|
202
|
+
self._sandbox_ready = True
|
|
203
|
+
return venv_path
|
|
204
|
+
|
|
205
|
+
if not python_bin.exists():
|
|
206
|
+
if uv:
|
|
207
|
+
_run([uv, "venv", str(venv_path), "--seed", "--python", sys.executable])
|
|
208
|
+
else:
|
|
209
|
+
_run([sys.executable, "-m", "venv", str(venv_path)])
|
|
210
|
+
|
|
211
|
+
install = _pip_install(uv, python_bin)
|
|
212
|
+
|
|
213
|
+
# Install curated packages
|
|
214
|
+
if self._packages:
|
|
215
|
+
_run([*install, *self._packages])
|
|
216
|
+
|
|
217
|
+
_run([*install, *install_spec])
|
|
218
|
+
marker.write_text(built_with)
|
|
219
|
+
|
|
220
|
+
self._uv = uv
|
|
166
221
|
self._sandbox_ready = True
|
|
167
222
|
return venv_path
|
|
168
223
|
|
|
@@ -170,7 +225,7 @@ class ProcessIPythonBackend:
|
|
|
170
225
|
def sandbox_python(self) -> str:
|
|
171
226
|
"""Return the path to the sandbox venv's Python interpreter."""
|
|
172
227
|
venv = self._ensure_sandbox()
|
|
173
|
-
return str(venv
|
|
228
|
+
return str(_venv_python(venv))
|
|
174
229
|
|
|
175
230
|
# -- cell execution ------------------------------------------------------
|
|
176
231
|
|
|
@@ -185,7 +240,7 @@ class ProcessIPythonBackend:
|
|
|
185
240
|
) -> dict[str, Any]:
|
|
186
241
|
"""Execute a code cell in an isolated subprocess and return normalized outputs."""
|
|
187
242
|
venv_path = self._ensure_sandbox()
|
|
188
|
-
python_bin = str(venv_path
|
|
243
|
+
python_bin = str(_venv_python(venv_path))
|
|
189
244
|
|
|
190
245
|
payload = {
|
|
191
246
|
"code_str": code_str,
|
|
@@ -200,8 +255,8 @@ class ProcessIPythonBackend:
|
|
|
200
255
|
child_env.update(self._env)
|
|
201
256
|
|
|
202
257
|
# Per-cell temp directory for any packages installed by experiment code.
|
|
203
|
-
#
|
|
204
|
-
#
|
|
258
|
+
# The command in INSTALL_COMMAND_ENV installs into this directory with
|
|
259
|
+
# `pip install --target`, keeping the base sandbox venv clean.
|
|
205
260
|
cell_tmp = tempfile.mkdtemp(prefix="cell_pkgs_")
|
|
206
261
|
try:
|
|
207
262
|
cell_install_dir = os.path.join(cell_tmp, "lib")
|
|
@@ -209,24 +264,17 @@ class ProcessIPythonBackend:
|
|
|
209
264
|
|
|
210
265
|
existing_pp = child_env.get("PYTHONPATH", "")
|
|
211
266
|
child_env["PYTHONPATH"] = (
|
|
212
|
-
f"{cell_install_dir}
|
|
267
|
+
f"{cell_install_dir}{os.pathsep}{existing_pp}" if existing_pp else cell_install_dir
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
install = _pip_install(self._uv, Path(python_bin))
|
|
271
|
+
child_env[INSTALL_COMMAND_ENV] = json.dumps(
|
|
272
|
+
[*install, "--target", cell_install_dir, "--quiet"]
|
|
213
273
|
)
|
|
214
274
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
os.makedirs(script_dir, exist_ok=True)
|
|
219
|
-
real_uv = _find_uv()
|
|
220
|
-
script_path = os.path.join(script_dir, "install-package")
|
|
221
|
-
with open(script_path, "w") as f:
|
|
222
|
-
f.write(f"""#!/bin/sh
|
|
223
|
-
# Install a Python package into the per-process temp directory.
|
|
224
|
-
exec "{real_uv}" pip install --target "{cell_install_dir}" --quiet "$@"
|
|
225
|
-
""")
|
|
226
|
-
os.chmod(script_path, 0o755)
|
|
227
|
-
|
|
228
|
-
venv_bin = str(venv_path / "bin")
|
|
229
|
-
child_env["PATH"] = f"{script_dir}:{venv_bin}:{child_env.get('PATH', '')}"
|
|
275
|
+
child_env["PATH"] = os.pathsep.join(
|
|
276
|
+
[str(_venv_bin(venv_path)), child_env.get("PATH", "")]
|
|
277
|
+
)
|
|
230
278
|
child_env["VIRTUAL_ENV"] = str(venv_path)
|
|
231
279
|
|
|
232
280
|
try:
|
|
@@ -235,6 +283,7 @@ exec "{real_uv}" pip install --target "{cell_install_dir}" --quiet "$@"
|
|
|
235
283
|
input=json.dumps(payload),
|
|
236
284
|
capture_output=True,
|
|
237
285
|
text=True,
|
|
286
|
+
errors="replace",
|
|
238
287
|
timeout=timeout_s,
|
|
239
288
|
cwd=self._cwd,
|
|
240
289
|
env=child_env,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{asta_code_execution-2.0.0 → asta_code_execution-2.0.1}/src/code_execution/ipython_session.py
RENAMED
|
File without changes
|
|
File without changes
|