Example #1
0
def history(name):
  histfile = os.path.join(os.environ["HOME"],name)
  try:
    readline.read_history_file(histfile)
    atexit.register(readline.write_history_file, histfile)
  except IOError:
    pass
Example #2
0
def reg():
    histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file,histfile)
Example #3
0
    def main_loop(self):
        readline.parse_and_bind('tab: complete')
        readline.set_completer(self.rl_completer_safe)
        #print 'delims: ', repr(readline.get_completer_delims())

        hist_file = os.path.expanduser("~/.qadmin_history")
        try:
            readline.read_history_file(hist_file)
        except IOError:
            pass

        print "Use 'show help;' to see available commands."
        while 1:
            try:
                ln = self.line_input()
                self.exec_string(ln)
            except KeyboardInterrupt:
                print
            except EOFError:
                print
                break
            self.reset_comp_cache()

        try:
            readline.write_history_file(hist_file)
        except IOError:
            pass
Example #4
0
def setup_readline():
    histfile = os.path.join(os.path.expanduser("~"), ".flux_robot_history")
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
Example #5
0
	def interactive(self):
		try:
			readline.read_history_file(Debugger.HISTORY_FILE)
		except IOError:
			pass
		cpu = self.cpu
		try:
			while True:
				cmd,args = self.prompt()
				if cmd in 'xq':
					return
				elif cmd == 's':
					cpu.step()
				elif cmd == 'k':
					self.stack(args)
				elif cmd == 'b':
					self.breakpoint(args)
				elif cmd == 'r':
					self.run()
				elif cmd == 'ret':
					self.runUntilRet()
				elif cmd == '?':
					self.evalPrint(args)
				elif cmd == 'l':
					self.listSource(args)
				else:
					print 'Unknown command! (%s)' % cmd
		finally:
			readline.write_history_file(Debugger.HISTORY_FILE)
Example #6
0
    def register_readline():
        import atexit
        try:
            import readline
            import rlcompleter
        except ImportError:
            return

        # Reading the initialization (config) file may not be enough to set a
        # completion key, so we set one first and then read the file
        if 'libedit' in getattr(readline, '__doc__', ''):
            readline.parse_and_bind('bind ^I rl_complete')
        else:
            readline.parse_and_bind('tab: complete')

        try:
            readline.read_init_file()
        except OSError:
            # An OSError here could have many causes, but the most likely one
            # is that there's no .inputrc file (or .editrc file in the case of
            # Mac OS X + libedit) in the expected location.  In that case, we
            # want to ignore the exception.
            pass

        history = os.path.join(os.path.expanduser('~'), '.python_history')
        try:
            readline.read_history_file(history)
        except IOError:
            pass
        atexit.register(readline.write_history_file, history)
Example #7
0
def Interact(session):
    import readline
    try:
        readline.read_history_file(session.config.history_file())
    except IOError:
        pass

    # Negative history means no saving state to disk.
    history_length = session.config.sys.history_length
    if history_length >= 0:
        readline.set_history_length(history_length)
    else:
        readline.set_history_length(-history_length)

    try:
        prompt = session.ui.palette.color('mailpile> ',
                                          color=session.ui.palette.BLACK,
                                          weight=session.ui.palette.BOLD)
        while True:
            session.ui.block()
            opt = raw_input(prompt).decode('utf-8').strip()
            session.ui.unblock()
            if opt:
                if ' ' in opt:
                    opt, arg = opt.split(' ', 1)
                else:
                    arg = ''
                try:
                    session.ui.display_result(Action(session, opt, arg))
                except UsageError, e:
                    session.error(unicode(e))
                except UrlRedirectException, e:
                    session.error('Tried to redirect to: %s' % e.url)
 def _load_history(self):
     try:
         hist_file = self.hist_file()
         if hist_file:
             readline.read_history_file(os.path.expanduser(hist_file))
     except FileNotFoundError:
         pass
Example #9
0
def run_classic_shell(locals, globals, first_time):
    if first_time:
        banner = "Hit Ctrl-D to return to PuDB."
    else:
        banner = ""

    ns = SetPropagatingDict([locals, globals], locals)

    from pudb.settings import get_save_config_path
    from os.path import join
    hist_file = join(
            get_save_config_path(),
            "shell-history")

    if HAVE_READLINE:
        readline.set_completer(
                rlcompleter.Completer(ns).complete)
        readline.parse_and_bind("tab: complete")
        try:
            readline.read_history_file(hist_file)
        except IOError:
            pass

    from code import InteractiveConsole
    cons = InteractiveConsole(ns)

    cons.interact(banner)

    if HAVE_READLINE:
        readline.write_history_file(hist_file)
Example #10
0
    def run_shell(self):
        do_readline = sys.stdin.isatty() and ('-', sys.stdin) in self.files
        if do_readline and os.path.exists(os.path.expanduser('~/.fsh_history')):
            readline.read_history_file(os.path.expanduser('~/.fsh_history'))
            for file in ('/etc/inputrc', os.path.expanduser('~/.inputrc')):
                if os.path.exists(file):
                    with open(file) as fd:
                        readline.parse_and_bind(fd.read())

        while self.files:
            self.curfile, self.curfd, self.curline = self.files[0]
            self.files = self.files[1:]
            if self.verbose:
                print "%s(Now processing %s)" % (self.ps4, self.curfile)
            try:
                while True:
                    line = self.get_input()
                    if not line:
                        break
                    if self.verbose:
                        print '%s%s (%s, line %d)' % (self.ps4, line, self.curfile, self.curline)
                    self.parse_and_run(line)

            except:
                if do_readline:
                    readline.write_history_file(os.path.expanduser('~/.fsh_history'))
                raise

        if do_readline:
            readline.write_history_file(os.path.expanduser('~/.fsh_history'))
Example #11
0
 def init_history(self):
     histfile=os.path.expanduser("~/.%s-history" % self.__name)
     try:
         readline.read_history_file(histfile)
     except IOError:
         pass
     atexit.register(self.save_history, histfile)
Example #12
0
    def main(self, argv):
        cmd_args = argv[1:]
        if cmd_args:
            cmd_line = u' '.join(cmd_args)
            cmds = cmd_line.split(';')
            for cmd in cmds:
                ret = self.onecmd(cmd)
                if ret:
                    return ret
        elif self.DISABLE_REPL:
            self._parser.print_help()
            self._parser.exit()
        else:
            try:
                import readline
            except ImportError:
                pass
            else:
                # Remove '-' from delims
                readline.set_completer_delims(readline.get_completer_delims().replace('-', ''))

                history_filepath = os.path.join(self.weboob.workdir, '%s_history' % self.APPNAME)
                try:
                    readline.read_history_file(history_filepath)
                except IOError:
                    pass

                def savehist():
                    readline.write_history_file(history_filepath)
                atexit.register(savehist)

            self.intro += '\nLoaded backends: %s\n' % ', '.join(sorted(backend.name for backend in self.weboob.iter_backends()))
            self._interactive = True
            self.cmdloop()
Example #13
0
def _test_main():
    import os
    import readline
    import atexit
    # Setup history and readline facility for remote q:
    histfile = os.path.join(os.environ['HOME'], '.pyhistory')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)

    conn = connect()
    print('"conn" is your handle to rserve. Type e.g. "conn(\'1\')" '
          'for string evaluation.')
    # r('x<-1:20; y<-x*2; lm(y~x)')
    sc = open('../testData/test-script.R').read()
    v = conn.r(sc)
    open('r-test-png.png', 'w').write(v[3])
    conn.r.v = 'abc'
    conn.r('func0 <- function() { 3 }')
    conn.r('func1 <- function(a1) { a1 }')
    conn.r('func2 <- function(a1, a2) { list(a1, a2) }')
    conn.r('funcKW <- function(a1=1, a2=4) { list(a1, a2) }')
    conn.r('squared<-function(t) t^2')
Example #14
0
 def _init_history(self):
     try:
         readline.set_history_length(100)
         readline.read_history_file(self._history_file)
     except IOError:
         pass
     atexit.register(self._save_history)
Example #15
0
 def _init_history_file(self):
     if hasattr(readline, "read_history_file"):
         try:
             readline.read_history_file(self.history_file)
         except FileNotFoundError:
             pass
         atexit.register(self.save_history)
Example #16
0
def interact():

    import readline

    history_file = os.path.expanduser(HISTORY_FILE_TEMPLATE)
    try:
        readline.read_history_file(history_file)
    except IOError:
        pass
    atexit.register(readline.write_history_file, history_file)

    interp = gcode.Interpreter()
    while True:
        try:
            line = raw_input('> ')
        except (EOFError, KeyboardInterrupt):
            print
            return
        restart_as_needed()
        try:
            sline = gcode.SourceLine(line, source='<tty>')
            action = interp.interpret_line(sline)
            if action:
                print 'ACTION', action
        except gcode.GCodeSyntaxError:
            z = traceback.format_exc(0)
            z = z.split('\n', 3)[3]   # discard first three lines
            z = z.replace('  ^', '^') # move caret left two columns
            z = z.rstrip()            # discard trailing newline
            print >>sys.stderr, z
        except gcode.GCodeException:
            z = traceback.format_exc(0)
            z = z.split('\n', 1)[1]   # discard first line
            z = z.rstrip()            # discard trailing newline
            print >>sys.stderr, z
