Skip to content

Commit

Permalink
Fix: Support relative paths in the run command (#72)
Browse files Browse the repository at this point in the history
This addresses an issue where relative paths for dependencies were
broken due to changes in how file contents are handled. With this
update, temporary files (like the runtime script and lockfile) are now
properly cleaned up within the runtime script itself.
  • Loading branch information
manzt authored Jan 15, 2025
1 parent 0dedc41 commit 8dc7ea0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/juv/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
7 changes: 6 additions & 1 deletion src/juv/_run_managed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion src/juv/_run_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions src/juv/_run_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}:
Expand All @@ -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}:
Expand All @@ -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}:
Expand All @@ -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}:
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 8dc7ea0

Please sign in to comment.