Passes a command list to subprocess.check_output, returning None
    if an expected exception is raised
    :param cmd: The command to execute
    :return: Stripped string output of the command, or None if error
329111
def check_output_and_strip(cmd):  # type: (List[Text]) -> Optional[Text]
    
    try:
        result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        return result.strip()
    except (OSError, subprocess.CalledProcessError, TypeError, AttributeError):
        # OSError is raised if command doesn't exist
        # CalledProcessError is raised if command returns nonzero
        # AttributeError is raised if result cannot be strip()ped
        return None
836685
def until_cmd(listcmd, end_expects=None, save2logfile=None, coding = encoding):
        '''         
        '''
        
        
        if end_expects and not isinstance(end_expects, p_compat.str):
            raise Exception("invalide unicode string: '%s'" %end_expects)
        
        lines = []    
        subp = subprocess.Popen(listcmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        while subp.poll()==None:
            next_line = subp.stdout.readline().decode(coding)
            if next_line:
#                 print(next_line)
                lines.append(next_line)
                if end_expects and re.search(end_expects, next_line):
                    result = True
                else:
                    result = False        
        subp.stdout.close()
        
        if subp.returncode:
            result = False
            lines.append("sub command error code: %s" %subp.returncode)
        
        if save2logfile:
            with open(save2logfile, 'a') as f:
                f.writelines(lines)
                                    
        return result
281991
def cmd_ok(cmd):
     
    try:
        sp.check_call(cmd, stderr=sp.PIPE, stdout=sp.PIPE)
    except sp.CalledProcessError:
        # bwa gives return code of 1 with no argument
        pass
    except:
        sys.stderr.write("{} not found, skipping\n".format(cmd))
        return False
    return True
756277
def handle_command(command):
  
  try:
    cmds = command.split(None, 1)
    cmd = cmds[0]
    if cmd == 'new':
      add_task(get_arg(cmds))
    elif cmd == 'done':
      mark_done(int(get_arg(cmds)))
    elif cmd == 'list':
      for task in format_tasks(list_tasks()):
        print task
    elif cmd == 'delete':
      delete_task(int(get_arg(cmds)))
    else:
      print_usage()
  except Exception, e:  # pylint: disable=broad-except
    print e
    print_usage()
602666
def cmd_output(self, args):
        ''''''
        if len(args) < 1 or args[0] == "list":
            self.cmd_output_list()
        elif args[0] == "add":
            if len(args) != 2:
                print("Usage: output add OUTPUT")
                return
            self.cmd_output_add(args[1:])
        elif args[0] == "remove":
            if len(args) != 2:
                print("Usage: output remove OUTPUT")
                return
            self.cmd_output_remove(args[1:])
        elif args[0] == "sysid":
            if len(args) != 3:
                print("Usage: output sysid SYSID OUTPUT")
                return
            self.cmd_output_sysid(args[1:])
        else:
            print("usage: output <list|add|remove|sysid>")
520191
def check_output(self, cmd):
        
        p = self.Popen(cmd, stdout=subprocess.PIPE)
        output, _ = p.communicate()
        if p.returncode != 0:
            raise RemoteCalledProcessError(p.returncode, cmd, self.host, output=output)
        return output
188134
def get_cmd_output_now(self, exe, suggest_filename=None,
                           root_symlink=False, timeout=300, stderr=True,
                           chroot=True, runat=None, env=None,
                           binary=False, sizelimit=None, pred=None):
        
        if not self.test_predicate(cmd=True, pred=pred):
            self._log_info("skipped cmd output '%s' due to predicate (%s)" %
                           (exe, self.get_predicate(cmd=True, pred=pred)))
            return None

        return self._get_cmd_output_now(exe, timeout=timeout, stderr=stderr,
                                        chroot=chroot, runat=runat,
                                        env=env, binary=binary,
                                        sizelimit=sizelimit)
707952
def get_cmd_output(*args, encoding: str = SYS_ENCODING) -> str:
    
    log.debug("get_cmd_output(): args = {!r}", args)
    p = subprocess.Popen(args, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    return stdout.decode(encoding, errors='ignore')
233530
def check_commands(self, cmd_list):
        
        # if a string is passed then convert it to a list.  This prevents an
        # easy mistake that could be made
        if isinstance(cmd_list, basestring):
            cmd_list = [cmd_list]

        for cmd in cmd_list:
            if self.command_run("which {}".format(cmd)) == 0:
                return cmd
919424
def subproc_call(cmd, timeout=None):
    
    try:
        output = subprocess.check_output(
            cmd, stderr=subprocess.STDOUT,
            shell=True, timeout=timeout)
        return output, 0
    except subprocess.TimeoutExpired as e:
        logger.warn("Command '{}' timeout!".format(cmd))
        logger.warn(e.output.decode('utf-8'))
        return e.output, -1
    except subprocess.CalledProcessError as e:
        logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode))
        logger.warn(e.output.decode('utf-8'))
        return e.output, e.returncode
    except Exception:
        logger.warn("Command '{}' failed to run.".format(cmd))
        return "", -2
