1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00
libvirt/scripts/check-pot.py
Jiri Denemark 35ad5e85dd build: Add checks for permutable format strings
Since all messages marked for translation contain permutable format
strings, we can add checks for enforcing them.

The syntax check does not catch all cases as it only checks format
strings between _(" and the first ". In other words messages where \"
appears before the first format string or multi-line messages where the
first format strings is not in the first line will not be checked. On
the other hand, it's run automatically by "meson test".

check-pot.py python script will detect all incorrect format strings, but
it's not as easy to use as it requires libvirt.pot to be regenerated and
this does not happen during a standard build. The following steps are
needed to check messages with check-pot.py:

    meson compile libvirt-pot-dep
    meson compile libvirt-pot
    meson compile libvirt-pot-check

Don't forget to revert changes to libvirt.pot if you run these commands
locally as we don't want each patch series to update libvirt.pot.

Shell scripts (tools/libvirt-guests.sh.in is the only one currently)
need to be exempt from this check as shell's printf function does not
understand the permutable format strings.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
2023-04-01 11:40:36 +02:00

57 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import re
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} POTFILE", file=sys.stderr)
sys.exit(1)
potfile = sys.argv[1]
failed = 0
def print_msg(files, msgs):
if len(msgs) == 0:
return
print("\n".join(files))
for m in msgs:
print(f" {m}")
global failed
failed += 1
with open(potfile, "r") as pot:
files = []
msgs = []
cFormat = False
for line in pot:
if not line or line.startswith("msgstr "):
print_msg(files, msgs)
files = []
msgs = []
cFormat = False
continue
if line.startswith("#: "):
files.extend(line[3:].split())
continue
if line.startswith("#,"):
cFormat = " c-format" in line
continue
m = re.search(r'^(msgid )?"(.*%[^%$ ]*[a-zA-Z].*)"', line)
if cFormat and m is not None:
msgs.append(m.group(2))
if failed:
print(f"Found {failed} messages without permutable format strings!",
file=sys.stderr)
sys.exit(1)