Example #17
0
def main(args=None, options=None):
    if options is None:
        options = ClientOptions()
    options.realize(args, doc=__doc__)
    c = Controller(options)
    if options.args:
        c.onecmd(" ".join(options.args))
    if options.interactive:
        try:
            import readline
            if options.history_file:
                try:
                    readline.read_history_file(options.history_file)
                except IOError:
                    pass
                def save():
                    try:
                        readline.write_history_file(options.history_file)
                    except IOError:
                        pass
                import atexit
                atexit.register(save)
        except ImportError:
            pass
        try:
            c.cmdqueue.append('status')
            c.cmdloop()
        except KeyboardInterrupt:
            c.output('')
            pass
Example #18
0
    def init(self, **kw):
        if kw.has_key('stdin'):
            cmd.Cmd.__init__(self, None, stdin=kw['stdin'])
            self.use_rawinput = False
        else:
            cmd.Cmd.__init__(self)

        # initialize a new local namespace.
        namespaces.new_local_dict()

        # import readline history, if available.
        if readline:
            try:
                readline.read_history_file('.twill-history')
            except IOError:
                pass

        # fail on unknown commands? for test-shell, primarily.
        self.fail_on_unknown = kw.get('fail_on_unknown', False)

        # handle initial URL argument
        if kw.get('initial_url'):
            commands.go(kw['initial_url'])
            
        self._set_prompt()

        self.names = []
        
        global_dict, local_dict = namespaces.get_twill_glocals()

        ### add all of the commands from twill.
        for command in parse.command_list:
            fn = global_dict.get(command)
            self.add_command(command, fn.__doc__)
Example #19
0
    def __init__(self, inp=None, out=None, opts={}):
        get_option = lambda key: Mmisc.option_set(opts, key,
                                                  DEFAULT_USER_SETTINGS)

        Minput = import_relative('input', '..inout', 'trepan')
        Moutput = import_relative('output', '..inout', 'trepan')

        atexit.register(self.finalize)
        self.interactive = True  # Or at least so we think initially
        self.input       = inp or Minput.DebuggerUserInput()
        self.output      = out or Moutput.DebuggerUserOutput()

        if self.input.use_history():
            complete = get_option('complete')
            if complete:
                parse_and_bind("tab: complete")
                set_completer(complete)
                pass
            self.histfile = get_option('histfile')
            if self.histfile:
                try:
                    read_history_file(histfile)
                except IOError:
                    pass
                set_history_length(50)
                atexit.register(write_history_file, self.histfile)
                pass
        return
Example #20
0
File: cli.py Project: pwman3/pwman3
    def __init__(self, db, hasxsel, callback, config_parser, **kwargs):
        """
        initialize CLI interface, set up the DB
        connecion, see if we have xsel ...
        """
        super(PwmanCli, self).__init__(**kwargs)
        self.intro = "%s %s (c) visit: %s" % ('pwman3', version, website)
        self._historyfile = config_parser.get_value("Readline", "history")
        self.hasxsel = hasxsel
        self.config = config_parser

        try:
            enc = CryptoEngine.get()
            enc.callback = callback()
            self._db = db
            #  this cascades down all the way to setting the database key
            self._db.open()
        except Exception as e:  # pragma: no cover
            self.error(e)
            sys.exit(1)

        try:
            readline.read_history_file(self._historyfile)
        except IOError as e:  # pragma: no cover
            pass

        self.prompt = "pwman> "
Example #21
0
 def init_history(self, histfile):
     if hasattr(readline, "read_history_file"):
         try:
             readline.read_history_file(histfile)
         except IOError:
             pass
         atexit.register(self.save_history, histfile)
Example #22
0
 def history_init(self, filename, size):
     self.history_file = filename
     self.history_size = size
     if filename and os.path.exists(filename):
         readline.read_history_file(filename)
     readline.set_history_length(size)
     self.history_reset()
Example #23
0
    def exec_cmdloop(self, args, options):
        try:
            import readline
            delims = readline.get_completer_delims()
            delims = delims.replace(':', '')  # "group:process" as one word
            delims = delims.replace('*', '')  # "group:*" as one word
            delims = delims.replace('-', '')  # names with "-" as one word
            readline.set_completer_delims(delims)

            if options.history_file:
                try:
                    readline.read_history_file(options.history_file)
                except IOError:
                    pass

                def save():
                    try:
                        readline.write_history_file(options.history_file)
                    except IOError:
                        pass

                import atexit
                atexit.register(save)
        except ImportError:
            pass
        try:
            self.cmdqueue.append('status')
            self.cmdloop()
        except KeyboardInterrupt:
            self.output('')
            pass
Example #24
0
def rlinput(prompt, prefill='', oneline=False, ctxkey=''):
    """
    Get user input with readline editing support.
    """

    sentinel = ''
    if prefill is None:
        prefill = ''
    
    def only_once(text):
        """ generator for startup hook """
        readline.insert_text(text)
        yield
        while True:
            yield

    savedhist = NamedTemporaryFile()
    readline.write_history_file(savedhist.name)
    ctxhistname = ".tl" + ctxkey + "history"
    ctxhistfile = os.path.join(G.ProjectFolder, ctxhistname)
    try:
        readline.clear_history()
    except AttributeError:
        print "This readline doesn't support clear_history()"
        raise
    
    savedcompleter = readline.get_completer()
    try:
        ulines = uniqify(ctxhistfile)
        readline.read_history_file(ctxhistfile)
        readline.set_completer(HistoryCompleter(ulines).complete)
    except IOError:
        pass

    readline.parse_and_bind('tab: complete')
    saveddelims = readline.get_completer_delims()
    readline.set_completer_delims('') ## No delims. Complete entire lines.
    readline.set_completion_display_matches_hook(match_display_hook)
    gen = only_once(prefill)
    readline.set_startup_hook(gen.next)
    try:
        if oneline:
            edited = raw_input(prompt)
        else:
            print prompt
            edited = "\n".join(iter(raw_input, sentinel))

        if edited.endswith(r'%%'):
            ## Invoke external editor
            edited = external_edit(edited[0:-2])
        return edited
    finally:
        ## Restore readline state 
        readline.write_history_file(ctxhistfile)
        readline.clear_history()
        readline.read_history_file(savedhist.name)
        savedhist.close()
        readline.set_completer(savedcompleter)
        readline.set_completer_delims(saveddelims)
        readline.set_startup_hook()    
Example #25
0
File: __init__.py Project: gera/spg
def get_input_with_readline(filename):
	"""
	sets up readline support for entering sitenames, completed from the
	existing list and accepts a line of input.
	"""
	if not os.path.exists(filename):
		file(filename, "w").close()

	all_lines = map(lambda x: x.strip(), file(filename).readlines())

	def  completer(text, state):
		"""
		custom readline completer
		"""
		candidates = filter(lambda x: x.startswith(text), all_lines)
		if state <= len(candidates):
			return candidates[state-1]
		else:
			return None

	try:
		import readline
		readline.set_completer(completer)
		readline.read_history_file(filename)
		readline.parse_and_bind('tab: complete')
		if hasattr(readline, 'readline'):
			print "sitename: ",
			readline._issued = True
			return readline.readline(history=all_lines, histfile=None)
		else:
			return raw_input("sitename: ")
	except:
		# no readline?
		return raw_input("sitename: ")
    def setup(self):
        """ Initialization of third-party libraries

        Setting interpreter history.
        Setting appropriate completer function.

        :return:
        """
        if not os.path.exists(self.history_file):
            with open(self.history_file, 'a+') as history:
                if is_libedit():
                    history.write("_HiStOrY_V2_\n\n")

        readline.read_history_file(self.history_file)
        readline.set_history_length(self.history_length)
        atexit.register(readline.write_history_file, self.history_file)

        readline.parse_and_bind('set enable-keypad on')

        readline.set_completer(self.complete)
        readline.set_completer_delims(' \t\n;')
        if is_libedit():
            readline.parse_and_bind("bind ^I rl_complete")
        else:
            readline.parse_and_bind("tab: complete")
def main(argv):
  parser = optparse.OptionParser()
  parser.add_option('-s', '--server', dest='server',
                    help='The hostname your app is deployed on. '
                         'Defaults to <app_id>.appspot.com.')
  (options, args) = parser.parse_args()

  if not args or len(args) > 2:
    print >> sys.stderr, __doc__
    if len(args) > 2:
      print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
    sys.exit(1)

  appid = args[0]
  if len(args) == 2:
    path = args[1]
  else:
    path = DEFAULT_PATH

  remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
                                     servername=options.server)
  remote_api_stub.MaybeInvokeAuthentication()

  readline.parse_and_bind('tab: complete')
  atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
  sys.ps1 = '%s> ' % appid
  if os.path.exists(HISTORY_PATH):
    readline.read_history_file(HISTORY_PATH)

  code.interact(banner=BANNER, local=globals())
Example #28
0
def enable_readline():
    """
    Imports the `readline` module to enable advanced repl text manipulation,
    and command history navigation.

    Returns True if success, otherwise False.
    """
    try:
        import readline
    except ImportError:
        return False

    import os
    import atexit
    histfile = os.path.join(os.path.expanduser("~"), ".clojurepyhist")
    if not os.path.isfile(histfile):
        with open(histfile, 'a'):
            os.utime(histfile, None)
        os.chmod(histfile, int('640',8))
    try:
        readline.read_history_file(histfile)
    except IOError:
        # Pass here as there isn't any history file, so one will be
        # written by atexit
        pass
    atexit.register(readline.write_history_file, histfile)
    return True
 def __enter__(self):
     if readline_loaded:
         if readline.__doc__ and 'libedit' in readline.__doc__:
             readline.parse_and_bind("bind '\t' rl_complete")  # Enable tab completions on MacOS
         else:
             readline.parse_and_bind("tab: complete")  # and on other OSs
         readline.parse_and_bind("set completion-ignore-case on")
         readline.parse_and_bind("set show-all-if-ambiguous on")
         readline.parse_and_bind("set completion-map-case on")
         readline.parse_and_bind("set show-all-if-unmodified on")
         readline.parse_and_bind("set expand-tilde on")
         history_file_dir = appdirs.user_data_dir(self.this_program_name, self.this_program_name)
         os.makedirs(history_file_dir, exist_ok=True)
         self.history_file_path = os.path.join(history_file_dir, "." + self.this_program_name + "_console_history")
         try:
             readline.read_history_file(self.history_file_path)
         except Exception:  # Corrupt or non existent history file might raise an exception
             try:
                 os.remove(self.history_file_path)
             except Exception:
                 pass  # if removing the file also fail - just ignore it
     if colorama_loaded:
         colorama.init()
     self.prompt = self.this_program_name + ": "
     self.save_dir = os.getcwd()
     return self
def setup_readline_history(histfile=None):
    """Handle readline / history."""
    if not histfile:
        histfile = "./.python_history"
        # histfile = os.path.join(os.path.expanduser("~"), ".python_history")

    if sys.version_info.major >= 3:
        # python3
        try:
            readline.read_history_file(histfile)
            h_len = readline.get_current_history_length()
        except os.FileNotFoundError:
            open(histfile, 'wb').close()
            h_len = 0
    elif sys.version_info.major == 2:
        # python2
        try:
            readline.read_history_file(histfile)
            h_len = readline.get_current_history_length()
        except IOError:
            open(histfile, 'wb').close()
            h_len = 0
    else:
        h_len = 0

    print("readline history length: {}".format(
        readline.get_current_history_length()
    ))

    atexit.register(save, h_len, histfile)
Example #31
0
    def start(self):
        # log start
        log.info('Starting viper-cli')

        # Logo.
        logo()

        # Setup shell auto-complete.
        def complete(text, state):
            # filesystem path completion only makes sense for a few commands/modules
            fs_path_completion = False

            # clean up user input so far (no leading/trailing/duplicate spaces)
            line = " ".join(readline.get_line_buffer().split())
            words = line.split(" ")  # split words; e.g. store -f /tmp -> ['store', '-f', '/tmp']

            if words[0] in [i for i in self.cmd.commands]:
                # handle completion for commands

                # enable filesystem path completion for certain commands (e.g. export, store)
                if words[0] in [x for x in self.cmd.commands if self.cmd.commands[x]["fs_path_completion"]]:
                    fs_path_completion = True

                options = [key for key in self.cmd.commands[words[0]]["parser_args"]]
                completions = [i for i in options if i.startswith(text) and i not in words]

            elif words[0] in [i for i in __modules__]:
                # handle completion for modules
                if len(words) == 1:
                    # only the module name is give so far - present all args and the subparsers (if any)
                    options = [key for key in __modules__[words[0]]["parser_args"]]
                    options += [key for key in __modules__[words[0]]["subparser_args"]]

                elif len(words) == 2:
                    # 1 complete word and one either complete or incomplete that specifies the subparser or an arg
                    if words[1] in list(__modules__[words[0]]["parser_args"]):
                        # full arg for a module is given
                        options = [key for key in __modules__[words[0]]["parser_args"]]

                    elif words[1] in list(__modules__[words[0]]["subparser_args"]):
                        # subparser is specified - get all subparser args
                        options = [key for key in __modules__[words[0]]["subparser_args"][words[1]]]

                    else:
                        options = [key for key in __modules__[words[0]]["parser_args"]]
                        options += [key for key in __modules__[words[0]]["subparser_args"]]

                else:  # more that 2 words
                    if words[1] in list(__modules__[words[0]]["subparser_args"]):
                        # subparser is specified - get all subparser args
                        options = [key for key in __modules__[words[0]]["subparser_args"][words[1]]]
                    else:
                        options = [key for key in __modules__[words[0]]["parser_args"]]

                completions = [i for i in options if i.startswith(text) and i not in words]

            else:
                # initial completion for both commands and modules
                completions = [i for i in self.cmd.commands if i.startswith(text)]
                completions += [i for i in __modules__ if i.startswith(text)]

            if state < len(completions):
                return completions[state]

            if fs_path_completion:
                # completion for paths only if it makes sense
                if text.startswith("~"):
                    text = "{0}{1}".format(expanduser("~"), text[1:])
                return (glob.glob(text + '*') + [None])[state]

            return

        # Auto-complete on tabs.
        readline.set_completer_delims(' \t\n;')
        readline.parse_and_bind('tab: complete')
        readline.set_completer(complete)

        # Save commands in history file.
        def save_history(path):
            readline.write_history_file(path)

        # If there is an history file, read from it and load the history
        # so that they can be loaded in the shell.
        # Now we are storing the history file in the local project folder
        history_path = os.path.join(__project__.path, 'history')

        if os.path.exists(history_path):
            readline.read_history_file(history_path)

        readline.set_history_length(10000)

        # Register the save history at program's exit.
        atexit.register(save_history, path=history_path)

        # Main loop.
        while self.active:
            # If there is an open session, we include the path to the opened
            # file in the shell prompt.
            # TODO: perhaps this block should be moved into the session so that
            # the generation of the prompt is done only when the session's
            # status changes.
            prefix = ''
            if __project__.name:
                prefix = bold(cyan(__project__.name)) + ' '

            if __sessions__.is_set():
                stored = ''
                filename = ''
                if __sessions__.current.file:
                    filename = __sessions__.current.file.name
                    if not Database().find(key='sha256', value=__sessions__.current.file.sha256):
                        stored = magenta(' [not stored]', True)

                misp = ''
                if __sessions__.current.misp_event:
                    misp = ' [MISP'
                    if __sessions__.current.misp_event.event.id:
                        misp += ' {}'.format(__sessions__.current.misp_event.event.id)
                    else:
                        misp += ' New Event'
                    if __sessions__.current.misp_event.off:
                        misp += ' (Offline)'
                    misp += ']'

                prompt = (prefix + cyan('viper ', True) +
                          white(filename, True) + blue(misp, True) + stored + cyan(' > ', True))
            # Otherwise display the basic prompt.
            else:
                prompt = prefix + cyan('viper > ', True)

            # force str (Py3) / unicode (Py2) for prompt
            if sys.version_info <= (3, 0):
                prompt = prompt.encode('utf-8')
            else:
                prompt = str(prompt)

            # Wait for input from the user.
            try:
                data = input(prompt).strip()
            except KeyboardInterrupt:
                print("")
            # Terminate on EOF.
            except EOFError:
                self.stop()
                print("")
                continue
            # Parse the input if the user provided any.
            else:
                # If there are recognized keywords, we replace them with
                # their respective value.
                data = self.keywords(data)
                # Skip if the input is empty.
                if not data:
                    continue

                # Check for output redirection
                # If there is a > in the string, we assume the user wants to output to file.
                if '>' in data:
                    data, console_output['filename'] = data.split('>', 1)
                    if ';' in console_output['filename']:
                        console_output['filename'], more_commands = console_output['filename'].split(';', 1)
                        data = '{};{}'.format(data, more_commands)
                    print("Writing output to {0}".format(console_output['filename'].strip()))

                # If the input starts with an exclamation mark, we treat the
                # input as a bash command and execute it.
                # At this point the keywords should be replaced.
                if data.startswith('!'):
                    os.system(data[1:])
                    continue

                # Try to split commands by ; so that you can sequence multiple
                # commands at once.
                # For example:
                # viper > find name *.pdf; open --last 1; pdf id
                # This will automatically search for all PDF files, open the first entry
                # and run the pdf module against it.
                split_commands = data.split(';')
                for split_command in split_commands:
                    split_command = split_command.strip()
                    if not split_command:
                        continue

                    # If it's an internal command, we parse the input and split it
                    # between root command and arguments.
                    root, args = self.parse(split_command)

                    # Check if the command instructs to terminate.
                    if root in ('exit', 'quit'):
                        self.stop()
                        continue

                    try:
                        # If the root command is part of the embedded commands list we
                        # execute it.
                        if root in self.cmd.commands:
                            self.cmd.commands[root]['obj'](*args)
                            del(self.cmd.output[:])
                        # If the root command is part of loaded modules, we initialize
                        # the module and execute it.
                        elif root in __modules__:
                            module = __modules__[root]['obj']()
                            module.set_commandline(args)
                            module.run()

                            if cfg.modules.store_output and __sessions__.is_set():
                                try:
                                    Database().add_analysis(__sessions__.current.file.sha256, split_command, module.output)
                                except:
                                    pass
                            del(module.output[:])
                        else:
                            print("Command not recognized.")
                    except KeyboardInterrupt:
                        pass
                    except Exception:
                        print_error("The command {0} raised an exception:".format(bold(root)))
                        traceback.print_exc()

                console_output['filename'] = None   # reset output to stdout
