Beispiel #1
0
def run(popenargs,
        sudo=False,
        input=None,
        timeout=None,
        check=True,
        universal_newlines=True,
        stdout=_ADAPTIVE,
        log_threshold=logging.DEBUG,
        **kwargs):
    """
    Small wrapper around subprocess.run().
    """

    assert type(popenargs) is list

    subprocess_stdout = stdout

    if subprocess_stdout == _ADAPTIVE:
        if logging.getLogger().isEnabledFor(log_threshold):
            subprocess_stdout = None
        else:
            subprocess_stdout = subprocess.PIPE

    all_args = list()
    if not sudo and os.getuid() == 0:
        current_user = get_user()
        if current_user != 'root':
            # drop privileges
            all_args.extend(['sudo', '-u', current_user])
    elif sudo and os.getuid() != 0:
        all_args.append('sudo')

    all_args.extend(popenargs)

    logging.log(log_threshold, "Running command: {0}".format(all_args))

    result = mockablerun.run_mockable(all_args,
                                      input=input,
                                      timeout=timeout,
                                      check=check,
                                      universal_newlines=universal_newlines,
                                      stdout=subprocess_stdout,
                                      **kwargs)

    if (logging.getLogger().isEnabledFor(log_threshold)
            and subprocess_stdout is subprocess.PIPE):
        logging.log(log_threshold, result.stdout)

    return result
Beispiel #2
0
def run(popenargs,
        sudo=False,
        input=None,
        timeout=None,
        check=True,
        universal_newlines=True,
        stdout=_ADAPTIVE,
        **kwargs):
    """
    Small wrapper around subprocess.run().
    """

    assert type(popenargs) is list

    subprocess_stdout = stdout

    if subprocess_stdout == _ADAPTIVE:
        if logging.getLogger().isEnabledFor(logging.INFO):
            subprocess_stdout = None
        else:
            subprocess_stdout = subprocess.PIPE

    myargs = popenargs.copy()

    if not sudo and os.getuid() == 0:
        current_user = get_user()
        if current_user != 'root':
            # drop privileges
            myargs = ["sudo", "-u", current_user] + myargs
    elif sudo and os.getuid() != 0:
        myargs.insert(0, "sudo")

    logging.info("Running command: {0}".format(myargs))

    result = mockablerun.run_mockable(myargs,
                                      input=input,
                                      timeout=timeout,
                                      check=check,
                                      universal_newlines=universal_newlines,
                                      stdout=subprocess_stdout,
                                      **kwargs)

    if (logging.getLogger().isEnabledFor(logging.INFO)
            and subprocess_stdout is subprocess.PIPE):
        logging.info(result.stdout)

    return result