Exemple #1
0
def shell():
    context = make_shell_context()
    banner = SHELL_BANNER.format(version=sys.version,
                                 context=format_context(context))
    try:
        try:
            # 0.10.x
            from IPython.Shell import IPShellEmbed
            ipshell = IPShellEmbed(banner=banner)
            ipshell(global_ns={}, local_ns=context)
        except ImportError:
            # 0.12+
            from IPython import embed
            embed(banner1=banner, user_ns=context)
        return
    except ImportError:
        pass
    # fallback to basic python shell
    code.interact(banner, local=context)
    return
Exemple #2
0
def prompt(vars, message):

    prompt_message = message
    try:
        from IPython.Shell import IPShellEmbed
        ipshell = IPShellEmbed(argv=[''],
                               banner=prompt_message,
                               exit_msg="Goodbye")
        return ipshell
    except ImportError:
        # this doesn't quite work right, in that it doesn't go to the right env
        # so we just fail.
        import code
        import rlcompleter
        readline.parse_and_bind("tab: complete")
        # calling this with globals ensures we can see the environment
        if prompt_message:
            print prompt_message
        shell = code.InteractiveConsole(vars)
        return shell.interact
Exemple #3
0
def setup_ipython():
    try:
        import IPython
        from IPython.config.loader import Config
        from IPython.terminal.embed import InteractiveShellEmbed

        cfg = Config()
        cfg.PromptManager.in_template = "MyCV:\\#> "
        cfg.PromptManager.out_template = "MyCV:\\#: "
        #~ cfg.InteractiveShellEmbed.prompt_in1 = "MyCV:\\#> "
        #~ cfg.InteractiveShellEmbed.prompt_out="MyCV:\\#: "
        scvShell = InteractiveShellEmbed(config=cfg,
                                         banner1=banner,
                                         exit_msg=exit_msg)
        scvShell.define_magic("tutorial", magic_tutorial)
        scvShell.define_magic("clear", magic_clear)
        scvShell.define_magic("example", magic_examples)
        scvShell.define_magic("forums", magic_forums)
        scvShell.define_magic("walkthrough", magic_walkthrough)
        scvShell.define_magic("docs", magic_docs)
    except ImportError:
        try:
            from IPython.Shell import IPShellEmbed

            argsv = [
                '-pi1', 'MyCV:\\#>', '-pi2', '   .\\D.:', '-po', 'MyCV:\\#>',
                '-nosep'
            ]
            scvShell = IPShellEmbed(argsv)
            scvShell.set_banner(banner)
            scvShell.set_exit_msg(exit_msg)
            scvShell.IP.api.expose_magic("tutorial", magic_tutorial)
            scvShell.IP.api.expose_magic("clear", magic_clear)
            scvShell.IP.api.expose_magic("example", magic_examples)
            scvShell.IP.api.expose_magic("forums", magic_forums)
            scvShell.IP.api.expose_magic("walkthrough", magic_walkthrough)
            scvShell.IP.api.expose_magic("docs", magic_docs)
        except ImportError:
            raise

    return scvShell()
Exemple #4
0
    def _run_shell(self, base_module, locs, disable_ipython):
        banner = "  All objects from %s are available\n" % base_module
        banner += "  Additional Objects:\n"
        banner += "  %-10s -  %s\n" % ('wsgiapp',
                                       "This project's WSGI App instance")
        banner += "  %-10s -  %s\n" % ('app',
                                       'WebTest.TestApp wrapped around wsgiapp')

        try:
            if disable_ipython:
                raise ImportError()

            # try to use IPython if possible
            try:
                try:
                    # ipython >= 1.0
                    from IPython.terminal.embed import InteractiveShellEmbed
                except ImportError:
                    # ipython >= 0.11
                    from IPython.frontend.terminal.embed import InteractiveShellEmbed
                shell = InteractiveShellEmbed.instance(banner2=banner)
            except ImportError:
                # ipython < 0.11
                from IPython.Shell import IPShellEmbed
                shell = IPShellEmbed()
                shell.set_banner(shell.IP.BANNER + '\n\n' + banner)

            shell(local_ns=locs)
        except ImportError:
            import code
            py_prefix = sys.platform.startswith('java') and 'J' or 'P'
            newbanner = "TurboGears2 Interactive Shell\n%sython %s\n\n" % \
                        (py_prefix, sys.version)
            banner = newbanner + banner
            shell = code.InteractiveConsole(locals=locs)
            try:
                import readline
            except ImportError:
                pass

            shell.interact(banner)
