diff scripts/i18n_utils.py @ 8315:93dabafa567e

scripts/i18n: add command 'normalized-diff' Add a command 'normalized-diff' that takes two (po) files and diff normalized copies of them.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Thu, 07 Nov 2019 01:52:16 +0100
parents ae9d205f4407
children 5553ecc962e0
line wrap: on
line diff
--- a/scripts/i18n_utils.py	Thu Dec 19 00:14:27 2019 +0100
+++ b/scripts/i18n_utils.py	Thu Nov 07 01:52:16 2019 +0100
@@ -15,7 +15,9 @@
 
 import os
 import re
+import shutil
 import subprocess
+import tempfile
 
 
 do_debug = False  # set from scripts/i18n --debug
@@ -165,3 +167,19 @@
             normalized_content = _normalize_po(raw_content)
             dest.write(normalized_content)
         os.rename(po_tmp, po_file)
+
+def _normalized_diff(file1, file2, strip=False):
+    # Create temporary copies of both files
+    temp1 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file1))
+    temp2 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file2))
+    debug('normalized_diff: %s -> %s / %s -> %s' % (file1, temp1.name, file2, temp2.name))
+    shutil.copyfile(file1, temp1.name)
+    shutil.copyfile(file2, temp2.name)
+    # Normalize them in place
+    _normalize_po_file(temp1.name, strip=strip)
+    _normalize_po_file(temp2.name, strip=strip)
+    # Now compare
+    try:
+        runcmd(['diff', '-u', temp1.name, temp2.name])
+    except subprocess.CalledProcessError as e:
+        return e.returncode