<commit_before>'''
A module for shelling out

Keep in mind that this module is insecure, in that it can give whomever has
access to the master root execution access to all salt minions
'''

import logging
import os
import subprocess
import tempfile
import salt.utils
import pwd

# Set up logging
log = logging.getLogger(__name__)

# Set the default working directory to the home directory
# of the user salt-minion is running as.  Default:  /root
DEFAULT_CWD = os.path.expanduser('~')

# Set up the default outputters
__outputter__ = {
    'run': 'txt',
}
def _run(cmd,
        cwd=DEFAULT_CWD,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        quiet=False,
        runas=None):
    '''
    Do the DRY thing and only call subprocess.Popen() once
    '''
    ret = {}
    uid = os.getuid()
    euid = os.geteuid()

    def su():
        os.setuid(runas_uid)
        os.seteuid(runas_uid)

    if runas:
        try:
            p = pwd.getpwnam(runas)
        except KeyError:
            stderr_str = 'The user %s is not available' % runas
            if stderr == subprocess.STDOUT:
                ret['stdout'] = stderr_str
            else:
                ret['stdout'] = ''
                ret['stderr'] = stderr_str
            ret['retcode'] = 1
            return ret
        runas_uid = p.pw_uid
        preexec = su
    else:
        preexec = None

    if not quiet:
        if runas:
            log.info('Executing command {0} as user {1} in directory {2}'.format(
                    cmd, runas, cwd))
        else:
            log.info('Executing command {0} in directory {1}'.format(cmd, cwd))

    try:
        proc = subprocess.Popen(cmd,
            cwd=cwd,
            shell=True,
            stdout=stdout,
            stderr=stderr,
            preexec_fn=preexec
        )

        out = proc.communicate()
        ret['stdout'] = out[0]
        ret['stderr'] = out[1]
        ret['retcode'] = proc.returncode
        ret['pid'] = proc.pid
    except OSError:
        stderr_str = 'Unable to change to user %s: permission denied' % runas 
        if stderr == subprocess.STDOUT:
            ret['stdout'] = stderr_str
        else:
            ret['stdout'] = ''
            ret['stderr'] = stderr_str
        ret['retcode'] = 2
    return ret


