diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-17 11:38:13 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-17 12:14:06 +0100 |
commit | 19ddeaafc678110dd6d9429fb11a57825f72650a (patch) | |
tree | 1b8f98efafb009d097fc55e7b41097e5971c446e /sandboxlib | |
parent | 776019ae8df349cb35fdeb2c37e2e8561bcad31b (diff) | |
download | sandboxlib-19ddeaafc678110dd6d9429fb11a57825f72650a.tar.gz |
Allow overriding backend autodetection with SANDBOXLIB_BACKEND variable
This fixes https://github.com/CodethinkLabs/sandboxlib/issues/3
Diffstat (limited to 'sandboxlib')
-rw-r--r-- | sandboxlib/__init__.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/sandboxlib/__init__.py b/sandboxlib/__init__.py index 8179d72..11140b1 100644 --- a/sandboxlib/__init__.py +++ b/sandboxlib/__init__.py @@ -27,6 +27,7 @@ import os import platform import pipes import subprocess +import warnings class ProgramNotFound(Exception): @@ -123,14 +124,52 @@ def run_sandbox_with_redirection(command, **sandbox_config): raise NotImplementedError() +def get_executor(name): + '''Return the execution module with the given name. + + KeyError is raised if the backend isn't found. + + This function will, for convenience, convert '-' to '_'. This means + "linux-user-chroot" will return the "linux_user_chroot" backend, instead of + raising an error. + + ''' + + name = name.replace('-', '_') + + try: + executor = getattr(sandboxlib, name) + except AttributeError: + raise KeyError( + "%s is not a known executor in this version of 'sandboxlib'." % + name) + + return executor + + def executor_for_platform(): - '''Returns an execution module that will work on the current platform.''' + '''Returns an execution module that will work on the current platform. + + The autodetection can be overridden by setting SANDBOXLIB_BACKEND in the + environment of the process, which can be useful for testing and debugging. + + ''' log = logging.getLogger("sandboxlib") backend = None - if platform.uname()[0] == 'Linux': + if 'SANDBOXLIB_BACKEND' in os.environ: + backend_name = os.environ['SANDBOXLIB_BACKEND'] + logging.info("Got %s from SANDBOXLIB_BACKEND variable.", backend_name) + try: + backend = get_executor(backend_name) + except KeyError: + warnings.warn( + "SANDBOXLIB_BACKEND environment variable is set to an invalid " + "value %s." % backend_name) + + if backend is None and platform.uname()[0] == 'Linux': log.info("Linux detected, looking for 'linux-user-chroot'.") try: program = sandboxlib.linux_user_chroot.linux_user_chroot_program() |