Exemple #5
0
 def start_shell(self, runner):
     try:
         import IPython
     except:
         IPython = None
     jsobj = JSObject(self.bridge, window_string)
     
     if IPython is None or self.options.usecode:
         import code
         code.interact(local={"jsobj":jsobj, 
                              "getBrowserWindow":lambda : getBrowserWindow(self.bridge),
                              "back_channel":self.back_channel,
                              })
     else:
         from IPython.Shell import IPShellEmbed
         ipshell = IPShellEmbed([])
         ipshell(local_ns={"jsobj":jsobj, 
                           "getBrowserWindow":lambda : getBrowserWindow(self.bridge),
                           "back_channel":self.back_channel,
                           })
     runner.stop()
Exemple #6
0
def main():
    argv = sys.argv[1:]

    banner = "\nnetaddr shell %s - %s\n" % (netaddr.__version__, __doc__)
    exit_msg = "\nShare and enjoy!"
    rc_override = None

    try:
        try:
            # ipython >= 0.11
            from IPython.terminal.embed import InteractiveShellEmbed
            ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)
        except ImportError:
            # ipython < 0.11
            from IPython.Shell import IPShellEmbed
            ipshell = IPShellEmbed(argv, banner, exit_msg, rc_override)
    except ImportError:
        sys.stderr.write('IPython (http://ipython.scipy.org/) not found!\n')
        sys.exit(1)

    ipshell()
Exemple #7
0
def shell(config):
    '''Starts up an interactive shell.'''
    context = {u'app': config.app}

    # attempt to use IPthon if it's installed
    try:
        try:
            # IPython 0.10.x
            from IPython.Shell import IPShellEmbed
            ipshell = IPShellEmbed()
            ipshell(global_ns={}, local_ns={})
        except ImportError:
            # IPython 0.12+
            from IPython import embed
            embed(user_ns=context)
        return
    except ImportError:
        pass

    with context[u'app'].app_context():
        code.interact(None, local=context)
Exemple #8
0
    def action(ipython=use_ipython):
        """Start a new interactive python session."""
        namespace = init_func()
        if ipython:
            try:
                try:
                    from IPython.frontend.terminal.embed import InteractiveShellEmbed

                    sh = InteractiveShellEmbed(banner1=banner)
                except ImportError:
                    from IPython.Shell import IPShellEmbed

                    sh = IPShellEmbed(banner=banner)
            except ImportError:
                pass
            else:
                sh(global_ns={}, local_ns=namespace)
                return
        from code import interact

        interact(banner, local=namespace)
Exemple #9
0
    def launch_shell(self, argv=[]):
        # Configure prompts and messages
        in_template  = 'L2A: In <\\#>: '
        in_template2 = '   .\\D.:'
        out_template = 'L2A: Out<\\#>: '
        banner = '*** Launcing L2 Analysis Shell ***\nAvailable analysis routines:\n%s' % self.formatted_routine_names()
        exit_msg = '*** Bye ***'
        
        # Set pylab environment as interactive
        pylab.interactive(True)

        # Try using an older version of IPython first 
        try:
            argv += [ '-pi1', in_template, '-pi2', in_template2, '-po', out_template ]

            from IPython.Shell import IPShellEmbed

            ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg)
            ipshell(local_ns=self._local_namespace, global_ns=self._global_namespace)

        except ImportError as imp_err:
            # Newer version of IPython, 0.11 onward use this interface
            from IPython.config.loader import Config

            cfg = Config()
            prompt_config = cfg.PromptManager
            prompt_config.in_template = in_template
            prompt_config.in2_template = in_template2 
            prompt_config.out_template = out_template 

            from IPython.frontend.terminal.embed import InteractiveShellEmbed

            ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg)

            # There is no access to global namespace in this version of IPython
            # put everything into the local namespace
            namespace = {}
            namespace.update(self._local_namespace)
            namespace.update(self._global_namespace)
            ipshell(local_ns=namespace)