Example #32
0
    # import some modules which might be useful on the command line
    import numpy as np

    # Use CLI history and tab completion
    import atexit
    import os
    historyPath = os.path.expanduser("~/.pyhistory")
    try:
        import readline
    except ImportError:
        print("Import Error in __main__: Module readline not available.")
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
        if os.path.exists(historyPath):
            readline.read_history_file(historyPath)

    def save_history(new_historyPath=historyPath):
        try:
            import readline
        except ImportError:
            print("Import Error in __main__: Module readline not available.")
        else:
            readline.write_history_file(new_historyPath)

    atexit.register(save_history)
else:
    # non-interactive, start application in different modes
    if args.profile:
        # with profiler
        import cProfile, pstats
        #import tab completion functionality
        import rlcompleter

        #Override completer from rlcompleter to disable automatic ( on callable
        completer_obj = rlcompleter.Completer()

        def nop(val, word):
            return word

        completer_obj._callable_postfix = nop
        readline.set_completer(completer_obj.complete)

        #activate tab completion
        readline.parse_and_bind("tab: complete")
        readline.set_history_length(_history_length)
        readline.read_history_file(_history_file_name)
        atexit.register(at_exit_callback)

        import sys
        print("Python " + sys.version + " on " + sys.platform)

    sys.path.append(os.path.abspath(os.curdir))
    # output = subprocess.check_output("../install/bin/clang-cl.exe -Xclang -ast-print -fsyntax-only sample.c")

    command_line = [
        "../install/bin/clang-cl.exe", "-Xclang", "-ast-print",
        "-fsyntax-only", "-Wno-unused-command-line-argument",
        "-Wno-macro-redefined", "-Wno-unused-parameter",
        "-fms-compatibility-version=1700", "-fms-compatibility", "-m64"
    ]
    command_line.append(
Example #34
0
                dict([(k, self._base % v) for k, v in self.COLOR_TEMPLATES]))
        else:
            self.update(
                dict([(k, self.NoColor) for k, v in self.COLOR_TEMPLATES]))


_c = TermColors()

# Enable a History
##################

HISTFILE = "%s/.pyhistory" % os.environ["HOME"]

# Read the existing history if there is one
if os.path.exists(HISTFILE):
    readline.read_history_file(HISTFILE)

# Set maximum number of items that will be written to the history file
readline.set_history_length(1000)


def savehist():
    readline.write_history_file(HISTFILE)


readline.parse_and_bind("tab: complete")
atexit.register(savehist)

# Enable Color Prompts
######################
Example #35
0
 def _pop_history(self, tmp_file):
     readline.clear_history()
     readline.read_history_file(tmp_file.name)
     tmp_file.close()
Example #36
0
 def load_console_history(self):
     if os.path.exists(self._history_file):
         readline.read_history_file(self._history_file)
     return
Example #37
0
        cmd.Cmd.__init__(self)
        self.intro = "%s %s (c) %s <%s>" % (pwman.appname, pwman.version,
                                            pwman.author, pwman.authoremail)
        self._historyfile = config.get_value("Readline", "history")

        try:
            enc = CryptoEngine.get()
            enc.set_callback(CLICallback())
            self._db = db
            self._db.open()
        except Exception, e:
            self.error(e)
            sys.exit(1)

        try:
            readline.read_history_file(self._historyfile)
        except Exception, e:
            pass

        self.prompt = "pwman> "


_defaultwidth = 10


def getonechar(question, width=_defaultwidth):
    question = "%s " % (question)
    print question.ljust(width),
    sys.stdout.flush()

    fd = sys.stdin.fileno()
Example #38
0
    def start(self):
        from stf.core.dataset import __datasets__
        # Logo.
        logo()
        self.db.list()

        # Setup shell auto-complete.
        def complete(text, state):
            # Try to autocomplete commands.
            cmds = [i for i in self.cmd.commands if i.startswith(text)]
            if state < len(cmds):
                return cmds[state]

            # Try to autocomplete modules.
            #mods = [i for i in __modules__ if i.startswith(text)]
            #if state < len(mods):
            #return mods[state]

            # Then autocomplete paths.
            if text.startswith("~"):
                text = "{0}{1}".format(os.getenv("HOME"), text[1:])
            return (glob.glob(text + '*') + [None])[state]

        # Auto-complete on tabs.
        readline.set_completer_delims(' \t\n;')
        readline.parse_and_bind('tab: complete')
        readline.parse_and_bind('set editing-mode vi')
        readline.set_completer(complete)

        # Save commands in history file.
        def save_history(path):
            readline.write_history_file(path)

        if os.path.exists(self.history_path):
            readline.read_history_file(self.history_path)

        # Register the save history at program's exit.
        atexit.register(save_history, path=self.history_path)

        # Main loop.
        while self.active:
            if __datasets__.current:
                self.prefix = red(__datasets__.current.get_name() + ': ')
            else:
                self.prefix = ''
            prompt = self.prefix + cyan('stf > ', True)

            # Wait for input from the user.
            try:
                data = raw_input(prompt).strip()
            except KeyboardInterrupt:
                print("")
            # Terminate on EOF.
            except EOFError:
                self.stop()
                print("")
                continue
            # Parse the input if the user provided any.
            else:
                # Skip if the input is empty.
                if not data:
                    continue

                # If the input starts with an exclamation mark, we treat the
                # input as a bash command and execute it.
                if data.startswith('!'):
                    os.system(data[1:])
                    continue

                # Try to split commands by ; so that you can sequence multiple
                # commands at once.
                # For example:
                # viper > find name *.pdf; open --last 1; pdf id
                # This will automatically search for all PDF files, open the first entry
                # and run the pdf module against it.
                split_commands = data.split(';')
                for split_command in split_commands:
                    split_command = split_command.strip()
                    if not split_command:
                        continue

                    # If it's an internal command, we parse the input and split it
                    # between root command and arguments.
                    root, args = self.parse(split_command)

                    # Check if the command instructs to terminate.
                    if root in ('exit', 'quit'):
                        self.stop()
                        continue

                    try:
                        # If the root command is part of the embedded commands list we
                        # execute it.
                        if root in self.cmd.commands:
                            self.cmd.commands[root]['obj'](*args)

                        # If the root command is part of loaded modules, we initialize
                        # the module and execute it.
                        #elif root in __modules__:
                        #    module = __modules__[root]['obj']()
                        #    module.set_commandline(args)
                        #    module.run()

                        #    self.print_output(module.output, filename)
                        #    del(module.output[:])
                        else:
                            print("Command not recognized.")
                    except KeyboardInterrupt:
                        pass
                    except Exception as e:
                        print_error(
                            "The command {0} raised an exception:".format(
                                bold(root)))
                        traceback.print_exc()
Example #39
0
 def read_history(self):
     if hasReadline and self._history_path:
         try:
             readline.read_history_file(self._history_path)
         except:
             pass
Example #40
0
 def save_history(historyPath=historyPath):
     import readline
     readline.write_history_file(historyPath)
     if os.path.exists(historyPath):
         readline.read_history_file(historyPath)
