[diffoscope] 01/02: comparators/apk: less kludgy way of detecting APKs

Ximin Luo infinity0 at debian.org
Tue Jul 25 17:03:56 CEST 2017


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

infinity0 pushed a commit to branch master
in repository diffoscope.

commit 00a4c141fc94d9886d6aaca54c5f01394895627c
Author: Ximin Luo <infinity0 at debian.org>
Date:   Tue Jul 25 16:41:42 2017 +0200

    comparators/apk: less kludgy way of detecting APKs
---
 diffoscope/comparators/apk.py        | 10 +++++++++-
 diffoscope/comparators/utils/file.py |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/diffoscope/comparators/apk.py b/diffoscope/comparators/apk.py
index dc8a136..9ac2887 100644
--- a/diffoscope/comparators/apk.py
+++ b/diffoscope/comparators/apk.py
@@ -146,10 +146,18 @@ class ApkContainer(Archive):
         return differences
 
 class ApkFile(File):
-    RE_FILE_TYPE = re.compile(r'^((Java|Zip) archive data|DOS/MBR boot sector).*\b')
+    RE_FILE_TYPE_FALLBACK_HEADER = b"PK\x03\x04"
+    RE_FILE_TYPE = re.compile(r'^(Java|Zip) archive data.*\b')
     RE_FILE_EXTENSION = re.compile(r'\.apk$')
     CONTAINER_CLASS = ApkContainer
 
+    @staticmethod
+    def recognizes(file):
+        if not ApkFile.RE_FILE_EXTENSION.search(file.name):
+            return False
+        return (ApkFile.RE_FILE_TYPE.search(file.magic_file_type) or
+                file.file_header[:4] == ApkFile.RE_FILE_TYPE_FALLBACK_HEADER)
+
     def compare_details(self, other, source=None):
         zipinfo_difference = Difference.from_command(Zipinfo, self.path, other.path) or \
                              Difference.from_command(ZipinfoVerbose, self.path, other.path)
diff --git a/diffoscope/comparators/utils/file.py b/diffoscope/comparators/utils/file.py
index f38cead..4fa996c 100644
--- a/diffoscope/comparators/utils/file.py
+++ b/diffoscope/comparators/utils/file.py
@@ -150,6 +150,13 @@ class File(object, metaclass=abc.ABCMeta):
         return self._magic_file_type
 
     @property
+    def file_header(self):
+        if not hasattr(self, '_file_header'):
+            with open(self.path, 'rb') as f:
+                self._file_header = f.read(16)
+        return self._file_header
+
+    @property
     def file_type(self):
         for x, y in (
             (self.is_device, "device"),

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


More information about the diffoscope mailing list