]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
build: Add checks for permutable format strings
authorJiri Denemark <jdenemar@redhat.com>
Wed, 8 Mar 2023 09:58:23 +0000 (10:58 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Sat, 1 Apr 2023 09:40:36 +0000 (11:40 +0200)
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>
build-aux/syntax-check.mk
po/meson.build
scripts/check-pot.py [new file with mode: 0755]

index 21f6b311ce4c09a68dfe280cfd4a8fffddd4b0d2..5829dc9011141e6d14d6cfcc3e4fb19ada7df39c 100644 (file)
@@ -455,6 +455,11 @@ sc_prohibit_diagnostic_without_format:
          { echo 'found diagnostic without %' 1>&2; \
            exit 1; } || :
 
+sc_require_permutable_format_in_translation:
+       @prohibit='\<N?_ *\("[^"]*%[^%$$ ]*[a-zA-Z][^"]*"' \
+       halt='non-permutable format string(s)' \
+         $(_sc_search_regexp)
+
 # The strings "" and "%s" should never be marked for translation.
 # Files under tests/ and examples/ should not be translated.
 sc_prohibit_useless_translation:
index fbfdc2d08d431f2e0aeeed065d860359f64c947b..a20877ad34ab26096a8b9d730ae7ca7b0922ba38 100644 (file)
@@ -25,3 +25,11 @@ potfiles_dep = [
 ]
 
 alias_target('libvirt-pot-dep', potfiles_dep)
+
+run_target(
+  'libvirt-pot-check',
+  command: [
+    '@0@/scripts/check-pot.py'.format(meson.project_source_root()),
+    '@0@/po/libvirt.pot'.format(meson.project_source_root())
+  ],
+)
diff --git a/scripts/check-pot.py b/scripts/check-pot.py
new file mode 100755 (executable)
index 0000000..f6b4fbf
--- /dev/null
@@ -0,0 +1,56 @@
+#!/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)