def _init():
    import atexit
    import os
    import sys
    import readline
    import types
    import time
    import uuid
    import pprint
    import hashlib
    import datetime
    try:
        import __builtin__
    except ImportError:
        import builtins as __builtin__

    __import__('rlcompleter')
    histdir = os.path.expanduser('~/.pyhist')
    try:
        os.makedirs(histdir)
    except OSError:
        pass

    def _b(x):
        if not isinstance(x, bytes):
            x = x.encode('utf-8')
        return x

    histfile = os.path.join(
        histdir,
        hashlib.sha1(os.path.normpath(_b(os.path.abspath(
            sys.prefix)))).hexdigest())

    try:
        readline.read_history_file(histfile)
    except IOError:
        pass

    if 'libedit' in readline.__doc__:
        readline.parse_and_bind("bind '\t' rl_complete")
    else:
        readline.parse_and_bind("tab: complete")

    atexit.register(readline.write_history_file, histfile)

    def _magic_uuid(val=None):
        if val is None:
            return uuid.uuid4()
        elif isinstance(val, uuid.UUID):
            return val
        elif len(val) == 16:
            return uuid.UUID(bytes=val)
        return uuid.UUID(val)

    helpers = types.ModuleType('helpers')
    helpers.histfile = histfile
    helpers.pp = pprint.pprint
    helpers.uuid = _magic_uuid
    helpers.UUID = uuid.UUID
    helpers.uuid3 = uuid.uuid3
    helpers.uuid4 = uuid.uuid4
    helpers.uuid5 = uuid.uuid5
    helpers.dt = datetime.datetime
    helpers.datetime = datetime.datetime
    helpers.td = datetime.timedelta
    helpers.timedelta = datetime.timedelta
    helpers.time = time.time
    __builtin__.h = helpers
Example #42
0
import os
import readline
import atexit
import json

import vibrance

histfile = os.path.join(os.path.expanduser("~"), ".vibrance_history")

try:
    readline.read_history_file(histfile)
    readline.set_history_length(1000)
except FileNotFoundError:
    pass

atexit.register(readline.write_history_file, histfile)

api = vibrance.Interface("Terminal Composer")


@api.loop
def loop():
    i = input("Messages> ")
    api.messages = json.loads(i)
Example #43
0
try:
    client = APP.CLIClient()
    signature = client.recv()
except socket.error:
    print "Connection failed."
    sys.exit(1)

cliRoot = CreateCliTree(signature)
cliRoot.root = True
print signature
print cliRoot.printall()


try:
    if hasReadline:
        readline.read_history_file ( os.path.expanduser("~/.prov-history") )
except IOError:
    pass
if hasReadline:
    atexit.register(readline.write_history_file, os.path.expanduser("~/.prov-history") )
    readline.parse_and_bind("tab: complete")
    readline.set_completer ( command_completer )
    readline.set_completer_delims("\n")

currentLevel = cliRoot

while True:
    try:        
        command = raw_input("(provcli) %s> " % currentLevel.path())

        if command.upper() in ["EXIT", "QUIT"]: break
Example #44
0
def init_history(histfile: str) -> None:
    if hasattr(readline, "read_history_file"):
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass
Example #45
0
def shell_thread(**kwargs):
    hist_loc = ".overwatch_shell.history"
    console = InteractiveConsole()
    console.locals = locals()
    console.locals.update(kwargs)
    console.locals.update(globals())
    console.push("import sys")
    console.push("import os")
    console.push("sys.path.append(os.getcwd())")
    readline.set_completer(rlcompleter.Completer(console.locals).complete)
    readline.parse_and_bind("tab: complete")
    if not os.path.isfile(hist_loc):
        f = open(hist_loc, "x")
        f.write("\n")
        f.close()
    readline.read_history_file(hist_loc)
    # Opens a Python shell

    todo = TodoPointer()

    def snip(location, _todo=todo):
        _todo.location = location

    console.locals["snip"] = snip

    buf = []

    def get_input(prompt):
        if len(buf) > 0:
            l = buf.pop(0)
            print("::: " + l.rstrip("\n"))
            return l
        else:
            return input(prompt)

    while True:
        try:
            c = get_input(">>> ")
            if (not c == ""):
                if console.push(
                        c
                ):  # console.push(c) executes the string c in the console.
                    # if it is True, it means it expects more input
                    # (for example a function definition)
                    _input = ' '
                    totalcommand = ''
                    while _input != '':
                        _input = get_input('... ')
                        totalcommand += _input + '\n'
                    console.push(totalcommand)

                if todo.location is not None:
                    with open(todo.location, 'r') as f:
                        code = f.readlines()
                        buf.extend(code)
                        buf.append("")
                    todo.location = None
        except (EOFError, KeyboardInterrupt):
            print("\nShell ended.")
            break
        except Exception as e:
            console.showtraceback()
        readline.write_history_file(hist_loc)
Example #46
0
def main():

    global xnodes
    global no_run

    # read all the input files
    if readline:
        history = os.path.join(os.path.expanduser("~"), ".steuermann_history")
        try:
            readline.read_history_file(history)
        except IOError:
            pass
        import atexit
        atexit.register(readline.write_history_file, history)

    # easyargs spec definition:
    #
    #        '-v' : '',              # arg takes no parameter, opt['-v'] is
    #                                # how many times it occurred
    #        '-f' : '=',             # arg takes a parameter
    #        '-mf' : '=+',           # arg takes a parameter, may be specified
    #                                # several times to get a list
    #        '--verbose' : '-v',     # arg is an alias for some other arg

    opt, args = easyargs.get(allowed_flags, allow_unexpected=True)

    do_all = opt['-a']
    no_run = opt['-n']
    ''' 
    NOTE: moving this functionality into nodes.py
    
    # find any unknown arguments like --something=whatever, set as conditions
    arguments = sys.argv[1:]
    for a in arguments:
        if '--' in a and '=' in a:
            
            not_allowed_flag = True
            for f in allowed_flags.keys():
                if a.startswith(f):
                    not_allowed_flag = False
                    break
            if not_allowed_flag:
                a = a.lstrip('--')
                k, v = a.split('=')
                nodes.saved_conditions[k] = eval(v)
    '''

    sm_files = [
        os.path.abspath(a) for a in args if ('--' not in a and '=' not in a)
    ]
    di_nodes = nodes.read_file_list(sm_files)
    xnodes = di_nodes.node_index

    # get run name
    if '-r' in opt:
        run_name = opt['-r']
    else:
        run_name = "user_%s_%s" % (username, str(
            datetime.datetime.now()).replace(' ', '_'))

    # get hosts (*.ini) file name
    if '-h' in opt:
        hosts_ini = opt['-h']
    else:
        hosts_ini = os.path.join(os.path.dirname(__file__), 'hosts.ini')

        # Use a user-defined config if it exists
        if os.path.exists(config.hosts_config):
            hosts_ini = config.hosts_config

    # parse common resources from hosts INI file
    get_common_resources(hosts_ini)

    db = config.open_db()

    if do_all:
        run_all(xnodes, run_name, hosts_ini, db)
    else:
        run_interactive(xnodes, run_name, hosts_ini, db)
      fout = open(os.path.join(rundir,filename))
      measure_rate(fout,samples,triggers)
      fout.close()
    elif keys[0] == "help":
      print "Possible commands are:"
      for i in range(len(commands)):
        print "  ",commands[i]

    else:
      print "Unknown command"
  except (ValueError, struct.error), e:
    print "Bad Input:",e


if os.path.exists(HISTORY_FILENAME):
        readline.read_history_file(HISTORY_FILENAME)


# keyboard input loop
try:
  while True:
    line = raw_input()
    if line:
      if line.startswith("readall"):
        nline = line.replace('readall', 'read')
        nruns     = int(get_key_value(line.split(),"n",1))
        for i in range(0,nruns):
          transmissiondone=0
          process_command(nline)
          while transmissiondone==0:
            time.sleep(1)
Example #48
0
 def __enter__(self) -> 'HistoryCompleter':
     if self.history_path:
         with suppress(Exception):
             readline.read_history_file(self.history_path)
         readline.set_completer(self.complete)
     return self
