Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Support relative paths in the run command #72

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading