Ejemplo n.º 1
0
def command(params,child_in=None):
    cmd_str=' '.join(('"%s"' % i if ' ' in i else i for i in params))
    ld('>',cmd_str,child_in)
    if win32pipe:
        (stdin,stdout,stderr)=win32pipe.popen3(cmd_str,'t')
        if child_in:
            stdin.write(child_in)
        stdin.close()
        child_out=stdout.read()
        child_err=stderr.read()
        if child_err:
            logging.warning(child_err)
    else:
        process=Popen(params,stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        (child_out,child_err)=process.communicate(child_in)
        if process.returncode != 0: 
            raise Exception("*** External program failed: %s\n%s" % (cmd_str,child_err))
    ld('<',child_out,child_err)
    return child_out
def command(params, child_in=None):
    cmd_str = ' '.join(('"%s"' % i if ' ' in i else i for i in params))
    ld('>', cmd_str, child_in)
    if win32pipe:
        (stdin, stdout, stderr) = win32pipe.popen3(cmd_str, 't')
        if child_in:
            stdin.write(child_in)
        stdin.close()
        child_out = stdout.read()
        child_err = stderr.read()
        if child_err:
            logging.warning(child_err)
    else:
        process = Popen(params, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        (child_out, child_err) = process.communicate(child_in)
        if process.returncode != 0:
            error("*** External program error: %s\n%s" % (cmd_str, child_err))
            raise EnvironmentError(process.returncode, child_err)
    ld('<', child_out, child_err)
    return child_out
Ejemplo n.º 3
0
    def setup_pty( self, _use_pty ):

        self.using_pty = _use_pty

        if _use_pty:

            ##  The lower this number is the more responsive some commands
            ##  may be ( printing prompt, ls ), but also the quicker others
            ##  may timeout reading their output ( ping, ftp )

            self.delay = 0.1

            ##  Hack to get pty name until I can figure out to get name
            ##  of slave pty using pty.fork() I've tried everything
            ##  including using all of the python src for pty.fork().
            ##  I'm probably trying to do something I can't do. However,
            ##  there does seem to be a std call named ptsname() which
            ##  returns the slave pty name i.e. /dev/pty/XX

            ##  Assumption is, that between the dummy call to
            ##  master_open is done and the pty.fork happens, we'll be
            ##  the next pty entry after the one from pty.master_open()
            ##  According to SysV docs it will look for the first
            ##  unused, so this shouldn't be too bad besides its looks.
            ##  Only have to make sure they're not already in use and
            ##  if it is try the next one etc.

            self.master, pty_name = pty.master_open()
            dbg_print ( 'setup_pty: slave pty name is ' + pty_name )

            self.pid, self.fd = pty.fork()

            self.outd = self.fd
            self.ind  = self.fd
            self.errd = self.fd

            signal.signal( signal.SIGCHLD, self.sigchld_handler )

            if self.pid == 0:

                ##  In spawned shell process, NOTE: any 'print'ing done within
                ##  here will corrupt vim.

                attrs = tty.tcgetattr( 1 )

                attrs[ 6 ][ tty.VMIN ]  = 1
                attrs[ 6 ][ tty.VTIME ] = 0
                attrs[ 0 ] = attrs[ 0 ] | tty.BRKINT
                attrs[ 0 ] = attrs[ 0 ] & tty.IGNBRK
                attrs[ 3 ] = attrs[ 3 ] & ~tty.ICANON & ~tty.ECHO

                tty.tcsetattr( 1, tty.TCSANOW, attrs )

                if self.arg != '':
                    os.execv( self.sh, [ self.sh, self.arg ] )

                else:
                    os.execv( self.sh, [ self.sh, ] )

            else:

                try:
                    attrs = tty.tcgetattr( 1 )

                    termios_keys = attrs[ 6 ]

                except:
                    dbg_print ( 'setup_pty: tcgetattr failed' )
                    return

                #  Get *real* key-sequence for standard input keys, i.e. EOF

                self.eof_key   = termios_keys[ tty.VEOF ]
                self.eol_key   = termios_keys[ tty.VEOL ]
                self.erase_key = termios_keys[ tty.VERASE ]
                self.intr_key  = termios_keys[ tty.VINTR ]
                self.kill_key  = termios_keys[ tty.VKILL ]
                self.susp_key  = termios_keys[ tty.VSUSP ]

        else:

            ##  Use pipes on Win32. not as reliable/nice. works OK but with limitations.

            self.delay = 0.2

            try:
                import win32pipe

                dbg_print ( 'setup_pty: using windows extensions' )
                self.stdin, self.stdout, self.stderr = win32pipe.popen3( self.sh + " " + self.arg )

            except ImportError:

                dbg_print ( 'setup_pty: not using windows extensions' )
                self.stdout, self.stdin, self.stderr = popen2.popen3( self.sh + " " + self.arg, -1, 'b' )

            self.outd = self.stdout.fileno()
            self.ind  = self.stdin.fileno ()
            self.errd = self.stderr.fileno()

            self.intr_key = ''
            self.eof_key  = ''
Ejemplo n.º 4
0
    def setup_pty(self, _use_pty):

        self.using_pty = _use_pty

        if _use_pty:

            ##  The lower this number is the more responsive some commands
            ##  may be ( printing prompt, ls ), but also the quicker others
            ##  may timeout reading their output ( ping, ftp )

            self.delay = 0.1

            ##  Hack to get pty name until I can figure out to get name
            ##  of slave pty using pty.fork( ) I've tried everything
            ##  including using all of the python src for pty.fork( ).
            ##  I'm probably trying to do something I can't do. However,
            ##  there does seem to be a std call named ptsname( ) which
            ##  returns the slave pty name i.e. /dev/pty/XX

            ##  Assumption is, that between the dummy call to
            ##  master_open is done and the pty.fork happens, we'll be
            ##  the next pty entry after the one from pty.master_open( )
            ##  According to SysV docs it will look for the first
            ##  unused, so this shouldn't be too bad besides its looks.
            ##  Only have to make sure they're not already in use and
            ##  if it is try the next one etc.

            self.master, pty_name = pty.master_open()
            dbg_print('setup_pty: slave pty name is ' + pty_name)

            self.pid, self.fd = pty.fork()

            self.outd = self.fd
            self.ind = self.fd
            self.errd = self.fd

            signal.signal(signal.SIGCHLD, self.sigchld_handler)

            if self.pid == 0:

                ##  In spawned shell process, NOTE: any 'print'ing done within
                ##  here will corrupt vim.

                attrs = tty.tcgetattr(1)

                attrs[6][tty.VMIN] = 1
                attrs[6][tty.VTIME] = 0
                attrs[0] = attrs[0] | tty.BRKINT
                attrs[0] = attrs[0] & tty.IGNBRK
                attrs[3] = attrs[3] & ~tty.ICANON & ~tty.ECHO

                tty.tcsetattr(1, tty.TCSANOW, attrs)

                dbg_print('setup_pty: terminal attributes after setting them')
                dump_attrs(attrs)

                if self.arg != '':
                    os.execv(self.sh, [self.sh, self.arg])

                else:
                    os.execv(self.sh, [
                        self.sh,
                    ])

                ##  dump_attrs( attrs )  <-- TODO: Get this to work

            else:

                try:
                    attrs = tty.tcgetattr(1)

                    termios_keys = attrs[6]

                except:
                    dbg_print('setup_pty: tcgetattr failed')
                    return

                #  Get *real* key-sequence for standard input keys, i.e. EOF

                self.eof_key = termios_keys[tty.VEOF]
                self.eol_key = termios_keys[tty.VEOL]
                self.erase_key = termios_keys[tty.VERASE]
                self.intr_key = termios_keys[tty.VINTR]
                self.kill_key = termios_keys[tty.VKILL]
                self.susp_key = termios_keys[tty.VSUSP]

        else:

            ##  Use pipes on Win32. not as reliable/nice. works OK but with limitations.

            self.delay = 0.2

            try:
                import win32pipe

                dbg_print('setup_pty: using windows extensions')
                self.stdin, self.stdout, self.stderr = win32pipe.popen3(
                    self.sh + " " + self.arg)

            except ImportError:

                dbg_print('setup_pty: not using windows extensions')
                self.stdout, self.stdin, self.stderr = popen2.popen3(
                    self.sh + " " + self.arg, -1, 'b')

            self.outd = self.stdout.fileno()
            self.ind = self.stdin.fileno()
            self.errd = self.stderr.fileno()

            self.intr_key = ''
            self.eof_key = ''