Esempio n. 1
0
    def parse_options(self, arg_str, opt_str, *long_opts, **kw):
        """Parse options passed to an argument string.

        The interface is similar to that of getopt(), but it returns back a
        Struct with the options as keys and the stripped argument string still
        as a string.

        arg_str is quoted as a true sys.argv vector by using shlex.split.
        This allows us to easily expand variables, glob files, quote
        arguments, etc.

        Options:
          -mode: default 'string'. If given as 'list', the argument string is
          returned as a list (split on whitespace) instead of a string.

          -list_all: put all option values in lists. Normally only options
          appearing more than once are put in a list.

          -posix (True): whether to split the input line in POSIX mode or not,
          as per the conventions outlined in the shlex module from the
          standard library."""

        # inject default options at the beginning of the input line
        caller = sys._getframe(1).f_code.co_name
        arg_str = "%s %s" % (self.options_table.get(caller, ""), arg_str)

        mode = kw.get("mode", "string")
        if mode not in ["string", "list"]:
            raise ValueError, "incorrect mode given: %s" % mode
        # Get options
        list_all = kw.get("list_all", 0)
        posix = kw.get("posix", os.name == "posix")
        strict = kw.get("strict", True)

        # Check if we have more than one argument to warrant extra processing:
        odict = {}  # Dictionary with options
        args = arg_str.split()
        if len(args) >= 1:
            # If the list of inputs only has 0 or 1 thing in it, there's no
            # need to look for options
            argv = arg_split(arg_str, posix, strict)
            # Do regular option processing
            try:
                opts, args = getopt(argv, opt_str, long_opts)
            except GetoptError, e:
                raise UsageError('%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts)))
            for o, a in opts:
                if o.startswith("--"):
                    o = o[2:]
                else:
                    o = o[1:]
                try:
                    odict[o].append(a)
                except AttributeError:
                    odict[o] = [odict[o], a]
                except KeyError:
                    if list_all:
                        odict[o] = [a]
                    else:
                        odict[o] = a
Esempio n. 2
0
def pudb(line):
    """
    Debug a script (like %run -d) in the IPython process, using PuDB.

    Usage:

    %pudb test.py [args]
        Run script test.py under PuDB.
    """

    # Get the running instance

    if not line.strip():
        print(pudb.__doc__)
        return

    from IPython.utils.process import arg_split
    args = arg_split(line)

    path = os.path.abspath(args[0])
    args = args[1:]
    if not os.path.isfile(path):
        from IPython.core.error import UsageError
        raise UsageError("%%pudb: file %s does not exist" % path)

    from pudb import runscript
    runscript(path, args)
Esempio n. 3
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             [u'hi', [u'hi']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 4
0
def pudb(line):
    """
    Debug a script (like %run -d) in the IPython process, using PuDB.

    Usage:

    %pudb test.py [args]
        Run script test.py under PuDB.
    """

    # Get the running instance

    if not line.strip():
        print(pudb.__doc__)
        return

    from IPython.utils.process import arg_split
    args = arg_split(line)

    path = os.path.abspath(args[0])
    args = args[1:]
    if not os.path.isfile(path):
        from IPython.core.error import UsageError
        raise UsageError("%%pudb: file %s does not exist" % path)

    from pudb import runscript
    runscript(path, args)
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [
        ['hi', ['hi']],
        [u'hi', [u'hi']],
    ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 6
0
def test_arg_split_win32():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             ['hi', ['hi']],
             ['hello there', ['hello', 'there']],
             ['h\u01cello', ['h\u01cello']],
             ['something "with quotes"', ['something', 'with quotes']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 7
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             ['hi', ['hi']],
             ['hello there', ['hello', 'there']],
             ['h\N{LATIN SMALL LETTER A WITH CARON}llo', ['h\N{LATIN SMALL LETTER A WITH CARON}llo']],
             ['something "with quotes"', ['something', '"with quotes"']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
def test_arg_split_win32():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             [u'hi', [u'hi']],
             ['hello there', ['hello', 'there']],
             [u'h\u01cello', [u'h\u01cello']],
             ['something "with quotes"', ['something', 'with quotes']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 9
0
def test_arg_split_win32():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [
        ["hi", ["hi"]],
        [u"hi", [u"hi"]],
        ["hello there", ["hello", "there"]],
        [u"h\u01cello", [u"h\u01cello"]],
        ['something "with quotes"', ["something", "with quotes"]],
    ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 10
0
 def magic_qtconsole(self, arg_s):
     """Open a qtconsole connected to this kernel.
     
     Useful for connecting a qtconsole to running notebooks, for better
     debugging.
     """
     try:
         p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
     except Exception as e:
         error("Could not start qtconsole: %r" % e)
         return
Esempio n. 11
0
 def magic_qtconsole(self, arg_s):
     """Open a qtconsole connected to this kernel.
     
     Useful for connecting a qtconsole to running notebooks, for better
     debugging.
     """
     try:
         p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
     except Exception as e:
         error("Could not start qtconsole: %r" % e)
         return
Esempio n. 12
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [
        ["hi", ["hi"]],
        [u"hi", [u"hi"]],
        ["hello there", ["hello", "there"]],
        [u"h\N{LATIN SMALL LETTER A WITH CARON}llo", [u"h\N{LATIN SMALL LETTER A WITH CARON}llo"]],
        ['something "with quotes"', ["something", '"with quotes"']],
    ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 13
0
 def _sqlalias(line, cell=None):
     """Alias to %sql"""
     opts, args = [], []
     for chunk in arg_split(line):
         # XXX: what about math?!:
         # select -1 + 5 * something from blah;
         if chunk.startswith('-') and len(chunk.strip()) > 1:
             opts.append(chunk)
         else:
             args.append(chunk)
     line = '%s %s %s' % (' '.join(opts), alias, ' '.join(args))
     return sqlmagics.sql(line, cell)
Esempio n. 14
0
 def _sqlalias(line, cell=None):
     """Alias to %sql"""
     opts, args = [], []
     for chunk in arg_split(line):
         # XXX: what about math?!:
         # select -1 + 5 * something from blah;
         if chunk.startswith('-') and len(chunk.strip()) > 1:
             opts.append(chunk)
         else:
             args.append(chunk)
     line = '%s %s %s' % (' '.join(opts), alias, ' '.join(args))
     return sqlmagics.sql(line, cell)
Esempio n. 15
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             ['hi', ['hi']],
             ['hello there', ['hello', 'there']],
             # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
             # Do not use \N because the tests crash with syntax error in
             # some cases, for example windows python2.6.
             ['h\u01cello', ['h\u01cello']],
             ['something "with quotes"', ['something', '"with quotes"']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 16
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [['hi', ['hi']],
             [u'hi', [u'hi']],
             ['hello there', ['hello', 'there']],
             # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
             # Do not use \N because the tests crash with syntax error in
             # some cases, for example windows python2.6.
             [u'h\u01cello', [u'h\u01cello']],
             ['something "with quotes"', ['something', '"with quotes"']],
             ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 17
0
def test_arg_split():
    """Ensure that argument lines are correctly split like in a shell."""
    tests = [
        ['hi', ['hi']],
        [u'hi', [u'hi']],
        ['hello there', ['hello', 'there']],
        [
            u'h\N{LATIN SMALL LETTER A WITH CARON}llo',
            [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']
        ],
        ['something "with quotes"', ['something', '"with quotes"']],
    ]
    for argstr, argv in tests:
        nt.assert_equal(arg_split(argstr), argv)
Esempio n. 18
0
    def shebang(self, line, cell):
        """Run a cell via a shell command
        
        The `%%script` line is like the #! line of script,
        specifying a program (bash, perl, ruby, etc.) with which to run.
        
        The rest of the cell is run by that program.
        
        Examples
        --------
        ::
        
            In [1]: %%script bash
               ...: for i in 1 2 3; do
               ...:   echo $i
               ...: done
            1
            2
            3
        """
        argv = arg_split(line, posix = not sys.platform.startswith('win'))
        args, cmd = self.shebang.parser.parse_known_args(argv)

        p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        
        cell = cell.encode('utf8', 'replace')
        if args.bg:
            if args.out:
                self.shell.user_ns[args.out] = p.stdout
            if args.err:
                self.shell.user_ns[args.err] = p.stderr
            self.job_manager.new(self._run_script, p, cell)
            return
        
        out, err = p.communicate(cell)
        out = py3compat.bytes_to_str(out)
        err = py3compat.bytes_to_str(err)
        if args.out:
            self.shell.user_ns[args.out] = out
        else:
            sys.stdout.write(out)
            sys.stdout.flush()
        if args.err:
            self.shell.user_ns[args.err] = err
        else:
            sys.stderr.write(err)
            sys.stderr.flush()
Esempio n. 19
0
def call_pydb(self, args):
    """Invoke pydb with the supplied parameters."""
    try:
        import pydb
    except ImportError:
        raise ImportError("pydb doesn't seem to be installed.")

    if not hasattr(pydb.pydb, "runv"):
        raise ImportError("You need pydb version 1.19 or later installed.")

    argl = arg_split(args)
    # print argl # dbg
    if len(inspect.getargspec(pydb.runv)[0]) == 2:
        pdb = debugger.Pdb(color_scheme=self.colors)
        ip.history_saving_wrapper(lambda: pydb.runv(argl, pdb))()
    else:
        ip.history_saving_wrapper(lambda: pydb.runv(argl))()
Esempio n. 20
0
def call_pydb(self, args):
    """Invoke pydb with the supplied parameters."""
    try:
        import pydb
    except ImportError:
        raise ImportError("pydb doesn't seem to be installed.")

    if not hasattr(pydb.pydb, "runv"):
        raise ImportError("You need pydb version 1.19 or later installed.")

    argl = arg_split(args)
    # print argl # dbg
    if len(inspect.getargspec(pydb.runv)[0]) == 2:
        pdb = debugger.Pdb(color_scheme=self.colors)
        ip.history_saving_wrapper(lambda: pydb.runv(argl, pdb))()
    else:
        ip.history_saving_wrapper(lambda: pydb.runv(argl))()
Esempio n. 21
0
    def qtconsole(self, arg_s):
        """Open a qtconsole connected to this kernel.

        Useful for connecting a qtconsole to running notebooks, for better
        debugging.
        """

        # %qtconsole should imply bind_kernel for engines:
        # FIXME: move to ipyparallel Kernel subclass
        if 'ipyparallel' in sys.modules:
            from ipyparallel import bind_kernel
            bind_kernel()

        try:
            connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
        except Exception as e:
            warnings.warn("Could not start qtconsole: %r" % e)
            return
Esempio n. 22
0
 def qtconsole(self, arg_s):
     """Open a qtconsole connected to this kernel.
     
     Useful for connecting a qtconsole to running notebooks, for better
     debugging.
     """
     
     # %qtconsole should imply bind_kernel for engines:
     try:
         from IPython.parallel import bind_kernel
     except ImportError:
         # technically possible, because parallel has higher pyzmq min-version
         pass
     else:
         bind_kernel()
     
     try:
         p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
     except Exception as e:
         error("Could not start qtconsole: %r" % e)
         return
Esempio n. 23
0
 def arguments(self, func, line):  # pylint: disable=no-self-use
     """Get arguments from magic"""
     argv = arg_split(line, posix=not sys.platform.startswith("win"))
     args = magic_arguments.parse_argstring(func, line)
     return argv, args
Esempio n. 24
0
 def shebang(self, line, cell):
     """Run a cell via a shell command
     
     The `%%script` line is like the #! line of script,
     specifying a program (bash, perl, ruby, etc.) with which to run.
     
     The rest of the cell is run by that program.
     
     Examples
     --------
     ::
     
         In [1]: %%script bash
            ...: for i in 1 2 3; do
            ...:   echo $i
            ...: done
         1
         2
         3
     """
     argv = arg_split(line, posix = not sys.platform.startswith('win'))
     args, cmd = self.shebang.parser.parse_known_args(argv)
     
     try:
         p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
     except OSError as e:
         if e.errno == errno.ENOENT:
             print("Couldn't find program: %r" % cmd[0])
             return
         else:
             raise
     
     cell = cell.encode('utf8', 'replace')
     if args.bg:
         self.bg_processes.append(p)
         self._gc_bg_processes()
         if args.out:
             self.shell.user_ns[args.out] = p.stdout
         if args.err:
             self.shell.user_ns[args.err] = p.stderr
         self.job_manager.new(self._run_script, p, cell, daemon=True)
         if args.proc:
             self.shell.user_ns[args.proc] = p
         return
     
     try:
         out, err = p.communicate(cell)
     except KeyboardInterrupt:
         try:
             p.send_signal(signal.SIGINT)
             time.sleep(0.1)
             if p.poll() is not None:
                 print("Process is interrupted.")
                 return
             p.terminate()
             time.sleep(0.1)
             if p.poll() is not None:
                 print("Process is terminated.")
                 return
             p.kill()
             print("Process is killed.")
         except OSError:
             pass
         except Exception as e:
             print("Error while terminating subprocess (pid=%i): %s" \
                 % (p.pid, e))
         return
     out = py3compat.bytes_to_str(out)
     err = py3compat.bytes_to_str(err)
     if args.out:
         self.shell.user_ns[args.out] = out
     else:
         sys.stdout.write(out)
         sys.stdout.flush()
     if args.err:
         self.shell.user_ns[args.err] = err
     else:
         sys.stderr.write(err)
         sys.stderr.flush()
Esempio n. 25
0
    def run_cell(self, line, cell):
        """Run a cell via a shell command
        
        The `%%anybody` invokes the anybody console application on the rest of
        the cell.        
        
        Parameters
        ----------
        --dir <Path>
        --out <output var>        
        --bg <>
        --proc <baground process variable >
        --anybodycon <path to anybodycon>
        
        Examples
        --------
        ::
            In [1]: %%anybody
               ...: load "mymodel.any"
               ...: operation Main.MyStudy.Kinematics
               ...: run
        """
        argv = arg_split(line, posix=not sys.platform.startswith('win'))
        args, dummy = self.run_cell.parser.parse_known_args(argv)

        if args.anybodycon:
            if os.path.exists(args.anybodycon):
                abcpath = args.anybodycon
            elif self.shell.user_ns.has_key(args.anybodycon):
                abcpath = self.shell.user_ns[args.anybodycon]
        elif sys.platform == 'win32':
            import _winreg
            try:
                abpath = _winreg.QueryValue(
                    _winreg.HKEY_CLASSES_ROOT,
                    'AnyBody.AnyScript\shell\open\command')
                abpath = abpath.rsplit(' ', 1)[0].strip('"')
                abcpath = os.path.join(os.path.dirname(abpath),
                                       'AnyBodyCon.exe')
            except:
                raise Exception(
                    'Could not find AnyBody Modeling System in windows registry'
                )
        else:
            raise Exception('Cannot find the specified anybodycon')
        if not os.path.exists(abcpath):
            raise Exception('Cannot find the specified anybodycon: %s' %
                            abcpath)

        if args.dir and os.path.isdir(args.dir):
            folder = args.dir
        elif self.shell.user_ns.has_key(args.dir):
            folder = self.shell.user_ns[args.dir]
        else:
            folder = os.getcwd()

        cell = cell.encode('utf8', 'replace')
        macro = cell if cell.endswith('\n') else cell + '\n'
        macrofile = NamedTemporaryFile(mode='w+b',
                                       prefix='macro_',
                                       suffix='.anymcr',
                                       dir=folder,
                                       delete=False)

        macrofile.write(macro)
        macrofile.flush()

        cmd = [
            abcpath, '/d', folder, '--macro=', macrofile.name, '/ni', "1>&2"
        ]

        try:
            p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
        except OSError as e:
            if e.errno == errno.ENOENT:
                print "Couldn't find program: %r" % cmd[0]
                return
            else:
                raise

        if args.bg:
            self.bg_processes.append(p)
            self._gc_bg_processes()
            if args.out:
                self.shell.user_ns[args.out] = p.stderr
            self.job_manager.new(self._run_script, p, macrofile, daemon=True)
            if args.proc:
                self.shell.user_ns[args.proc] = p
            return

        random_tag = ''.join(random.sample(string.ascii_uppercase, 6))

        def htmlbox(text):
            raw_html = """<div id="anyscriptbox_{0}" style="height: 120px ; width : auto; border:1px dotted black;padding:0.5em;overflow:auto;background-color:#E0E0E0 ; font:3px Geogia"><font size="2px" face="courier"> {1} </font></div> <script> var myDiv = document.getElementById("anyscriptbox_{0}");
            myDiv.scrollTop = myDiv.scrollHeight;</script> """.format(
                random_tag, text)
            return HTML(raw_html)

        try:
            raw_out = []
            for line in iter(p.stderr.readline, b''):
                line = py3compat.bytes_to_str(line)
                raw_out.append(line)
                if not args.pager:
                    clear_output()
                    display(htmlbox("<br/>".join(raw_out)))
                #sys.stdout.flush()

            if args.pager:
                page("".join(raw_out))
            p.communicate()

        except KeyboardInterrupt:
            try:
                p.send_signal(signal.SIGINT)
                time.sleep(0.1)
                if p.poll() is not None:
                    print "Process is interrupted."
                    return
                p.terminate()
                time.sleep(0.1)
                if p.poll() is not None:
                    print "Process is terminated."
                    return
                p.kill()
                print "Process is killed."
            except OSError:
                pass
            except Exception as e:
                print "Error while terminating subprocess (pid=%i): %s" \
                    % (p.pid, e)
            return

        if args.out:
            self.shell.user_ns[args.out] = "\n".join(raw_out)

        if args.dump:
            output = _parse_anybodycon_output("\n".join(raw_out))
            if len(output.keys()):
                print 'Dumped variables:'
            for k, v in output.iteritems():
                varname = k.replace('.', '_')
                self.shell.user_ns[varname] = v
                print '- ' + varname
        try:
            macrofile.close()
            os.remove(macrofile.name)
        except:
            print 'Error removing macro file'
Esempio n. 26
0
def test_arg_split_win32(argstr, argv):
    """Ensure that argument lines are correctly split like in a shell."""
    assert arg_split(argstr) == argv
Esempio n. 27
0
    def file_matches(self, text):
        """Match filenames, expanding ~USER type strings.

        Most of the seemingly convoluted logic in this completer is an
        attempt to handle filenames with spaces in them.  And yet it's not
        quite perfect, because Python's readline doesn't expose all of the
        GNU readline details needed for this to be done correctly.

        For a filename with a space in it, the printed completions will be
        only the parts after what's already been typed (instead of the
        full completions, as is normally done).  I don't think with the
        current (as of Python 2.3) Python readline it's possible to do
        better."""

        #io.rprint('Completer->file_matches: <%r>' % text) # dbg

        # chars that require escaping with backslash - i.e. chars
        # that readline treats incorrectly as delimiters, but we
        # don't want to treat as delimiters in filename matching
        # when escaped with backslash
        if text.startswith('!'):
            text = text[1:]
            text_prefix = '!'
        else:
            text_prefix = ''

        text_until_cursor = self.text_until_cursor
        # track strings with open quotes
        open_quotes = has_open_quotes(text_until_cursor)

        if '(' in text_until_cursor or '[' in text_until_cursor:
            lsplit = text
        else:
            try:
                # arg_split ~ shlex.split, but with unicode bugs fixed by us
                lsplit = arg_split(text_until_cursor)[-1]
            except ValueError:
                # typically an unmatched ", or backslash without escaped char.
                if open_quotes:
                    lsplit = text_until_cursor.split(open_quotes)[-1]
                else:
                    return []
            except IndexError:
                # tab pressed on empty line
                lsplit = ""

        if not open_quotes and lsplit != protect_filename(lsplit):
            # if protectables are found, do matching on the whole escaped name
            has_protectables = True
            text0,text = text,lsplit
        else:
            has_protectables = False
            text = os.path.expanduser(text)

        if text == "":
            return [text_prefix + protect_filename(f) for f in self.glob("*")]

        # Compute the matches from the filesystem
        m0 = self.clean_glob(text.replace('\\',''))

        if has_protectables:
            # If we had protectables, we need to revert our changes to the
            # beginning of filename so that we don't double-write the part
            # of the filename we have so far
            len_lsplit = len(lsplit)
            matches = [text_prefix + text0 +
                       protect_filename(f[len_lsplit:]) for f in m0]
        else:
            if open_quotes:
                # if we have a string with an open quote, we don't need to
                # protect the names at all (and we _shouldn't_, as it
                # would cause bugs when the filesystem call is made).
                matches = m0
            else:
                matches = [text_prefix +
                           protect_filename(f) for f in m0]

        #io.rprint('mm', matches)  # dbg

        # Mark directories in input list by appending '/' to their names.
        matches = [x+'/' if os.path.isdir(x) else x for x in matches]
        return matches
Esempio n. 28
0
 def arguments(self, func, line):
     argv = arg_split(line, posix = not sys.platform.startswith('win'))
     args = magic_arguments.parse_argstring(func, line)
     return argv, args
Esempio n. 29
0
 def arguments(self, func, line):                                             # pylint: disable=no-self-use
     """Get arguments from magic"""
     argv = arg_split(line, posix=not sys.platform.startswith("win"))
     args = magic_arguments.parse_argstring(func, line)
     return argv, args
Esempio n. 30
0
 def parse_argstring(self, argstring):
     """ Split a string into an argument list and parse that argument list.
     """
     argv = arg_split(argstring)
     return self.parse_args(argv)
Esempio n. 31
0
    def shebang(self, line, cell):
        """Run a cell via a shell command
        
        The `%%script` line is like the #! line of script,
        specifying a program (bash, perl, ruby, etc.) with which to run.
        
        The rest of the cell is run by that program.
        
        Examples
        --------
        ::
        
            In [1]: %%script bash
               ...: for i in 1 2 3; do
               ...:   echo $i
               ...: done
            1
            2
            3
        """
        async def _handle_stream(stream, stream_arg, file_object):
            while True:
                line = (await stream.readline()).decode("utf8")
                if not line:
                    break
                if stream_arg:
                    self.shell.user_ns[stream_arg] = line
                else:
                    file_object.write(line)
                    file_object.flush()

        async def _stream_communicate(process, cell):
            process.stdin.write(cell)
            process.stdin.close()
            stdout_task = asyncio.create_task(
                _handle_stream(process.stdout, args.out, sys.stdout))
            stderr_task = asyncio.create_task(
                _handle_stream(process.stderr, args.err, sys.stderr))
            await asyncio.wait([stdout_task, stderr_task])
            await process.wait()

        if sys.platform.startswith("win"):
            asyncio.set_event_loop_policy(
                asyncio.WindowsProactorEventLoopPolicy())
        loop = asyncio.get_event_loop()

        argv = arg_split(line, posix=not sys.platform.startswith("win"))
        args, cmd = self.shebang.parser.parse_known_args(argv)
        try:
            p = loop.run_until_complete(
                asyncio.create_subprocess_exec(
                    *cmd,
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE,
                    stdin=asyncio.subprocess.PIPE,
                ))
        except OSError as e:
            if e.errno == errno.ENOENT:
                print("Couldn't find program: %r" % cmd[0])
                return
            else:
                raise

        if not cell.endswith('\n'):
            cell += '\n'
        cell = cell.encode('utf8', 'replace')
        if args.bg:
            self.bg_processes.append(p)
            self._gc_bg_processes()
            to_close = []
            if args.out:
                self.shell.user_ns[args.out] = p.stdout
            else:
                to_close.append(p.stdout)
            if args.err:
                self.shell.user_ns[args.err] = p.stderr
            else:
                to_close.append(p.stderr)
            self.job_manager.new(self._run_script,
                                 p,
                                 cell,
                                 to_close,
                                 daemon=True)
            if args.proc:
                self.shell.user_ns[args.proc] = p
            return

        try:
            loop.run_until_complete(_stream_communicate(p, cell))
        except KeyboardInterrupt:
            try:
                p.send_signal(signal.SIGINT)
                time.sleep(0.1)
                if p.returncode is not None:
                    print("Process is interrupted.")
                    return
                p.terminate()
                time.sleep(0.1)
                if p.returncode is not None:
                    print("Process is terminated.")
                    return
                p.kill()
                print("Process is killed.")
            except OSError:
                pass
            except Exception as e:
                print("Error while terminating subprocess (pid=%i): %s" %
                      (p.pid, e))
            return
        if args.raise_error and p.returncode != 0:
            # If we get here and p.returncode is still None, we must have
            # killed it but not yet seen its return code. We don't wait for it,
            # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
            rc = p.returncode or -9
            raise CalledProcessError(rc, cell)
Esempio n. 32
0
    def run_cell(self, line, cell):
        """Run a cell via a shell command
        
        The `%%anybody` invokes the anybody console application on the rest of
        the cell.        
        
        Parameters
        ----------
        --dir <Path>
        --out <output var>        
        --bg <>
        --proc <baground process variable >
        --anybodycon <path to anybodycon>
        
        Examples
        --------
        ::
            In [1]: %%anybody
               ...: load "mymodel.any"
               ...: operation Main.MyStudy.Kinematics
               ...: run
        """
        argv = arg_split(line, posix = not sys.platform.startswith('win'))
        args, dummy = self.run_cell.parser.parse_known_args(argv)
        
        if args.anybodycon:
            if os.path.exists(args.anybodycon):
                abcpath = args.anybodycon
            elif self.shell.user_ns.has_key(args.anybodycon):
                abcpath = self.shell.user_ns[args.anybodycon]
        elif sys.platform == 'win32':
            import _winreg
            try:        
                abpath = _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT,
                                'AnyBody.AnyScript\shell\open\command')
                abpath = abpath.rsplit(' ',1)[0].strip('"')
                abcpath  = os.path.join(os.path.dirname(abpath),'AnyBodyCon.exe')
            except:
                raise Exception('Could not find AnyBody Modeling System in windows registry')
        else: 
            raise Exception('Cannot find the specified anybodycon')
        if not os.path.exists(abcpath):
            raise Exception('Cannot find the specified anybodycon: %s'%abcpath)


    
        if args.dir and os.path.isdir(args.dir):
            folder = args.dir
        elif self.shell.user_ns.has_key(args.dir):
            folder = self.shell.user_ns[args.dir]
        else:
            folder = os.getcwd()
              
        cell = cell.encode('utf8', 'replace')
        macro = cell if cell.endswith('\n') else cell+'\n'
        macrofile = NamedTemporaryFile(mode='w+b',
                                         prefix ='macro_',
                                         suffix='.anymcr',
                                         dir= folder,
                                         delete = False)
        
        macrofile.write(macro)
        macrofile.flush()

        
        
        
        cmd = [abcpath ,'/d',folder, '--macro=', macrofile.name, '/ni', "1>&2"]        
        
        try:
            p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell= True)
        except OSError as e:
            if e.errno == errno.ENOENT:
                print "Couldn't find program: %r" % cmd[0]
                return
            else:
                raise
        
        if args.bg:
            self.bg_processes.append(p)
            self._gc_bg_processes()
            if args.out:
                self.shell.user_ns[args.out] = p.stderr
            self.job_manager.new(self._run_script, p, macrofile, daemon=True)
            if args.proc:
                self.shell.user_ns[args.proc] = p
            return
        
        random_tag = ''.join( random.sample(string.ascii_uppercase,6) )
        def htmlbox(text):
            raw_html = """<div id="anyscriptbox_{0}" style="height: 120px ; width : auto; border:1px dotted black;padding:0.5em;overflow:auto;background-color:#E0E0E0 ; font:3px Geogia"><font size="2px" face="courier"> {1} </font></div> <script> var myDiv = document.getElementById("anyscriptbox_{0}");
            myDiv.scrollTop = myDiv.scrollHeight;</script> """.format(random_tag, text )
            return HTML(raw_html)
        
        try:
            raw_out = []
            for line in iter(p.stderr.readline,b''):
                line = py3compat.bytes_to_str( line )
                raw_out.append(line)
                if not args.pager:
                    clear_output()
                    display(  htmlbox("<br/>".join( raw_out ) ) )
                #sys.stdout.flush()
                
            if args.pager:
                page("".join(raw_out))
            p.communicate();
                
        except KeyboardInterrupt:
            try:
                p.send_signal(signal.SIGINT)
                time.sleep(0.1)
                if p.poll() is not None:
                    print "Process is interrupted."
                    return
                p.terminate()
                time.sleep(0.1)
                if p.poll() is not None:
                    print "Process is terminated."
                    return
                p.kill()
                print "Process is killed."
            except OSError:
                pass
            except Exception as e:
                print "Error while terminating subprocess (pid=%i): %s" \
                    % (p.pid, e)
            return

        if args.out:
            self.shell.user_ns[args.out] = "\n".join(raw_out)
        
        if args.dump:
            output =  _parse_anybodycon_output( "\n".join(raw_out) )
            if len(output.keys()):
                print 'Dumped variables:'
            for k,v in output.iteritems():
                varname = k.replace('.','_')
                self.shell.user_ns[varname] = v
                print '- ' + varname
        try:
            macrofile.close()            
            os.remove(macrofile.name) 
        except:
            print 'Error removing macro file'    
    def parse_options(self, arg_str, opt_str, *long_opts, **kw):
        """Parse options passed to an argument string.

        The interface is similar to that of :func:`getopt.getopt`, but it
        returns a :class:`~IPython.utils.struct.Struct` with the options as keys
        and the stripped argument string still as a string.

        arg_str is quoted as a true sys.argv vector by using shlex.split.
        This allows us to easily expand variables, glob files, quote
        arguments, etc.

        Parameters
        ----------

        arg_str : str
          The arguments to parse.

        opt_str : str
          The options specification.

        mode : str, default 'string'
          If given as 'list', the argument string is returned as a list (split
          on whitespace) instead of a string.

        list_all : bool, default False
          Put all option values in lists. Normally only options
          appearing more than once are put in a list.

        posix : bool, default True
          Whether to split the input line in POSIX mode or not, as per the
          conventions outlined in the :mod:`shlex` module from the standard
          library.
        """

        # inject default options at the beginning of the input line
        caller = sys._getframe(1).f_code.co_name
        arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)

        mode = kw.get('mode','string')
        if mode not in ['string','list']:
            raise ValueError('incorrect mode given: %s' % mode)
        # Get options
        list_all = kw.get('list_all',0)
        posix = kw.get('posix', os.name == 'posix')
        strict = kw.get('strict', True)

        # Check if we have more than one argument to warrant extra processing:
        odict = {}  # Dictionary with options
        args = arg_str.split()
        if len(args) >= 1:
            # If the list of inputs only has 0 or 1 thing in it, there's no
            # need to look for options
            argv = arg_split(arg_str, posix, strict)
            # Do regular option processing
            try:
                opts,args = getopt(argv, opt_str, long_opts)
            except GetoptError as e:
                raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
                                        " ".join(long_opts)))
            for o,a in opts:
                if o.startswith('--'):
                    o = o[2:]
                else:
                    o = o[1:]
                try:
                    odict[o].append(a)
                except AttributeError:
                    odict[o] = [odict[o],a]
                except KeyError:
                    if list_all:
                        odict[o] = [a]
                    else:
                        odict[o] = a

        # Prepare opts,args for return
        opts = Struct(odict)
        if mode == 'string':
            args = ' '.join(args)

        return opts,args
Esempio n. 34
0
    def shebang(self, line, cell):
        """Run a cell via a shell command

        The `%%script` line is like the #! line of script,
        specifying a program (bash, perl, ruby, etc.) with which to run.

        The rest of the cell is run by that program.

        Examples
        --------
        ::

            In [1]: %%script bash
               ...: for i in 1 2 3; do
               ...:   echo $i
               ...: done
            1
            2
            3
        """

        # Create the event loop in which to run script magics
        # this operates on a background thread
        if self.event_loop is None:
            if sys.platform == "win32":
                # don't override the current policy,
                # just create an event loop
                event_loop = asyncio.WindowsProactorEventLoopPolicy(
                ).new_event_loop()
            else:
                event_loop = asyncio.new_event_loop()
            self.event_loop = event_loop

            # start the loop in a background thread
            asyncio_thread = Thread(target=event_loop.run_forever, daemon=True)
            asyncio_thread.start()
        else:
            event_loop = self.event_loop

        def in_thread(coro):
            """Call a coroutine on the asyncio thread"""
            return asyncio.run_coroutine_threadsafe(coro, event_loop).result()

        async def _handle_stream(stream, stream_arg, file_object):
            while True:
                line = (await stream.readline()).decode("utf8")
                if not line:
                    break
                if stream_arg:
                    self.shell.user_ns[stream_arg] = line
                else:
                    file_object.write(line)
                    file_object.flush()

        async def _stream_communicate(process, cell):
            process.stdin.write(cell)
            process.stdin.close()
            stdout_task = asyncio.create_task(
                _handle_stream(process.stdout, args.out, sys.stdout))
            stderr_task = asyncio.create_task(
                _handle_stream(process.stderr, args.err, sys.stderr))
            await asyncio.wait([stdout_task, stderr_task])
            await process.wait()

        argv = arg_split(line, posix=not sys.platform.startswith("win"))
        args, cmd = self.shebang.parser.parse_known_args(argv)

        try:
            p = in_thread(
                asyncio.create_subprocess_exec(
                    *cmd,
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE,
                    stdin=asyncio.subprocess.PIPE,
                ))
        except OSError as e:
            if e.errno == errno.ENOENT:
                print("Couldn't find program: %r" % cmd[0])
                return
            else:
                raise

        if not cell.endswith('\n'):
            cell += '\n'
        cell = cell.encode('utf8', 'replace')
        if args.bg:
            self.bg_processes.append(p)
            self._gc_bg_processes()
            to_close = []
            if args.out:
                self.shell.user_ns[args.out] = _AsyncIOProxy(
                    p.stdout, event_loop)
            else:
                to_close.append(p.stdout)
            if args.err:
                self.shell.user_ns[args.err] = _AsyncIOProxy(
                    p.stderr, event_loop)
            else:
                to_close.append(p.stderr)
            event_loop.call_soon_threadsafe(
                lambda: asyncio.Task(self._run_script(p, cell, to_close)))
            if args.proc:
                proc_proxy = _AsyncIOProxy(p, event_loop)
                proc_proxy.stdout = _AsyncIOProxy(p.stdout, event_loop)
                proc_proxy.stderr = _AsyncIOProxy(p.stderr, event_loop)
                self.shell.user_ns[args.proc] = proc_proxy
            return

        try:
            in_thread(_stream_communicate(p, cell))
        except KeyboardInterrupt:
            try:
                p.send_signal(signal.SIGINT)
                in_thread(asyncio.wait_for(p.wait(), timeout=0.1))
                if p.returncode is not None:
                    print("Process is interrupted.")
                    return
                p.terminate()
                in_thread(asyncio.wait_for(p.wait(), timeout=0.1))
                if p.returncode is not None:
                    print("Process is terminated.")
                    return
                p.kill()
                print("Process is killed.")
            except OSError:
                pass
            except Exception as e:
                print("Error while terminating subprocess (pid=%i): %s" %
                      (p.pid, e))
            return

        if args.raise_error and p.returncode != 0:
            # If we get here and p.returncode is still None, we must have
            # killed it but not yet seen its return code. We don't wait for it,
            # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
            rc = p.returncode or -9
            raise CalledProcessError(rc, cell)