From 526a76729430897b4d5115f8b7d85efd26b094af Mon Sep 17 00:00:00 2001
From: Paul Wise <pabs@debian.org>
Date: Mon, 23 Apr 2018 13:54:35 +0800
Subject: [PATCH] Add support for comparing monitor EDID information

Uses edid-decode and parse-edid to convert to text.
---
 debian/control                     |  1 +
 diffoscope/comparators/__init__.py |  1 +
 diffoscope/comparators/edid.py     | 60 ++++++++++++++++++++++++++++++
 diffoscope/external_tools.py       |  6 +++
 4 files changed, 68 insertions(+)
 create mode 100644 diffoscope/comparators/edid.py

diff --git a/debian/control b/debian/control
index 23a7265..e607c95 100644
--- a/debian/control
+++ b/debian/control
@@ -23,6 +23,7 @@ Build-Depends:
  docx2txt <!nocheck>,
  dpkg-dev (>= 1.17.14),
  e2fsprogs <!nocheck>,
+ edid-decode <!nocheck> | read-edid <!nocheck>,
  enjarify <!nocheck>,
  flake8 <!nocheck>,
  fontforge-extras <!nocheck>,
diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index 2e4e98f..3dc70c8 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -93,6 +93,7 @@ class ComparatorManager(object):
         ('ogg.OggFile',),
         ('xsb.XsbFile',),
         ('berkeley_db.BerkeleyDBFile',),
+        ('edid.EdidFile',),
     )
 
     _singleton = {}
diff --git a/diffoscope/comparators/edid.py b/diffoscope/comparators/edid.py
new file mode 100644
index 0000000..5c2f3d3
--- /dev/null
+++ b/diffoscope/comparators/edid.py
@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2018 Paul Wise <pabs@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 <https://www.gnu.org/licenses/>.
+
+import logging
+
+from diffoscope.tools import tool_required
+from diffoscope.difference import Difference
+
+from .utils.file import File
+from .utils.command import Command
+
+logger = logging.getLogger(__name__)
+
+
+class EdidDecode(Command):
+    @tool_required('edid-decode')
+    def cmdline(self):
+        return ['edid-decode', self.path]
+
+    # edid-decode returns 1 for even mildly misformed files
+    # return 0 so that diffoscope will still compare the output
+    # TODO: add an option to diffoscope to always compare the output
+    @property
+    def returncode(self):
+        return 0
+
+class ParseEdid(Command):
+    @tool_required('parse-edid')
+    def cmdline(self):
+        return ['parse-edid']
+
+    def stdin(self):
+        return open(self.path, 'rb')
+
+
+class EdidFile(File):
+    DESCRIPTION = "Monitor EDID configuration"
+    FILE_TYPE_HEADER_PREFIX = b'\x00\xff\xff\xff\xff\xff\xff\x00'
+
+    def compare_details(self, other, source=None):
+        differences = []
+        differences.append(Difference.from_command(EdidDecode, self.path, other.path, source='edid-decode'))
+        differences.append(Difference.from_command(ParseEdid, self.path, other.path, source='parse-edid'))
+        return differences
diff --git a/diffoscope/external_tools.py b/diffoscope/external_tools.py
index 618be77..d6eb16c 100644
--- a/diffoscope/external_tools.py
+++ b/diffoscope/external_tools.py
@@ -64,6 +64,9 @@ EXTERNAL_TOOLS = {
         'debian': 'docx2txt',
         'arch': 'docx2txt',
     },
+    'edid-decode': {
+        'debian': 'edid-decode',
+    },
     'enjarify': {
         'debian': 'enjarify',
         'arch': 'enjarify',
@@ -173,6 +176,9 @@ EXTERNAL_TOOLS = {
     'oggDump': {
         'debian': 'oggvideotools',
     },
+    'parse-edid': {
+        'debian': 'read-edid',
+    },
     'pgpdump': {
         'debian': 'pgpdump',
         'arch': 'pgpdump',
-- 
2.17.0

