[Reproducible-commits] [diffoscope] 02/02: Graphical image comparison using libcaca.

Jérémy Bobbio lunar at moszumanska.debian.org
Thu Dec 3 16:59:32 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 a07cce40a9efb3d862a214fdb08263d6302f35e8
Author: Chris Lamb <lamby at debian.org>
Date:   Thu Dec 3 17:50:22 2015 +0200

    Graphical image comparison using libcaca.
---
 diffoscope/__init__.py             |   1 +
 diffoscope/comparators/__init__.py |   4 +-
 diffoscope/comparators/image.py    |  52 +++++++++++++++++++++
 tests/comparators/test_image.py    |  59 ++++++++++++++++++++++++
 tests/data/image_expected_diff     |  92 +++++++++++++++++++++++++++++++++++++
 tests/data/test1.jpg               | Bin 0 -> 6020 bytes
 tests/data/test2.jpg               | Bin 0 -> 5265 bytes
 7 files changed, 207 insertions(+), 1 deletion(-)

diff --git a/diffoscope/__init__.py b/diffoscope/__init__.py
index a493abb..b5f36d8 100644
--- a/diffoscope/__init__.py
+++ b/diffoscope/__init__.py
@@ -47,6 +47,7 @@ class RequiredToolNotFound(Exception):
                 , 'ghc':        { 'debian': 'ghc' }
                 , 'gpg':        { 'debian': 'gnupg' }
                 , 'gzip':       { 'debian': 'gzip' }
+                , 'img2txt':    { 'debian': 'caca-utils' }
                 , 'isoinfo':    { 'debian': 'genisoimage' }
                 , 'javap':      { 'debian': 'default-jdk | java-sdk' }
                 , 'ls':         { 'debian': 'coreutils' }
diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index 00cd242..86080f9 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -52,6 +52,7 @@ from diffoscope.comparators.fonts import TtfFile
 from diffoscope.comparators.gettext import MoFile
 from diffoscope.comparators.gzip import GzipFile
 from diffoscope.comparators.haskell import HiFile
+from diffoscope.comparators.image import ImageFile
 from diffoscope.comparators.ipk import IpkFile
 from diffoscope.comparators.iso9660 import Iso9660File
 from diffoscope.comparators.mono import MonoExeFile
@@ -151,7 +152,8 @@ FILE_CLASSES = (
     SquashfsFile,
     TarFile,
     XzFile,
-    ZipFile
+    ZipFile,
+    ImageFile,
     )
 
 
