diff --git a/src/juv/_run.py b/src/juv/_run.py index d41d93f..e27a750 100644 --- a/src/juv/_run.py +++ b/src/juv/_run.py @@ -87,8 +87,19 @@ def run( # noqa: PLR0913 elif mode == "managed": from ._run_managed import run as run_managed - run_managed(script, args, str(path), lockfile_contents) + run_managed( + script=script, + args=args, + filename=str(path), + lockfile_contents=lockfile_contents, + dir=target.parent, + ) else: from ._run_replace import run as run_replace - run_replace(script, args, lockfile_contents) + run_replace( + script=script, + args=args, + lockfile_contents=lockfile_contents, + dir=target.parent, + ) diff --git a/src/juv/_run_managed.py b/src/juv/_run_managed.py index a473482..d997231 100644 --- a/src/juv/_run_managed.py +++ b/src/juv/_run_managed.py @@ -105,7 +105,11 @@ def display(url: str) -> None: def run( - script: str, args: list[str], filename: str, lockfile_contents: str | None + script: str, + args: list[str], + filename: str, + lockfile_contents: str | None, + dir: Path, # noqa: A002 ) -> None: console = Console() output_queue = Queue() @@ -114,6 +118,7 @@ def run( mode="w+", delete=True, suffix=".py", + dir=dir, encoding="utf-8", ) as f: lockfile = Path(f"{f.name}.lock") diff --git a/src/juv/_run_replace.py b/src/juv/_run_replace.py index 998c601..c8cbbc7 100644 --- a/src/juv/_run_replace.py +++ b/src/juv/_run_replace.py @@ -12,11 +12,12 @@ IS_WINDOWS = sys.platform.startswith("win") -def run(script: str, args: list[str], lockfile_contents: str | None) -> None: +def run(script: str, args: list[str], lockfile_contents: str | None, dir: Path) -> None: # noqa: A002 with tempfile.NamedTemporaryFile( mode="w+", delete=True, suffix=".py", + dir=dir, encoding="utf-8", ) as f: lockfile = Path(f"{f.name}.lock") diff --git a/src/juv/_run_template.py b/src/juv/_run_template.py index 3857631..14c0502 100644 --- a/src/juv/_run_template.py +++ b/src/juv/_run_template.py @@ -88,6 +88,20 @@ def update_uv_lock(notebook_path: str): update_uv_lock(r"{notebook}") """ +DELETE_RUN_SCRIPT_AND_LOCKFILE = """ +import os +from pathlib import Path + +def delete_run_script_and_lockfile(): + Path(str(__file__)).unlink(missing_ok=True) + lockfile_path = os.environ.get("JUV_LOCKFILE_PATH") + if not lockfile_path: + return + Path(lockfile_path).unlink(missing_ok=True) + +delete_run_script_and_lockfile() +""" + SETUP_JUPYTER_DATA_DIR = """ import tempfile @@ -157,6 +171,7 @@ def handle_termination(signum, frame): from jupyterlab.labapp import main {UPDATE_LOCKFILE} +{DELETE_RUN_SCRIPT_AND_LOCKFILE} {SETUP_JUPYTER_DATA_DIR} if {is_managed}: @@ -177,6 +192,7 @@ def handle_termination(signum, frame): from notebook.app import main {UPDATE_LOCKFILE} +{DELETE_RUN_SCRIPT_AND_LOCKFILE} {SETUP_JUPYTER_DATA_DIR} if {is_managed}: @@ -197,6 +213,7 @@ def handle_termination(signum, frame): from notebook.notebookapp import main {UPDATE_LOCKFILE} +{DELETE_RUN_SCRIPT_AND_LOCKFILE} {SETUP_JUPYTER_DATA_DIR} if {is_managed}: @@ -217,6 +234,7 @@ def handle_termination(signum, frame): from nbclassic.notebookapp import main {UPDATE_LOCKFILE} +{DELETE_RUN_SCRIPT_AND_LOCKFILE} {SETUP_JUPYTER_DATA_DIR} if {is_managed}: @@ -247,6 +265,7 @@ def prepare_run_script_and_uv_run_args( # noqa: PLR0913 args=jupyter_args, SETUP_JUPYTER_DATA_DIR=SETUP_JUPYTER_DATA_DIR, UPDATE_LOCKFILE=UPDATE_LOCKFILE.format(notebook=target), + DELETE_RUN_SCRIPT_AND_LOCKFILE=DELETE_RUN_SCRIPT_AND_LOCKFILE, is_managed=mode == "managed", ) args = [