示例#1
0
class GSH:
    "The main class.  First you build it, then you tweak it, then you execute it."

    def __init__(self, command, path=os.getcwd()):
        self.command = command;
        path = os.path.realpath(os.path.abspath(path))
        self.client = GitFSClient.getClientByPath(path)
        self.path = self.client.makeRootRelative(path)
        if self.path is None or self.path == '':
            self.path = '.'

    def execute(self, host):
        """Actually execute the command on host."""
        #The command has to be cd `find-path` && gsync && command
        self.client.sync()
        #ssh_command = "cd `ginfo.py -r \"%s\"` && gsync.py . && cd %s && %s" %(self.client.getID(), self.path, self.command)
        ssh_command = 'gsh.py --remote --id \"%s\" --path \"%s\" --command \"%s\"' %(self.client.getID(), self.path, self.command)
        self.ssh = SSH(host, [ ssh_command ]);
        self.ssh.execute()

    def setNonBlocking(self):
        fcntl.fcntl(self.stderr(), fcntl.F_SETFL, os.O_NDELAY)
        fcntl.fcntl(self.stdout(), fcntl.F_SETFL, os.O_NDELAY)
        fcntl.fcntl(self.stdin(), fcntl.F_SETFL, os.O_NDELAY)

    def pollInfo(self):
        """returns a dict of names: (fileno, read, write, error, ...) tuples.
        names are only used to pass back into the handler for read/write/errors.
        Anything after the 4th entry in the tuple is ignored.  The whole return from
        this function is passed back in, so extra information can be stashed all over the
        place.
        """
        return { 'stdout': [ self.ssh.stdin(), False, len(self.out_buffer) > 0, True ],
                'stdin' : [ self.ssh.stdout(),  len(self.in_buffer) < self.buffer_max, False, True ],
                'stderr': [ self.ssh.stderr(),  len(self.err_buffer) < self.buffer_max, False, True ]
                }

    def pollHit(self, name, res):
        if name == 'stdout':
            self.readStdout()
        elif name == 'stdin':
            self.writeStdin()
        elif name == 'stderr':
            self.readStderr()

    def _read(self, file, size):
        try:
            buff = file.read(size)
        except OS.IOError as ioe:
            if ioe.errno != errno.EAGAIN and ioe.errno != errno.EWOULDBLOCK:
                raise ioe
            buff = ''
        return buff
            
    def _write(self, file, buffer):
        try:
            ret = file.write(buffer)
        except OS.IOError as ioe:
            if ioe.errno != errno.EAGAIN and ioe.errno != errno.EWOULDBLOCK:
                raise ioe
            ret = 0
        return ret

    def readStdin(self):
        buff = self._read(self.stdin(), self.max_in_buff - len(self.in_buffer) )
        self.in_buffer.append(buff)
        for f in self.stdin_call_backs:
            f(self)

    def readStdout(self):
        buff = self._read(self.stdout(), self.max_out_buff - len(self.out_buffer) )
        self.out_buffer.append(buff)
        for f in self.stdout_call_backs:
            f(self)

    def readStderr(self):
        buff = self._read(self.stderr(), self.max_err_buff - len(self.err_buffer) )
        self.err_buffer.append(buff)
        for f in self.stderr_call_backs:
            f(self)

    def writeStdIn(self):
        if len(self.in_buff) > 0:
            w = self._write(self.stdin(), self.in_buff)
            if w > 0:
                self.in_buff = self.in_buff[w:]
                for f in self.stdin_call_backs:
                    f(self)

    def stdout(self):
        if self.ssh is None:
            return None
        return self.ssh.stdout()

    def stderr(self):
        if self.ssh is None:
            return None
        return self.ssh.stderr()

    def stderr(self):
        if self.ssh is None:
            return None
        return self.ssh.stderr()

    def read(self):
        return self.stdout().read()

    def write(self, buff):
        return self.stdin().write(buff)

    def readerr(self):
        return self.stderr().read()

    def displayAndWait(self):
        if self.ssh is not None:
            self.ssh.displayAndWait()