Skip to content

Add a --check mode to the extract command - #1326

Open
ChrisJr404 wants to merge 1 commit into
python-babel:masterfrom
ChrisJr404:extract-check
Open

Add a --check mode to the extract command#1326
ChrisJr404 wants to merge 1 commit into
python-babel:masterfrom
ChrisJr404:extract-check

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #900.

The update command already has a --check mode that verifies catalogs are up to date without writing them, which is very handy in CI. This adds the equivalent for extract.

With --check, messages are extracted from the source as usual, but the output file is not written. Instead the freshly extracted catalog is compared against the existing --output-file, and a non-zero exit code (a BaseError) is raised if:

  • the output file does not exist, or
  • the extracted template would differ from what is on disk.

If nothing would change, the command logs that the file is up to date and returns normally.

POT-Creation-Date

A fresh extraction always regenerates the POT-Creation-Date header, so a naive comparison would always report a difference. The check therefore ignores that header alone (it copies the existing file's creation date onto the freshly extracted catalog before comparing), mirroring how update --check neutralises the volatile revision date. Any real change to the messages or the other headers is still reported.

The comparison round-trips the extracted catalog through write_po/read_po with the same options that would be used to write it, so the check reflects exactly what would end up on disk.

Usage

$ pybabel extract --check -o messages.pot mypackage/

Tests

Added test_extract_check and test_extract_check_ignores_pot_creation_date in tests/messages/frontend/test_cli.py, covering the up-to-date, changed, and missing-file cases, that the check never writes/creates the output file, and that a differing POT-Creation-Date alone does not trigger a failure. The docs for the extract command are updated as well.

pytest tests/messages passes (389 tests) and ruff check is clean.

The update command already supports a --check mode that verifies catalogs
are up to date without writing them, which is handy in CI. This adds the
equivalent for extract: with --check, messages are extracted as usual but
the output file is not written; instead the freshly extracted catalog is
compared against the existing output file, and a non-zero exit code is
returned if the file is missing or would be changed.

Differences in the POT-Creation-Date header alone are ignored, since it is
regenerated on every extraction.

Closes python-babel#900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check mode for exctract command

1 participant