Exemple #10
0
    def invoke(cls, ns, banner):  # pragma: nocover
        """
        :param ns: local namespace
        :param banner: interactive shell startup banner

        Embed an interactive ipython shell.
        Try the InteractiveShellEmbed API first, fall back on
        IPShellEmbed for older IPython versions.
        """
        try:
            from IPython.frontend.terminal.embed import (InteractiveShellEmbed)
            # try and load their default profile
            from IPython.frontend.terminal.ipapp import (load_default_config)
            config = load_default_config()
            shell = InteractiveShellEmbed(config=config, banner2=banner)
            shell(local_ns=ns)
        except ImportError:
            # Support for the IPython <= 0.10 shell API
            from IPython.Shell import IPShellEmbed
            shell = IPShellEmbed(argv=[])
            shell.set_banner(shell.IP.BANNER + '\n\n' + banner)
            shell(local_ns=ns, global_ns={})
Exemple #11
0
def shell(transaction=True):
    context = make_shell_context(auto_transact=transaction)
    banner = SHELL_BANNER.format(
        version=sys.version,
        context=format_context(context),
        transaction=TRANSACTION_WARNING if transaction else '')
    try:
        try:
            # 0.10.x
            from IPython.Shell import IPShellEmbed
            ipshell = IPShellEmbed(banner=banner)
            ipshell(global_ns={}, local_ns=context)
        except ImportError:
            # 0.12+
            from IPython import embed
            embed(banner1=banner, user_ns=context)
        return
    except ImportError:
        pass
    # fallback to basic python shell
    code.interact(banner, local=context)
    return
Exemple #12
0
def interact(scope):
    banner = "gem5 Interactive Console"

    ipshell = None
    prompt_in1 = "gem5 \\#> "
    prompt_out = "gem5 \\#: "

    # Is IPython version 0.10 or earlier available?
    try:
        from IPython.Shell import IPShellEmbed
        ipshell = IPShellEmbed(
            argv=["-prompt_in1", prompt_in1, "-prompt_out", prompt_out],
            banner=banner,
            user_ns=scope)
    except ImportError:
        pass

    # Is IPython version 0.11 or later available?
    if not ipshell:
        try:
            import IPython
            from IPython.config.loader import Config
            from IPython.terminal.embed import InteractiveShellEmbed

            cfg = Config()
            cfg.PromptManager.in_template = prompt_in1
            cfg.PromptManager.out_template = prompt_out
            ipshell = InteractiveShellEmbed(config=cfg,
                                            user_ns=scope,
                                            banner1=banner)
        except ImportError:
            pass

    if ipshell:
        ipshell()
    else:
        # Use the Python shell in the standard library if IPython
        # isn't available.
        code.InteractiveConsole(scope).interact(banner)
def shell(no_ipython):
    """Start a new interactive python session."""
    banner = "Interactive Werkzeug Shell"
    namespace = make_shell()
    if not no_ipython:
        try:
            try:
                from IPython.frontend.terminal.embed import InteractiveShellEmbed

                sh = InteractiveShellEmbed.instance(banner1=banner)
            except ImportError:
                from IPython.Shell import IPShellEmbed

                sh = IPShellEmbed(banner=banner)
        except ImportError:
            pass
        else:
            sh(local_ns=namespace)
            return
    from code import interact

    interact(banner, local=namespace)
Exemple #14
0
def console(variables):
    import os
    import sys
    # hook thunderstorm tree in path
    thunderstorm_dir = os.path.dirname(os.path.realpath(__file__))
    sys.path.insert(0, thunderstorm_dir)

    # Change the current directory to the one specified as argument
    # This allow drag and drop start on windows
    if len(sys.argv) == 2:
        os.chdir(sys.argv[1])

    init_message = """Welcome in Istorm
Copyright (C) 2010  David Trémouilles
This program comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute
it under certain
conditions; read the COPYING* files for details.
Available tester import plugins are:"""
    for plug_name in Load.plug_list:
        init_message += "\t- " + plug_name + "\n"
    init_message += """Use Load.'tester_plugin_name'("datafile")
to load a measurement
Enjoy!
"""
    plt_info = "Using -%s- matplotlib backend" % matplotlib.get_backend()
    try:
        from IPython.Shell import IPShellEmbed
        ipshell = IPShellEmbed(exit_msg='Thanks for using istrom')
        return (ipshell, init_message + plt_info + " and -IPython shell-")
    except ImportError:
        import code
        import rlcompleter
        import readline
        readline.parse_and_bind("tab: complete")
        print init_message
        shell = code.InteractiveConsole(variables)
        return (shell.interact,
                init_message + plt_info + " and -Interactive Console-")
Exemple #15
0
def ipython(globals=None, locals=None):
    """Interactive python prompt (see :ref:`Example: IPython <example
    IPython>`)."""
    # NOTE: WE ASSUME IPython HAS BEEN ALREADY IMPORTED!
    # We use an embedded ipython session
    # (http://ipython.scipy.org/doc/manual/node9.html)
    # to inspect the current state. The calling_frame magic is necessary
    # to get the context of the place where this ipython() function is called
    # (and not where IPShellEmded([]) is invoked.
    calling_frame = sys._getframe(1)
    if globals == None:
        globals = calling_frame.f_globals
    if locals == None:
        locals = calling_frame.f_locals

    import IPython
    if hasattr(IPython, "InteractiveShell"):
        from IPython.config.loader import Config
        cfg = Config()
        IPython.embed(config=cfg)
    else:
        from IPython.Shell import IPShellEmbed
        IPShellEmbed([])(local_ns=locals, global_ns=globals)
Exemple #16
0
def shell(*args):
    """Interactive Shell"""
    if not "--ipython" in args:
        from code import InteractiveConsole
        console = InteractiveConsole()
        console.push("import infogami")
        console.push("from infogami.utils import delegate")
        console.push("from infogami.core import db")
        console.push("from infogami.utils.context import context as ctx")
        console.push("delegate.fakeload()")
        console.interact()
    else:
        """IPython Interactive Shell - IPython must be installed to use."""
        # remove an argument that confuses ipython
        sys.argv.pop(sys.argv.index("--ipython"))
        from IPython.Shell import IPShellEmbed
        import infogami
        from infogami.utils import delegate
        from infogami.core import db
        from infogami.utils.context import context as ctx
        delegate.fakeload()
        ipshell = IPShellEmbed()
        ipshell()
Exemple #17
0
def ipythonShell():
	'''
	Starts an interactive ipython session if the session is not already started
	'''
	global ipythonRunning
	if not ipythonRunning():

		
		import IPython
		

		try:
			# This code does only work with IPython version < 0.11. 
			# interactive iPython console
			from IPython.Shell import IPShellEmbed
			ipshell = IPShellEmbed()
			# ion() is hack that allows to run a threaded ipython session with pylab. Thus it is possible to plot() without the show() command and without blocking the input
			ipshell.IP.runlines('from pylab import ion; ion()')
			ipshell()
			ipythonRunning = True
			

		except:
			
			# This code should work with IPython version > 0.11. 
			try: 				
				
				IPython.embed(exit_msg='')
				ipythonRunning = True

			
			except:			
				logging.warning('Unable to intialize an embedded IPython shell. Try to run the model from within IPython itself.')

				logging.info('Instead, pylab.show() is called to display any matplotlib figures plotted run time')
				from pylab import show
				show()
Exemple #18
0
def start_python_console(namespace=None, noipython=False, banner=''):
    """Start Python console binded to the given namespace. If IPython is
    available, an IPython console will be started instead, unless `noipython`
    is True. Also, tab completion will be used on Unix systems.
    """
    if namespace is None:
        namespace = {}

    try:
        try: # use IPython if available
            if noipython:
                raise ImportError()

            try:
                import IPython
                if IPython.version_info[0] >= 1:
                    from IPython.terminal.embed import InteractiveShellEmbed
                else:
                    from IPython.frontend.terminal.embed import InteractiveShellEmbed
                sh = InteractiveShellEmbed(banner1=banner)
            except ImportError:
                from IPython.Shell import IPShellEmbed
                sh = IPShellEmbed(banner=banner)

            sh(global_ns={}, local_ns=namespace)
        except ImportError:
            import code
            try: # readline module is only available on unix systems
                import readline
            except ImportError:
                pass
            else:
                import rlcompleter
                readline.parse_and_bind("tab:complete")
            code.interact(banner=banner, local=namespace)
    except SystemExit: # raised when using exit() in python code.interact
        pass
Exemple #19
0
def startshell(locals_={}, header='SiCDS Interactive Shell\n', footer=''):
    if not footer and locals_:
        footer = '\nThe following variables are available:\n  {0}'.format(
            ', '.join(locals_))
    try:
        # try to use IPython if possible
        from IPython.Shell import IPShellEmbed
        shell = IPShellEmbed(argv=sys.argv)
        banner = header + shell.IP.BANNER + footer
        shell.set_banner(banner)
        shell(local_ns=locals_, global_ns={})
    except ImportError:
        import code
        pyver = 'Python %s' % sys.version
        banner = header + pyver + footer
        shell = code.InteractiveConsole(locals=locals_)
        try:
            import readline
        except ImportError:
            pass
        try:
            shell.interact(banner)
        finally:
            pass
Exemple #20
0
def run(args, argv):

    # Activate the python debugger if requested
    if args.debug:
        from IPython.Shell import IPShellEmbed
        ip = IPShellEmbed(["-pdb"], rc_override=dict(quiet=True))

    if not args.filename:
        log.fatal("Please specify an HepMC file or Pythia log file to run on. "
                  "Use --help for help.")
        raise FatalError

    log.info("MCViz Copyright (C) 2011 : See http://mcviz.net/AUTHORS")
    log.info("Licensed under GNU AGPL version 3. "
             "Please see http://mcviz.net/license.txt")

    filename = args.filename
    log.verbose('trying to read event from "%s"' % filename)
    with timer('read event from "%s"' % filename):
        try:
            event_graph = EventGraph.load(args)
        except EventParseError, x:
            log.fatal("No success in reading events from %s!" % filename)
            raise FatalError
Exemple #21
0
    def run(self, no_ipython, no_bpython):
        """
        Runs the shell.  If no_bpython is False or use_bpython is True, then
        a BPython shell is run (if installed).  Else, if no_ipython is False or
        use_python is True then a IPython shell is run (if installed).
        """

        context = self.get_context()

        if not no_bpython:
            # Try BPython
            try:
                from bpython import embed
                embed(banner=self.banner, locals_=context)
                return
            except ImportError:
                pass

        if not no_ipython:
            # Try IPython
            try:
                try:
                    # 0.10.x
                    from IPython.Shell import IPShellEmbed
                    ipshell = IPShellEmbed(banner=self.banner)
                    ipshell(global_ns=dict(), local_ns=context)
                except ImportError:
                    # 0.12+
                    from IPython import embed
                    embed(banner1=self.banner, user_ns=context)
                return
            except ImportError:
                pass

        # Use basic python shell
        code.interact(self.banner, local=context)
Exemple #22
0
                        res.solution.joint_state.header.stamp = rospy.Time.now(
                        )
                        res.solution.joint_state.name = [
                            j.GetName()
                            for j in robot.GetJoints(manip.GetArmIndices())
                        ]
                        res.solution.joint_state.position = list(solution)
                    return res

        sub = rospy.Subscriber("/joint_states",
                               sensor_msgs.msg.JointState,
                               UpdateRobotJoints,
                               queue_size=1)
        s1 = rospy.Service('IK', orrosplanning.srv.IK, IKFn)
        s2 = rospy.Service('GetPositionIK', kinematics_msgs.srv.GetPositionIK,
                           GetPositionIKFn)
        rospy.loginfo('openrave services ready: %s, %s' %
                      (s1.resolved_name, s2.resolved_name))

        if options.ipython:
            from IPython.Shell import IPShellEmbed
            ipshell = IPShellEmbed(
                argv='',
                banner='Dropping into IPython',
                exit_msg='Leaving Interpreter, back to program.')
            ipshell(local_ns=locals())
        else:
            rospy.spin()
    finally:
        RaveDestroy()
Exemple #23
0
    print "DCSC2 intervals of validity:"
    pprint_objects(result[:30])
    print

    output_channels = system.mapping.keys()
    iovs = fetch_iovs("DCSOFL", since, until, output_channels)

    #print hex(hash(tuple(iovs))), hex(hash(tuple(result)))

    print "Original DCSC intervals of validity:"
    pprint_objects(iovs[:30])

    return

    compare_iovs(iovs, result)

    #for calc_iov, ref_iov in zip(result, iovs):
    #print calc_iov.since, calc_iov.until, ref_iov.since, ref_iov.until
    #pprint_objects(result)


if __name__ == "__main__":

    from IPython.Shell import IPShellEmbed
    ipython_instance = IPShellEmbed(["-pdb"], rc_override=dict(quiet=True))

    from DQUtils.logger import init_logging
    init_logging()

    main()
Exemple #24
0
 def do_debug(self,p):
     from IPython.Shell import IPShellEmbed
     ipshell = IPShellEmbed()
     ipshell(local_ns=locals())
