Skip to content

Commit 77a5659

Browse files
authored
Add --clear flag to lock to clear lockfile metadata (#69)
Clears lockfile metadata from the Jupyter notebook with the addition of the `--clear` to the command.
1 parent 145a716 commit 77a5659

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/juv/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,22 @@ def remove(
461461

462462
@cli.command()
463463
@click.argument("file", type=click.Path(exists=True), required=True)
464+
@click.option("--clear", is_flag=True, help="Clear the lockfile contents.")
464465
def lock(
465466
*,
466467
file: str,
468+
clear: bool,
467469
) -> None:
468470
"""Update the notebooks's lockfile."""
469471
from ._lock import lock
470472

471473
try:
472-
lock(path=Path(file))
474+
lock(path=Path(file), clear=clear)
473475
path = os.path.relpath(Path(file).resolve(), Path.cwd())
474-
rich.print(f"Locked `[cyan]{path}[/cyan]`")
476+
if clear:
477+
rich.print(f"Cleared lockfile `[cyan]{path}[/cyan]`")
478+
else:
479+
rich.print(f"Locked `[cyan]{path}[/cyan]`")
475480
except RuntimeError as e:
476481
rich.print(e, file=sys.stderr)
477482
sys.exit(1)

src/juv/_lock.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
from ._uv import uv
1010

1111

12-
def lock(
13-
path: Path,
14-
) -> None:
12+
def lock(*, path: Path, clear: bool) -> None:
1513
notebook = jupytext.read(path, fmt="ipynb")
1614

15+
if clear:
16+
notebook.get("metadata", {}).pop("uv.lock", None)
17+
write_ipynb(notebook, path)
18+
return
19+
1720
cell = find(
1821
lambda cell: (
1922
cell["cell_type"] == "code"

tests/test_juv.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,3 +1221,38 @@ def test_tree_updates_lock(
12211221
[options]
12221222
exclude-newer = "2023-02-01T02:00:00Z"
12231223
""")
1224+
1225+
1226+
def test_clear_lock(
1227+
tmp_path: pathlib.Path,
1228+
monkeypatch: pytest.MonkeyPatch,
1229+
) -> None:
1230+
monkeypatch.chdir(tmp_path)
1231+
1232+
invoke(["init", "test.ipynb"])
1233+
invoke(["add", "test.ipynb", "attrs"])
1234+
invoke(["lock", "test.ipynb"])
1235+
assert jupytext.read(tmp_path / "test.ipynb").metadata.get("uv.lock") == snapshot("""\
1236+
version = 1
1237+
requires-python = ">=3.13"
1238+
1239+
[options]
1240+
exclude-newer = "2023-02-01T02:00:00Z"
1241+
1242+
[manifest]
1243+
requirements = [{ name = "attrs" }]
1244+
1245+
[[package]]
1246+
name = "attrs"
1247+
version = "22.2.0"
1248+
source = { registry = "https://pypi.org/simple" }
1249+
sdist = { url = "https://files.pythonhosted.org/packages/21/31/3f468da74c7de4fcf9b25591e682856389b3400b4b62f201e65f15ea3e07/attrs-22.2.0.tar.gz", hash = "sha256:c9227bfc2f01993c03f68db37d1d15c9690188323c067c641f1a35ca58185f99", size = 215900 }
1250+
wheels = [
1251+
{ url = "https://files.pythonhosted.org/packages/fb/6e/6f83bf616d2becdf333a1640f1d463fef3150e2e926b7010cb0f81c95e88/attrs-22.2.0-py3-none-any.whl", hash = "sha256:29e95c7f6778868dbd49170f98f8818f78f3dc5e0e37c0b1f474e3561b240836", size = 60018 },
1252+
]
1253+
""")
1254+
1255+
result = invoke(["lock", "test.ipynb", "--clear"])
1256+
assert result.exit_code == 0
1257+
assert result.stdout == snapshot("Cleared lockfile `test.ipynb`\n")
1258+
assert jupytext.read(tmp_path / "test.ipynb").metadata.get("uv.lock") is None

0 commit comments

Comments
 (0)