Only call file(1) program if it is available.

This commit is contained in:
Bastian Kleineidam 2010-03-01 16:11:44 +01:00
parent 9a80938e0a
commit ae8a796fdd
1 changed files with 8 additions and 5 deletions

View File

@ -54,11 +54,14 @@ def guess_mime (filename):
and then file(1) as fallback."""
mime, encoding = mimedb.guess_type(filename, strict=False)
if mime is None and os.path.isfile(filename):
cmd = ["file", "--brief", "--mime-type", filename]
try:
mime = backtick(cmd).strip()
except OSError, msg:
pass
file_prog = find_program("file")
if file_prog:
cmd = [file_prog, "--brief", "--mime-type", filename]
try:
mime = backtick(cmd).strip()
except OSError, msg:
# ignore errors, as file(1) is only a fallback
pass
return mime, encoding