Repository: sbidoul/pip-deepfreeze (language: python)

Issue:
Add `--pre-sync-command`



Discussion / hints:

Fix patch:
diff --git a/news/152.feature b/news/152.feature
new file mode 100644
index 0000000..1b912c4
--- /dev/null
+++ b/news/152.feature
@@ -0,0 +1,1 @@
+Add support for pre-sync commands.
diff --git a/src/pip_deepfreeze/__main__.py b/src/pip_deepfreeze/__main__.py
index 48d3a4d..2b62eb6 100644
--- a/src/pip_deepfreeze/__main__.py
+++ b/src/pip_deepfreeze/__main__.py
@@ -62,6 +62,14 @@ def sync(
             "If not specified, ask confirmation."
         ),
     ),
+    pre_sync_commands: List[str] = typer.Option(
+        [],
+        "--pre-sync-command",
+        help=(
+            "Command to run before the sync operation. "
+            "Can be specified multiple times."
+        ),
+    ),
     post_sync_commands: List[str] = typer.Option(
         [],
         "--post-sync-command",
@@ -89,6 +97,7 @@ def sync(
         extras=[canonicalize_name(extra) for extra in comma_split(extras)],
         uninstall_unneeded=uninstall_unneeded,
         project_root=ctx.obj.project_root,
+        pre_sync_commands=pre_sync_commands,
         post_sync_commands=post_sync_commands,
         installer=installer,
     )
diff --git a/src/pip_deepfreeze/sync.py b/src/pip_deepfreeze/sync.py
index 582ebbd..6a09abf 100644
--- a/src/pip_deepfreeze/sync.py
+++ b/src/pip_deepfreeze/sync.py
@@ -59,9 +59,13 @@ def sync(
     extras: List[NormalizedName],
     uninstall_unneeded: Optional[bool],
     project_root: Path,
+    pre_sync_commands: Sequence[str] = (),
     post_sync_commands: Sequence[str] = (),
     installer: Installer = Installer.pip,
 ) -> None:
+    # run pre-sync commands
+    run_commands(pre_sync_commands, project_root, "pre-sync")
+    # sync
     project_name = get_project_name(python, project_root)
     project_name_with_extras = make_project_name_with_extras(project_name, extras)
     constraints_path = _constraints_path(project_root)


Test patch:
diff --git a/tests/test_sync.py b/tests/test_sync.py
index f098c8f..3c7ec70 100644
--- a/tests/test_sync.py
+++ b/tests/test_sync.py
@@ -388,7 +388,7 @@ def test_sync_extras(virtualenv_python, testpkgs, tmp_path):
     assert "pkgc==0.0.2\n" in requirements_c_txt
 
 
-def test_post_sync_command(virtualenv_python, testpkgs, tmp_path):
+def test_pre_post_sync_command(virtualenv_python, testpkgs, tmp_path):
     (tmp_path / "constraints.txt").write_text(
         textwrap.dedent(
             f"""\
@@ -414,6 +414,8 @@ def test_post_sync_command(virtualenv_python, testpkgs, tmp_path):
             "--python",
             virtualenv_python,
             "sync",
+            "--pre-sync-command",
+            "echo pre-sync-cmd-1",
             "--post-sync-command",
             "echo post-sync-cmd-1",
             "--post-sync-command",
@@ -424,6 +426,7 @@ def test_post_sync_command(virtualenv_python, testpkgs, tmp_path):
         check=True,
         capture_output=True,
     )
+    assert res.stdout.startswith("pre-sync-cmd-1\n")
     assert res.stdout.endswith("post-sync-cmd-1\npost-sync-cmd-2\n")
     res = subprocess.run(
         [