Merge pull request #96 from benjaminwinger/file_error_fix

File error fix
This commit is contained in:
Yaroslav Halchenko 2021-02-17 11:50:32 -05:00 committed by GitHub
commit 8bb375e2f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -294,16 +294,15 @@ def guess_mime_file (filename):
cmd = [file_prog, "--brief", "--mime", "--uncompress", filename] cmd = [file_prog, "--brief", "--mime", "--uncompress", filename]
try: try:
outparts = backtick(cmd).strip().split(";") outparts = backtick(cmd).strip().split(";")
mime2 = outparts[0].split(" ", 1)[0]
except OSError: except OSError:
# ignore errors, as file(1) is only a fallback mime2 = None
return mime, encoding
mime2 = outparts[0].split(" ", 1)[0]
# Some file(1) implementations return an empty or unknown mime type # Some file(1) implementations return an empty or unknown mime type
# when the uncompressor program is not installed, other # when the uncompressor program is not installed, other
# implementation return the original file type. # implementation return the original file type.
# The following detects both cases. # The following detects both cases.
if (mime2 in ('application/x-empty', 'application/octet-stream') or if (mime2 in ('application/x-empty', 'application/octet-stream') or
mime2 in Mime2Encoding): mime2 in Mime2Encoding or not mime2):
# The uncompressor program file(1) uses is not installed # The uncompressor program file(1) uses is not installed
# or is not able to uncompress. # or is not able to uncompress.
# Try to get mime information from the file extension. # Try to get mime information from the file extension.
@ -494,7 +493,10 @@ def shell_quote_nt (value):
def stripext (filename): def stripext (filename):
"""Return the basename without extension of given filename.""" """Return the basename without extension of given filename."""
return os.path.splitext(os.path.basename(filename))[0] basename, _ = os.path.splitext(os.path.basename(filename))
if basename.endswith(".tar"):
basename, _ = os.path.splitext(basename)
return basename
def get_single_outfile (directory, archive, extension=""): def get_single_outfile (directory, archive, extension=""):

View File

@ -37,3 +37,10 @@ class UtilTest (unittest.TestCase):
filename2 = os.path.join(parentdir, '.') filename2 = os.path.join(parentdir, '.')
self.assertFalse(util.is_same_file(filename1, filename2)) self.assertFalse(util.is_same_file(filename1, filename2))
self.assertFalse(util.is_same_filename(filename1, filename2)) self.assertFalse(util.is_same_filename(filename1, filename2))
def test_stripext(self):
self.assertTrue(util.stripext("bar.gz") == "bar")
self.assertTrue(util.stripext("foo/bar.tar.gz") == "bar")
self.assertTrue(util.stripext("foo/bartar.gz") == "bartar")
self.assertTrue(util.stripext("foo/bar.7z") == "bar")
self.assertTrue(util.stripext("foo/bar") == "bar")