openssl_wrap.py 741 Bytes
Newer Older
ale's avatar
ale committed
1
import logging
ale's avatar
ale committed
2
import os
ale's avatar
ale committed
3 4 5 6 7 8 9 10 11
import subprocess

log = logging.getLogger(__name__)


class CommandError(Exception):
    pass


ale's avatar
ale committed
12
def run(*args, **env_vars):
ale's avatar
ale committed
13 14
    cmd = ['openssl']
    cmd.extend(args)
ale's avatar
ale committed
15 16
    env = dict(os.environ)
    env.update(env_vars)
ale's avatar
ale committed
17
    log.debug('executing "%s"' % ' '.join(cmd))
ale's avatar
ale committed
18
    pipe = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE)
ale's avatar
ale committed
19 20 21 22 23 24 25
    stdout, _ = pipe.communicate()
    if pipe.returncode != 0:
        raise CommandError('openssl exited with status %d' % (
                pipe.returncode,))
    return stdout


ale's avatar
ale committed
26
def run_with_config(caroot, config_file, *args):
ale's avatar
ale committed
27 28
    cmd = args[0]
    args = args[1:]
ale's avatar
ale committed
29 30
    caroot = os.path.abspath(caroot)
    return run(cmd, '-config', config_file, '-batch', *args, CAROOT=caroot)