From 55f3716eb67c9bc14f923c69522ceeb9f1d41763 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Sun, 23 Jan 2011 09:30:11 -0600 Subject: [PATCH] Fall back to file extension detection for MIME type when the uncompressor file(1) uses for compressed TAR archives is not installed. --- doc/changelog.txt | 8 +++++--- patoolib/util.py | 11 +++++++++-- tests/test_mime.py | 42 +++++++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/doc/changelog.txt b/doc/changelog.txt index 1d530b0..1070083 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -1,11 +1,13 @@ -0.13 "" (released xx.xx.xxxx) +0.13 "" (released xx.xx.2011) * Fix command argument order when extracting cpio archives. * Added correct cpio MIME type on Debian systems. * Added support for the clzip and pdlzip programs who are both capable of handling LZIP (.lz) archives. - * Support ZIP (.zip) file creation with the 7z and 7za programs. Also - fixes the test_repack test case when /usr/bin/zip is not installed. + * Support ZIP (.zip) file creation with the 7z and 7za programs. + * Improved MIME type detection for compressed TAR archives. + * Fix needed archive programs for several test cases, including + test_repack and test_mime. 0.12 "Galaxy Quest" (released 20.11.2010) diff --git a/patoolib/util.py b/patoolib/util.py index 5db3c7a..0179a4b 100644 --- a/patoolib/util.py +++ b/patoolib/util.py @@ -186,8 +186,15 @@ def guess_mime_file_mime (file_prog, filename): except OSError, msg: # ignore errors, as file(1) is only a fallback return mime, encoding - mime2 = outparts[0] - if mime2 in ArchiveMimetypes: + mime2 = outparts[0].split(" ", 1)[0] + if mime2 == 'application/x-empty': + # The uncompressor program file(1) uses is not installed. + # Try to get mime information from the file extension. + mime2, encoding2 = guess_mime_mimedb(filename) + if mime2 in ArchiveMimetypes: + mime = mime2 + encoding = encoding2 + elif mime2 in ArchiveMimetypes: mime = mime2 encoding = get_file_mime_encoding(outparts) if mime not in ArchiveMimetypes: diff --git a/tests/test_mime.py b/tests/test_mime.py index d38d7b1..e9db4a6 100644 --- a/tests/test_mime.py +++ b/tests/test_mime.py @@ -74,23 +74,17 @@ class TestMime (unittest.TestCase): self.mime_test_file("t.rpm.foo", "application/x-rpm", None) self.mime_test_file("t.tar", "application/x-tar", None) self.mime_test_file("t.tar.foo", "application/x-tar", None) + self.mime_test_file("t.tar.lz", "application/x-tar", "lzip") self.mime_test_file("t.tar.bz2", "application/x-tar", "bzip2") - self.mime_test_file("t.tar.bz2.foo", "application/x-tar", "bzip2") + self.mime_test_file("t.tbz2", "application/x-tar", "bzip2") self.mime_test_file("t.tar.gz", "application/x-tar", "gzip") - self.mime_test_file("t.tar.gz.foo", "application/x-tar", "gzip") + self.mime_test_file("t.taz", "application/x-tar", "gzip") + self.mime_test_file("t.tgz", "application/x-tar", "gzip") + self.mime_test_file("t.tar.xz", "application/x-tar", "xz") + self.mime_test_file("t.tar.Z", "application/x-tar", "compress") # file(1) does not recognize .lzma files #self.mime_test_file("t.tar.lzma", "application/x-tar", "lzma") #self.mime_test_file("t.tar.lzma.foo", "application/x-tar", "lzma") - self.mime_test_file("t.tar.xz", "application/x-tar", "xz") - self.mime_test_file("t.tar.xz.foo", "application/x-tar", "xz") - self.mime_test_file("t.tar.Z", "application/x-tar", "compress") - self.mime_test_file("t.tar.Z.foo", "application/x-tar", "compress") - self.mime_test_file("t.taz", "application/x-tar", "gzip") - self.mime_test_file("t.taz.foo", "application/x-tar", "gzip") - self.mime_test_file("t.tbz2", "application/x-tar", "bzip2") - self.mime_test_file("t.tbz2.foo", "application/x-tar", "bzip2") - self.mime_test_file("t.tgz", "application/x-tar", "gzip") - self.mime_test_file("t.tgz.foo", "application/x-tar", "gzip") self.mime_test_file("t.txt.gz", "application/x-gzip", None) self.mime_test_file("t.txt.gz.foo", "application/x-gzip", None) self.mime_test_file("t .xz", "application/x-xz", None) @@ -124,9 +118,31 @@ class TestMime (unittest.TestCase): @needs_program('file') @needs_program('lzip') def test_mime_file_lzip (self): - self.mime_test_file("t.tar.lz", "application/x-tar", "lzip") self.mime_test_file("t.tar.lz.foo", "application/x-tar", "lzip") + @needs_program('file') + @needs_program('bzip2') + def test_mime_file_bzip (self): + self.mime_test_file("t.tar.bz2.foo", "application/x-tar", "bzip2") + self.mime_test_file("t.tbz2.foo", "application/x-tar", "bzip2") + + @needs_program('file') + @needs_program('gzip') + def test_mime_file_gzip (self): + self.mime_test_file("t.tar.gz.foo", "application/x-tar", "gzip") + self.mime_test_file("t.taz.foo", "application/x-tar", "gzip") + self.mime_test_file("t.tgz.foo", "application/x-tar", "gzip") + + @needs_program('file') + @needs_program('xz') + def test_mime_file_xzip (self): + self.mime_test_file("t.tar.xz.foo", "application/x-tar", "xz") + + @needs_program('file') + @needs_program('uncompress') + def test_mime_file_compress (self): + self.mime_test_file("t.tar.Z.foo", "application/x-tar", "compress") + def test_mime_mimedb (self): self.mime_test_mimedb("t .7z", "application/x-7z-compressed", None) self.mime_test_mimedb("t.arj", "application/x-arj", None)