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

Support windows with juv run #54

Merged
merged 5 commits into from
Dec 4, 2024
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
30 changes: 22 additions & 8 deletions src/juv/_run_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@

from uv import find_uv_bin

IS_WINDOWS = sys.platform.startswith("win")


def run(script: str, args: list[str]) -> None:
process = subprocess.Popen( # noqa: S603
[os.fsdecode(find_uv_bin()), *args],
stdin=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr,
preexec_fn=os.setsid, # noqa: PLW1509
)
if not IS_WINDOWS:
process = subprocess.Popen( # noqa: S603
[os.fsdecode(find_uv_bin()), *args],
stdin=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr,
preexec_fn=os.setsid, # noqa: PLW1509
)
else:
process = subprocess.Popen( # noqa: S603
[os.fsdecode(find_uv_bin()), *args],
stdin=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
)

assert process.stdin is not None # noqa: S101
process.stdin.write(script.encode())
Expand All @@ -25,6 +36,9 @@ def run(script: str, args: list[str]) -> None:
try:
process.wait()
except KeyboardInterrupt:
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
if not IS_WINDOWS:
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
else:
os.kill(process.pid, signal.SIGTERM)
finally:
process.wait()
19 changes: 14 additions & 5 deletions src/juv/_run_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ def as_with_arg(self) -> str:
juv_data_dir = Path(user_data_dir("juv"))
juv_data_dir.mkdir(parents=True, exist_ok=True)

temp_dir = tempfile.TemporaryDirectory(dir=juv_data_dir)
# Custom TemporaryDirectory for Python < 3.10
# TODO: Use `ignore_cleanup_errors=True` in Python 3.10+
class TemporaryDirectoryIgnoreErrors(tempfile.TemporaryDirectory):
def cleanup(self):
try:
super().cleanup()
except Exception:
pass # Ignore cleanup errors

temp_dir = TemporaryDirectory(dir=juv_data_dir)
merged_dir = Path(temp_dir.name)

def handle_termination(signum, frame):
Expand Down Expand Up @@ -129,7 +138,7 @@ def handle_termination(signum, frame):
version = importlib.metadata.version("jupyterlab")
print("JUV_MANGED=" + "jupyterlab" + "," + version, file=sys.stderr)

sys.argv = ["jupyter-lab", "{notebook}", *{args}]
sys.argv = ["jupyter-lab", r"{notebook}", *{args}]
main()
"""

Expand All @@ -148,7 +157,7 @@ def handle_termination(signum, frame):
version = importlib.metadata.version("notebook")
print("JUV_MANGED=" + "notebook" + "," + version, file=sys.stderr)

sys.argv = ["jupyter-notebook", "{notebook}", *{args}]
sys.argv = ["jupyter-notebook", r"{notebook}", *{args}]
main()
"""

Expand All @@ -167,7 +176,7 @@ def handle_termination(signum, frame):
version = importlib.metadata.version("notebook")
print("JUV_MANGED=" + "notebook" + "," + version, file=sys.stderr)

sys.argv = ["jupyter-notebook", "{notebook}", *{args}]
sys.argv = ["jupyter-notebook", r"{notebook}", *{args}]
manzt marked this conversation as resolved.
Show resolved Hide resolved
main()
"""

Expand All @@ -187,7 +196,7 @@ def handle_termination(signum, frame):
print("JUV_MANGED=" + "nbclassic" + "," + version, file=sys.stderr)

os.environ["JUPYTER_DATA_DIR"] = str(merged_dir)
sys.argv = ["jupyter-nbclassic", "{notebook}", *{args}]
sys.argv = ["jupyter-nbclassic", r"{notebook}", *{args}]
main()
"""

Expand Down
35 changes: 22 additions & 13 deletions tests/test_juv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os
import pathlib
import re
Expand All @@ -21,6 +22,14 @@
SELF_DIR = pathlib.Path(__file__).parent


# Custom TemporaryDirectory for Python < 3.10
# TODO: Use `ignore_cleanup_errors=True` in Python 3.10+ # noqa: TD002, TD003
class TemporaryDirectoryIgnoreErrors(tempfile.TemporaryDirectory):
def cleanup(self) -> None:
with contextlib.suppress(Exception):
super().cleanup()


def invoke(args: list[str], uv_python: str = "3.13") -> Result:
return CliRunner().invoke(
cli,
Expand Down Expand Up @@ -810,7 +819,7 @@ def test_stamp(
) -> None:
# we need to run these tests in this folder because it uses the git history

with tempfile.TemporaryDirectory(dir=SELF_DIR) as tmpdir:
with TemporaryDirectoryIgnoreErrors(dir=SELF_DIR) as tmpdir:
tmp_path = pathlib.Path(tmpdir)
monkeypatch.chdir(tmp_path)

Expand Down Expand Up @@ -840,7 +849,7 @@ def test_stamp_script(
) -> None:
# we need to run these tests in this folder because it uses the git history

with tempfile.TemporaryDirectory(dir=SELF_DIR) as tmpdir:
with TemporaryDirectoryIgnoreErrors(dir=SELF_DIR) as tmpdir:
tmp_path = pathlib.Path(tmpdir)
monkeypatch.chdir(tmp_path)

Expand All @@ -851,11 +860,11 @@ def test_stamp_script(
# ///


def main() -> None:
print("Hello from foo.py!")
if __name__ == "__main__":
def main() -> None: |
print("Hello from foo.py!") |
|
|
if __name__ == "__main__": |
manzt marked this conversation as resolved.
Show resolved Hide resolved
main()
""")
result = invoke(["stamp", "foo.py", "--date", "2006-01-02"])
Expand All @@ -874,11 +883,11 @@ def main() -> None:
# ///


def main() -> None:
print("Hello from foo.py!")
if __name__ == "__main__":
def main() -> None: |
print("Hello from foo.py!") |
|
|
if __name__ == "__main__": |
main()
""")

Expand All @@ -889,7 +898,7 @@ def test_stamp_clear(
) -> None:
# we need to run these tests in this folder because it uses the git history

with tempfile.TemporaryDirectory(dir=SELF_DIR) as tmpdir:
with TemporaryDirectoryIgnoreErrors(dir=SELF_DIR) as tmpdir:
tmp_path = pathlib.Path(tmpdir)
monkeypatch.chdir(tmp_path)

Expand Down
Loading