Now guess hook type using mimetypes lib

This commit is contained in:
Alexandre Aubin 2019-08-24 15:56:52 +02:00
parent 2ffb413a46
commit ff2caae183

View file

@ -27,6 +27,7 @@ import os
import re import re
import sys import sys
import tempfile import tempfile
import mimetypes
from glob import iglob from glob import iglob
from importlib import import_module from importlib import import_module
@ -320,17 +321,6 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
if not os.path.isfile(path): if not os.path.isfile(path):
raise YunohostError('file_does_not_exist', path=path) raise YunohostError('file_does_not_exist', path=path)
# Check the type of the hook (bash by default)
hook_type = "bash"
# (non-bash hooks shall start with something like "#!/usr/bin/env language")
hook_ext = os.path.splitext(path)[1]
if hook_ext == ".py":
hook_type = "python"
else:
# TODO / FIXME : if needed in the future, implement support for other
# languages...
assert hook_ext in ["", ".sh"], "hook_exec only supports bash and python hooks for now"
# Define output loggers and call command # Define output loggers and call command
loggers = ( loggers = (
lambda l: logger.debug(l.rstrip()+"\r"), lambda l: logger.debug(l.rstrip()+"\r"),
@ -338,13 +328,13 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
lambda l: logger.info(l.rstrip()) lambda l: logger.info(l.rstrip())
) )
if hook_type == "bash": # Check the type of the hook (bash by default)
returncode, returndata = _hook_exec_bash(path, args, no_trace, chdir, env, user, return_format, loggers) # For now we support only python and bash hooks.
elif hook_type == "python": hook_type = mimetypes.MimeTypes().guess_type(path)[0]
if hook_type == 'text/x-python':
returncode, returndata = _hook_exec_python(path, args, env, loggers) returncode, returndata = _hook_exec_python(path, args, env, loggers)
else: else:
# Doesn't happen ... c.f. previous assertion returncode, returndata = _hook_exec_bash(path, args, no_trace, chdir, env, user, return_format, loggers)
returncode, returndata = None, {}
# Check and return process' return code # Check and return process' return code
if returncode is None: if returncode is None: