[Reproducible-commits] [diffoscope] 19/21: Improve RPM header conversion

Jérémy Bobbio lunar at moszumanska.debian.org
Mon Sep 21 17:39:29 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository diffoscope.

commit 7c645a665fcf4952af8afd336f65390da71c8a8a
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Mon Sep 21 19:23:36 2015 +0200

    Improve RPM header conversion
    
    We now convert lists to have one item on each line, special case unicode
    strings to prepare for Python 3, and use repr to transform other types.
---
 diffoscope/comparators/rpm.py       | 33 ++++++++++++-----
 tests/data/rpm_header_expected_diff | 71 +++++++++++++++++++++----------------
 2 files changed, 65 insertions(+), 39 deletions(-)

diff --git a/diffoscope/comparators/rpm.py b/diffoscope/comparators/rpm.py
index ddeab56..b8aa299 100644
--- a/diffoscope/comparators/rpm.py
+++ b/diffoscope/comparators/rpm.py
@@ -19,6 +19,7 @@
 # along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
 
 from __future__ import absolute_import
+from io import StringIO
 import os.path
 import subprocess
 import rpm
@@ -28,8 +29,26 @@ from diffoscope.comparators.binary import needs_content
 from diffoscope.comparators.utils import Archive, make_temp_directory
 from diffoscope.difference import Difference
 
+def convert_header_field(io, header):
+    if isinstance(header, list):
+        if len(header) == 0:
+            io.write(u"[]")
+        else:
+            io.write(u"\n")
+            for item in header:
+                io.write(u" - ")
+                convert_header_field(io, item)
+    elif isinstance(header, str):
+        try:
+            io.write(header.decode('utf-8'))
+        except UnicodeDecodeError:
+            io.write(header.encode('hex_codec').decode('us-ascii'))
+    else:
+        io.write(repr(header).decode('us-ascii'))
+
+
 def get_rpm_header(path, ts):
-    header = ''
+    s = StringIO()
     with open(path, 'r') as f:
         try:
             hdr = ts.hdrFromFdno(f)
@@ -39,14 +58,10 @@ def get_rpm_header(path, ts):
         for rpmtag in sorted(rpm.tagnames):
             if rpmtag not in hdr:
                 continue
-            # header fields can contain binary data
-            try:
-                value = str(hdr[rpmtag]).decode('utf-8')
-            except UnicodeDecodeError:
-                value = str(hdr[rpmtag]).encode('hex_codec')
-            header += "%s: %s\n" % (rpm.tagnames[rpmtag], value)
-
-    return header
+            s.write(u"%s: " % rpm.tagnames[rpmtag])
+            convert_header_field(s, hdr[rpmtag])
+            s.write(u"\n")
+    return s.getvalue()
 
 
 def compare_rpm_headers(path1, path2):
diff --git a/tests/data/rpm_header_expected_diff b/tests/data/rpm_header_expected_diff
index 01d5329..27cfef6 100644
--- a/tests/data/rpm_header_expected_diff
+++ b/tests/data/rpm_header_expected_diff
@@ -1,11 +1,12 @@
-@@ -1,36 +1,36 @@
+@@ -1,47 +1,47 @@
 -HEADERIMMUTABLE: 00000030000001b00000003f00000007000001a00000001000000064000000080000000000000001000003e8000000060000000200000001000003e9000000060000000700000001000003ea000000060000000900000001000003ec000000090000000b00000001000003ed000000090000002700000001000003ee000000040000003400000001000003ef000000060000003800000001000003f1000000040000004000000001000003f6000000060000004400000001000003f8000000090000004b00000001000003fd000000060000005f00000001000003fe0000000600000065000000010000040400 [...]
 +HEADERIMMUTABLE: 00000030000001b00000003f00000007000001a00000001000000064000000080000000000000001000003e8000000060000000200000001000003e9000000060000000700000001000003ea000000060000000900000001000003ec000000090000000b00000001000003ed000000090000002700000001000003ee000000040000003400000001000003ef000000060000003800000001000003f1000000040000004000000001000003f6000000060000004400000001000003f8000000090000004b00000001000003fd000000060000005f00000001000003fe0000000600000065000000010000040400 [...]
- HEADERI18NTABLE: ['C']
--SIGSIZE: 1583
+ HEADERI18NTABLE: 
+  - C
+-SIGSIZE: 1583L
 -SIGMD5: 06166011d2d66f8433d50b417358a31d
 -SHA1HEADER: 331667229aa7e4d600f682e9cd0c6372b93b082a
-+SIGSIZE: 1722
++SIGSIZE: 1722L
 +SIGMD5: 746686e44fc68a5836e8043f722e436a
 +SHA1HEADER: 2780015bd7bd365949d05e29618fa833e62e4604
  NAME: test
@@ -13,35 +14,45 @@
  RELEASE: 0
  SUMMARY: Test package for debbindiff
  DESCRIPTION: Test package
--BUILDTIME: 1435168523
-+BUILDTIME: 1435168561
+-BUILDTIME: 1435168523L
++BUILDTIME: 1435168561L
  BUILDHOST: loar
--SIZE: 446
-+SIZE: 671
+-SIZE: 446L
++SIZE: 671L
  LICENSE: Public
  GROUP: Applications/System
  OS: linux
  ARCH: x86_64
--FILESIZES: [446L]
-+FILESIZES: [671L]
- FILEMODES: [33188L]
- FILERDEVS: [0L]
--FILEMTIMES: [1435168518L]
--FILEMD5S: ['7873b606429a70284d63f0674e6787bc']
-+FILEMTIMES: [1435168559L]
-+FILEMD5S: ['17866262a0c60d916e3cf3219724b2e7']
- FILELINKTOS: ['']
- FILEFLAGS: [0L]
- FILEUSERNAME: ['root']
- FILEGROUPNAME: ['root']
+ FILESIZES: 
+- - 446L
++ - 671L
+ FILEMODES: 
+  - 33188L
+ FILERDEVS: 
+  - 0L
+ FILEMTIMES: 
+- - 1435168518L
++ - 1435168559L
+ FILEMD5S: 
+- - 7873b606429a70284d63f0674e6787bc
++ - 17866262a0c60d916e3cf3219724b2e7
+ FILELINKTOS: 
+  - 
+ FILEFLAGS: 
+  - 0L
+ FILEUSERNAME: 
+  - root
+ FILEGROUPNAME: 
+  - root
  SOURCERPM: test-0-0.src.rpm
- FILEVERIFYFLAGS: [4294967295L]
--ARCHIVESIZE: 696
-+ARCHIVESIZE: 920
- PROVIDES: ['test', 'test(x86-64)']
- REQUIREFLAGS: [16777226L, 16777226L]
- REQUIRES: ['rpmlib(CompressedFileNames)', 'rpmlib(PayloadFilesHavePrefix)']
- REQUIREVERSION: ['3.0.4-1', '4.0-1']
- RPMVERSION: 4.12.0.1
- FILEDEVICES: [1L]
- FILEINODES: [1L]
+ FILEVERIFYFLAGS: 
+  - 4294967295L
+-ARCHIVESIZE: 696L
++ARCHIVESIZE: 920L
+ PROVIDES: 
+  - test - test(x86-64)
+ REQUIREFLAGS: 
+  - 16777226L - 16777226L
+ REQUIRES: 
+  - rpmlib(CompressedFileNames) - rpmlib(PayloadFilesHavePrefix)
+ REQUIREVERSION: 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list