Exemple #25
0
#!/usr/bin/env python
import ecto, ecto_test, sys

plasm = ecto.Plasm()

gen = ecto_test.Generate("Gen", step=1.0, start=0.0)
inc = ecto_test.Increment("Increment 0", delay=100)
printer = ecto_test.Printer("Printy")

plasm.connect(gen[:] >> inc[:], inc[:] >> printer[:])

sched = ecto.schedulers.Threadpool(plasm)
ecto.log_to_file("ectolog.txt")
sched.execute_async(niter=100)

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell()
Exemple #26
0
    import profile
try:
    import pstats
    pstats_available = True
except ImportError:
    pstats_available = False

try:
    import IPython
    IPython_available = True
    from IPython.Shell import IPShellEmbed
except:
    IPython_available = False
else:
    ipshell = IPShellEmbed([''],
                           banner='\n# Dropping into Python interpreter',
                           exit_msg='\n# Leaving Interpreter, back to Pyomo\n')

from pyutilib.misc import Options
try:
    from pympler import muppy
    from pympler import summary
    from pympler.asizeof import *
    pympler_available = True
except:
    pympler_available = False
memory_data = Options()

import pyutilib.misc
from pyomo.util.plugin import ExtensionPoint, Plugin, implements
from pyutilib.misc import Container
Exemple #27
0
        argv = ['-pi1','In <\\#>:','-pi2','   .\\D.:','-po','Out<\\#>:']
        banner = '*** Starting Interactive Shell - Ctrl-D to exit...\n\nnapi is your NexposeAPI variable to play with\n'

        if IPython.__version__ >= "0.11":
            from IPython.config.loader import Config
            cfg = Config()
            cfg.InteractiveShellEmbed.prompt_in1="myprompt [\\#]> "
            cfg.InteractiveShellEmbed.prompt_out="myprompt [\\#]: "
            #cfg.InteractiveShellEmbed.profile=ipythonprofile
            # directly open the shell
            IPython.embed(config=cfg, banner2=banner)
        else:
            try:
                from IPython.Shell import IPShellEmbed
                argv = ['-pi1','In <\\#>:','-pi2','   .\\D.:','-po','Out<\\#>:']
                ipshell = IPShellEmbed(argv,banner='*** Starting Interactive Shell - Ctrl-D to exit...\n\nnapi is your NexposeAPI variable to play with\n')
                ipshell.set_exit_msg('Buh-bye!')
                ipshell()
            except ImportError, e:
                sys.exit("IPython not installed, won't continue...")

    if options.listvulns:
        vuln_class = VulnData(napi.sessionid)
        vuln_class.populate_summary()
        if (vuln_class.vulnerabilities) > 0:
            if options.listvulns.upper() == "CSV":
                print vuln_class.csvout()
            else:
                print vuln_class.vulnxml
        else:
            print "Error: No Vulnerabilities loaded, check your Nexpose server address or user/pass"
