|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Find changed msgid fields without a change in POT-Creation-Date. |
| 15 | +
|
| 16 | +When following the instructions in |
| 17 | +https://github.com/google/comprehensive-rust/blob/main/TRANSLATIONS.md, |
| 18 | +one of two things should happen: |
| 19 | +
|
| 20 | +- The `msgid` fields change because `msgmerge --update` was used. This |
| 21 | + will also update the POT-Creation-Date field since a new timestamp |
| 22 | + is merged in from the messages.pot file. |
| 23 | +
|
| 24 | +- Translations are added or updated. This should not change the |
| 25 | + `msgid` fields: only the `msgstr` fields should change. If the PO |
| 26 | + editor being used inadvertently changes the wrapping of both `msgid` |
| 27 | + and `msgstr` fields, then `dprint fmt` can be used to normalize them |
| 28 | + all. |
| 29 | +
|
| 30 | +The code here detects if both of these happen at the same time: if one |
| 31 | +or more `msgid` fields changed without a corresponding change to the |
| 32 | +POT-Creation-Date field. If this happens, the translator should fix it |
| 33 | +by running: |
| 34 | +
|
| 35 | + dprint fmt |
| 36 | +
|
| 37 | +Commit and push to the branch again. |
| 38 | +""" |
| 39 | + |
1 | 40 | import os
|
2 | 41 |
|
| 42 | +# TODO: move the `git reset` from the action code to here. Infact, we |
| 43 | +# should be able to determine this with read-only operations. |
| 44 | + |
| 45 | +# TODO: use Git plumbing commands instead of porcelain, see |
| 46 | +# https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git.html. |
3 | 47 | for filename in os.popen("git diff --name-only").read().split():
|
4 | 48 | if not filename.endswith(".po"):
|
5 | 49 | continue
|
6 | 50 |
|
| 51 | + # If POT-Creation-Date has changed, then we assume that the commit |
| 52 | + # is the result of `msgmerge --update`. It is expected that the |
| 53 | + # `msgid` fields change when `msgmerge` is run, so there is |
| 54 | + # nothing to check. |
7 | 55 | if "POT-Creation-Date" in os.popen(
|
8 | 56 | f"git diff --unified=0 {filename}").read():
|
9 | 57 | print(
|
|
18 | 66 | if line.startswith("00000000")
|
19 | 67 | }
|
20 | 68 |
|
| 69 | + # Look for a changed line between `msgid` and `msgstr`. |
21 | 70 | saw_msgid = False
|
22 | 71 | with open(filename, "r") as f:
|
23 | 72 | line = f.readline()
|
|
0 commit comments