Run a command on a unit, return the output and exit code.
172273
def run_cmd_unit(self, sentry_unit, cmd):
        """"""
        output, code = sentry_unit.run(cmd)
        if code == 0:
            self.log.debug('{} `{}` command returned {} '
                           '(OK)'.format(sentry_unit.info['unit_name'],
                                         cmd, code))
        else:
            msg = ('{} `{}` command returned {} '
                   '{}'.format(sentry_unit.info['unit_name'],
                               cmd, code, output))
            amulet.raise_status(amulet.FAIL, msg=msg)
        return str(output), code
770016
def run(self):
        """"""
        self.logger.debug(f'Running command: {self.cmd}')

        def execute(cmd):
            output = ""
            popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                                     universal_newlines=True, shell=True)
            for stdout_line in iter(popen.stdout.readline, ""):
                stdout_line = stdout_line.strip('\n')
                output += stdout_line
                yield stdout_line
            popen.stdout.close()
            return_code = popen.wait()
            if return_code:
                raise subprocess.CalledProcessError(return_code, cmd, output)

        try:
            for out in execute(self.cmd):
                self.logger.info(out)
            return "", 0
        except subprocess.CalledProcessError as e:
            return e.output, e.returncode
394115
def execute(self, command):
        

        logger.info("running command %s", command)
        cmd = ["podman", "exec", self.get_id()] + command
        output = run_cmd(cmd, return_output=True)

        return output
30127
def run_with_codes(command)
      # pid, stdin, stdout, stderr = Open4::popen4(command)
      # ignored, status = Process::waitpid2 pid
      # exit_code = status.to_i
      exit_signal = nil
      
      stdout_data = ''
      stderr_data = ''
      status = POpen4::popen4(command) do |stdout, stderr, stdin, pid|
        stdin.close
        stdout_data = stdout.read
        stderr_data = stderr.read.strip
      end

      exit_code = status ? status.exitstatus : 0

      return stdout_data, stderr_data, exit_code, exit_signal
    end
763776
def runu(command, timeout=30, withexitstatus=False, events=None,
        extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
    
    kwargs.setdefault('encoding', 'utf-8')
    return run(command, timeout=timeout, withexitstatus=withexitstatus,
                events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
                env=env, **kwargs)
447019
def run(cmd, shell=False, debug=False):
    ''
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell)
    (out, _) = proc.communicate()  # no need for stderr
    if debug:
        print(cmd)
        print(out)
    return out
1095274
def _run_cmd(cmd):
    """"""
    if not os.path.exists(cmd[0]):
        cmd[0] = shutil.which(cmd[0])
        assert cmd[0] is not None

    shebang_parts = parseshebang.parse(cmd[0])

    proc = subprocess.Popen(shebang_parts + cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    return {
        "stdout": stdout,
        "stderr": stderr,
        "code": proc.returncode
    }
171513
def execute_command(cmd, execute, echo=True):
    """"""
    if execute:
        if echo:
            print("Executing: " + cmd)
        return os.system(cmd)
    else:
        print(cmd)
        return 0
1201717
def run_command(self, codeobj):
        """"""
        try:
            value, stdout = yield from self.attempt_exec(codeobj, self.namespace)
        except Exception:
            yield from self.send_exception()
            return
        else:
            yield from self.send_output(value, stdout)
371291
def call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
         on_error='ignore', returncode=False):
    
    # start external command process
    p = subprocess.Popen(command, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    # get outputs
    out, _ = p.communicate()

    # throw exception if process failed
    if p.returncode != 0 and on_error == 'raise':
        raise GitInvocationError('Failed to run "%s"' % " ".join(command))

    if returncode:
        return out.strip(), p.returncode
    else:
        return out.strip()
313416
def run_cli
      exit_code = begin
        $stderr = @stderr
        $stdin = @stdin
        $stdout = @stdout

        Codewars::CLI.start(@argv)

        0
      rescue StandardError => e
        b = e.backtrace
        @stderr.puts("#{b.shift}: #{e.message} (#{e.class})")
        @stderr.puts(b.map { |s| "\tfrom #{s}" }.join("\n"))
        1
      rescue SystemExit => e
        e.status
      ensure
        $stderr = STDERR
        $stdin = STDIN
        $stdout = STDOUT
      end

      @kernel.exit(exit_code)
    end
501848
def check_commands_on_units(self, commands, sentry_units):
        
        self.log.debug('Checking exit codes for {} commands on {} '
                       'sentry units...'.format(len(commands),
                                                len(sentry_units)))
        for sentry_unit in sentry_units:
            for cmd in commands:
                output, code = sentry_unit.run(cmd)
                if code == 0:
                    self.log.debug('{} `{}` returned {} '
                                   '(OK)'.format(sentry_unit.info['unit_name'],
                                                 cmd, code))
                else:
                    return ('{} `{}` returned {} '
                            '{}'.format(sentry_unit.info['unit_name'],
                                        cmd, code, output))
        return None
311826
def run_cmd(command, verbose=True, shell='/bin/bash'):
    """"""
    process = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT, executable=shell)
    output = process.stdout.read().decode().strip().split('\n')
    if verbose:
        # return full output including empty lines
        return output
    return [line for line in output if line.strip()]
411915
def runCmd(cls, cmd):
        
        cit.echo(cmd, "command")
        result = os.system(cmd)
        cls.checkResult(result)
148542
def _run(self, command: List[str], notebook: Optional[str] = None) -> int:
        """"""
        if notebook is None:
            return Popen(command, cwd=self._build_dir).wait()
        cmd = Popen(command, cwd=self._build_dir, stdout=PIPE, stderr=STDOUT)
        while True:
            line = cmd.stdout.readline()
            if line == b'' and cmd.poll() is not None:
                return cmd.poll()
            print(line.decode('utf-8'), end='')
        raise Exception()
494614
def run(command, show=True, *args, **kwargs):
    
    if show:
        print_command(command)
    with hide("running"):
        return _run(command, *args, **kwargs)
297089
def execd_run(command, execd_dir=None, die_on_error=True, stderr=subprocess.STDOUT):
    """"""
    for submodule_path in execd_submodule_paths(command, execd_dir):
        try:
            subprocess.check_output(submodule_path, stderr=stderr,
                                    universal_newlines=True)
        except subprocess.CalledProcessError as e:
            hookenv.log("Error ({}) running  {}. Output: {}".format(
                e.returncode, e.cmd, e.output))
            if die_on_error:
                sys.exit(e.returncode)