Exemple #28
0
    def execute(self):
        if self.opts.write:
            mode = "rb+"
        else:
            mode = "rb"
        self.ctx = {}
        (self.addr_space, self.symtab,
         self.types) = load_and_identify_image(self.op, self.opts)
        self.profile = Profile()
        self.pslist = process_list(self.addr_space, self.types, self.symtab)
        self.eprocs = [
            Object("_EPROCESS", p, self.addr_space, profile=self.profile)
            for p in self.pslist
        ]

        if self.opts.filename is None:
            self.op.error("volshell -f <filename:required> [options]")
        else:
            filename = self.opts.filename

        try:
            flat_address_space = FileAddressSpace(filename, mode=mode)
        except:
            self.op.error("Unable to open image file %s" % (filename))

        flat_address_space = find_addr_space(flat_address_space, self.types)

        if not self.opts.offset is None:
            try:
                offset = int(self.opts.offset, 16)
            except:
                self.op.error("EPROCESS offset must be a hexadecimal number.")

            # Special case: physical EPROCESS offset
            self.dtb = process_dtb(flat_address_space, types, offset)
            self.process_address_space = create_addr_space(
                self.addr_space, self.dtb)

            if self.process_address_space is None:
                print "Error obtaining address space for offset %#08x" % (
                    offset)
                return

            self.image_name = process_imagename(flat_address_space, self.types,
                                                offset)
            self.image_pid = process_pid(flat_address_space, self.types,
                                         offset)
            self.image_ppid = process_inherited_from(flat_address_space,
                                                     self.types, offset)
            self.image_offset = offset
            print "Current context: process %s, pid=%d, ppid=%d DTB=%#x" % (
                self.image_name, self.image_pid, self.image_ppid, self.dtb)

        elif self.opts.pid is not None:
            self.set_context(pid=self.opts.pid)
        elif self.opts.imname is not None:
            self.set_context(name=self.opts.imname)
        else:
            # Just use the first process, whatever it is
            self.set_context(offset=self.pslist[0])

        # Functions inside the shell
        def cc(offset=None, pid=None, name=None):
            """Change current shell context.

            This function changes the current shell context to to the process
            specified. The process specification can be given as a virtual address
            (option: offset), PID (option: pid), or process name (option: name).

            If multiple processes match the given PID or name, you will be shown a
            list of matching processes, and will have to specify by offset.
            """
            self.set_context(offset=offset, pid=pid, name=name)

        def db(address, length=0x80, width=16):
            """Print bytes as canonical hexdump.
            
            This function prints bytes at the given virtual address as a canonical
            hexdump. The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The length parameter (default: 0x80) specifies how many bytes to print,
            and the width parameter (default: 16) allows you to change how many
            bytes per line should be displayed.
            """
            #if length % 4 != 0:
            #    length = (length+4) - (length%4)
            data = self.process_address_space.read(address, length)
            if not data:
                print "Memory unreadable at %08x" % address
                return

            FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.'
                              for x in range(256)])
            N = 0
            result = ''
            while data:
                s, data = data[:width], data[width:]
                hexa = ' '.join(["%02X" % ord(x) for x in s])
                s = s.translate(FILTER)
                result += "%08X   %-*s   %s\n" % (address + N, width * 3, hexa,
                                                  s)
                N += width
            print result

        def dd(address, length=0x80):
            """Print dwords at address.

            This function prints the data at the given address, interpreted as
            a series of dwords (unsigned four-byte integers) in hexadecimal.
            The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The optional length parameter (default: 0x80) controls how many bytes
            to display.
            """
            # round up to multiple of 4
            if length % 4 != 0:
                length = (length + 4) - (length % 4)
            data = self.process_address_space.read(address, length)
            if not data:
                print "Memory unreadable at %08x" % address
                return
            dwords = []
            for i in range(0, length, 4):
                (dw, ) = unpack("<L", data[i:i + 4])
                dwords.append(dw)

            if len(dwords) % 4 == 0: lines = len(dwords) / 4
            else: lines = len(dwords) / 4 + 1

            for i in range(lines):
                ad = address + i * 0x10
                lwords = dwords[i * 4:i * 4 + 4]
                print("%08x  " % ad) + " ".join("%08x" % l for l in lwords)

        def ps():
            """Print a process listing.

            Prints a process listing with PID, PPID, image name, and offset.
            """
            self.ps()

        def list_entry(head, objname, offset=-1, fieldname=None, forward=True):
            """Traverse a _LIST_ENTRY.

            Traverses a _LIST_ENTRY starting at virtual address head made up of
            objects of type objname. The value of offset should be set to the
            offset of the _LIST_ENTRY within the desired object."""

            vm = self.process_address_space
            profile = self.eproc.profile
            seen = set()

            if fieldname:
                offset, typ = get_obj_offset(types, [objname, fieldname])
                if typ != "_LIST_ENTRY":
                    print(
                        "WARN: given field is not a LIST_ENTRY, attempting to "
                        "continue anyway.")

            lst = Object("_LIST_ENTRY", head, vm, profile=profile)
            seen.add(lst)
            if not lst.is_valid(): return
            while True:
                if forward:
                    lst = lst.Flink
                else:
                    lst = lst.Blink

                if not lst.is_valid(): return

                if lst in seen: break
                else: seen.add(lst)

                obj = Object(objname, lst.offset - offset, vm, profile=profile)
                yield obj

        def dt(obj, address=None, space=None):
            """Describe an object or show type info.

            Show the names and values of a complex object (struct). If the name of a
            structure is passed, show the struct's members and their types."""

            if space is None:
                space = self.process_address_space

            if address is not None:
                obj = Object(obj, address, space, None, profile=self.profile)

            if isinstance(obj, str):
                size = self.types[obj][0]
                membs = self.types[obj][1]
                membs = [(get_obj_offset(self.types, [obj, m])[0], m,
                          self.types[obj][1][m][1]) for m in membs]
                membs.sort()
                print obj, "(%d bytes)" % size
                for o, m, t in membs:
                    print "%-6s: %-30s %s" % (hex(o), m, t)
            else:
                membs = obj.get_member_names()
                membs = [(obj.get_member_offset(m, relative=True), m)
                         for m in membs if m not in obj.extra_members]
                membs.sort()
                print obj
                for o, m in membs:
                    val = getattr(obj, m)
                    if isinstance(val, list):
                        val = [str(v) for v in val]
                    print "%-6s: %-30s %s" % (hex(o), m, val)
                if obj.extra_members:
                    print "Calculated members:"
                    for m in obj.extra_members:
                        print "        %-30s %s" % (m, getattr(obj, m))

        shell_funcs = {
            'cc': cc,
            'dd': dd,
            'db': db,
            'ps': ps,
            'dt': dt,
            'list_entry': list_entry
        }

        def hh(cmd=None):
            """Get help on a command."""
            shell_funcs['hh'] = hh
            import pydoc
            from inspect import getargspec, formatargspec
            if not cmd:
                for f in shell_funcs:
                    doc = pydoc.getdoc(shell_funcs[f])
                    synop, full = pydoc.splitdoc(doc)
                    print "%-40s : %s" % (
                        f + formatargspec(*getargspec(shell_funcs[f])), synop)
                print
                print "For help on a specific command, type 'hh(<command>)'"
            elif type(cmd) == str:
                try:
                    doc = pydoc.getdoc(shell_funcs[cmd])
                except KeyError:
                    print "No such command: %s" % cmd
                    return
                print doc
            else:
                doc = pydoc.getdoc(cmd)
                print doc

        # Break into shell
        banner = "Welcome to volshell! Current memory image is:\n%s\n" % filename
        banner += "To get help, type 'hh()'"
        try:
            from IPython.Shell import IPShellEmbed
            shell = IPShellEmbed([], banner=banner)
            shell()
        except ImportError:
            import code, inspect

            frame = inspect.currentframe()

            # Try to enable tab completion
            try:
                import rlcompleter, readline
                readline.parse_and_bind("tab: complete")
            except:
                pass

            # evaluate commands in current namespace
            namespace = frame.f_globals.copy()
            namespace.update(frame.f_locals)

            code.interact(banner=banner, local=namespace)
