Fall back to file extension detection for MIME type when the uncompressor file(1) uses for compressed TAR archives is not installed.

This commit is contained in:
Bastian Kleineidam 2011-01-23 09:30:11 -06:00
parent cf9264ebe1
commit 55f3716eb6
3 changed files with 43 additions and 18 deletions

View File

@ -1,11 +1,13 @@
0.13 "" (released xx.xx.xxxx) 0.13 "" (released xx.xx.2011)
* Fix command argument order when extracting cpio archives. * Fix command argument order when extracting cpio archives.
* Added correct cpio MIME type on Debian systems. * Added correct cpio MIME type on Debian systems.
* Added support for the clzip and pdlzip programs who are both capable * Added support for the clzip and pdlzip programs who are both capable
of handling LZIP (.lz) archives. of handling LZIP (.lz) archives.
* Support ZIP (.zip) file creation with the 7z and 7za programs. Also * Support ZIP (.zip) file creation with the 7z and 7za programs.
fixes the test_repack test case when /usr/bin/zip is not installed. * 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) 0.12 "Galaxy Quest" (released 20.11.2010)

View File

@ -186,8 +186,15 @@ def guess_mime_file_mime (file_prog, filename):
except OSError, msg: except OSError, msg:
# ignore errors, as file(1) is only a fallback # ignore errors, as file(1) is only a fallback
return mime, encoding return mime, encoding
mime2 = outparts[0] mime2 = outparts[0].split(" ", 1)[0]
if mime2 in ArchiveMimetypes: 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 mime = mime2
encoding = get_file_mime_encoding(outparts) encoding = get_file_mime_encoding(outparts)
if mime not in ArchiveMimetypes: if mime not in ArchiveMimetypes:

View File

@ -74,23 +74,17 @@ class TestMime (unittest.TestCase):
self.mime_test_file("t.rpm.foo", "application/x-rpm", None) 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", "application/x-tar", None)
self.mime_test_file("t.tar.foo", "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", "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", "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 # 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", "application/x-tar", "lzma")
#self.mime_test_file("t.tar.lzma.foo", "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", "application/x-gzip", None)
self.mime_test_file("t.txt.gz.foo", "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) self.mime_test_file("t .xz", "application/x-xz", None)
@ -124,9 +118,31 @@ class TestMime (unittest.TestCase):
@needs_program('file') @needs_program('file')
@needs_program('lzip') @needs_program('lzip')
def test_mime_file_lzip (self): 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") 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): def test_mime_mimedb (self):
self.mime_test_mimedb("t .7z", "application/x-7z-compressed", None) self.mime_test_mimedb("t .7z", "application/x-7z-compressed", None)
self.mime_test_mimedb("t.arj", "application/x-arj", None) self.mime_test_mimedb("t.arj", "application/x-arj", None)