Example #49
0
def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
    global session
    import code,sys,cPickle,os,getopt,re
    from config import conf
    conf.interactive = True
    if loglevel is not None:
        conf.logLevel=loglevel

    the_banner = "Welcome to Scapy (%s)"
    if mybanner is not None:
        the_banner += "\n"
        the_banner += mybanner

    if argv is None:
        argv = sys.argv

    import atexit
    try:
        import rlcompleter,readline
    except ImportError:
        log_loading.info("Can't load Python libreadline or completer")
        READLINE=0
    else:
        READLINE=1
        class ScapyCompleter(rlcompleter.Completer):
            def global_matches(self, text):
                matches = []
                n = len(text)
                for lst in [dir(__builtin__), session.keys()]:
                    for word in lst:
                        if word[:n] == text and word != "__builtins__":
                            matches.append(word)
                return matches
        
    
            def attr_matches(self, text):
                m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
                if not m:
                    return
                expr, attr = m.group(1, 3)
                try:
                    object = eval(expr)
                except:
                    object = eval(expr, session)
                if isinstance(object, Packet) or isinstance(object, Packet_metaclass):
                    words = filter(lambda x: x[0]!="_",dir(object))
                    words += [x.name for x in object.fields_desc]
                else:
                    words = dir(object)
                    if hasattr( object,"__class__" ):
                        words = words + rlcompleter.get_class_members(object.__class__)
                matches = []
                n = len(attr)
                for word in words:
                    if word[:n] == attr and word != "__builtins__":
                        matches.append("%s.%s" % (expr, word))
                return matches
    
        readline.set_completer(ScapyCompleter().complete)
        readline.parse_and_bind("C-o: operate-and-get-next")
        readline.parse_and_bind("tab: complete")
    
    
    session=None
    session_name=""
    STARTUP_FILE = DEFAULT_STARTUP_FILE
    PRESTART_FILE = DEFAULT_PRESTART_FILE


    iface = None
    try:
        opts=getopt.getopt(argv[1:], "hs:Cc:Pp:d")
        for opt, parm in opts[0]:
            if opt == "-h":
                _usage()
            elif opt == "-s":
                session_name = parm
            elif opt == "-c":
                STARTUP_FILE = parm
            elif opt == "-C":
                STARTUP_FILE = None
            elif opt == "-p":
                PRESTART_FILE = parm
            elif opt == "-P":
                PRESTART_FILE = None
            elif opt == "-d":
                conf.logLevel = max(1,conf.logLevel-10)
        
        if len(opts[1]) > 0:
            raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1]))


    except getopt.GetoptError as msg:
        log_loading.error(msg)
        sys.exit(1)

    if PRESTART_FILE:
        _read_config_file(PRESTART_FILE)

    scapy_builtins = __import__("all",globals(),locals(),".").__dict__
    __builtin__.__dict__.update(scapy_builtins)
    globkeys = scapy_builtins.keys()
    globkeys.append("scapy_session")
    scapy_builtins=None # XXX replace with "with" statement
    if mydict is not None:
        __builtin__.__dict__.update(mydict)
        globkeys += mydict.keys()
    

    conf.color_theme = DefaultTheme()
    if STARTUP_FILE:
        _read_config_file(STARTUP_FILE)
        
    if session_name:
        try:
            os.stat(session_name)
        except OSError:
            log_loading.info("New session [%s]" % session_name)
        else:
            try:
                try:
                    session = cPickle.load(gzip.open(session_name,"rb"))
                except IOError:
                    session = cPickle.load(open(session_name,"rb"))
                log_loading.info("Using session [%s]" % session_name)
            except EOFError:
                log_loading.error("Error opening session [%s]" % session_name)
            except AttributeError:
                log_loading.error("Error opening session [%s]. Attribute missing" %  session_name)

        if session:
            if "conf" in session:
                conf.configure(session["conf"])
                session["conf"] = conf
        else:
            conf.session = session_name
            session={"conf":conf}
            
    else:
        session={"conf": conf}

    __builtin__.__dict__["scapy_session"] = session


    if READLINE:
        if conf.histfile:
            try:
                readline.read_history_file(conf.histfile)
            except IOError:
                pass
        atexit.register(scapy_write_history_file,readline)
    
    atexit.register(scapy_delete_temp_files)
    
    IPYTHON=False
    if conf.interactive_shell.lower() == "ipython":
        try:
            import IPython
            IPYTHON=True
        except ImportError as e:
            log_loading.warning("IPython not available. Using standard Python shell instead.")
            IPYTHON=False
        
    if IPYTHON:
        banner = the_banner % (conf.version) + " using IPython %s" % IPython.__version__

        # Old way to embed IPython kept for backward compatibility
        try:
          args = ['']  # IPython command line args (will be seen as sys.argv)
          ipshell = IPython.Shell.IPShellEmbed(args, banner = banner)
          ipshell(local_ns=session)
        except AttributeError as e:
          pass

        # In the IPython cookbook, see 'Updating-code-for-use-with-IPython-0.11-and-later'
        IPython.embed(user_ns=session, banner2=banner)

    else:
        code.interact(banner = the_banner % (conf.version),
                      local=session, readfunc=conf.readfunc)

    if conf.session:
        save_session(conf.session, session)


    for k in globkeys:
        try:
            del(__builtin__.__dict__[k])
        except:
            pass
Example #50
0
 def preloop(self):
     if readline and os.path.exists(histfile):
         readline.read_history_file(histfile)
Example #51
0
# -*- coding: UTF-8 -*-
# Example snippet to use in a PYTHONSTARTUP file
try:
    import pyreadline.rlmain

    # pyreadline.rlmain.config_path=r"c:\xxx\pyreadlineconfig.ini"
    import readline, atexit
    import pyreadline.unicode_helper

    #
    #
    # Normally the codepage for pyreadline is set to be sys.stdout.encoding
    # if you need to change this uncomment the following line
    # pyreadline.unicode_helper.pyreadline_codepage="utf8"
except ImportError:
    print "Module readline not available."
else:
    # import tab completion functionality
    import rlcompleter

    # activate tab completion
    readline.parse_and_bind("tab: complete")
    readline.read_history_file()
    atexit.register(readline.write_history_file)
    del readline, rlcompleter, atexit
Example #52
0
    def run(self):

        super(ConsoleCLI, self).run()

        sshpass = None
        becomepass = None
        vault_pass = None

        # hosts
        if len(self.args) != 1:
            self.pattern = 'all'
        else:
            self.pattern = self.args[0]
        self.options.cwd = self.pattern

        # dynamically add modules as commands
        self.modules = self.list_modules()
        for module in self.modules:
            setattr(
                self,
                'do_' + module,
                lambda arg, module=module: self.default(module + ' ' + arg))
            setattr(self,
                    'help_' + module,
                    lambda module=module: self.helpdefault(module))

        self.normalize_become_options()
        (sshpass, becomepass) = self.ask_passwords()
        self.passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        self.loader = DataLoader()

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = CLI.read_vault_password_file(
                self.options.vault_password_file, loader=self.loader)
            self.loader.set_vault_password(vault_pass)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords()[0]
            self.loader.set_vault_password(vault_pass)

        self.variable_manager = VariableManager()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=self.options.inventory)
        self.variable_manager.set_inventory(self.inventory)

        if len(self.inventory.list_hosts(self.pattern)) == 0:
            # Empty inventory
            display.warning(
                "provided hosts list is empty, only localhost is available")

        self.inventory.subset(self.options.subset)
        self.groups = self.inventory.list_groups()
        self.hosts = [x.name for x in self.inventory.list_hosts(self.pattern)]

        # This hack is to work around readline issues on a mac:
        #  http://stackoverflow.com/a/7116997/541202
        if 'libedit' in readline.__doc__:
            readline.parse_and_bind("bind ^I rl_complete")
        else:
            readline.parse_and_bind("tab: complete")

        histfile = os.path.join(os.path.expanduser("~"),
                                ".ansible-console_history")
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass

        atexit.register(readline.write_history_file, histfile)
        self.set_prompt()
        self.cmdloop()
Example #53
0
def start_repl(args):
    exploits = []

    # Transactions to frontrun
    if args.skip_mythril is False:
        print("Scanning for exploits in contract: {contract}".format(
            contract=args.contract))
        exploits += exploits_from_mythril(
            rpcHTTP=args.rpc_http,
            rpcWS=args.rpc_ws,
            rpcIPC=args.rpc_ipc,
            contract=args.contract,
            account_pk=args.account_pk,
            timeout=args.timeout,
        )
    if args.load_file is not None:
        exploits += exploits_from_file(
            file=args.load_file,
            rpcHTTP=args.rpc_http,
            rpcWS=args.rpc_ws,
            rpcIPC=args.rpc_ipc,
            contract=args.contract,
            account_pk=args.account_pk,
            timeout=args.timeout,
        )

    if len(exploits) == 0:
        print("No exploits found. You're going to need to load some exploits.")
    else:
        print("")
        print("Found exploits(s):")
        print(exploits)

    # Add local tools for console
    w3 = Web3(
        Web3.HTTPProvider(args.rpc_http,
                          request_kwargs={"timeout": args.timeout}))
    from theo.exploit.exploit import Exploit
    from theo.exploit.tx import Tx

    # Imports for REPL
    import os, atexit, readline, rlcompleter

    # Load history
    history_path = os.path.join(os.environ["HOME"], ".theo_history")
    if os.path.isfile(history_path):
        readline.read_history_file(history_path)
    # Trigger history save on exit
    atexit.register(readline.write_history_file, history_path)
    # Load variables
    vars = globals()
    vars.update(locals())
    # Start REPL
    readline.set_completer(rlcompleter.Completer(vars).complete)
    readline.parse_and_bind("tab: complete")
    del os, atexit, readline, rlcompleter
    code.InteractiveConsole(vars).interact(banner="""
Tools available in the console:
- `exploits` is an array of loaded exploits found by Mythril or read from a file
- `w3` an initialized instance of web3py for the provided HTTP RPC endpoint
- `dump()` writing a json representation of an object to a local file

Check the readme for more info:
https://github.com/cleanunicorn/theo

Theo version {version}.

""".format(version=__version__))

    print("Shutting down")