diff --git a/diffoscope/comparators/image.py b/diffoscope/comparators/image.py
new file mode 100644
index 0000000..c44c2f1
--- /dev/null
+++ b/diffoscope/comparators/image.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2015 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+import re
+
+from diffoscope import tool_required
+from diffoscope.difference import Difference
+from diffoscope.comparators.utils import Command
+from diffoscope.comparators.binary import File, needs_content
+
+re_ansi_escapes = re.compile(r'\x1b[^m]*m')
+
+class Img2Txt(Command):
+    @tool_required('img2txt')
+    def cmdline(self):
+        return [
+            'img2txt',
+            '--width', '60',
+            '--format', 'utf8',
+            self.path,
+        ]
+
+    def filter(self, line):
+        # Strip ANSI escapes
+        return re_ansi_escapes.sub('', line.decode('utf-8')).encode('utf-8')
+
+class ImageFile(File):
+    RE_FILE_TYPE = re.compile(r'\bMS Windows icon resource|JPEG image data\b')
+
+    @staticmethod
+    def recognizes(file):
+        return ImageFile.RE_FILE_TYPE.search(file.magic_file_type)
+
+    @needs_content
+    def compare_details(self, other, source=None):
+        return [Difference.from_command(Img2Txt, self.path, other.path)]
diff --git a/tests/comparators/test_image.py b/tests/comparators/test_image.py
new file mode 100644
index 0000000..abab26e
--- /dev/null
+++ b/tests/comparators/test_image.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2015 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+import pytest
+import os.path
+
+from conftest import tool_missing
+from diffoscope.comparators import specialize
+from diffoscope.comparators.binary import FilesystemFile, NonExistingFile
+from diffoscope.comparators.image import ImageFile
+from diffoscope.config import Config
+
+TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.jpg')
+TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.jpg')
+
+ at pytest.fixture
+def image1():
+    return specialize(FilesystemFile(TEST_FILE1_PATH))
+
+ at pytest.fixture
+def image2():
+    return specialize(FilesystemFile(TEST_FILE2_PATH))
+
+def test_identification(image1):
+    assert isinstance(image1, ImageFile)
+
+def test_no_differences(image1):
+    difference = image1.compare(image1)
+    assert difference is None
+
+ at pytest.fixture
+def differences(image1, image2):
+    return image1.compare(image2).details
+
+ at pytest.mark.skipif(tool_missing('img2txt'), reason='img2txt')
+def test_diff(differences):
+    expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/image_expected_diff')).read()
+    assert differences[0].unified_diff == expected_diff
+
+def test_compare_non_existing(monkeypatch, image1):
+    monkeypatch.setattr(Config, 'new_file', True)
+    difference = image1.compare(NonExistingFile('/nonexisting', image1))
+    assert difference.source2 == '/nonexisting'
diff --git a/tests/data/image_expected_diff b/tests/data/image_expected_diff
new file mode 100644
index 0000000..72961f9
--- /dev/null
+++ b/tests/data/image_expected_diff
@@ -0,0 +1,92 @@
+@@ -1,46 +1,46 @@
+                                                             
+-  .  . .  .  . .  .  . .  .  . .  .  . .  .  . .  .  . .  . 
+-   .       .       .       .       .       .       .        
+- .   . . .   . . .. . . .    . . .   .....  .  . .   . . . .
+-8S8S%8X8SS8SSX8SS : .:8S @8S%8X8SS8SSX8%.:88S%X8S @8S @8S @8
+-8t:tt;t;%%;ttt;t::   8 ttt:%t;t;%%;ttS at 8.:  :tt:ttt:ttt:ttt%
+-  . ........::..        .. .......:.:;: .:;..   ....... .:  
+-   . ......:...           .     ....... ...      .     .  . 
+-     ...; 88@:.:             .  ...888;   : .  .    .       
+- . . ..8.8SX% ..         . . :.8:  SSX8t     .   ... .   .  
+-      .@@%:.. .        .   . ;;;.. . :8%       .  t%t% .   .
+-  . .  XS.. .. .  .      . . .;8   . :S      .    ...%      
+-       @S.  . . .   .  .   . .t8:. : :8        . .. .S  .   
+-t%%t%%S8 at ...:StS%%%%t%. .:%%X:.8 %%:..8 at t%t%;t%:... .8%%t%%%
+- ....;%% . .:;:::::..:  ...::.;t;;;..;%t;::..........:::....
+- .  ..... ....  . . ..;888%;..:......    .  . .S.8X:.. . . .
+-88888X88% ..:888888888t   888; ;888:. :888888888  %. S888888
+-.:;;:.:8X  ....::;:;;:   .::.::8 ::; :8S;;:;;;:.  ;:.S::;;::
+-.    ..8@   :  ..     .. . ..:;8  .:.:8:.. .....  :..S .  . 
+-  . ... S ..888@@@@@@@8@@@   :S8X@@@88t8@@8@  X8@@S at 8888@@@@
+-      t:88t.88 X @ X X X S  ;88X XS8888 X%@X  SX XS8 at X@@8@ X
+-  .  ..: @88   ;;.:::.t:;.. .:.;;. 888t ;t.. . :;  ..;%t  ;.
+-      .          .:888.  . .  . ..      88@     .. ...... . 
+-   .             .8888X     .      .88:X at 8:      ...88% . . 
+- .   . .          8888@  .    . .. 8Xt:S8 at 8      .. 8 at 8  .. 
+-                  8888@   .  .  .  88%. at 8tS         ;88  .. 
+-  .  .  .  ::%%%X%8S at X .SSXXX at 888X S at X; 8t88%SSXSX8 at 8%8tSXX%
+-           :8 at 8888@88X88888 at 888@88S88888888@@8 at 88@8888X8888@
+-  .  . . .  ;t;t:.8%8% :;;:::8t:8 .88t: @X8S.;;:;;t;%@ t:;;.
+-   .         .  : 8888X:.....:....:88t:@88X  . .. ..8;8; . .
+-      .   .   . . 8:888:t;;;;;;;;;.88::X888 :;t;:;% 8@%tttt;
+- .  .   .   .   . ;Xt at X:8t8;8;8;8t 8 at 8:8888%8:8; @%888X8:8;8
+-             .   . %@88                :S88   .             
+-  . .  .  .   . ..    :.     @X.        :t    X:            
+-      .     .   ..    :     @:;           8X:8Xt      8%    
+-  .      .         . .....  8888:      .  ;::888.     8X    
+-   . .     .  . .    . .St;; %888:t%%8;%8X: 8;8 ;t%SS8%@.%;%
+-       . .        .  . ;8;8t8 at 8@8;8%88 at 88;8.88: 8;8S8 at X8;8;;
+- .  .       .  .   . .      8 8S    t .  ;X:.8%.  : ; 88    
+-   .  .  .       .    .     8%8X.   ...   ;X:8 @. ...;X :   
+-          .  .      .      :;SS ;...8@@X at 8@S;888 at StX X:StS%t
+- .  .  .       .  .        . :S@ ...  .:8:...%8.  8; ..: .  
+-   .    .  . .       .       .   .     S8S ... 8@ 8.     t..
+-     .    .     .  .   .   .     .. 88888:88888 X88 @8 at X8 @8
+- .    .      .           .    . .  ::  SX   8:8S8 @  . t8   
+-   .    .  .   . .  .  .    .    .      @8;:%:%8X;88S . @8. 
++                                                            
++                                                            
++                . . .                 . . .                 
++%%%%%S%%%%%%S%%S%8...8%%%%%S%%%%%%S%%%%;..S%%%%%%S%%%%%%S%%%
++88888888888888888.:...88888888888888888%:.888888888888888888
++  . ................. . . . . . .. ..::.:... . . . . . . .  
++     ...........                 ...........                
++     ....X888.:.                ..:888S:...                 
++      .tXt.:..:.             XX% ...::@.:..       XXX.      
++      .8... .. .            ...X .  . @....       .  :      
++      .8..  .               . .S  .   @...      .  . :      
++      .8..  .               .  X.  .. @ ..        .  :      
++.:.:.::8.. ..:::.:.:.:.. ..:;..S:::...@:::.::.:..... ;:.:.:.
++     .:... :.;..     ... . .: .:..t.. .:..... ....: ;. .    
++  .  .... . .... . . .%XS;........ . . ... .  .:SS....   .  
++ttttttt8 ....tttttttt;..:.tt; .%tt; . Xt%tttttt;.....%tttttt
++ ......8. . .....    ..........X...... at . ........ .. :.     
++ .. ...8... .. ....... . .... .S... .. at . ...... .....:......
++      .8: ..8t::::;:;;.;;:....%@;::88@%t:::%..;;;;8@@t;:;:;:
++     ..S8:. 88XXXSXSSX%X at X::. at 8SSXS888XXX@St..SSSX@@XXSXSXSX
++     ...:8XSS::...........:....:...StX8.::....:....:........
++      .. ..... ....X8@:..... .....  . .:@@8................ 
++                ..8%;;t ...     ...8X%.;;;S.    ....@@t.. : 
++                ..;;;;;....     .. X;X.:;;%      .. ;;S.... 
++           . . ...;;ttt.... .    ..@;X:;;t%.    ....t;%:... 
++           .XSXXSSt;%;8SSSXSSS%%SSXS;8%@%:%SSSSSSS%St;8SSSXS
++           .;;t;%;;;ttt:t:t;;;;;;;;;%;t;tt;;;;;;;;tt:%t:;;;;
++           .X at XXXX;t;;8X at X@X@%::t at XX;8X8;;%@X at X@XX;:%;8XX at X@
++           .......t;;;;.::::...::::XtS::;;%:..::..:.%t%.:.:.
++                 .t;;;t.ttt;tt;ttt;@;XSt%SXStt;:.:tt%Xt%t;t;
++                 .tt;;;.;.........t at t:..%S%::.;8:@.:%%;.....
++                ..:XSX;:......... .  ..:XS@:....  ......... 
++                ..::.:.......t8X.     ..::;:.88;.     ....: 
++                 ...........t at S8.     ...X@:.8%;.     88..  
++                     .......tXS8.     ... at S:. at S;..   .X8.:. 
++                     ...%SSSXSX8%SS%S%%S%8XXSXX at SSSS%SX8SSSS
++                      ..XSSSX%SSSSXSX88 at XSSSX%SXSSSS at 8%XSSSS
++                       ....:tSX8;:.....::8X;;@Xt;:..::X8:::.
++                       .. ..tSS8:......:. at S;:@S;:.:..:X8....
++                           .:8S8...:X888888%t at 888@8%. at 888888
++                           .:.:;......:;Stt;.;t;::X;:.t;::..
++                            ..  . . ...X8t;:..:;8.8S:...8t: 
++                                . . ttt88Xt;;;;X8t8X;t;;8S;t
++                                  ..SXS88XXXXtS at 8S88XSX%8 at XX
++                                   ...:@8;:.:.:t8:8%::..8;:.
diff --git a/tests/data/test1.jpg b/tests/data/test1.jpg
new file mode 100644
index 0000000..9b9a9a6
Binary files /dev/null and b/tests/data/test1.jpg differ
diff --git a/tests/data/test2.jpg b/tests/data/test2.jpg
new file mode 100644
index 0000000..f1de7a6
Binary files /dev/null and b/tests/data/test2.jpg differ

-- 
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