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

Add --check-change option, for pre-commit export hook #252

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 20 additions & 2 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class ExportCommand(GroupCommand):
),
option("all-extras", None, "Include all sets of extra dependencies."),
option("with-credentials", None, "Include credentials for extra indices."),
option(
"check-change",
None,
"Return code 1 if file changed. Useful for pre-commit.",
),
]

@property
Expand Down Expand Up @@ -101,6 +106,15 @@ def handle(self) -> int:
)
return 1

check_change = self.option("check-change")
if check_change and not output:
self.line_error(
"<error>You must specify"
" `<fg=yellow;options=bold>--output</>` if"
" using `<fg=yellow;options=bold>--check-change</>`.</error>"
)
return 1

extras: Iterable[NormalizedName]
if self.option("all-extras"):
extras = self.poetry.package.extras.keys()
Expand All @@ -122,6 +136,10 @@ def handle(self) -> int:
exporter.with_hashes(not self.option("without-hashes"))
exporter.with_credentials(self.option("with-credentials"))
exporter.with_urls(not self.option("without-urls"))
exporter.export(fmt, Path.cwd(), output or self.io)

return 0
if check_change:
changed = exporter.export_and_check_change(fmt, Path.cwd(), output)
return 1 if changed else 0
else:
exporter.export(fmt, Path.cwd(), output or self.io)
return 0
10 changes: 9 additions & 1 deletion src/poetry_plugin_export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib.parse

from functools import partialmethod
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Iterable

Expand All @@ -15,7 +16,6 @@

if TYPE_CHECKING:
from collections.abc import Collection
from pathlib import Path
from typing import ClassVar

from packaging.utils import NormalizedName
Expand Down Expand Up @@ -80,6 +80,14 @@ def export(self, fmt: str, cwd: Path, output: IO | str) -> None:

getattr(self, self.EXPORT_METHODS[fmt])(cwd, output)

def export_and_check_change(self, fmt: str, cwd: Path, output: str) -> bool:
output_path = Path(output)
orig_content = output_path.read_bytes() if output_path.exists() else None
self.export(fmt, cwd, output)
new_content = output_path.read_bytes()
changed = new_content != orig_content
return changed

def _export_generic_txt(
self, cwd: Path, output: IO | str, with_extras: bool, allow_editable: bool
) -> None:
Expand Down