Example #54
0
def main():
    reload(sys)
    sys.setdefaultencoding('utf-8')
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
    #defino argumentos de script
    parser = argparse.ArgumentParser(description='menu for t-hoarder_kit')
    parser.add_argument('root',
                        type=str,
                        help='path where t_hoarder_kit was installed')
    action = parser.add_mutually_exclusive_group(required=True)
    action.add_argument('--windows', action='store_true', help='windows OS')
    action.add_argument('--linux', action='store_true', help='linux OS')
    action.add_argument('--mac', action='store_true', help='mac OS')
    args = parser.parse_args()
    root = args.root
    if args.windows:
        path_keys = '%s\\keys\\' % root
        path_scripts = '%s\\scripts\\' % root
        path_store = '%s\\store\\' % root
    if args.linux or args.mac:
        path_keys = '%s/keys/' % root
        path_scripts = '%s/scripts/' % root
        path_store = '%s/store/' % root
    list_suboptions_2 = [
        'profile', 'followers', 'following', 'relations', 'tweets', 'h_index'
    ]
    list_suboptions_6 = ['RT', 'reply', 'mentions']
    list_suboptions_7 = ['sort', 'entities', 'classify', 'users', 'spread']
    enviroment = False
    option = 8
    exit = 'n'
    histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
    readline.parse_and_bind("tab: complete")
    try:
        readline.read_history_file(histfile)
        # default history len is -1 (infinite), which may grow unruly
        readline.set_history_length(1000)
    except IOError:
        pass
    if not enviroment:
        print 'working in', root
        print ' '
        print '----------------------------------------'
        print '------>    Environment  data     <------'
        print '----------------------------------------'

        app_keys = get_inputfile(
            'Enter the file name with the application keys: ', path_keys)
        user = raw_input('Enter a twitter user: '******'Enter experiment name: ', path_store)
        enviroment = True
    file_app_keys = '%s%s' % (path_keys, app_keys)
    file_user_keys = '%s%s.key' % (path_keys, user)
    if args.windows:
        path_experiment = '%s%s\\' % (path_store, experiment)
        path_resources = '%s\resources\\' % (root)
    elif args.linux or args.mac:
        path_experiment = '%s%s/' % (path_store, experiment)
        path_resources = '%s/resources/' % (root)

    while exit != 'y':
        try:
            print '--------------------------------'
            print ' Working with:'
            print '   app:', app_keys
            print '   user:'******'   experiment:', experiment
            print '--------------------------------'
            print 'What function do you want to run?'
            print '--------------------------------'
            print '1. Get a user token access'
            print '2. Get users information (profile | followers | following | relations | tweets | h_index)'
            print '3. Make a query on Twitter'
            print '4. Get tweets on real time'
            print '5. Generate the declared relations graph (followers or following or both)'
            print '6. Generate the dynamic relations graph (RT | reply | mentions)'
            print '7. Processing tweets (sort |entities| classify| users | spread)'
            print '8. Exit'
            print ' '
            while True:
                try:
                    option = int(raw_input('--> Enter option: '))
                    break
                except:
                    pass
            if option == 1:
                os.chdir(path_keys)
                command = "python %stweet_auth.py '%s' '%s'" % (path_scripts,
                                                                app_keys, user)
                os.system(command)
            elif option == 2:
                os.chdir(path_experiment)
                inputfile = get_inputfile(
                    'Enter input file name with the list of users or list of profiles (each user in a line): ',
                    path_experiment)
                option_rest = get_suboption(
                    'Enter an option (profile | followers | following |relations | tweets| h_index) : ',
                    list_suboptions_2)
                command = "python %stweet_rest.py '%s' '%s' '%s' '--%s'" % (
                    path_scripts, file_app_keys, file_user_keys, inputfile,
                    option_rest)
                os.system(command)
            elif option == 3:
                os.chdir(path_experiment)
                query = raw_input(
                    'Enter a query (allows AND / OR connectors): ')
                outputfile = get_outputfile('Enter output file name: ',
                                            path_experiment)
                if outputfile != None:
                    command = "python %stweet_search.py '%s' '%s' '--query' '%s' '--file_out' '%s'" % (
                        path_scripts, file_app_keys, file_user_keys, query,
                        outputfile)
                    os.system(command)
                else:
                    print 'Option not executed'
            elif option == 4:
                os.chdir(path_experiment)
                file = get_inputfile(
                    'Enter input file name with the keywords separated by , : ',
                    path_experiment)
                outputfile = get_outputfile('Enter output file name: ',
                                            path_experiment)
                if outputfile != None:
                    command = "python %stweet_streaming.py '%s' '%s' '%s' '%s'  '--words' '%s'" % (
                        path_scripts, file_app_keys, file_user_keys,
                        path_experiment, outputfile, file)
                    os.system(command)
                else:
                    print 'Option not executed'
            elif option == 5:
                os.chdir(path_experiment)
                inputfile = get_inputfile(
                    'Enter input file name with the users profiles (It is necessary to get before the users profiles): ',
                    path_experiment)
                command = "python %stweet_rest.py '%s' '%s' '%s'  '--connections' " % (
                    path_scripts, file_app_keys, file_user_keys, inputfile)
                fast = raw_input('opción --fast? (y/n:) ')
                if fast == 'y':
                    command = command + '--fast'
                os.system(command)
            elif option == 6:
                os.chdir(path_experiment)
                inputfile = get_inputfile(
                    'Enter input file name with the tweets (got from a query or in real time): ',
                    path_experiment)
                relation = get_suboption(
                    'Enter the relationship type (RT | reply | mention): ',
                    list_suboptions_6)
                top = raw_input('Introduce top size (100-50000): ')
                command = "python %stweets_grafo.py '%s' '--%s' '--top_size' '%s'" % (
                    path_scripts, inputfile, relation, top)
                os.system(command)
            elif option == 7:
                os.chdir(path_experiment)
                option_processing = get_suboption(
                    'Enter option (sort |entities| classify| users | spread): ',
                    list_suboptions_7)
                if option_processing == 'sort':
                    inputfile = get_inputfile(
                        'Enter input file name with the tweets (got from a query or in real time): ',
                        path_experiment)
                    if args.linux:
                        filename, file_extension = os.path.splitext(inputfile)
                        command = '(head -n 1 %s && tail -n +2 %s | sort -u) > %s_ok%s' % (
                            inputfile, inputfile, filename, file_extension)
                        os.system(command)
                        print 'file sorted in  %s_ok%s)' % (filename,
                                                            file_extension)
                    if args.windows:
                        print 'in construccion'
                elif option_processing == 'entities':
                    inputfile = get_inputfile(
                        'Enter input file name with the tweets (got from a query or in real time): ',
                        path_experiment)
                    time_setting = raw_input(
                        'Offset GMT time (in Spain 1 in winter, 2 in summer): '
                    )
                    command = "python %stweets_entity.py '%s' '%s' '%s' '--top_size' '10' '--TZ' '%s'" % (
                        path_scripts, inputfile, path_experiment,
                        path_resources, time_setting)
                    os.system(command)
                elif option_processing == 'classify':
                    inputfile = get_inputfile(
                        'Enter input file name with the tweets (got from a query or in real time): ',
                        path_experiment)
                    topicsfile = get_inputfile(
                        'Enter file name  with topics dictionary: ',
                        path_experiment)
                    command = "python %stweets_classify.py '%s' '%s' '%s' " % (
                        path_scripts, inputfile, topicsfile, path_experiment)
                    os.system(command)
                elif option_processing == 'users':
                    inputfile = get_inputfile(
                        'Enter input file name with the tweets (got from a query or in real time): ',
                        path_experiment)
                    command = "python %susers_types.py '%s' '%s' " % (
                        path_scripts, inputfile, path_experiment)
                    os.system(command)
                elif option_processing == 'spread':
                    inputfile = get_inputfile(
                        'Enter input file name with the tweets (got from a query or in real time): ',
                        path_experiment)
                    time_setting = raw_input(
                        'Offset GMT time (in Spain 1 in winter, 2 in summer): '
                    )
                    command = "python %stweets_spread.py '%s' '%s' '--top_size' '1000' '--TZ' '%s' " % (
                        path_scripts, inputfile, path_experiment, time_setting)
                    os.system(command)
            elif option == 8:
                exit = 'y'
        except KeyboardInterrupt:
            pass
        finally:
            pass