def _run_quiet(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Helper for running commands quietly for minion startup
    '''
    return _run(cmd, runas=runas, cwd=cwd, stderr=subprocess.STDOUT, quiet=True)['stdout']


def run(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Execute the passed command and return the output as a string

    CLI Example::

        salt '*' cmd.run "ls -l | awk '/foo/{print $2}'"
    '''
    out = _run(cmd, runas=runas, cwd=cwd, stderr=subprocess.STDOUT)['stdout']
    log.debug(out)
    return out


def run_stdout(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Execute a command, and only return the standard out

    CLI Example::

        salt '*' cmd.run_stdout "ls -l | awk '/foo/{print $2}'"
    '''
    stdout = _run(cmd, runas=runas, cwd=cwd)["stdout"]
    log.debug(stdout)
    return stdout


def run_stderr(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Execute a command and only return the standard error

    CLI Example::

        salt '*' cmd.run_stderr "ls -l | awk '/foo/{print $2}'"
    '''
    stderr = _run(cmd, runas=runas, cwd=cwd)["stderr"]
    log.debug(stderr)
    return stderr


def run_all(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Execute the passed command and return a dict of return data

    CLI Example::

        salt '*' cmd.run_all "ls -l | awk '/foo/{print $2}'"
    '''
    ret = _run(cmd, runas=runas, cwd=cwd)
    if ret['retcode'] != 0:
        log.error('Command {0} failed'.format(cmd))
        log.error('retcode: {0}'.format(ret['retcode']))
        log.error('stdout: {0}'.format(ret['stdout']))
        log.error('stderr: {0}'.format(ret['stderr']))
    else:
        log.info('stdout: {0}'.format(ret['stdout']))
        log.info('stderr: {0}'.format(ret['stderr']))
    return ret


def retcode(cmd, runas=None, cwd=DEFAULT_CWD):
    '''
    Execute a shell command and return the command's return code.

    CLI Example::

        salt '*' cmd.retcode "file /bin/bash"
    '''
    return _run(cmd, runas=runas, cwd=cwd)['retcode']


def has_exec(cmd):
    '''
    Returns true if the executable is available on the minion, false otherwise

    CLI Example::

        salt '*' cmd.has_exec cat
    '''
    return bool(salt.utils.which(cmd))

def which(cmd):
    '''
    Returns the path of an executable available on the minion, None otherwise

    CLI Example::

        salt '*' cmd.which cat
    '''
    return salt.utils.which(cmd)
<commit_msg>move runas to the last arg<commit_after>'''
A module for shelling out

Keep in mind that this module is insecure, in that it can give whomever has
access to the master root execution access to all salt minions
'''

import logging
import os
import subprocess
import tempfile
import salt.utils
import pwd

# Set up logging
log = logging.getLogger(__name__)

# Set the default working directory to the home directory
# of the user salt-minion is running as.  Default:  /root
DEFAULT_CWD = os.path.expanduser('~')

# Set up the default outputters
__outputter__ = {
    'run': 'txt',
}
def _run(cmd,
        cwd=DEFAULT_CWD,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        quiet=False,
        runas=None):
    '''
    Do the DRY thing and only call subprocess.Popen() once
    '''
    ret = {}
    uid = os.getuid()
    euid = os.geteuid()

    def su():
        os.setuid(runas_uid)
        os.seteuid(runas_uid)

    if runas:
        try:
            p = pwd.getpwnam(runas)
        except KeyError:
            stderr_str = 'The user {0} is not available'.format(runas)
            if stderr == subprocess.STDOUT:
                ret['stdout'] = stderr_str
            else:
                ret['stdout'] = ''
                ret['stderr'] = stderr_str
            ret['retcode'] = 1
            return ret
        runas_uid = p.pw_uid
        preexec = su
    else:
        preexec = None

    if not quiet:
        if runas:
            log.info('Executing command {0} as user {1} in directory {2}'.format(
                    cmd, runas, cwd))
        else:
            log.info('Executing command {0} in directory {1}'.format(cmd, cwd))

    try:
        proc = subprocess.Popen(cmd,
            cwd=cwd,
            shell=True,
            stdout=stdout,
            stderr=stderr,
            preexec_fn=preexec
        )

        out = proc.communicate()
        ret['stdout'] = out[0]
        ret['stderr'] = out[1]
        ret['retcode'] = proc.returncode
        ret['pid'] = proc.pid
    except OSError:
        stderr_str = 'Unable to change to user {0}: permission denied'.format(runas)
        if stderr == subprocess.STDOUT:
            ret['stdout'] = stderr_str
        else:
            ret['stdout'] = ''
            ret['stderr'] = stderr_str
        ret['retcode'] = 2
    return ret


def _run_quiet(cmd, cwd=DEFAULT_CWD, runas=None):
    '''
    Helper for running commands quietly for minion startup
    '''
    return _run(cmd, runas=runas, cwd=cwd, stderr=subprocess.STDOUT, quiet=True)['stdout']


def run(cmd, cwd=DEFAULT_CWD, runas=None):
    '''
    Execute the passed command and return the output as a string

    CLI Example::

        salt '*' cmd.run "ls -l | awk '/foo/{print $2}'"
    '''
    out = _run(cmd, runas=runas, cwd=cwd, stderr=subprocess.STDOUT)['stdout']
    log.debug(out)
    return out


def run_stdout(cmd, cwd=DEFAULT_CWD,  runas=None):
    '''
    Execute a command, and only return the standard out

    CLI Example::

        salt '*' cmd.run_stdout "ls -l | awk '/foo/{print $2}'"
    '''
    stdout = _run(cmd, runas=runas, cwd=cwd)["stdout"]
    log.debug(stdout)
    return stdout


def run_stderr(cmd, cwd=DEFAULT_CWD, runas=None):
    '''
    Execute a command and only return the standard error

    CLI Example::

        salt '*' cmd.run_stderr "ls -l | awk '/foo/{print $2}'"
    '''
    stderr = _run(cmd, runas=runas, cwd=cwd)["stderr"]
    log.debug(stderr)
    return stderr


def run_all(cmd, cwd=DEFAULT_CWD, runas=None):
    '''
    Execute the passed command and return a dict of return data

    CLI Example::

        salt '*' cmd.run_all "ls -l | awk '/foo/{print $2}'"
    '''
    ret = _run(cmd, runas=runas, cwd=cwd)
    if ret['retcode'] != 0:
        log.error('Command {0} failed'.format(cmd))
        log.error('retcode: {0}'.format(ret['retcode']))
        log.error('stdout: {0}'.format(ret['stdout']))
        log.error('stderr: {0}'.format(ret['stderr']))
    else:
        log.info('stdout: {0}'.format(ret['stdout']))
        log.info('stderr: {0}'.format(ret['stderr']))
    return ret


def retcode(cmd, cwd=DEFAULT_CWD, runas=None):
    '''
    Execute a shell command and return the command's return code.

    CLI Example::

        salt '*' cmd.retcode "file /bin/bash"
    '''
    return _run(cmd, runas=runas, cwd=cwd)['retcode']


def has_exec(cmd):
    '''
    Returns true if the executable is available on the minion, false otherwise

    CLI Example::

        salt '*' cmd.has_exec cat
    '''
    return bool(salt.utils.which(cmd))

def which(cmd):
    '''
    Returns the path of an executable available on the minion, None otherwise

    CLI Example::

        salt '*' cmd.which cat
    '''
    return salt.utils.which(cmd)

{'fraction_non_alphanumeric': 0.129229471908928, 'fraction_numerical': 0.004427110783176979, 'mean_word_length': 2.326788218793829, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725183', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3070, 'n_words': 855}