diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bf4d185..a1e72f4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -100,7 +100,7 @@ repos: # * pyproject2conda - repo: . - rev: 20fa620bcb6427e160c0843104b7a20071430eec + rev: 34a73ece96f660ccad2b349dad4e5228afdb38cc hooks: - id: pyproject2conda-project alias: requirements diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 88f21c9..14e31d5 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -5,7 +5,7 @@ entry: pyproject2conda project language: python files: ^pyproject\.toml$ - args: ["--overwrite=force", "--verbose", "--custom-command=pre-commit"] + args: ["--overwrite=force", "--verbose"] pass_filenames: false additional_dependencies: [] minimum_pre_commit_version: "2.9.2" @@ -16,8 +16,7 @@ entry: pyproject2conda yaml language: python files: ^pyproject\.toml$ - args: - ["--group=dev", "--output=environment.yaml", "--custom-command=pre-commit"] + args: ["--group=dev", "--output=environment.yaml"] pass_filenames: false additional_dependencies: [] minimum_pre_commit_version: "2.9.2" @@ -28,12 +27,7 @@ entry: pyproject2conda requirements language: python files: ^pyproject\.toml$ - args: - [ - "--group=dev", - "--output=requirements-dev.in", - "--custom-command=pre-commit", - ] + args: ["--group=dev", "--output=requirements-dev.in"] pass_filenames: false additional_dependencies: [] minimum_pre_commit_version: "2.9.2" diff --git a/README.md b/README.md index b632527..e3bb930 100644 --- a/README.md +++ b/README.md @@ -81,9 +81,13 @@ environment file with the extra `dev-complete` rev: { version } # replace with current version hooks: - id: pyproject2conda-yaml - args: ["-e", "dev-complete", "-o", "environment-dev.yaml"] + args: ["-e", "dev-complete", "-o", "environment-dev.yaml", "-w", "force"] ``` +Note that if called from pre-commit (detected by the presence of `PRE_COMMIT` +environment variable), the default is to set `--custom-command="pre-commit"`. +You can explicitly pass in `--custom-command` to override this. + ## Quick start diff --git a/changelog.d/20250129_075247_wpk_pre_commit_hook.md b/changelog.d/20250129_075247_wpk_pre_commit_hook.md new file mode 100644 index 0000000..8fb73ad --- /dev/null +++ b/changelog.d/20250129_075247_wpk_pre_commit_hook.md @@ -0,0 +1,44 @@ + + + + + + +### Changed + +- Changed default of `--overwrite` to `force`. This simplifies using with + `pre-commit`. +- `--commit-command` defaults to `pre-commit` when run under pre-commit + + + + diff --git a/noxfile.py b/noxfile.py index a4b4017..eb6d1fb 100644 --- a/noxfile.py +++ b/noxfile.py @@ -151,7 +151,6 @@ class SessionParams(DataclassParser): ) # requirements - requirements_force: bool = False requirements_no_notify: bool = add_option( default=False, help="Skip notification of lock-compile", diff --git a/src/pyproject2conda/cli.py b/src/pyproject2conda/cli.py index e19769c..594f7e6 100644 --- a/src/pyproject2conda/cli.py +++ b/src/pyproject2conda/cli.py @@ -182,7 +182,7 @@ class Overwrite(str, Enum): check = "check" skip = "skip" - foce = "force" + force = "force" OVERWRITE_CLI = Annotated[ @@ -435,6 +435,9 @@ def _get_header_cmd( if custom_command is not None: return custom_command + if "PRE_COMMIT" in os.environ: + return "pre-commit" + if header is None: header = output is not None @@ -559,7 +562,7 @@ def yaml( sort: SORT_DEPENDENCIES_CLI = True, header: HEADER_CLI = None, custom_command: CUSTOM_COMMAND_CLI = None, - overwrite: OVERWRITE_CLI = Overwrite.check, + overwrite: OVERWRITE_CLI = Overwrite.force, verbose: VERBOSE_CLI = None, deps: DEPS_CLI = None, reqs: REQS_CLI = None, @@ -621,7 +624,7 @@ def requirements( sort: SORT_DEPENDENCIES_CLI = True, header: HEADER_CLI = None, custom_command: CUSTOM_COMMAND_CLI = None, - overwrite: OVERWRITE_CLI = Overwrite.check, + overwrite: OVERWRITE_CLI = Overwrite.force, verbose: VERBOSE_CLI = None, reqs: REQS_CLI = None, allow_empty: Annotated[bool, ALLOW_EMPTY_OPTION] = False, @@ -670,7 +673,7 @@ def project( sort: SORT_DEPENDENCIES_CLI = True, header: HEADER_CLI = None, custom_command: CUSTOM_COMMAND_CLI = None, - overwrite: OVERWRITE_CLI = Overwrite.check, + overwrite: OVERWRITE_CLI = Overwrite.force, verbose: VERBOSE_CLI = None, dry: DRY_CLI = False, pip_only: PIP_ONLY_CLI = False, @@ -828,7 +831,7 @@ def to_json( deps: DEPS_CLI = None, reqs: REQS_CLI = None, verbose: VERBOSE_CLI = None, - overwrite: OVERWRITE_CLI = Overwrite.check, + overwrite: OVERWRITE_CLI = Overwrite.force, ) -> None: """ Create json representation. diff --git a/tests/test_cli.py b/tests/test_cli.py index 1882868..ee15cbf 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -136,9 +136,7 @@ def test_create(fname, style, runner) -> None: check_result(result, expected) - cmd = " ".join([Path(sys.argv[0]).name] + sys.argv[1:]) - - expected = f"""\ + expected = """\ # # This file is autogenerated by pyproject2conda # with the following command: @@ -159,6 +157,7 @@ def test_create(fname, style, runner) -> None: """ result = do_run(runner, "yaml", "--header", filename=filename) + cmd = " ".join([Path(sys.argv[0]).name] + sys.argv[1:]) check_result(result, expected.format(cmd=cmd)) # custom command @@ -166,6 +165,13 @@ def test_create(fname, style, runner) -> None: check_result(result, expected.format(cmd="make hello")) + # check if have pre-commit variable + result = do_run( + runner, "yaml", "--header", filename=filename, env={"PRE_COMMIT": "1"} + ) + + check_result(result, expected.format(cmd="pre-commit")) + # -p flag expected = """\ channels: @@ -753,7 +759,7 @@ def test_json(fname, opt, runner) -> None: check_results_json(d / "hello.json", expected) orig_time = path.stat().st_mtime - do_run(runner, "json", "-o", str(path), filename=filename) + do_run(runner, "json", "-o", str(path), "--overwrite=check", filename=filename) check_results_json(d / "hello.json", expected) assert path.stat().st_mtime == orig_time @@ -806,8 +812,6 @@ def test_json(fname, opt, runner) -> None: str(d / "there.json"), opt, "dev", - "-w", - "force", filename=filename, ) @@ -859,7 +863,7 @@ def test_overwrite(filename, caplog) -> None: str(path), "-v", "-w", - "force", + "check", catch_exceptions=False, filename=filename, ) @@ -899,8 +903,6 @@ def test_overwrite(filename, caplog) -> None: "-o", str(d / "out.txt"), "-v", - "-w", - "force", catch_exceptions=False, filename=filename, ) diff --git a/tests/test_config.py b/tests/test_config.py index c16e9c4..372ff1d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,7 @@ # mypy: disable-error-code="no-untyped-def, no-untyped-call" # pylint: disable=duplicate-code +from __future__ import annotations + import filecmp import logging import tempfile @@ -1036,6 +1038,12 @@ def test_version(runner) -> None: ) +def get_times(path: Path) -> dict[str, Path]: + return { + p: p.stat().st_mtime for ext in ("txt", "yaml") for p in path.glob(f"*.{ext}") + } + + @pytest.mark.parametrize( ("fname", "opt"), [ @@ -1050,7 +1058,7 @@ def test_multiple(fname, opt, runner, caplog) -> None: caplog.set_level(logging.INFO) t1 = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with - path1 = t1.name + path1 = Path(t1.name) do_run_( runner, @@ -1063,11 +1071,14 @@ def test_multiple(fname, opt, runner, caplog) -> None: assert "Creating" in caplog.text + orig_times = get_times(path1) + # running this again? do_run_( runner, "project", "-v", + "--overwrite=check", "--template-python", f"{path1}/" + "py{py}-{env}", "--template", @@ -1076,16 +1087,21 @@ def test_multiple(fname, opt, runner, caplog) -> None: assert "Skipping requirements" in caplog.text - # run again no verbose: + assert orig_times == get_times(path1) + + # and again (without verbose) do_run_( runner, "project", + "--overwrite=check", "--template-python", f"{path1}/" + "py{py}-{env}", "--template", f"{path1}/" + "{env}", ) + assert orig_times == get_times(path1) + t2 = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with path2 = t2.name