Skip to content

Commit 1357f7a

Browse files
authored
Add comments and license text to prevent-msgid.py (#1364)
1 parent a06d985 commit 1357f7a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

.github/workflows/prevent-msgid.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
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+
140
import os
241

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.
347
for filename in os.popen("git diff --name-only").read().split():
448
if not filename.endswith(".po"):
549
continue
650

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.
755
if "POT-Creation-Date" in os.popen(
856
f"git diff --unified=0 {filename}").read():
957
print(
@@ -18,6 +66,7 @@
1866
if line.startswith("00000000")
1967
}
2068

69+
# Look for a changed line between `msgid` and `msgstr`.
2170
saw_msgid = False
2271
with open(filename, "r") as f:
2372
line = f.readline()

0 commit comments

Comments
 (0)