1247768
def run_command(cmd, redirect_output=True, check_exit_code=True):
    
    if redirect_output:
        stdout = subprocess.PIPE
    else:
        stdout = None
    proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
    output = proc.communicate()[0]
    if check_exit_code and proc.returncode != 0:
        raise Exception('Command "%s" failed.\n%s' % (' '.join(cmd), output))
    return output
1114801
def check_output(params):
    
    popen = subprocess.Popen(params, shell=True, stderr=subprocess.STDOUT,
                             stdout=subprocess.PIPE)

    output, _ = popen.communicate()
    returncode = popen.poll()
    if returncode != 0:
        error = subprocess.CalledProcessError(returncode=returncode, cmd=params)
        error.output = output
        raise error
    return output
1008646
def cmd(str, print_ret=False, usr_pwd=None, run=True):
	
	if usr_pwd:
		str = 'echo {} | sudo -u {} {} '.format(usr_pwd[1], usr_pwd[0], str)

	print('  [>] {}'.format(str))

	if run:
		err, ret = commands.getstatusoutput(str)
	else:
		err = None
		ret = None

	if err:
		print('  [x] {}'.format(ret))
		raise Exception(ret)
	if ret and print_ret:
		lines = ret.split('\n')
		for line in lines:
			print('  [<] {}'.format(line))
	return ret
217445
def execute(cmd, cwd=None):
    
    try:
        lines = subprocess \
            .check_output(cmd, cwd=cwd, stderr=DEVNULL) \
            .splitlines()
    except subprocess.CalledProcessError:
        return None
    else:
        if lines:
            return decode(lines[0].strip())
        else:
            return None
911214
def strip_command(self, command_string, output):
        """"""
        output_list = output.split(command_string)
        return self.RESPONSE_RETURN.join(output_list)
697212
def command_output(command, expect_command = true)  #:doc:
      # get everything except for the ending prompt.
      ret =
          if (prompt_pos = (output =~ prompt_match))
            output[0...prompt_pos]
          else
            output
          end

      if expect_command
        command_regex = command_match(command)

        # Go until we run out of data or we find one of the possible command starts.
        # Note that we EXPECT the command to the first line of the output from the command because we expect the
        # shell to echo it back to us.
        result_cmd,_,result_data = ret.partition("\n")
        until result_data.to_s.strip == '' || result_cmd.strip =~ command_regex
          result_cmd,_,result_data = result_data.partition("\n")
        end

        if result_cmd.nil? || !(result_cmd =~ command_regex)
          STDERR.puts "SHELL WARNING: Failed to match #{command_regex.inspect}."
        end

        result_data
      else
        ret
      end
    end
427496
def runCommand(cmd, timeout=None):
    
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''

    out, err = p.communicate()
    p.wait(timeout)

    return (out, err)