Example #55
0
def main():
	print "Slinkie IP Debug Host Tool 1.0"
	print "Copyright (C)2003,2004 Dan Potter"
	print ""

	# Initialize readline
	histfile = os.path.join(os.environ["HOME"], ".slshhist")
	try:
		readline.read_history_file(histfile)
	except IOError:
		pass

	# Read the user's config, if it exists
	global conf
	conffile = os.path.join(os.environ["HOME"], ".slshrc")
	conf = Config()
	conf.load(conffile)

	# Spawn off a file server thread
	srv = Net()
	#thread.start_new_thread(srv.serve, ())

	# Create a command processor object
	cmdproc = CommandProcessor()

	# Are we auto-connecting?
	if conf.autoConnect:
		cmdproc.doConnect("connect")

	# Our main command loop
	while 1:
		# What's our prompt?
		cmdprompt = cmdproc.getPrompt()

		# Get a line of input
		try:
			s = raw_input("%s> " % cmdprompt)
		except EOFError:
			print ""
			break

		# Skip blanks
		if len(string.strip(s)) == 0: continue

		# Parse out the command piece
		cmd = string.split(s)[0]
		if cmd == '?': cmd = "help"
		cmdname = "do" + string.capitalize(cmd)

		# Is it a quit request?
		if cmd == 'quit':
			break

		# Try to invoke it
		if not hasattr(cmdproc, cmdname):
			print "Unknown command '%s'" % cmd
			continue
		
		cmdfunc = getattr(cmdproc, cmdname)
		cmdfunc(s)

	# Write out the history file from readline
	try:
		readline.write_history_file(histfile)
	except IOError:
		print "Warning: couldn't write history file '%s'" % histfile

	return 0
Example #56
0
def cli(
    unet,
    histfile=None,
    sockpath=None,
    force_window=False,
    title=None,
    prompt=None,
    background=True,
):
    logger = logging.getLogger("cli-client")

    if prompt is None:
        prompt = "unet> "

    if force_window or not sys.stdin.isatty():
        # Run CLI in another window b/c we have no tty.
        sock, sockdir, sockpath = cli_server_setup(unet)

        python_path = unet.get_exec_path(["python3", "python"])
        us = os.path.realpath(__file__)
        cmd = "{} {}".format(python_path, us)
        if histfile:
            cmd += " --histfile=" + histfile
        if title:
            cmd += " --prompt={}".format(title)
        cmd += " " + sockpath

        try:
            unet.run_in_window(cmd,
                               new_window=True,
                               title=title,
                               background=background)
            return cli_server(unet, sock)
        finally:
            unet.cmd_status("rm -rf " + sockdir)

    if not unet:
        logger.debug("client-cli using sockpath %s", sockpath)

    try:
        if histfile is None:
            histfile = os.path.expanduser("~/.micronet-history.txt")
            if not os.path.exists(histfile):
                if unet:
                    unet.cmd("touch " + histfile)
                else:
                    subprocess.run("touch " + histfile)
        if histfile:
            readline.read_history_file(histfile)
    except Exception:
        pass

    try:
        if sockpath:
            cli_client(sockpath, prompt=prompt)
        else:
            local_cli(unet, sys.stdout, prompt=prompt)
    except EOFError:
        pass
    except Exception as ex:
        logger.critical("cli: got exception: %s", ex, exc_info=True)
        raise
    finally:
        readline.write_history_file(histfile)
Example #57
0
def _init():
    import atexit
    import os
    import sys
    try:
        import readline
    except Exception:
        readline = None
    import types
    import time
    import uuid
    import json
    import pprint
    import hashlib
    import subprocess
    import datetime
    try:
        import __builtin__
    except ImportError:
        import builtins as __builtin__

    PY2 = sys.version_info[0] == 2
    __import__('rlcompleter')
    histdir = os.path.expanduser('~/.pyhist')
    try:
        os.makedirs(histdir)
    except OSError:
        pass

    if PY2:
        text_type = unicode
    else:
        text_type = str

    def _b(x):
        if not isinstance(x, bytes):
            x = x.encode('utf-8')
        return x

    histfile = os.path.join(
        histdir,
        hashlib.sha1(os.path.normpath(_b(os.path.abspath(
            sys.prefix)))).hexdigest())

    if readline is not None:
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass

        if 'libedit' in readline.__doc__:
            readline.parse_and_bind("bind '\t' rl_complete")
        else:
            readline.parse_and_bind("tab: complete")

        atexit.register(readline.write_history_file, histfile)

    def _magic_uuid(val=None):
        if val is None:
            return uuid.uuid4()
        elif isinstance(val, uuid.UUID):
            return val
        elif len(val) == 16:
            return uuid.UUID(bytes=val)
        return uuid.UUID(val)

    def _dump_json(x, as_string=False, indent=2, cp=False):
        s = '\n'.join(
            x.rstrip()
            for x in json.dumps(x, indent=indent).rstrip().splitlines())
        if cp:
            _copy(s)
        if as_string:
            return s
        print(s)

    def _cat(path):
        with open(path, 'rb') as f:
            return f.read()

    def _tcat(path):
        return _cat(path).decode('utf-8')

    def _paste():
        return subprocess.Popen(['pbpaste'],
                                stdout=subprocess.PIPE).communicate()[0]

    def _tpaste():
        return _paste().decode('utf-8')

    def _jpaste():
        return json.loads(_paste())

    def _copy(val):
        if isinstance(val, text_type):
            val = val.encode('utf-8')
        return subprocess.Popen(['pbcopy'],
                                stdin=subprocess.PIPE).communicate(val)

    def _jcopy(val, indent=None):
        _copy(_dump_json(val, indent=indent, as_string=True))

    helpers = types.ModuleType('helpers')
    helpers.histfile = histfile
    helpers.pp = pprint.pprint
    helpers.uuid = _magic_uuid
    helpers.UUID = uuid.UUID
    helpers.uuid3 = uuid.uuid3
    helpers.uuid4 = uuid.uuid4
    helpers.uuid5 = uuid.uuid5
    helpers.dt = datetime.datetime
    helpers.datetime = datetime.datetime
    helpers.td = datetime.timedelta
    helpers.timedelta = datetime.timedelta
    helpers.time = time.time
    helpers.j = _dump_json
    helpers.cat = _cat
    helpers.tcat = _tcat
    helpers.cp = _copy
    helpers.jcp = _jcopy
    helpers.copy = _copy
    helpers.jcopy = _jcopy
    helpers.paste = _paste
    helpers.tpaste = _tpaste
    helpers.jpaste = _jpaste
    __builtin__.h = helpers
    __builtin__.true = True
    __builtin__.false = False
    __builtin__.null = None
Example #58
0
 def init_readline(self):
     try:
         readline.read_history_file(".pupy_history")
     except Exception:
         pass
     self.init_completer()
Example #59
0
            raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1]))


    except getopt.GetoptError, msg:
        log_loading.error(msg)
        sys.exit(1)

    if PRESTART_FILE:
        _read_config_file(PRESTART_FILE)

    globkeys = init_session(mydict, session_name, STARTUP_FILE)

    if READLINE:
        if conf.histfile:
            try:
                readline.read_history_file(conf.histfile)
            except IOError:
                pass
        atexit.register(scapy_write_history_file,readline)
    
    atexit.register(scapy_delete_temp_files)
    
    IPYTHON=False
    if conf.interactive_shell.lower() == "ipython":
        try:
            import IPython
            IPYTHON=True
        except ImportError, e:
            log_loading.warning("IPython not available. Using standard Python shell instead.")
            IPYTHON=False
        
Example #60
0
    def __init__(self, conndata):
        NicosClient.__init__(self, self.put_error)
        # connection data as an object
        self.conndata = conndata
        # whether to suppress printing history and other info on connection
        self.quiet_connect = False
        # various state variables
        self.in_question = False
        self.in_editing = False
        self.tip_shown = False
        # number of automatic reconnect tries before giving up
        self.reconnect_count = 0
        self.reconnect_time = 0
        # current script, line within it and filename of script
        self.current_script = ['']
        self.current_line = -1
        self.current_filename = ''
        # pending requests (i.e. scripts) in the daemon
        self.pending_requests = OrderedDict()
        # filename of last edited/simulated script
        self.last_filename = ''
        # instrument name from NICOS, pre-filled with server name
        self.instrument = conndata.host.split('.')[0]
        # script directory from NICOS
        self.scriptpath = '.'
        # execution mode of the NICOS session
        self.current_mode = MASTER
        # messages queueing up while the editor is running
        self.message_queue = []
        # whether a stop is pending
        self.stop_pending = False
        # whether we are in debugging mode
        self.debug_mode = False
        # whether we are in spy mode (entering commands disabled)
        self.spy_mode = False
        # detected text-mode browser for help display
        self.browser = None
        # used for determining how much history to print by default
        self.tsize = terminalSize()
        # output stream to print to
        self.out = sys.stdout
        # uuid of the last simulation
        self.simuuid = ''
        # whether we display timestamps with subsecond precision
        self.subsec_ts = False
        # current ETA information
        self.cur_eta = ''

        # set up readline
        for line in DEFAULT_BINDINGS.splitlines():
            readline.parse_and_bind(line)
        readline.set_completer(self.completer)
        readline.set_history_length(10000)
        self.histfile = os.environ.get('NICOS_HISTORY_FILE',
                                       path.expanduser('~/.nicoshistory'))
        if path.isfile(self.histfile):
            readline.read_history_file(self.histfile)
        self.completions = []

        # set up "wakeup" pipe to notify readline of output and changed prompt
        self.wakeup_pipe_r, self.wakeup_pipe_w = os.pipe()

        # pre-set prompt to sane default
        self.set_status('disconnected')