示例#1
0
    def streamCommand(cmd, cwd=None, shell=False):
        try:
            logger.debug("STREAM COMMAND: %s", cmd)
            proc = Popen(shlex.split(cmd),
                         shell=shell,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         cwd=cwd)
            stdout = b''
            stderr = b''
            while True:
                stdout = readAppendFlush(sys.stdout, stdout, 1)
                stderr = readAppendFlush(sys.stderr, stderr, 1)

                if not d and not de and proc.poll() is not None:
                    break

            stdout = stdout.encode()
            stderr = stderr.encode()

            if proc.returncode == 0:
                return OK({"stdout": stdout, "stderr": stderr})
            else:
                return Fail(
                    ShellCommandError(code=proc.returncode,
                                      message=stdout,
                                      stdout=stdout,
                                      stderr=stderr))
        except OSError as err:
            err.strerror = "Error running '%s': %s" % (cmd, err.strerror)
            return Fail(err)
        except KeyboardInterrupt:
            logger.info("CTRL-C Received...Exiting.")
            return Fail(UserInterruptError())
示例#2
0
 def chmod(path, mode):
     try:
         os.chmod(path, mode)
         return OK(path)
     except Exception as err:
         return Fail(
             ShellCommandError(code=1,
                               message="Failed to chmod %s: %s" %
                               (path, err)))
示例#3
0
 def command(cmd):
     logger.debug("COMMAND: %s", cmd)
     try:
         out = check_output(cmd, shell=True)
         return OK(out.decode().strip())
     except CalledProcessError as err:
         return Fail(
             ShellCommandError(code=err.returncode,
                               message=err.output,
                               stdout=err.output))
示例#4
0
 def nukeDirectory(path):
     try:
         if path and path is not '/' and path is not 'C:\\':
             shutil.rmtree(path)
         return OK(None)
     except Exception as err:
         return Fail(
             ShellCommandError(code=1,
                               message="Failed to rmtree: %s: %s" %
                               (path, err)))
示例#5
0
 def makeDirectory(path, mode=0o750):
     if not os.path.exists(path):
         try:
             os.makedirs(path, mode)
             return OK(path)
         except Exception as err:
             return Fail(
                 ShellCommandError(code=1,
                                   message="Failed to create %s: %s" %
                                   (path, err)))
     return OK(path)
示例#6
0
 def call(cmd, cwd=None, shell=True, sudo=False):
     try:
         mustSleep = False
         if sudo and not shell:
             if isCygwin():
                 mustSleep = True
                 cmd = ["cygstart", "--action=runas"] + cmd
             else:
                 cmd = ["sudo"] + cmd
         logger.debug("COMMAND[%s]: %s", cwd, cmd)
         returncode = call(cmd, shell=shell, cwd=cwd)
         if returncode == 0:
             # Allow command to finish on Windows (cygstart exits early)
             return OK(None).then(lambda: None
                                  if not mustSleep else time.sleep(1))
         else:
             return Fail(ShellCommandError(code=returncode))
     except CalledProcessError as err:
         return Fail(
             ShellCommandError(code=err.returncode,
                               message=err.output,
                               stdout=err.output))
示例#7
0
 def procCommand(cmd, cwd=None):
     try:
         logger.debug("COMMAND: %s", cmd)
         proc = Popen(shlex.split(cmd),
                      shell=False,
                      stdout=subprocess.PIPE,
                      stderr=subprocess.PIPE,
                      cwd=cwd)
         pout, perr = proc.communicate()
         if proc.returncode == 0:
             return OK({"stdout": pout, "stderr": perr})
         else:
             return Fail(
                 ShellCommandError(code=proc.returncode,
                                   message=pout,
                                   stdout=pout,
                                   stderr=perr))
     except KeyboardInterrupt:
         logger.info("CTRL-C Received...Exiting.")
         return Fail(UserInterruptError())
示例#8
0
    def streamCommand(cmd, cwd=None, shell=False):
        try:
            logger.debug("COMMAND: %s", cmd)
            proc = Popen(shlex.split(cmd),
                         shell=shell,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         cwd=cwd)
            stdout = ''
            stderr = ''
            while True:
                d = proc.stdout.read(1)
                if d != '':
                    stdout += d
                    sys.stdout.write(d)
                    sys.stdout.flush()

                de = proc.stderr.read(1)
                if de != '':
                    stderr += de
                    sys.stderr.write(de)
                    sys.stderr.flush()

                if d == '' and de == '' and proc.poll() is not None:
                    break

            if proc.returncode == 0:
                return OK({"stdout": stdout, "stderr": stderr})
            else:
                return Fail(
                    ShellCommandError(code=proc.returncode,
                                      message=stdout,
                                      stdout=stdout,
                                      stderr=stderr))
        except OSError as err:
            err.strerror = "Error running '%s': %s" % (cmd, err.strerror)
            return Fail(err)
        except KeyboardInterrupt:
            logger.info("CTRL-C Received...Exiting.")
            return Fail(UserInterruptError())