Ejemplo n.º 1
0
def tmpfile():
    """Return a closed file object to a new temporary file."""
    f = None
    try:
        f = misc.TmpFile('gdb')
        f.write('\n')
    finally:
        if f:
            f.close()
    return f
Ejemplo n.º 2
0
    def getargv(self):
        """Return the gdb argv list."""
        argv = [self.pgm]
        if os.name != 'nt':
            argv += ['-tty=%s' % self.options.tty]

        # build the gdb init temporary file
        try:
            self.f_init = misc.TmpFile('gdbscript')
            self.f_init.write(GDB_INIT)
        finally:
            if self.f_init:
                self.f_init.close()

        argv += ['-x'] + [self.f_init.name] + ['--interpreter=mi']
        if self.arglist:
            argv += self.arglist
        return argv
Ejemplo n.º 3
0
def gdb_batch(pgm, job):
    """Run job in gdb batch mode and return the result as a string."""
    # create the gdb script as a temporary file
    f = None
    try:
        f = misc.TmpFile('gdbscript')
        f.write(job)
    finally:
        if f:
            f.close()

    result = None
    try:
        result = subprocess.Popen(
            (pgm, '--interpreter=mi', '-batch', '-nx', '-x', f.name),
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE).communicate()[0]
    except OSError:
        raise ClewnError('cannot start gdb as "%s"' % pgm)

    return result
Ejemplo n.º 4
0
    def _vim_script(self, options):
        """Build the vim script.

        Each clewn vim command can be invoked as 'prefix' + 'cmd' with optional
        arguments.  The command with its arguments is invoked with ':nbkey' and
        received by pyclewn in a keyAtPos netbeans event.

        Return the file object of the vim script.

        """
        # create the vim script in a temporary file
        prefix = options.prefix.capitalize()
        f = None
        try:
            # pyclewn is started from within vim
            if not options.editor:
                if options.cargs:
                    f = open(options.cargs[0], 'w')
                else:
                    return None
            else:
                f = misc.TmpFile('vimscript')

            # set 'cpo' option to its vim default value
            f.write('let s:cpo_save=&cpo\n')
            f.write('set cpo&vim\n')

            # vim autocommands
            bufferlist_autocmd = ''
            if options.noname_fix == '0':
                bufferlist_autocmd = BUFFERLIST_AUTOCMD
            f.write(
                string.Template(AUTOCOMMANDS).substitute(
                    console=netbeans.CONSOLE,
                    variables=netbeans.VARIABLES_BUFFER,
                    bufferlist_autocmd=bufferlist_autocmd))

            # utility vim functions
            if options.noname_fix == '0':
                f.write(FUNCTION_BUFLIST)
            f.write(FUNCTION_SPLIT)
            f.write(FUNCTION_CONSOLE)

            # mapkeys function
            f.write(
                string.Template(FUNCTION_MAPKEYS).substitute(
                    console=netbeans.CONSOLE, location=options.window))

            # unmapkeys function
            f.write('function s:unmapkeys()\n')
            f.write('    try\n')
            f.write('    cunmap nbkey\n')
            f.write('   catch /.*/\n')
            f.write('   endtry\n')
            for key in self.mapkeys:
                f.write('try\n')
                f.write('unmap <%s>\n' % key)
                f.write('catch /.*/\n')
                f.write('endtry\n')
            f.write('endfunction\n')

            # setup pyclewn vim user defined commands
            if options.noname_fix != '0':
                function_nbcommand = FUNCTION_NBCOMMAND
            else:
                function_nbcommand = FUNCTION_NBCOMMAND_RESTRICT

            split_dbgvar_buf = ''
            if netbeans.Netbeans.getLength_fix != '0':
                split_dbgvar_buf = string.Template(
                    SPLIT_DBGVAR_BUF).substitute(
                        dbgvar_buf=netbeans.VARIABLES_BUFFER)
            f.write(
                string.Template(function_nbcommand).substitute(
                    console=netbeans.CONSOLE,
                    location=options.window,
                    split_dbgvar_buf=split_dbgvar_buf))
            noCompletion = string.Template(
                'command -bar -nargs=* ${pre}${cmd} '
                'call s:nbcommand("${cmd}", <f-args>)\n')
            fileCompletion = string.Template(
                'command -bar -nargs=* '
                '-complete=file ${pre}${cmd} '
                'call s:nbcommand("${cmd}", <f-args>)\n')
            listCompletion = string.Template(
                'command -bar -nargs=* '
                '-complete=custom,s:Arg_${cmd} ${pre}${cmd} '
                'call s:nbcommand("${cmd}", <f-args>)\n')
            argsList = string.Template('function s:Arg_${cmd}(A, L, P)\n'
                                       '\treturn "${args}"\n'
                                       'endfunction\n')
            for cmd, completion in self._get_cmds().iteritems():
                if cmd in ('mapkeys', 'unmapkeys'):
                    f.write(
                        string.Template('command! -bar ${pre}${cmd} call '
                                        's:${cmd}()\n').substitute(cmd=cmd,
                                                                   pre=prefix))
                    continue

                try:
                    iter(completion)
                except TypeError:
                    f.write(fileCompletion.substitute(pre=prefix, cmd=cmd))
                else:
                    if not completion:
                        f.write(noCompletion.substitute(pre=prefix, cmd=cmd))
                    else:
                        f.write(listCompletion.substitute(pre=prefix, cmd=cmd))
                        args = '\\n'.join(completion)
                        f.write(argsList.substitute(args=args, cmd=cmd))

            # add debugger specific vim statements
            f.write(self.vim_script_custom(prefix))

            # reset 'cpo' option
            f.write('let &cpo = s:cpo_save\n')

            # delete the vim script after it has been sourced
            f.write('\ncall delete(expand("<sfile>"))\n')
        finally:
            if f:
                f.close()

        return f