runs bash in a subshell, returns list of
 commands that were run in the session
352923
def bash(command = nil)
      Tempfile.open("tetra-history") do |history_file|
        Tempfile.open("tetra-bashrc") do |bashrc_file|
          kit = Tetra::Kit.new(@project)
          ant_path = kit.find_executable("ant")
          ant_in_kit = ant_path != nil
          ant_commandline = Tetra::Ant.commandline(@project.full_path, ant_path)

          mvn_path = kit.find_executable("mvn")
          mvn_in_kit = mvn_path != nil
          mvn_commandline = Tetra::Mvn.commandline(@project.full_path, mvn_path)

          bashrc_content = Bashrc.new(history_file.path, ant_in_kit, ant_commandline, mvn_in_kit, mvn_commandline).to_s
          log.debug "writing bashrc file: #{bashrc_file.path}"
          log.debug bashrc_content

          bashrc_file.write(bashrc_content)
          bashrc_file.flush

          if command
            run("bash --rcfile #{bashrc_file.path} -i -c '#{command}'")
            [command]
          else
            run_interactive("bash --rcfile #{bashrc_file.path} -i")
            history = File.read(history_file)
            log.debug "history contents:"
            log.debug history

            history.split("\n").map(&:strip)
          end
        end
      end
    end
206953
def list_commands(self, ctx):
        """"""
        env = ctx.ensure_object(environment.Environment)
        env.load()

        return sorted(env.list_commands(*self.path))
1107009
def sh2(cmd):
    """"""
    p = Popen(cmd, stdout=PIPE, shell=True, env=sub_environment())
    out = p.communicate()[0]
    retcode = p.returncode
    if retcode:
        raise CalledProcessError(retcode, cmd)
    else:
        return out.rstrip()
934054
def run (cmd, verbosity=0, **kwargs):
    """"""
    # Note that shell_quote_nt() result is not suitable for copy-paste
    # (especially on Unix systems), but it looks nicer than shell_quote().
    if verbosity >= 0:
        log_info("running %s" % " ".join(map(shell_quote_nt, cmd)))
    if kwargs:
        if verbosity >= 0:
            log_info("    with %s" % ", ".join("%s=%s" % (k, shell_quote(str(v)))\
                                           for k, v in kwargs.items()))
        if kwargs.get("shell"):
            # for shell calls the command must be a string
            cmd = " ".join(cmd)
    if verbosity < 1:
        # hide command output on stdout
        with open(os.devnull, 'wb') as devnull:
            kwargs['stdout'] = devnull
            res = subprocess.call(cmd, **kwargs)
    else:
        res = subprocess.call(cmd, **kwargs)
    return res
1153105
def _subprocess(cmd):
    '''
    
    '''

    log.debug('Running: "%s"', ' '.join(cmd))
    try:
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        ret = salt.utils.stringutils.to_unicode(proc.communicate()[0]).strip()
        retcode = proc.wait()

        if ret:
            return ret
        elif retcode != 1:
            return True
        else:
            return False
    except OSError as err:
        log.error(err)
        return False
127226
def cmd_ls(self, *args):
        
        cmd_str = ' '.join(['ls'] + list(args))
        self.plugin.exec_shell(cmd_str)
1133296
def bash(filename):
    """"""
    sys.stdout.flush()
    subprocess.call("bash {}".format(filename), shell=True)
210538
def exec_command(self):
        
        if not self.original_command_string:
            raise SSHEnvironmentError('no SSH command found; '
                                      'interactive shell disallowed.')

        command_info = {'from': self.get_client_ip(),
                        'keyname': self.keyname,
                        'ssh_original_comand': self.original_command_string,
                        'time': time.time()}

        os.environ['AUTHPROGS_KEYNAME'] = self.keyname

        retcode = 126
        try:
            match = self.find_match()
            command_info['command'] = match.get('command')
            self.logdebug('find_match returned "%s"\n' % match)

            command = match['command']
            retcode = subprocess.call(command)
            command_info['code'] = retcode
            self.log('result: %s\n' % command_info)
            sys.exit(retcode)
        except (CommandRejected, OSError) as err:
            command_info['exception'] = '%s' % err
            self.log('result: %s\n' % command_info)
            sys.exit(retcode)
