python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.29k stars 433 forks source link

Extraction check for CI integration #1034

Open azmeuk opened 8 months ago

azmeuk commented 8 months ago

I would like to setup a CI job that would check if users have forgotten to extract the catalogs. The idea is that the CI would extract the catalogs, and fail if there was differences.

I was thinking a simple way to achieve this was to add a --dry-run for pybabel extract, that would return 0 if nothing changed and 1 elsewise. I suppose the return code would need to ignore some fields in the header that would always be updated (POT-Creation-Date or Generated-By).

What do you think?

Related to #910 and #725

oprypin commented 8 months ago

You can just extract and then git diff --exit-code https://github.com/oprypin/mkdocs-literate-nav/blob/1e586657e3ea19a080b6a4e1075eac3db5e18a82/.github/workflows/ci.yml#L74

azmeuk commented 8 months ago

I did not thought about this but this would be even simpler, thanks.

However, in any cases the POT-Creation-Date and the Generated-By headers are updated, and I could not find any option to ask pybabel to not update them.

Would a PR for adding options like --no-creation-date-update and --no-generated-by - or a --no-headers-update be OK?

oprypin commented 8 months ago

Hm there is actually something for POT-Creation-Date

But maybe not for Generated-By

oprypin commented 8 months ago

Also there is update-header-comment, but that's just for the # comment. Doesn't seem to be a way to avoid updating Generated-By, it would be nice.

oprypin commented 8 months ago

This command might do it?

git diff --exit-code --ignore-matching-lines '^"(POT-Creation-Date|Generated-By):'
oprypin commented 8 months ago

But I think I'd like to have a way to avoid updating Generated-By, or a flag to override its value. Then this can be used for not only checking diffs in CI but actually applying them automatically too.

Just note that I'm not a contributor to this project, I'm just making random comments :sweat_smile:

azmeuk commented 8 months ago

Thank your for your investigation and your workaround. I will try to test this soon.

azmeuk commented 8 months ago

Strangely enough, the --ignore-matching-lines seems to not be very constant:

git clone https://github.com/numerique-gouv/b3desk.git
cd b3desk
make install-dev
make translation-extract

git diff -I "POT-Creation-Date"
# expected result, POT-Creation-Date diff is hidden

git diff -I "Generated-By"
# unexpectedly, Generated-By diff is NOT hidden

git diff -I "#:"
# Most location comments are hidden, but not everyone.

It feels like a bug in git though and not related to python-babel.

oprypin commented 8 months ago

Yes it is like that, it's not a bug in Git but instead it's a stupid but documented behavior.

The behavior is: show diff hunks where at least one line still differs after filtering undesired lines.

So: if you filter out all irrelevant lines within 1 command and there is no diff then you can be sure that there are no other diffs (and exit status 0 is consistent)

If instead there is still some diff then there were in fact other diffs but the diff output is pretty much garbage (exit status 1 is still 100% correctly returned though!)

Not ideal for sure.