diff --git a/apertium_apy/handlers/translate_doc.py b/apertium_apy/handlers/translate_doc.py index 1fbe92cbc..66922efba 100644 --- a/apertium_apy/handlers/translate_doc.py +++ b/apertium_apy/handlers/translate_doc.py @@ -1,7 +1,8 @@ import os +import shutil +import subprocess import tempfile import zipfile -from subprocess import check_output, check_call, CalledProcessError, PIPE import tornado from tornado import gen @@ -11,9 +12,9 @@ FILE_SIZE_LIMIT_BYTES = 32E6 MIMETYPE_COMMANDS = { - 'mimetype': lambda x: check_output(['mimetype', '-b', x], universal_newlines=True).strip(), - 'xdg-mime': lambda x: check_output(['xdg-mime', 'query', 'filetype', x], universal_newlines=True).strip(), - 'file': lambda x: check_output(['file', '--mime-type', '-b', x], universal_newlines=True).strip(), + 'mimetype': lambda x: subprocess.check_output(['mimetype', '-b', x], universal_newlines=True).strip(), + 'xdg-mime': lambda x: subprocess.check_output(['xdg-mime', 'query', 'filetype', x], universal_newlines=True).strip(), + 'file': lambda x: subprocess.check_output(['file', '--mime-type', '-b', x], universal_newlines=True).strip(), } ALLOWED_MIME_TYPES = { @@ -62,12 +63,9 @@ class TranslateDocHandler(TranslateHandler): def get_mime_type(cls, f): if not cls.mime_type_command: for command in MIMETYPE_COMMANDS.keys(): - try: - check_call(['which', command], stdout=PIPE) + if shutil.which(command): cls.mime_type_command = command break - except CalledProcessError: - pass mime_type = MIMETYPE_COMMANDS[cls.mime_type_command](f).split(';')[0] if mime_type == 'application/zip':