458805
def exec_command(self, cmd, tmp_path, sudo_user,sudoable=False, executable='/bin/sh'):
        '''  '''

        ssh_cmd = self._password_cmd()
        ssh_cmd += ["ssh", "-tt", "-q"] + self.common_args + [self.host]

        if not self.runner.sudo or not sudoable:
            if executable:
                ssh_cmd.append(executable + ' -c ' + pipes.quote(cmd))
            else:
                ssh_cmd.append(cmd)
        else:
            sudocmd, prompt = utils.make_sudo_cmd(sudo_user, executable, cmd)
            ssh_cmd.append(sudocmd)

        vvv("EXEC %s" % ssh_cmd, host=self.host)
        try:
            # Make sure stdin is a proper (pseudo) pty to avoid: tcgetattr errors
            import pty
            master, slave = pty.openpty()
            p = subprocess.Popen(ssh_cmd, stdin=slave,
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdin = os.fdopen(master, 'w', 0)
        except:
            p = subprocess.Popen(ssh_cmd, stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdin = p.stdin

        self._send_password()

        if self.runner.sudo and sudoable and self.runner.sudo_pass:
            fcntl.fcntl(p.stdout, fcntl.F_SETFL,
                        fcntl.fcntl(p.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
            sudo_output = ''
            while not sudo_output.endswith(prompt):
                rfd, wfd, efd = select.select([p.stdout], [],
                                              [p.stdout], self.runner.timeout)
                if p.stdout in rfd:
                    chunk = p.stdout.read()
                    if not chunk:
                        raise errors.AnsibleError('ssh connection closed waiting for sudo password prompt')
                    sudo_output += chunk
                else:
                    stdout = p.communicate()
                    raise errors.AnsibleError('ssh connection error waiting for sudo password prompt')
            stdin.write(self.runner.sudo_pass + '\n')
            fcntl.fcntl(p.stdout, fcntl.F_SETFL, fcntl.fcntl(p.stdout, fcntl.F_GETFL) & ~os.O_NONBLOCK)

        # We can't use p.communicate here because the ControlMaster may have stdout open as well
        stdout = ''
        stderr = ''
        while True:
            rfd, wfd, efd = select.select([p.stdout, p.stderr], [], [p.stdout, p.stderr], 1)
            if p.stdout in rfd:
                dat = os.read(p.stdout.fileno(), 9000)
                stdout += dat
                if dat == '':
                    p.wait()
                    break
            elif p.stderr in rfd:
                dat = os.read(p.stderr.fileno(), 9000)
                stderr += dat
                if dat == '':
                    p.wait()
                    break
            elif p.poll() is not None:
                break
        stdin.close() # close stdin after we read from stdout (see also issue #848)

        if p.returncode != 0 and stderr.find('Bad configuration option: ControlPersist') != -1:
            raise errors.AnsibleError('using -c ssh on certain older ssh versions may not support ControlPersist, set ANSIBLE_SSH_ARGS="" (or ansible_ssh_args in the config file) before running again')

        return (p.returncode, '', stdout, stderr)
243485
function script(req, res, next) {
  switch (req.get('SHELL')) {
  case '/bin/sh':
  case '/bin/bash':
    res.render(__dirname + '/init.bash');
    break;
  default:
    next(new Error('Unsupported shell: ' + req.get('SHELL')));
  }
}
12346
def runCommandSplits(splits, silent=False, shell=False):
    
    try:
        if silent:
            with open(os.devnull, 'w') as devnull:
                subprocess.check_call(
                    splits, stdout=devnull, stderr=devnull, shell=shell)
        else:
            subprocess.check_call(splits, shell=shell)
    except OSError as exception:
        if exception.errno == 2:  # cmd not found
            raise Exception(
                "Can't find command while trying to run {}".format(splits))
        else:
            raise
1201073
def _do_run(cmd, checks, log_stdout=False):
    
    cmd, shell_arg, executable_arg = _normalize_cmd_args(cmd)
    s = subprocess.Popen(cmd, shell=shell_arg, executable=executable_arg,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT, close_fds=True)
    debug_stdout = collections.deque(maxlen=100)
    while 1:
        line = s.stdout.readline()
        if line:
            debug_stdout.append(line)
            if log_stdout:
                logger.debug(line.rstrip())
            else:
                logger.debug(line.rstrip())
        exitcode = s.poll()
        if exitcode is not None:
            for line in s.stdout:
                debug_stdout.append(line)
            if exitcode is not None and exitcode != 0:
                error_msg = " ".join(cmd) if not isinstance(cmd, basestring) else cmd
                error_msg += "\n"
                error_msg += "".join(debug_stdout)
                s.communicate()
                s.stdout.close()
                raise subprocess.CalledProcessError(exitcode, error_msg)
            else:
                break
    s.communicate()
    s.stdout.close()
    # Check for problems not identified by shell return codes
    if checks:
        for check in checks:
            if not check():
                raise IOError("External command failed")
1328452
def _run(cmd,
         cwd=None,
         stdin=None,
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         output_encoding=None,
         output_loglevel='debug',
         log_callback=None,
         runas=None,
         group=None,
         shell=DEFAULT_SHELL,
         python_shell=False,
         env=None,
         clean_env=False,
         prepend_path=None,
         rstrip=True,
         template=None,
         umask=None,
         timeout=None,
         with_communicate=True,
         reset_system_locale=True,
         ignore_retcode=False,
         saltenv='base',
         pillarenv=None,
         pillar_override=None,
         use_vt=False,
         password=None,
         bg=False,
         encoded_cmd=False,
         success_retcodes=None,
         success_stdout=None,
         success_stderr=None,
         **kwargs):
    '''
    
    '''
    if 'pillar' in kwargs and not pillar_override:
        pillar_override = kwargs['pillar']
    if output_loglevel != 'quiet' and _is_valid_shell(shell) is False:
        log.warning(
            'Attempt to run a shell command with what may be an invalid shell! '
            'Check to ensure that the shell <%s> is valid for this user.',
            shell
        )

    output_loglevel = _check_loglevel(output_loglevel)
    log_callback = _check_cb(log_callback)
    use_sudo = False

    if runas is None and '__context__' in globals():
        runas = __context__.get('runas')

    if password is None and '__context__' in globals():
        password = __context__.get('runas_password')

    # Set the default working directory to the home directory of the user
    # salt-minion is running as. Defaults to home directory of user under which
    # the minion is running.
    if not cwd:
        cwd = os.path.expanduser('~{0}'.format('' if not runas else runas))

        # make sure we can access the cwd
        # when run from sudo or another environment where the euid is
        # changed ~ will expand to the home of the original uid and
        # the euid might not have access to it. See issue #1844
        if not os.access(cwd, os.R_OK):
            cwd = '/'
            if salt.utils.platform.is_windows():
                cwd = os.path.abspath(os.sep)
    else:
        # Handle edge cases where numeric/other input is entered, and would be
        # yaml-ified into non-string types
        cwd = six.text_type(cwd)

    if bg:
        ignore_retcode = True
        use_vt = False

    if not salt.utils.platform.is_windows():
        if not os.path.isfile(shell) or not os.access(shell, os.X_OK):
            msg = 'The shell {0} is not available'.format(shell)
            raise CommandExecutionError(msg)
    if salt.utils.platform.is_windows() and use_vt:  # Memozation so not much overhead
        raise CommandExecutionError('VT not available on windows')

    if shell.lower().strip() == 'powershell':
        # Strip whitespace
        if isinstance(cmd, six.string_types):
            cmd = cmd.strip()

        # If we were called by script(), then fakeout the Windows
        # shell to run a Powershell script.
        # Else just run a Powershell command.
        stack = traceback.extract_stack(limit=2)

        # extract_stack() returns a list of tuples.
        # The last item in the list [-1] is the current method.
        # The third item[2] in each tuple is the name of that method.
        if stack[-2][2] == 'script':
            cmd = 'Powershell -NonInteractive -NoProfile -ExecutionPolicy Bypass -File ' + cmd
        elif encoded_cmd:
            cmd = 'Powershell -NonInteractive -EncodedCommand {0}'.format(cmd)
        else:
            cmd = 'Powershell -NonInteractive -NoProfile "{0}"'.format(cmd.replace('"', '\\"'))

    # munge the cmd and cwd through the template
    (cmd, cwd) = _render_cmd(cmd, cwd, template, saltenv, pillarenv, pillar_override)

    ret = {}

    # If the pub jid is here then this is a remote ex or salt call command and needs to be
    # checked if blacklisted
    if '__pub_jid' in kwargs:
        if not _check_avail(cmd):
            raise CommandExecutionError(
                'The shell command "{0}" is not permitted'.format(cmd)
            )

    env = _parse_env(env)

    for bad_env_key in (x for x, y in six.iteritems(env) if y is None):
        log.error('Environment variable \'%s\' passed without a value. '
                  'Setting value to an empty string', bad_env_key)
        env[bad_env_key] = ''

    def _get_stripped(cmd):
        # Return stripped command string copies to improve logging.
        if isinstance(cmd, list):
            return [x.strip() if isinstance(x, six.string_types) else x for x in cmd]
        elif isinstance(cmd, six.string_types):
            return cmd.strip()
        else:
            return cmd

    if output_loglevel is not None:
        # Always log the shell commands at INFO unless quiet logging is
        # requested. The command output is what will be controlled by the
        # 'loglevel' parameter.
        msg = (
            'Executing command {0}{1}{0} {2}{3}in directory \'{4}\'{5}'.format(
                '\'' if not isinstance(cmd, list) else '',
                _get_stripped(cmd),
                'as user \'{0}\' '.format(runas) if runas else '',
                'in group \'{0}\' '.format(group) if group else '',
                cwd,
                '. Executing command in the background, no output will be '
                'logged.' if bg else ''
            )
        )
        log.info(log_callback(msg))

    if runas and salt.utils.platform.is_windows():
        if not HAS_WIN_RUNAS:
            msg = 'missing salt/utils/win_runas.py'
            raise CommandExecutionError(msg)

        if isinstance(cmd, (list, tuple)):
            cmd = ' '.join(cmd)

        return win_runas(cmd, runas, password, cwd)

    if runas and salt.utils.platform.is_darwin():
        # we need to insert the user simulation into the command itself and not
        # just run it from the environment on macOS as that
        # method doesn't work properly when run as root for certain commands.
        if isinstance(cmd, (list, tuple)):
            cmd = ' '.join(map(_cmd_quote, cmd))

        cmd = 'su -l {0} -c "cd {1}; {2}"'.format(runas, cwd, cmd)
        # set runas to None, because if you try to run `su -l` as well as
        # simulate the environment macOS will prompt for the password of the
        # user and will cause salt to hang.
        runas = None

    if runas:
        # Save the original command before munging it
        try:
            pwd.getpwnam(runas)
        except KeyError:
            raise CommandExecutionError(
                'User \'{0}\' is not available'.format(runas)
            )

    if group:
        if salt.utils.platform.is_windows():
            msg = 'group is not currently available on Windows'
            raise SaltInvocationError(msg)
        if not which_bin(['sudo']):
            msg = 'group argument requires sudo but not found'
            raise CommandExecutionError(msg)
        try:
            grp.getgrnam(group)
        except KeyError:
            raise CommandExecutionError(
                'Group \'{0}\' is not available'.format(runas)
            )
        else:
            use_sudo = True

    if runas or group:
        try:
            # Getting the environment for the runas user
            # Use markers to thwart any stdout noise
            # There must be a better way to do this.
            import uuid
            marker = '<<<' + str(uuid.uuid4()) + '>>>'
            marker_b = marker.encode(__salt_system_encoding__)
            py_code = (
                'import sys, os, itertools; '
                'sys.stdout.write(\"' + marker + '\"); '
                'sys.stdout.write(\"\\0\".join(itertools.chain(*os.environ.items()))); '
                'sys.stdout.write(\"' + marker + '\");'
            )

            if use_sudo or __grains__['os'] in ['MacOS', 'Darwin']:
                env_cmd = ['sudo']
                # runas is optional if use_sudo is set.
                if runas:
                    env_cmd.extend(['-u', runas])
                if group:
                    env_cmd.extend(['-g', group])
                if shell != DEFAULT_SHELL:
                    env_cmd.extend(['-s', '--', shell, '-c'])
                else:
                    env_cmd.extend(['-i', '--'])
                env_cmd.extend([sys.executable])
            elif __grains__['os'] in ['FreeBSD']:
                env_cmd = ('su', '-', runas, '-c',
                           "{0} -c {1}".format(shell, sys.executable))
            elif __grains__['os_family'] in ['Solaris']:
                env_cmd = ('su', '-', runas, '-c', sys.executable)
            elif __grains__['os_family'] in ['AIX']:
                env_cmd = ('su', '-', runas, '-c', sys.executable)
            else:
                env_cmd = ('su', '-s', shell, '-', runas, '-c', sys.executable)
            msg = 'env command: {0}'.format(env_cmd)
            log.debug(log_callback(msg))

            env_bytes, env_encoded_err = subprocess.Popen(
                env_cmd,
                stderr=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stdin=subprocess.PIPE
            ).communicate(salt.utils.stringutils.to_bytes(py_code))
            marker_count = env_bytes.count(marker_b)
            if marker_count == 0:
                # Possibly PAM prevented the login
                log.error(
                    'Environment could not be retrieved for user \'%s\': '
                    'stderr=%r stdout=%r',
                    runas, env_encoded_err, env_bytes
                )
                # Ensure that we get an empty env_runas dict below since we
                # were not able to get the environment.
                env_bytes = b''
            elif marker_count != 2:
                raise CommandExecutionError(
                    'Environment could not be retrieved for user \'{0}\'',
                    info={'stderr': repr(env_encoded_err),
                          'stdout': repr(env_bytes)}
                )
            else:
                # Strip the marker
                env_bytes = env_bytes.split(marker_b)[1]

            if six.PY2:
                import itertools
                env_runas = dict(itertools.izip(*[iter(env_bytes.split(b'\0'))]*2))
            elif six.PY3:
                env_runas = dict(list(zip(*[iter(env_bytes.split(b'\0'))]*2)))

            env_runas = dict(
                (salt.utils.stringutils.to_str(k),
                 salt.utils.stringutils.to_str(v))
                for k, v in six.iteritems(env_runas)
            )
            env_runas.update(env)

            # Fix platforms like Solaris that don't set a USER env var in the
            # user's default environment as obtained above.
            if env_runas.get('USER') != runas:
                env_runas['USER'] = runas

            # Fix some corner cases where shelling out to get the user's
            # environment returns the wrong home directory.
            runas_home = os.path.expanduser('~{0}'.format(runas))
            if env_runas.get('HOME') != runas_home:
                env_runas['HOME'] = runas_home

            env = env_runas
        except ValueError as exc:
            log.exception('Error raised retrieving environment for user %s', runas)
            raise CommandExecutionError(
                'Environment could not be retrieved for user \'{0}\': {1}'.format(
                    runas, exc
                )
            )

    if reset_system_locale is True:
        if not salt.utils.platform.is_windows():
            # Default to C!
            # Salt only knows how to parse English words
            # Don't override if the user has passed LC_ALL
            env.setdefault('LC_CTYPE', 'C')
            env.setdefault('LC_NUMERIC', 'C')
            env.setdefault('LC_TIME', 'C')
            env.setdefault('LC_COLLATE', 'C')
            env.setdefault('LC_MONETARY', 'C')
            env.setdefault('LC_MESSAGES', 'C')
            env.setdefault('LC_PAPER', 'C')
            env.setdefault('LC_NAME', 'C')
            env.setdefault('LC_ADDRESS', 'C')
            env.setdefault('LC_TELEPHONE', 'C')
            env.setdefault('LC_MEASUREMENT', 'C')
            env.setdefault('LC_IDENTIFICATION', 'C')
            env.setdefault('LANGUAGE', 'C')
        else:
            # On Windows set the codepage to US English.
            if python_shell:
                cmd = 'chcp 437 > nul & ' + cmd

    if clean_env:
        run_env = env

    else:
        run_env = os.environ.copy()
        run_env.update(env)

    if prepend_path:
        run_env['PATH'] = ':'.join((prepend_path, run_env['PATH']))

    if python_shell is None:
        python_shell = False

    new_kwargs = {'cwd': cwd,
                  'shell': python_shell,
                  'env': run_env if six.PY3 else salt.utils.data.encode(run_env),
                  'stdin': six.text_type(stdin) if stdin is not None else stdin,
                  'stdout': stdout,
                  'stderr': stderr,
                  'with_communicate': with_communicate,
                  'timeout': timeout,
                  'bg': bg,
                  }

    if 'stdin_raw_newlines' in kwargs:
        new_kwargs['stdin_raw_newlines'] = kwargs['stdin_raw_newlines']

    if umask is not None:
        _umask = six.text_type(umask).lstrip('0')

        if _umask == '':
            msg = 'Zero umask is not allowed.'
            raise CommandExecutionError(msg)

        try:
            _umask = int(_umask, 8)
        except ValueError:
            raise CommandExecutionError("Invalid umask: '{0}'".format(umask))
    else:
        _umask = None

    if runas or group or umask:
        new_kwargs['preexec_fn'] = functools.partial(
                salt.utils.user.chugid_and_umask,
                runas,
                _umask,
                group)

    if not salt.utils.platform.is_windows():
        # close_fds is not supported on Windows platforms if you redirect
        # stdin/stdout/stderr
        if new_kwargs['shell'] is True:
            new_kwargs['executable'] = shell
        new_kwargs['close_fds'] = True

    if not os.path.isabs(cwd) or not os.path.isdir(cwd):
        raise CommandExecutionError(
            'Specified cwd \'{0}\' either not absolute or does not exist'
            .format(cwd)
        )

    if python_shell is not True \
            and not salt.utils.platform.is_windows() \
            and not isinstance(cmd, list):
        cmd = salt.utils.args.shlex_split(cmd)

    if success_retcodes is None:
        success_retcodes = [0]
    else:
        try:
            success_retcodes = [int(i) for i in
                                salt.utils.args.split_input(
                                    success_retcodes
                                )]
        except ValueError:
            raise SaltInvocationError(
                'success_retcodes must be a list of integers'
            )

    if success_stdout is None:
        success_stdout = []
    else:
        try:
            success_stdout = [i for i in
                              salt.utils.args.split_input(
                                 success_stdout
                              )]
        except ValueError:
            raise SaltInvocationError(
                'success_stdout must be a list of integers'
            )

    if success_stderr is None:
        success_stderr = []
    else:
        try:
            success_stderr = [i for i in
                              salt.utils.args.split_input(
                                 success_stderr
                              )]
        except ValueError:
            raise SaltInvocationError(
                'success_stderr must be a list of integers'
            )

    if not use_vt:
        # This is where the magic happens
        try:
            proc = salt.utils.timed_subprocess.TimedProc(cmd, **new_kwargs)
        except (OSError, IOError) as exc:
            msg = (
                'Unable to run command \'{0}\' with the context \'{1}\', '
                'reason: {2}'.format(
                    cmd if output_loglevel is not None else 'REDACTED',
                    new_kwargs,
                    exc
                )
            )
            raise CommandExecutionError(msg)

        try:
            proc.run()
        except TimedProcTimeoutError as exc:
            ret['stdout'] = six.text_type(exc)
            ret['stderr'] = ''
            ret['retcode'] = None
            ret['pid'] = proc.process.pid
            # ok return code for timeouts?
            ret['retcode'] = 1
            return ret

        if output_loglevel != 'quiet' and output_encoding is not None:
            log.debug('Decoding output from command %s using %s encoding',
                      cmd, output_encoding)

        try:
            out = salt.utils.stringutils.to_unicode(
                proc.stdout,
                encoding=output_encoding)
        except TypeError:
            # stdout is None
            out = ''
        except UnicodeDecodeError:
            out = salt.utils.stringutils.to_unicode(
                proc.stdout,
                encoding=output_encoding,
                errors='replace')
            if output_loglevel != 'quiet':
                log.error(
                    'Failed to decode stdout from command %s, non-decodable '
                    'characters have been replaced', cmd
                )

        try:
            err = salt.utils.stringutils.to_unicode(
                proc.stderr,
                encoding=output_encoding)
        except TypeError:
            # stderr is None
            err = ''
        except UnicodeDecodeError:
            err = salt.utils.stringutils.to_unicode(
                proc.stderr,
                encoding=output_encoding,
                errors='replace')
            if output_loglevel != 'quiet':
                log.error(
                    'Failed to decode stderr from command %s, non-decodable '
                    'characters have been replaced', cmd
                )

        if rstrip:
            if out is not None:
                out = out.rstrip()
            if err is not None:
                err = err.rstrip()
        ret['pid'] = proc.process.pid
        ret['retcode'] = proc.process.returncode
        if ret['retcode'] in success_retcodes:
            ret['retcode'] = 0
        ret['stdout'] = out
        ret['stderr'] = err
        if ret['stdout'] in success_stdout or ret['stderr'] in success_stderr:
            ret['retcode'] = 0
    else:
        formatted_timeout = ''
        if timeout:
            formatted_timeout = ' (timeout: {0}s)'.format(timeout)
        if output_loglevel is not None:
            msg = 'Running {0} in VT{1}'.format(cmd, formatted_timeout)
            log.debug(log_callback(msg))
        stdout, stderr = '', ''
        now = time.time()
        if timeout:
            will_timeout = now + timeout
        else:
            will_timeout = -1
        try:
            proc = salt.utils.vt.Terminal(
                    cmd,
                    shell=True,
                    log_stdout=True,
                    log_stderr=True,
                    cwd=cwd,
                    preexec_fn=new_kwargs.get('preexec_fn', None),
                    env=run_env,
                    log_stdin_level=output_loglevel,
                    log_stdout_level=output_loglevel,
                    log_stderr_level=output_loglevel,
                    stream_stdout=True,
                    stream_stderr=True
            )
            ret['pid'] = proc.pid
            while proc.has_unread_data:
                try:
                    try:
                        time.sleep(0.5)
                        try:
                            cstdout, cstderr = proc.recv()
                        except IOError:
                            cstdout, cstderr = '', ''
                        if cstdout:
                            stdout += cstdout
                        else:
                            cstdout = ''
                        if cstderr:
                            stderr += cstderr
                        else:
                            cstderr = ''
                        if timeout and (time.time() > will_timeout):
                            ret['stderr'] = (
                                'SALT: Timeout after {0}s\n{1}').format(
                                    timeout, stderr)
                            ret['retcode'] = None
                            break
                    except KeyboardInterrupt:
                        ret['stderr'] = 'SALT: User break\n{0}'.format(stderr)
                        ret['retcode'] = 1
                        break
                except salt.utils.vt.TerminalException as exc:
                    log.error('VT: %s', exc,
                              exc_info_on_loglevel=logging.DEBUG)
                    ret = {'retcode': 1, 'pid': '2'}
                    break
                # only set stdout on success as we already mangled in other
                # cases
                ret['stdout'] = stdout
                if not proc.isalive():
                    # Process terminated, i.e., not canceled by the user or by
                    # the timeout
                    ret['stderr'] = stderr
                    ret['retcode'] = proc.exitstatus
                    if ret['retcode'] in success_retcodes:
                        ret['retcode'] = 0
                    if ret['stdout'] in success_stdout or ret['stderr'] in success_stderr:
                        ret['retcode'] = 0
                ret['pid'] = proc.pid
        finally:
            proc.close(terminate=True, kill=True)
    try:
        if ignore_retcode:
            __context__['retcode'] = 0
        else:
            __context__['retcode'] = ret['retcode']
    except NameError:
        # Ignore the context error during grain generation
        pass

    # Log the output
    if output_loglevel is not None:
        if not ignore_retcode and ret['retcode'] != 0:
            if output_loglevel < LOG_LEVELS['error']:
                output_loglevel = LOG_LEVELS['error']
            msg = (
                'Command \'{0}\' failed with return code: {1}'.format(
                    cmd,
                    ret['retcode']
                )
            )
            log.error(log_callback(msg))
        if ret['stdout']:
            log.log(output_loglevel, 'stdout: %s', log_callback(ret['stdout']))
        if ret['stderr']:
            log.log(output_loglevel, 'stderr: %s', log_callback(ret['stderr']))
        if ret['retcode']:
            log.log(output_loglevel, 'retcode: %s', ret['retcode'])

    return ret
940375
def run_external_subprocess_in_background(command_list, env=None):
    """"""
    if system_os == "Windows":
        DETACHED_PROCESS = 0x00000008
        p = subprocess.Popen(command_list, shell=False, stdin=None, stdout=None,
                stderr=None, close_fds=True, creationflags=DETACHED_PROCESS, env=env)
    else:
        p = subprocess.Popen(command_list, shell=False, stdin=None, stdout=None,
                stderr=None, close_fds=True, env=env)
    return p
1033708
def _low_level_exec_command(self, conn, cmd, tmp, sudoable=False, executable=None):
        '''  '''

        if executable is None:
            executable = '/bin/sh'

        sudo_user = self.sudo_user
        rc, stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudo_user, sudoable=sudoable, executable=executable)

        if type(stdout) not in [ str, unicode ]:
            out = ''.join(stdout.readlines())
        else:
            out = stdout

        if type(stderr) not in [ str, unicode ]:
            err = ''.join(stderr.readlines())
        else:
            err = stderr

        if rc != None:
            return dict(rc=rc, stdout=out, stderr=err )
        else:
            return dict(stdout=out, stderr=err )
675155
def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
    """"""
    sub_cmds = []

    # Check if this is parser has sub-commands
    if parser is not None and parser._subparsers is not None:

        # Find the _SubParsersAction for the sub-commands of this parser
        for action in parser._subparsers._actions:
            if isinstance(action, argparse._SubParsersAction):
                for sub_cmd, sub_cmd_parser in action.choices.items():
                    sub_cmds.append(sub_cmd)

                    # Look for nested sub-commands
                    for nested_sub_cmd in get_sub_commands(sub_cmd_parser):
                        sub_cmds.append('{} {}'.format(sub_cmd, nested_sub_cmd))

                break

    sub_cmds.sort()
    return sub_cmds
1295754
def _run_tool(cmd, use_container=True, work_dir=None, log_file=None):
    
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join([str(x) for x in cmd])
    cmd = utils.local_path_export(at_start=use_container) + cmd
    if log_file:
        cmd += " 2>&1 | tee -a %s" % log_file
    try:
        print("Running: %s" % cmd)
        subprocess.check_call(cmd, shell=True)
    finally:
        if use_container and work_dir:
            _chown_workdir(work_dir)