[libvirt PATCH 50/51] build: Add checks for permutable format strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 build-aux/syntax-check.mk |  5 ++++
 po/meson.build            |  8 ++++++
 scripts/check-pot.py      | 54 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100755 scripts/check-pot.py

diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index 21f6b311ce..5829dc9011 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -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:
diff --git a/po/meson.build b/po/meson.build
index fbfdc2d08d..a20877ad34 100644
--- a/po/meson.build
+++ b/po/meson.build
@@ -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
index 0000000000..eca8382872
--- /dev/null
+++ b/scripts/check-pot.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+
+import sys
+import re
+
+excluded_files = [
+    'tools/libvirt-guests.sh.in',
+]
+
+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 or len(files) == 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 = []
+
+    for line in pot:
+        if not line or line.startswith("msgstr "):
+            print_msg(files, msgs)
+            files = []
+            msgs = []
+            continue
+
+        if line.startswith("#: "):
+            files.extend([f for f in line[3:].split() if not f.split(":")[0] in excluded_files])
+            continue
+
+        m = re.search(r'(msgid )?"(.*%[^%$ ]*[a-zA-Z].*)"', line)
+        if 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)
-- 
2.39.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux