Skip to content

Commit

Permalink
Merge pull request #153 from sbidoul/pre-sync-command
Browse files Browse the repository at this point in the history
Add support for pre-sync commands
  • Loading branch information
sbidoul authored Jul 2, 2024
2 parents 44419fa + 9334813 commit 7887530
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/152.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for pre-sync commands.
9 changes: 9 additions & 0 deletions src/pip_deepfreeze/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
)
Expand Down
4 changes: 4 additions & 0 deletions src/pip_deepfreeze/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""\
Expand All @@ -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",
Expand All @@ -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(
[
Expand Down

0 comments on commit 7887530

Please sign in to comment.