Exemple #29
0
if __name__ == '__main__':

    fname = "load-csv.csv"
    data = load_csv(fname, header_row=2)
    print(
        ('Data has been loaded from "%s" into the "data" dictionary.' % fname))
    print("It contains these keys:")
    print((list(data.keys())))
    print("Use the Python prompt to explore the data.")

    # Embed an IPython or standard Python interpreter.
    #    Based on
    #    http://writeonly.wordpress.com/2008/09/08/embedding-a-python-shell-in-a-python-script/,
    #    accessed 2010/11/2
    try:
        # IPython
        from IPython import embed
        embed()
    except ImportError:
        try:
            # IPython via old API style
            from IPython.Shell import IPShellEmbed
            IPShellEmbed(argv=['-noconfirm_exit'])()
            # Note: The -pylab option can't be embedded (see
            # http://article.gmane.org/gmane.comp.python.ipython.user/1190/match=pylab).
        except ImportError:
            # Standard Python
            from code import InteractiveConsole
            InteractiveConsole(globals()).interact()
Exemple #30
0
  """
  Manages _shared_cluster.
  """
  global _shared_cluster
  if _shared_cluster is None:
    _shared_cluster = MiniHadoopCluster()
    _shared_cluster.start()
    atexit.register(_shared_cluster.stop)
  return _shared_cluster

if __name__ == '__main__':
  """
  It's poor form to write tests for tests (the world-wide stack
  overflow exception), so this merely tries the code.
  """
  logging.basicConfig(level=logging.DEBUG)
  import desktop
  desktop.lib.conf.initialize([hadoop.conf])

  if True:
    cluster = MiniHadoopCluster(num_datanodes=5, num_tasktrackers=5)
    cluster.start()
    print cluster.namenode_port
    print cluster.jobtracker_port
    print cluster.config.get("dfs.thrift.address")
    cluster.dump_ini(sys.stdout)

    from IPython.Shell import IPShellEmbed
    IPShellEmbed()()
    cluster.stop()