class ArgumentAndOptionPrinter(cmd2.Cmd): """ Example cmd2 application where we create commands that just print the arguments they are called with.""" def __init__(self): # Uncomment this line to disable Python-style comments but still allow C-style comments # self.commentGrammars = pyparsing.Or([pyparsing.cStyleComment]) # Create command aliases which are shorter self.shortcuts.update({'ap': 'aprint', 'op': 'oprint'}) # Make sure to call this super class __init__ *after* setting commentGrammars and/or updating shortcuts cmd2.Cmd.__init__(self) # NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which # are not settable at runtime. This includes the commentGrammars, shortcuts, multilineCommands, etc. def do_aprint(self, arg): """Print the argument string this basic command is called with.""" print('aprint was called with argument: {!r}'.format(arg)) @options([ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option( '-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ], arg_desc='positional_arg_string') def do_oprint(self, arg, opts=None): """Print the options and argument list this options command was called with.""" print( 'oprint was called with the following\n\toptions: {!r}\n\targuments: {!r}' .format(opts, arg))
class DemoApp(Cmd): """Simple command processor example.""" @options([ make_option('-n', '--name', action="store", help="your name"), ]) def do_hello(self, arg, opts): if opts.name: self.stdout.write('Hello {}\n'.format(opts.name)) self.stdout.write('arg = {}\n'.format(arg)) def do_say(self, arg): """ Say something ... Usage: say [something] """ if arg: print("type(arg)={}, arg={}, arg.split()={}".format( type(arg), arg, arg.split())) print("shlex.split(arg) = {}".format(shlex.split(arg))) else: print("No argument provided") @options( [make_option('-d', '--depth', type="int", help="output [n] times")], arg_desc="[directory]") def do_dir(self, path, opts): """ Get a directory listing. """ recursion_depth = 0 if opts.depth: recursion_depth = opts.depth self.stdout.write('path = {}\n'.format(path))
class CmdLineApp(Cmd): def __init__(self): self.multilineCommands = ['orate'] self.maxrepeats = 3 # Add stuff to settable and shortcutgs before calling base class initializer self.settable['maxrepeats'] = 'max repetitions for speak command' self.shortcuts.update({'&': 'speak'}) # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False) # For option commands, pass a single argument string instead of a list of argument strings to the do_* methods set_use_arg_list(False) @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ]) def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input
class CmdLineApp(Cmd): multilineCommands = ['orate'] Cmd.shortcuts.update({'&': 'speak', 'h': 'hello'}) maxrepeats = 3 redirector = '->' Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed') @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ], arg_desc = '(text to say)') def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:].rstrip(), arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input
class CmdLineApp(Cmd): """ Example cmd2 application. """ # Build-in Cmd attributes intro = 'Welcome to the NP shell. Type help or ? to list commands.\n' prompt = '(NP) ' multilineCommands = ['orate'] Cmd.shortcuts.update({'&': 'speak'}) maxrepeats = 3 Cmd.settable.append('maxrepeats') # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # default_to_shell = True @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ]) def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input def do_greet(self, person): """greet [person] Greet the named person""" if person: print("type(arg) = {}, arg={}, arg.split()={}".format(type(person), person, person.split())) print("hi, {}".format(person)) else: print('hi') @options([make_option('-d', '--depth', type="int", help="depth")], arg_desc='test_args') def do_test(self, arg, opts=None): """ Prints out information about the arguments you give it. """ if arg: print("type(arg) = {}, arg={}, arg.split()={}".format(type(arg), arg, arg.split())) arg_join = ''.join(arg) print("''.join(arg) = {}".format(arg_join)) else: print('No arg')
class CmdLineApp(Cmd): """ Example cmd2 application. """ def __init__(self, ip_addr=None, port=None, transcript_files=None): self.multilineCommands = ['orate'] self.shortcuts.update({'&': 'speak'}) self.maxrepeats = 3 # Add stuff to settable and/or shortcuts before calling base class initializer self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed' # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False, transcript_files=transcript_files) # Disable cmd's usage of command-line arguments as commands to be run at invocation self.allow_cli_args = False # Example of args set from the command-line (but they aren't being used here) self._ip = ip_addr self._port = port # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # self.default_to_shell = True @options([ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ]) def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input
class OptionApp(cmd2.Cmd): @cmd2.options([cmd2.make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) def do_greet(self, arg, opts=None): arg = ''.join(arg) if opts.shout: arg = arg.upper() self.stdout.write(arg + '\n')
class DemoApp(Cmd): @options(make_option('-n', '--name', action="store", help="your name")) def do_hello(self, arg, opts): """Says hello.""" if opts.name: self.stdout.write('Hello {}\n'.format(opts.name)) else: self.stdout.write('Hello Nobody\n')
class CmdLineApp(Cmd): def __init__(self, *args, **kwargs): self.abbrev = True self.multilineCommands = ['orate'] self.maxrepeats = 3 self.redirector = '->' # Add stuff to settable and/or shortcuts before calling base class initializer self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed' # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x Cmd.__init__(self, *args, **kwargs) self.intro = 'This is an intro banner ...' # Configure how arguments are parsed for @options commands set_posix_shlex(False) set_strip_quotes(True) set_use_arg_list(False) opts = [ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ] @options(opts, arg_desc='(text to say)') def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:].rstrip(), arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input
class CmdLineApp(Cmd): @options([ make_option('-t', '--time', action="store_true", help="show time"), make_option('-f', '--file', action="store_true", help="indicate file"), make_option('-j', '--json', action="store_true", help="show json"), make_option('-l', '--lisp_string', action="store_true", help="show lisp_string"), make_option('-s', '--string', action="store_true", help="show string"), make_option('-k', '--tokens', action="store_true", help="show tokens"), make_option('-a', '--as_line', action="store_true", help="show as_line") ]) def do_parse(self, arg, opts=None): if len(arg) < 1: self.stdout.write( "Требуется ввести запрос или с помощью флага -f указать файл c запросом\n" ) sys.exit() if opts.file: p = Parser(input_data=arg, type=1) else: p = Parser(input_data=arg, type=2) if p.status is False: print("Некорректный запрос\n") sys.exit() if opts.time: self.stdout.write(p.get_time()) self.stdout.write('\n') if opts.json: self.stdout.write( json.dumps(p.to_json(), sort_keys=False, indent=4, separators=(',', ': '))) self.stdout.write('\n') if opts.lisp_string: self.stdout.write(p.to_lisp_string()) self.stdout.write('\n') if opts.string: self.stdout.write(p.to_string()) self.stdout.write('\n') if opts.tokens: self.stdout.write( json.dumps(p.tokens(), sort_keys=False, indent=4, separators=(',', ': '))) self.stdout.write('\n') if opts.as_line: self.stdout.write(p.as_line()) self.stdout.write('\n') if opts.json is None and opts.time is None and opts.lisp_string is None\ and opts.string is None and opts.tokens is None and opts.as_line is None: self.stdout.write("Не выбран параметр вывода\n")
class MultilineApp(cmd2.Cmd): def __init__(self, *args, **kwargs): self.multilineCommands = ['orate'] # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x cmd2.Cmd.__init__(self, *args, **kwargs) @cmd2.options([cmd2.make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) def do_orate(self, arg, opts=None): arg = ''.join(arg) if opts.shout: arg = arg.upper() self.stdout.write(arg + '\n')
class CmdLineApp(Cmd): """ Example cmd2 application. """ multilineCommands = ['orate'] Cmd.shortcuts.update({'&': 'speak'}) maxrepeats = 3 Cmd.settable.append('maxrepeats') # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # default_to_shell = True def __init__(self): # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False) @options([ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ]) def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.stdout.write(arg) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input
def buildOptions(self): if self.haveOptions: result = [] for s in self.options: tmpOption = s args = [s['short'], s['long']] del s['short'] del s['long'] kwargs = tmpOption result.append(cmd2.make_option(*args, **kwargs)) else: result = [] return result
class DemoApp(Cmd): @options([make_option('-n', '--name', action="store", help="your name"), ]) def do_hello(self, arg, opts): """Says hello.""" if opts.name: self.stdout.write('Hello {}\n'.format(opts.name)) else: self.stdout.write('Hello Nobody\n') def do_eat(self, arg): """Eat something, with a selection of sauces to choose from.""" sauce = self.select('sweet salty', 'Sauce? ') result = '{food} with {sauce} sauce, yum!' result = result.format(food=arg, sauce=sauce) self.stdout.write(result + '\n')
prefix = text[:-len(bn)] dn = os.path.dirname(os.path.normpath(os.path.join(self.cwd, text))) if dn not in self.dirs: return [] ret = [os.path.join(prefix, e['server_filename']) + (((e['server_filename'] == bn) and e['isdir']) and '/' or '') for e in self.dirs.get(dn) if e['server_filename'].startswith(bn) and filter(e)] return ret else: return [e['server_filename'] for e in self.dirs.get(self.cwd) if filter(e)] return complete_sth complete_mv = complete_meta = complete_rm = complete_ls = _complete_remote() complete_cd = _complete_remote(filter = lambda e: e['isdir']) complete_download = _complete_remote(filter = lambda e: not e['isdir']) @options([make_option('-b', '--blocksize', type="int", default=1<<21, help="download blocksize"), make_option('-r', '--retry', type="int", default=5, help="retry time after failure"), make_option('-i', '--index', help="the file index to download, separate with comma, e.g. 3,5,2, also range supported, e.g. 1-4,5,7"), ]) def do_download(self, args, opts): if not self.pcs: print 'please login first' return args = split_command_line(args) fps = [] if opts.index: if not self.dirs.get(self.cwd): print 'please use `ls` to list dir first to let me know which files you want to operate' return
class DevConsole(Cmd): def __init__(self): Cmd.__init__(self) global pubnub self.intro = "For Help type ? or help . " + \ "To quit/exit type exit" + "\n" + \ "Commands can also be provided on command line while starting console (in quotes) ex. " + \ "pubnub-console 'init -p demo -s demo'" self.default_channel = None self. async = False pubnub = Pubnub("demo", "demo") self.channel_truncation = 3 self.prompt = self.get_prompt() self.publish_key = "demo" self.subscribe_key = "demo" self.origin = "pubsub.pubnub.com" self.auth_key = None self.cipher_key = None self.secret_key = "demo" self.ssl = False self.uuid = None self.disable_pretty = False def get_channel_origin(self): cho = "" channels = get_channel_array() channels_str = ",".join(channels) sl = self.channel_truncation if len(channels) > int(sl) and int(sl) > 0: cho += ",".join( channels[:int(sl)]) + " " + str(len(channels) - int(sl)) + " more..." else: cho += ",".join(channels) if len(channels) > 0: cho = color.colorize(cho, "bold") + "@" origin = pubnub.get_origin().split("://")[1] origin += color.colorize( " (SSL)", "green") if pubnub.get_origin().split("://")[0] == "https" else "" return " [" + cho + color.colorize(origin, "blue") + "] > " def get_prompt(self): prompt = "[" + get_date() + "]" if self.default_channel is not None and len(self.default_channel) > 0: prompt += " [default channel: " + color.colorize( self.default_channel, "bold") + "]" prompt += self.get_channel_origin() return prompt def precmd(self, line): self.prompt = self.get_prompt() return line #def emptyline(self): # self.prompt = self.get_prompt() def cmdloop_with_keyboard_interrupt(self): try: self.cmdloop() except KeyboardInterrupt as e: pass sys.stdout.write('\n') kill_all_threads() @options([ make_option('-p', '--publish-key', action="store", default=None, help="Publish Key"), make_option('-s', '--subscribe-key', action="store", default=None, help="Subscribe Key"), make_option('-k', '--secret-key', action="store", default=None, help="cipher Key"), make_option('-c', '--cipher-key', action="store", default=None, help="Secret Key"), make_option('-a', '--auth-key', action="store", default=None, help="Auth Key"), make_option('--ssl-on', dest='ssl', action='store_true', default=False, help="SSL Enabled ?"), make_option('-o', '--origin', action="store", default=None, help="Origin"), make_option('-u', '--uuid', action="store", default=None, help="UUID"), make_option('--disable-pretty-print', dest='disable_pretty', action='store_true', default=False, help="Disable Pretty Print ?") ]) def do_init(self, command, opts): global pubnub global print_ok global print_error global print_ok_normal global print_error_normal global print_error_pretty global print_ok_pretty self.publish_key = opts.publish_key if opts.publish_key is not None else self.publish_key self.subscribe_key = opts.subscribe_key if opts.subscribe_key is not None else self.subscribe_key self.secret_key = opts.secret_key if opts.secret_key is not None else self.secret_key self.cipher_key = opts.cipher_key if opts.cipher_key is not None else self.cipher_key self.auth_key = opts.auth_key if opts.auth_key is not None else self.auth_key self.origin = opts.origin if opts.origin is not None else self.origin self.uuid = opts.uuid if opts.uuid is not None else self.uuid self.ssl = opts.ssl if opts.ssl is not None else self.ssl self.disable_pretty = opts.disable_pretty if opts.disable_pretty is not None else self.disable_pretty pubnub = Pubnub(self.publish_key, self.subscribe_key, self.secret_key, self.cipher_key, self.auth_key, self.ssl, self.origin, self.uuid) self.prompt = self.get_prompt() if opts.disable_pretty is True: print_ok = print_ok_normal print_error = print_error_normal else: print_ok = print_ok_pretty print_error = print_error_pretty def do_set_sync(self, command): """unset_async Unset Async mode""" self. async = False def do_set_async(self, command): """set_async Set Async mode""" self. async = True @options([ make_option('-n', '--count', action="store", default=3, help="Number of channels on prompt") ]) def do_set_channel_truncation(self, command, opts): """set_channel_truncation Set Channel Truncation""" self.channel_truncation = opts.count self.prompt = self.get_prompt() def do_unset_channel_truncation(self, command): """unset_channel_truncation Unset Channel Truncation""" self.channel_truncation = 0 self.prompt = self.get_prompt() def do_set_full_date(self, command): global full_date """do_set_full_date Set Full Date""" full_date = True self.prompt = self.get_prompt() def do_unset_full_date(self, command): global full_date """do_unset_full_date Unset Full Date""" full_date = False self.prompt = self.get_prompt() @options([ make_option('-c', '--channel', action="store", help="Default Channel") ]) def do_set_default_channel(self, command, opts): if opts.channel is None: print_error("Missing channel") return self.default_channel = opts.channel self.prompt = self.get_prompt() @options([ make_option('-f', '--file', action="store", default="./pubnub-console.log", help="Output file") ]) def do_set_output_file(self, command, opts): global of try: of = file(opts.file, 'w+') except IOError as e: print_error("Could not set output file. " + e.reason) @options([ make_option('-c', '--channel', action="store", help="Channel for here now data") ]) def do_here_now(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return _here_now_command_handler(opts.channel, async=self. async) @options([ make_option('-c', '--channel', action="store", help="Channel for history data"), make_option('-n', '--count', action="store", default=5, help="Number of messages") ]) def do_get_history(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return _history_command_handler(opts.channel, opts.count, async=self. async) @options([ make_option('-c', '--channel', action="store", help="Channel on which to publish") ]) def do_publish(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return if command is None: print_error("Missing message") return try: message = json.loads(str(command)) except ValueError as ve: message = str(command) _publish_command_handler(opts.channel, message, async=self. async) @options([ make_option('-c', '--channel', action="store", help="Channel on which to grant"), make_option('-a', '--auth-key', dest="auth_key", action="store", help="Auth Key"), make_option('-r', '--read-enabled', dest='read', action='store_true', default=False, help="Read ?"), make_option('-w', '--write-enabled', dest='write', action='store_true', default=False, help="Write ?"), make_option('-t', '--ttl', action="store", default=5, help="TTL"), make_option('-p', '--presence', action="store_true", dest="presence", default=False, help="Grant on presence channel ?") ]) def do_grant(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key _grant_command_handler(opts.channel, opts.auth_key, opts.read, opts.write, opts.ttl, async=self. async)
opts.read, opts.write, opts.ttl, async=self. async) if opts.presence is True: _grant_command_handler(opts.channel + '-pnpres', opts.auth_key, opts.read, opts.write, opts.ttl, async=self. async) @options([ make_option('-c', '--channel', action="store", help="Channel on which to revoke"), make_option('-a', '--auth-key', dest="auth_key", action="store", help="Auth Key"), make_option('-t', '--ttl', action="store", default=5, help="TTL"), make_option('-p', '--presence', action="store_true", dest="presence", default=False, help="Revoke on presence channel ?") ]) def do_revoke(self, command, opts):
def successfully_connect_to_number(self, arg): try: instance_number = int(arg) except ValueError: return False try: self.make_instance_current(instance_number) except IndexError: self.list_instances() return False if (self.rdbms == 'oracle') and self.serveroutput: self.curs.callproc('dbms_output.enable', []) return True @cmd2.options([cmd2.make_option('-a', '--add', action='store_true', help='add connection (keep current connection)'), cmd2.make_option('-c', '--close', action='store_true', help='close connection {N} (or current)'), cmd2.make_option('-C', '--closeall', action='store_true', help='close all connections'), cmd2.make_option('--postgresql', action='store_true', help='Connect to postgreSQL: `connect --postgresql [DBNAME [USERNAME]]`'), cmd2.make_option('--postgres', action='store_true', help='Connect to postgreSQL: `connect --postgres [DBNAME [USERNAME]]`'), cmd2.make_option('--oracle', action='store_true', help='Connect to an Oracle database'), cmd2.make_option('--mysql', action='store_true', help='Connect to a MySQL database'), cmd2.make_option('-H', '--hostname', type='string', help='Machine where database is hosted'), cmd2.make_option('-p', '--port', type='int', help='Port to connect to'), cmd2.make_option('--password', type='string', help='Password'), cmd2.make_option('-d', '--database', type='string',
class REPL(Cmd): prompt = "falkonry>> " def __init__(self): Cmd.__init__(self) global _self _self = self print_custom("Welcome to Falkonry Shell !!!", "green") @options([ make_option('--host', help="host url"), make_option('--token', help="auth token") ]) def do_login(self, args, opts=None): """login to the falkonry""" if (opts.host is None or opts.host == "") or (opts.token is None or opts.token == ""): print_error("Please pass host url and token") return if opts.host.find("https://") == -1: opts.host = "https://" + opts.host if validate_login(opts.host, opts.token): print_success("logged in to falkonry") def do_logout(self, line): """logout from the falkonry""" if check_login(): global _falkonry _falkonry = None print_success("logged out from falkonry") def do_login_details(self, line): """get login details""" if check_login(): print_info('Host : ' + _falkonry.host + "\n" + 'Token : ' + _falkonry.token) def do_exit(self, line): """exit the falkonry shell""" quit() def do_datastream_get_list(self, line): """list datastreams""" if check_login(): print_info("Listing Datastreams...") print_info( "==================================================================================================================" ) datastreamList = _falkonry.get_datastreams() if len(datastreamList) == 0: print_info("No Datastreams found") print_row("Datastream Name", "Id", "Created By", "Live Status") print_info( "==================================================================================================================" ) for datastream in datastreamList: print_row(datastream.get_name(), datastream.get_id(), datastream.get_created_by(), datastream.get_live()) print_info( "==================================================================================================================" ) @options([make_option('--id', help="datastream id")]) def do_datastream_get_by_id(self, arg, opts=None): """get datastream by id """ if check_login(): if opts.id is None or opts.id == "": print_error("Please pass datastream id") return print_info("Fetching Datastreams") try: datastreamObject = _falkonry.get_datastream(opts.id) print_datastream_details(datastreamObject.to_json()) except Exception as error: handle_error(error) @options([make_option('--id', help="datastream id")]) def do_datastream_default_set(self, arg, opts=None): """set default datastream""" if check_login(): try: if opts.id is None or opts.id == "": print_error("Please pass datastream id") return global _datastreamId datastreamObject = _falkonry.get_datastream(opts.id) _datastreamId = opts.id print_success("Default datastream set : " + opts.id) return except Exception as error: handle_error(error) return def do_datastream_default_get(self, line): """get default datastream""" global _datastreamId if check_login(): if _datastreamId is None: print_error("No default datastream set") return else: try: datastreamObject = _falkonry.get_datastream(_datastreamId) print_info("Default datastream set : " + _datastreamId + " Name : " + datastreamObject.get_name()) except Exception as error: _datastreamId = None handle_error(error) print_error("Please set the default datastream again") return def do_datastream_get_entity_meta(self, line): """get entitymeta of datastream""" global _datastreamId if check_login(): if _datastreamId is None: print_error("No default datastream set") return else: try: entityMeta = _falkonry.get_entity_meta(_datastreamId) print_info("Entity Meta of datastream: " + _datastreamId) for entity in entityMeta: print_info("Entity Label : " + entity.get_label() + ". Entity Id : " + entity.get_sourceId()) except Exception as error: handle_error(error) return @options([make_option('--path', help="file path of entity meta request")]) def do_datastream_add_entity_meta(self, arg, opts=None): """add entitymeta of datastream""" global _datastreamId if check_login(): if _datastreamId is None: print_error("No default datastream set") return else: try: if opts.path is None or opts.path == "": print_error( "Please pass json file path for adding entity meta" ) return try: file_extension = get_file_extension(opts.path) if file_extension != ".json": print_error("Only JSON file is accepted.") return with open(opts.path) as data_file: data = json.load(data_file) except Exception as error: print_error("Error in reading file." + str(error)) return entityMeta = _falkonry.add_entity_meta( _datastreamId, {}, data) print_info( "Entity Meta successfully added to datastream: " + _datastreamId) except Exception as error: handle_error(error) return @options([make_option('--path', help="file path of request")]) def do_datastream_create(self, arg, opts=None): """create datastream""" if check_login(): try: if opts.path is None or opts.path == "": print_error( "Please pass json file path for creating datastream") return # read file try: file_extension = get_file_extension(opts.path) if file_extension != ".json": print_error("Only JSON file is accepted.") return with open(opts.path) as data_file: data = json.load(data_file) except Exception as error: print_error("Error in reading file." + str(error)) return created_datastream = _falkonry.create_datastream(data) print_success("Datastream successfully created : " + created_datastream.get_id()) return except Exception as error: handle_error(error) return return @options([make_option('--id', help="datastream id")]) def do_datastream_delete(self, arg, opts=None): """delete datastream""" if check_login(): try: if opts.id is None: print_error("Please pass datastream id") return _falkonry.delete_datastream(opts.id) print_success("Datastream successfully deleted : " + opts.id) return except Exception as error: handle_error(error) return def do_datastream_start_live(self, line): """ turn on live monitoring of datastream """ global _datastreamId if check_login(): try: if check_default_datastream(): print_info("Turning on Live monitoring for datastream : " + _datastreamId) res = _falkonry.on_datastream(_datastreamId) print_success("Datastream is ON for live monitoring") return except Exception as error: handle_error(error) return return def do_datastream_stop_live(self, line): """ turn off live monitoring of datastream """ global _datastreamId if check_login(): try: if check_default_datastream(): print_info( "Turning off Live monitoring for datastream : " + _datastreamId) _falkonry.off_datastream(_datastreamId) print_success("Datastream is OFF for live monitoring") return except Exception as error: handle_error(error) return return @options([make_option('--path', help="file path of request")]) def do_datastream_add_historical_data(self, arg, opts=None): """ add historical data to datastream for model learning """ if check_login(): try: if opts.path is None or opts.path == "": print_error("Please pass historical data file path") return if check_default_datastream(): file_extension = get_file_extension(opts.path) if file_extension != ".csv" and file_extension != ".json": print_error("Only CSV or JSON file is accepted.") return data = io.open(opts.path) data_options = {'streaming': False, 'hasMoreData': False} response = _falkonry.add_input_stream( _datastreamId, file_extension.split(".")[1], data_options, data) print_info(str(response)) except Exception as error: handle_error(error) return @options([make_option('--path', help="file path of request")]) def do_datastream_add_live_data(self, arg, opts=None): """add live data to datastream for live monitoring """ if check_login(): try: if opts.path is None or opts.path == "": print_error("Please pass historical data file path") return if check_default_datastream(): file_extension = get_file_extension(opts.path) if file_extension != ".csv" and file_extension != ".json": print_error("Only CSV or JSON file is accepted.") return data = io.open(opts.path) data_options = {'streaming': True, 'hasMoreData': False} response = _falkonry.add_input_stream( _datastreamId, file_extension.split(".")[1], data_options, data) print_info(str(response)) except Exception as error: handle_error(error) return return def do_assessment_get_list(self, line): """ list assessments for default datastream""" if check_login(): try: if check_default_datastream(): print_info("Fetching assessment list of datastream : " + _datastreamId + "...") print_info( "==================================================================================================================" ) assessmentList = _falkonry.get_assessments() if len(assessmentList) == 0: print_info("No assessment found") print_row("Assessment Name", "Id", "Created By", "Live Status") print_info( "==================================================================================================================" ) for assessment in assessmentList: if assessment.get_datastream() == _datastreamId: print_row(assessment.get_name(), assessment.get_id(), assessment.get_created_by(), assessment.get_live()) print_info( "==================================================================================================================" ) return except Exception as error: handle_error(error) return return @options([make_option('--id', help="assessment id")]) def do_assessment_get_by_id(self, arg, opts=None): """ fetch assessment by id for default datastream""" if check_login(): try: if opts.id is None: print_error("Please pass assessment id") return assessmentObject = _falkonry.get_assessment(opts.id) print_assessment_details(assessmentObject.to_json()) return except Exception as error: handle_error(error) return return @options([make_option('--path', help="file path of request")]) def do_assessment_create(self, arg, opts=None): """ create assessment in default datastream""" if check_login(): try: if check_default_datastream(): if opts.path is None or opts.path == "": print_error( "Please pass json file path for creating assessment" ) return # read file try: file_extension = get_file_extension(opts.path) if file_extension != ".json": print_error("Only JSON file is accepted.") return with open(opts.path) as data_file: data = json.load(data_file) except Exception as error: print_error("Error in reading file." + str(error)) return data['datastream'] = _datastreamId created_assessment = _falkonry.create_assessment(data) print_success("Assessment successfully created : " + created_assessment.get_id()) return except Exception as error: handle_error(error) return return @options([make_option('--id', help="assessment id")]) def do_assessment_delete(self, arg, opts=None): """ delete assessment by id default datastream""" if check_login(): try: if opts.id is None: print_error("Please pass assessment id") return _falkonry.delete_assessment(opts.id) print_info("Assessment deleted successfully: " + opts.id) return except Exception as error: handle_error(error) return return @options([make_option('--id', help="assessment id")]) def do_assessment_default_set(self, arg, opts=None): """ set default assessment""" if check_login(): try: if opts.id is None: print_error("Please pass assessment id") return if check_default_datastream(): global _assessmentId assessmentObj = _falkonry.get_assessment(opts.id) if assessmentObj.get_datastream() != _datastreamId: print_error("Assessment id : " + opts.id + " does not belong to default datastream") _assessmentId = opts.id print_success("Default assessment set : " + opts.id) return except Exception as error: handle_error(error) return return def do_assessment_default_get(self, line): """ get default assessment""" global _assessmentId if check_login(): if _assessmentId is None: print_error("No default assessment set") return else: try: assessmentObj = _falkonry.get_assessment(_assessmentId) print_info("Default assessment set : " + _assessmentId + " Name : " + assessmentObj.get_name()) except Exception as error: _assessmentId = None handle_error(error) print_error("Please set the default assessment again") return @options([make_option('--path', help="file path of facts file")]) def do_assessment_add_facts(self, arg, opts=None): """ add facts to assessment""" if check_login(): try: if opts.path is None or opts.path == "": print_error("Please pass facts data file path") return if check_default_assessment(): file_extension = get_file_extension(opts.path) if file_extension != ".csv" and file_extension != ".json": print_error("Only CSV or JSON file is accepted.") return data = io.open(opts.path) response = _falkonry.add_facts_stream( _assessmentId, file_extension.split(".")[1], {}, data) print_info(str(response)) return except Exception as error: handle_error(error) return return @options([ make_option('--path', help="file path to write output"), make_option('--trackerId', help="tracker id of the previous output request"), make_option( '--modelIndex', help="index of the model of which output needs to be fetched "), make_option( '--startTime', help= "startTime of the output range should be in ISO8601 format 'YYYY-MM-DDTHH:mm:ss.SSSZ'" ), make_option( '--endTime', help= "endTime of the output range should be in ISO8601 format 'YYYY-MM-DDTHH:mm:ss.SSSZ'" ), make_option( '--format', help= "format of the output. For csv pass text/csv. For JSON output pass application/json" ) ]) def do_assessment_get_historical_output(self, arg, opts=None): """ get learn/test output of assessment""" if check_login(): try: if check_default_assessment(): output_ops = {} if opts.trackerId is not None and opts.trackerId != "": output_ops['trackerId'] = opts.trackerId if opts.modelIndex is not None and opts.modelIndex != "": output_ops['modelIndex'] = opts.modelIndex if opts.startTime is not None and opts.startTime != "": output_ops['startTime'] = opts.startTime if opts.endTime is not None and opts.endTime != "": output_ops['endTime'] = opts.endTime if opts.format is not None and opts.format != "": if opts.format != "application/json" and opts.format != "text/csv": print_error( "Unsupported response format. Only supported format are : application/json ,text/csv" ) return output_ops['format'] = opts.format if (opts.trackerId is None or opts.trackerId == "") and ( opts.startTime is None or opts.startTime == ""): print_error( "TrackerID or startTime is require for fetching output data" ) return output_response = _falkonry.get_historical_output( _assessmentId, output_ops) if output_response.status_code == 200: if opts.path: #write response to file try: file = open(opts.path, "w") file.write(str(output_response.text)) file.close() print_success( "Output data is written to the file : " + opts.path) except Exception as fileError: handle_error(fileError) else: print_info( "==================================================================================================================" ) print_info(str(output_response.text)) print_info( "==================================================================================================================" ) if output_response.status_code == 202: print_success(str(output_response.text)) json_resp = json.loads(str(output_response.text)) print_success( "Falkonry is generating your output. Please try following command in some time." ) print_success( "assessment_get_historical_output --trackerId=" + json_resp['__$id']) return return except Exception as error: handle_error(error) return return @options([ make_option( '--format', help= "format of the output. For csv pass text/csv. For JSON output pass application/json" ) ]) def do_assessment_output_listen(self, arg, opts=None): """ get live output of assessment""" if check_login(): try: if not check_default_assessment(): return output_ops = {} if opts.format is not None and opts.format != "": if opts.format != "application/json" and opts.format != "text/csv": print_error( "Unsupported response format. Only supported format are : application/json ,text/csv" ) return output_ops['format'] = opts.format output_response = _falkonry.get_output(_assessmentId, output_ops) print_info("Fetching live assessments : ") for event in output_response.events(): print_info(json.dumps(json.loads(event.data))) except Exception as error: handle_error(error) return return @options([ make_option('--path', help="file path to write output"), make_option( '--modelIndex', help="index of the model of which facts needs to be fetched "), make_option( '--startTime', help= "startTime of the facts range should be in ISO8601 format 'YYYY-MM-DDTHH:mm:ss.SSSZ'" ), make_option( '--endTime', help= "endTime of the facts range should be in ISO8601 format 'YYYY-MM-DDTHH:mm:ss.SSSZ'" ), make_option( '--format', help= "format of the facts data. For csv pass text/csv. For JSON output pass application/json" ) ]) def do_assessment_get_facts(self, arg, opts=None): """ get facts of assessment""" if check_login(): try: if not check_default_assessment(): return output_ops = {} if opts.modelIndex is not None and opts.modelIndex != "": output_ops['modelIndex'] = opts.modelIndex if opts.startTime is not None and opts.startTime != "": output_ops['startTime'] = opts.startTime if opts.endTime is not None and opts.endTime != "": output_ops['endTime'] = opts.endTime if opts.format is not None and opts.format != "": if opts.format != "application/json" and opts.format != "text/csv": print_error( "Unsupported response format. Only supported format are : application/json ,text/csv" ) return output_ops['format'] = opts.format output_response = _falkonry.get_facts(_assessmentId, output_ops) if opts.path is not None and opts.path != "": #write response to file try: file = open(opts.path, "w") file.write(str(output_response.text)) file.close() print_success("Facts data is written to the file : " + opts.path) except Exception as fileError: handle_error(fileError) else: print_info("Facts Data : ") print_info( "==================================================================================================================" ) print_info(str(output_response.text)) print_info( "==================================================================================================================" ) except Exception as error: handle_error(error) return return @options([ make_option('--path', help="file path to write output"), make_option( '--format', help= "format of the input data. For csv pass text/csv. For JSON output pass application/json" ) ]) def do_datastream_get_data(self, arg, opts=None): """ get data of datastream""" if check_login(): try: if not check_default_datastream(): return output_ops = {} if opts.format is not None and opts.format != "": if opts.format != "application/json" and opts.format != "text/csv": print_error( "Unsupported response format. Only supported format are : application/json ,text/csv" ) return output_ops['format'] = opts.format output_response = _falkonry.get_datastream_data( _datastreamId, output_ops) if opts.path is not None and opts.path != "": #write response to file try: file = open(opts.path, "w") file.write(str(output_response.text)) file.close() print_success("Input data is written to the file : " + opts.path) except Exception as fileError: handle_error(fileError) else: print_info("Input Data : ") print_info( "==================================================================================================================" ) print_info(str(output_response.text)) print_info( "==================================================================================================================" ) except Exception as error: handle_error(error) return return
pipe.stdin.close() pipe.wait() msg("Filter run %s halted by framework:", filter_run_name(filter_name, filter_args)) # TODO: Exception no longer has a message field in Python 3.0, produces a DeprecationWarning # under Python 2.7 msg("\t%s (%s)", e.message, e.__class__.__name__) self.last_exception = sys.exc_info() finally: sys.stdout = old_stdout if pipe: pipe.stdin.close() @options([ make_option('-S', '--pp-subtree', help='Pretty print each matching subtree.', dest='show_mode', action='store_const', const='pp_subtree', default='pp_subtree'), make_option('-s', '--subtree', help='Print each matching subtree only.', dest='show_mode', action='store_const', const='subtree'), make_option('-w', '--whole-tree', help='Print whole tree on match (not just matching subtrees).', dest='show_mode', action='store_const', const='whole_tree'), make_option('-W', '--pp-whole-tree', help='Pretty print whole tree on match (not just matching subtrees).', dest='show_mode', action='store_const', const='pp_whole_tree'), make_option('-y', '--synttree', help='Pretty print as TikZ/QTree LaTeX.', dest='show_mode', action='store_const', const='pp_synttree'), make_option('-m', '--matched-tag', help='Print matched tag.', dest='show_mode', action='store_const', const='matched_tag'), make_option('-b', '--tags-and-text', help='Print tree tags and text under.', dest='show_mode', action='store_const', const='tags_and_text'),
class CmdLineApp(Cmd): MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] MUMBLE_FIRST = ['so', 'like', 'well'] MUMBLE_LAST = ['right?'] def __init__(self, *args, **kwargs): self.abbrev = True self.multilineCommands = ['orate'] self.maxrepeats = 3 self.redirector = '->' # Add stuff to settable and/or shortcuts before calling base class initializer self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed' # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x Cmd.__init__(self, *args, **kwargs) self.intro = 'This is an intro banner ...' # Configure how arguments are parsed for @options commands set_posix_shlex(False) set_strip_quotes(True) set_use_arg_list(False) opts = [ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ] @options(opts, arg_desc='(text to say)') def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.poutput(arg) # recommend using the poutput function instead of # self.stdout.write or "print", because Cmd allows the user # to redirect output do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input @options( [make_option('-r', '--repeat', type="int", help="output [n] times")]) def do_mumble(self, arg, opts=None): """Mumbles what you tell me to.""" repetitions = opts.repeat or 1 arg = arg.split() for i in range(min(repetitions, self.maxrepeats)): output = [] if (random.random() < .33): output.append(random.choice(self.MUMBLE_FIRST)) for word in arg: if (random.random() < .40): output.append(random.choice(self.MUMBLES)) output.append(word) if (random.random() < .25): output.append(random.choice(self.MUMBLE_LAST)) self.poutput(' '.join(output))
class InteractiveClient(Cmd): client = clientlib.Client() quiet = False debug = True prompt = " > " def precmd(self, line): self.do_update() return line def postcmd(self, stop, line): self.do_update() return stop def format_prompt(self, username='', character='', zone=''): if not username: username = self.client.last_user if not character: character = self.client.last_character if not zone: # We want the actual name of the zone instead of the url. for zoneid, zone in self.client.zones.iteritems(): if zone == self.client.last_zone: # The instance type and instance owner are implied, # So we only want the actual zone name. zone = zoneid.split('-')[1] break zone = '@{0}'.format(zone) if zone else '' prompt_string = " {username}:{character}{zone} ({num_objs})> " self.prompt = prompt_string.format(username=username, character=(character or "No Character Selected"), zone=zone, num_objs=len(self.client.objects)) return self.prompt def logged_in(self): if not self.client.last_character: return False else: return True @options([make_option('-u', '--username', type="string", help="The name of the user you want to register."), make_option('-p', '--password', type="string", help="The password for the user."), make_option('-e', '--email', type="string", default=None, help="The email for the user."), ]) def do_register(self, args, opts=None): '''Register a username and password combination so you can log in. You only need to do this once per user you want to create. Calling this more than once with the same arguments will generate an error at worst, and be ignored at best.''' if not opts: username, password, email = args.split(" ") else: username = opts.username password = opts.password email = opts.email try: result = self.client.register(username, password, email) except clientlib.RegistrationError as exc: self.perror("Error: Registration failed: {0}" .format(exc.message)) return if result: self.pfeedback(result) @options([make_option('-u', '--username', type="string", default=settings.DEFAULT_USERNAME, help="The user you want to log in as."), make_option('-p', '--password', type="string", default=settings.DEFAULT_PASSWORD, help="The password for the user."), ]) def do_login(self, args, opts=None): '''Log in as a given user.''' if not opts: username, password = args.split(' ') else: username = opts.username password = opts.password return self.login(username, password) def login(self, username, password, charnum=None): # Sometimes the authserver may not be up. if not self.client.ping(): self.perror("Error: Authentication server is not online.") return # Let's see if the username and password are right: result = self.client.authenticate(username, password) if not result: self.perror("Authentication failed.") return else: self.pfeedback("Authentication successful. You are now logged in as {0}." .format(repr(username))) character = None if charnum: character = self.client.characters[list(self.client.characters)[int(charnum-1)]].name else: if self.client.characters: if len(self.client.characters) == 1: character = self.client.characters.keys()[0] else: character = self.select(self.client.characters, 'Select a Character: ') if character: self.client.last_character = character self.client.set_character_status(character) self.client.get_objects() else: self.pfeedback("No characters found.") character_name = raw_input("New character's name: ").strip() self.do_create_character(character_name) self.login(username, password, charnum) self.format_prompt() @options([make_option('-n', '--name', type="string", default="Graxnor", help="The name of the character you want to create.."), ]) def do_create_character(self, args, opts=None): if not opts: (name,) = args.split(' ') else: name = opts.name try: result = self.client.create_character(name) except (clientlib.ClientError, clientlib.UnexpectedHTTPStatus) as exc: self.perror("Error: Character creation failed: {0}".format(exc.message)) return return result def do_update(self, args=None): '''Update all the things that can be updated. This includes objects only at the moment.''' if not self.logged_in(): return self.client.get_objects() self.client.get_messages() for msg in self.format_messages(): self.pfeedback(msg) def format_messages(self): for msgid, message in self.client.messages.iteritems(): if not message.get('read'): message['read'] = True timestamp = datetime.fromtimestamp(message.get('last_modified', {}).get('$date', 0)/1000) yield "[{timestamp:%X}] "\ "<{sender}>: "\ "{body}".format(sender=message.get('sender'), body=message.get('body'), timestamp=timestamp) def do_map(self, args): '''Renders a map of the zone your character is currently in. By default this is ascii, but you can also ask it to render to an image.''' if not self.logged_in(): return # Get bounds (maxx, maxy, minx, miny) of all objects in the zone maxx = 0 maxy = 0 minx = 0 miny = 0 goodobjs = [] for objid, obj in self.client.objects.iteritems(): try: objloc = obj['loc'] except KeyError: # Some objects have no location, so skip em. continue try: if 'hidden' in obj['states']: # Skip hidden objects. continue except KeyError: # Stuff without states are A-OK pass maxx = max(maxx, objloc['x']) maxy = max(maxy, objloc['y']) minx = min(minx, objloc['x']) miny = min(miny, objloc['y']) goodobjs.append(obj) xlen, ylen = maxx-minx, maxy-miny deltax = maxx-minx deltay = maxy-miny if not args: # Default is fullscreen import os rows, columns = os.popen('stty size', 'r').read().split() mapsizex = int(rows)-2 mapsizey = int(columns)-1 else: # Otherwise use whatever args are passed. mapsizex, mapsizey = [int(n) for n in args.split()] import numpy # Build out the default array. maparray = numpy.array([['.']*mapsizey]*mapsizex) # This places the objects in the array based on their relative position. names = {} for obj in goodobjs: # Stupid rounding here, but good enough. x = int(((obj['loc']['x']-minx)/deltax)*mapsizex) y = int(((obj['loc']['y']-miny)/deltay)*mapsizey) try: name = names[obj['resource']] except KeyError: name = obj['resource'][0] names[obj['resource']] = name maparray[x-1, y-1] = name # Make a string because printing manually is dumb. mapstring = '\n'.join([''.join([y for y in x]) for x in maparray]) self.poutput(mapstring) def clean_dict(self, dirty): '''Clean up ugly values and drop private keys.''' newobj = {} for k, v in dirty.iteritems(): if type(v) == float: v = float("%.4f" % v) if type(v) == dict: v = self.clean_dict(v) if not k.startswith("_"): newobj[k] = v return newobj def format_object(self, objdata): '''Pretty-print an object from the client.''' newobj = {u'id': objdata['_id']['$oid']} newobj.update(self.clean_dict(objdata)) for k, v in objdata.iteritems(): # Prettify the last modified timestamp if k == "last_modified": v = datetime.fromtimestamp(v[r'$date']/1000.0)\ .strftime(settings.DATETIME_FORMAT) newobj[k] = v return pformat(newobj) def get_match(self, objname): '''Get a matching object from the client for a given id or name. It matches based on exact matches, starts with or contains, in that order.''' objname = objname.lower() # Try exact match first: try: obj = self.client.objects[objname] return obj except KeyError: # Couldn't find the exact match. pass # Try startswith match next: for objid, obj in self.client.objects.iteritems(): if objid.lower().startswith(objname) or obj['name'].lower().startswith(objname): return obj # Try contains match next: for objid, obj in self.client.objects.iteritems(): if objname in objid.lower() or objname in obj['name'].lower(): return obj # Couldn't find anything. return False def do_detail(self, args): '''Pass in the name or id of the object you want to look at.''' objname = args if objname: obj = self.get_match(objname) if obj: self.poutput(self.format_object(obj)) else: self.perror("Could not find anything resembling {0}.".format(objname))
class CmdLineApp(Cmd): def __init__(self, where): Cmd.__init__(self) self.walker = Walker(where) self.cprint = ConsoleManager() self.shortcuts.append(('xf', 'crossreferences')) @options([ make_option('-m', '--matchs_inmethod', action="store", help="Show details about matches in specified method.", default=False, metavar="<METHOD>") ]) def do_stringpatternmatch(self, arg, opts=None): if not arg: self.cprint.print_error( "Incorrect Syntax: help stringpatternmatch.\n") return strings_patterns = sorted(list(arg)) if len(strings_patterns[0]) == 0: strings_patterns = strings_patterns[1:] fnd = StringsFinder(strings_patterns) self.walker.assign_finder(fnd) results = self.walker.do_find() if results: if opts.matchs_inmethod: method_definition = repr(opts.matchs_inmethod)[2:-2] if results[method_definition]: self.cprint.print_title("· String Patterns %s matches at %s are:" % ( strings_patterns, method_definition)) self.cprint.print_list(results[method_definition]) else: self.cprint.print_title( ("· String Patterns %s have been located at above " "Application Methods:") % (strings_patterns)) self.cprint.print_list(results) def do_packageusage(self, arg, opts=None): if not arg: self.cprint.print_error("Incorrect Syntax: help packageusage.\n") arg = ''.join(arg) fnd = PackagesFinder(arg) self.walker.assign_finder(fnd) pkgData = self.walker.do_find() if pkgData: self.cprint.print_title( "* Analized Application uses the next %s Methods:" % arg) self.cprint.print_list(sorted(pkgData)) @options([ make_option('-f', '--full_graph', action="store_true", help="Include outmethod calls in flow.", default=False, metavar="<BOOL>"), make_option('--store_dot', action="store_true", default=False, help="Optional") ]) def do_instructionflow(self, arg, opts=None): if not arg: self.cprint.print_error( "Incorrect Syntax: help instructionflow.\n") else: method_definition = repr(''.join(arg))[2:-2] self.cprint.print_title( "* Method %s Instructions Flow saved to " "MethodInstructionsFlow.png." % (method_definition)) fnd = InstructionsFinder(method_definition, opts.full_graph) self.walker.assign_finder(fnd) resultados = self.walker.do_find() graph_mgr = GraphManager(True) for b1 in resultados: for lbl, target in b1.bifurcaciones: for b2 in resultados: if b2.etiqueta == target: graph_mgr.add_block_edge(b1, b2, lbl) break graph_mgr.draw("MethodInstructionsFlow", not opts.store_dot) @options([ make_option('--str_reg', action="store", help="Optional", metavar="[<REG>"), make_option('--max_levels', action="store", type="int", default=1, help="Optional", metavar="<LEVEL>"), make_option('--direction', action="store", type="int", default=2, help="Optional", metavar="<NUM> (0=to, 1=from, 2=both)"), make_option('--view_system_calls', action="store_true", default=False, help="Optional"), make_option('--store_dot', action="store_true", default=False, help="Optional") ]) def do_crossreferences(self, arg, opts=None): def cross_level(start_points, direction, view_system_calls, lvl): auxCalls = [] for methodPair in start_points: fcaller, _ = methodPair fnd = CallsFinder(fcaller) self.walker.assign_finder(fnd) mthXrefs = self.walker.do_find() for callPair in mthXrefs: caller, called = callPair if direction == 0: condition = (called == fcaller) elif direction == 1: condition = (caller == fcaller) else: condition = True if not condition: continue if callPair in auxCalls: continue if view_system_calls: auxCalls.append(callPair) continue if lvl == 0: auxCalls.append(callPair) continue if called.split('->')[0] in list(self.walker.AppInventory.keys()): auxCalls.append(callPair) return auxCalls if not arg: self.cprint.print_error( "Incorrect Syntax: help crossreferences.\n") return method_definition = arg[0] # str patterns StringMatch = None if opts.str_reg: strings_patterns = [opts.str_reg] fnd = StringsFinder(strings_patterns) self.walker.assign_finder(fnd) StringMatch = self.walker.do_find() if StringMatch: self.cprint.print_title( "* Cross-References with a %d recursion level and " "String Pattern search." % (opts.max_levels)) else: self.cprint.print_title( "* Cross-References with a %d recursion level." % (opts.max_levels)) level = 0 PackageUsage = [] results = [] PackageUsage.append((method_definition, '')) while True: PackageUsage2 = cross_level( PackageUsage, opts.direction, opts.view_system_calls, level) if len(PackageUsage) == len(PackageUsage2): equal = True for a in PackageUsage: equal = a in PackageUsage2 if not equal: break if equal: break results += PackageUsage2 PackageUsage = PackageUsage2 level += 1 if level == opts.max_levels: break graph_mgr = GraphManager() coincidences = {} for calls in results: caller, called = calls graph_mgr.add_xref_edge( caller, called, method_definition, StringMatch) if StringMatch: if (caller in StringMatch) and (caller not in coincidences): coincidences[caller] = StringMatch[caller] elif (called in StringMatch) and (called not in coincidences): coincidences[called] = StringMatch[called] self.cprint.print_dict(coincidences) if StringMatch: graph_mgr.draw("CrossReferencesWithPatterns", not opts.store_dot) else: graph_mgr.draw("CrossReferences", not opts.store_dot)
class CmdLineApp(Cmd): """ Example cmd2 application. """ # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # default_to_shell = True MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] MUMBLE_FIRST = ['so', 'like', 'well'] MUMBLE_LAST = ['right?'] def __init__(self): self.abbrev = True self.multilineCommands = ['orate'] self.maxrepeats = 3 # Add stuff to settable and shortcuts before calling base class initializer self.settable['maxrepeats'] = 'max repetitions for speak command' self.shortcuts.update({'&': 'speak'}) # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False) # For option commands, pass a single argument string instead of a list of argument strings to the do_* methods set_use_arg_list(False) opts = [ make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ] @options(opts, arg_desc='(text to say)') def do_speak(self, arg, opts=None): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): self.poutput(arg) # recommend using the poutput function instead of # self.stdout.write or "print", because Cmd allows the user # to redirect output do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input @options( [make_option('-r', '--repeat', type="int", help="output [n] times")]) def do_mumble(self, arg, opts=None): """Mumbles what you tell me to.""" repetitions = opts.repeat or 1 arg = arg.split() for i in range(min(repetitions, self.maxrepeats)): output = [] if (random.random() < .33): output.append(random.choice(self.MUMBLE_FIRST)) for word in arg: if (random.random() < .40): output.append(random.choice(self.MUMBLES)) output.append(word) if (random.random() < .25): output.append(random.choice(self.MUMBLE_LAST)) self.poutput(' '.join(output))
class ArmjitsuCmd(Cmd): """Command dispatch loop""" prompt = colorful.bold_green("(armjitsu) ") ruler = "-" debug = True def __init__(self): Cmd.__init__(self) self.bin_loaded = False self.bin_running = False self.arm_dbg = None if DEBUG_MODE: import ipdb; ipdb.set_trace() @options([make_option('-l', '--list', action="store_true", help="Show supported binary formats."), make_option('-r', '--raw', action="store_true", help="Load ARM RAW/Shellcode from file."), make_option('-e', '--elf', action="store_true", help="Load ARM ELF binary from file.") ]) def do_file(self, args, opts=None): """ Load an ARM binary file for emulation and debugging. To list ARMjitsu supported binary formats invoke: (armjitsu) file --list """ BIN_TYPE = armcpu_const.RAW_BIN if opts.raw: BIN_TYPE = armcpu_const.RAW_BIN elif opts.elf: BIN_TYPE = armcpu_const.ELF_BIN line = args if not line: print colorful.yellow("Supply the filename of the binary you wish to load please.") return None file_name = line if is_file(line) else None if not file_name or not BIN_TYPE: print colorful.yellow("Error with supplied filename.") return False self.arm_dbg = armcpu.ArmCPU(file_name, BIN_TYPE) self.bin_loaded = True print colorful.base1("Loaded binary file: {}".format(file_name)) # Synonyms for do_file do_load = do_file # REMOVE AFTER DEV def do_testing(self, line): self.arm_dbg = armcpu.ArmCPU("armraw.bin", armcpu_const.RAW_BIN) self.bin_loaded = True print colorful.bold_red("Developer testing mode! armraw.bin loaded!") print colorful.base1("Loaded binary file: {}".format(file_name)) # Synonyms for do_file do_load = do_file # REMOVE AFTER DEV def do_testing(self, line): self.arm_dbg = armcpu.ArmCPU("armraw.bin", armcpu_const.RAW_BIN) self.bin_loaded = True print colorful.bold_red("Developer testing mode! armraw.bin loaded!") do_t = do_testing def do_run(self, line): """Begins execution of ARM binary.""" if not self.bin_running: self.bin_running = True else: print colorful.yellow("Process is already running.") return None self.arm_dbg.start_execution() do_start = do_run do_r = do_run def do_continue(self, line): """Continue execution from a paused state.""" self.arm_dbg.continue_execution() do_c = do_continue do_con = do_continue def do_registers(self, line): """Display registers.""" self.arm_dbg.context_registers() do_regs = do_registers def do_step(self, line): self.arm_dbg.stop_next_instruction = False self.arm_dbg.use_step_mode = True self.arm_dbg.step_execution() # TODO: RF - check for error conditions def do_x(self, line): """Examine memory similar to GDB x/? command""" l = line.split() byte_count = l[0] address = int(l[1], 16) # Read memory as byte, half-word, word if byte_count == "b": size = 1 elif byte_count == "h": size = 2 elif byte_count == "w": size = 4 # Print our data data = self.arm_dbg.read_mem(address, size) data_list = [] for i in data: data_list.append("0x{:02x} ".format(i)) print " ".join(data_list) @options([make_option('-l', '--list', action="store_false", help="List all set breakpoints.")]) def do_break(self, line): pass def do_snapshot(self, line): """ Load/Save a snapshot """ l = line.split() usage = "snapshot load|save ini|file" if len(l) != 3: print usage return if l[0] == "load": if l[1] == "ini": bin_type = armcpu_const.INI_BIN elif l[1] == "file": bin_type = armcpu_const.SNAPSHOT_BIN else: print usage return else: print usage return self.arm_dbg = armcpu.ArmCPU(l[2], bin_type) print colorful.bold_green("Loaded snapshot: {}".format(l[2])) def do_info(self, line): pass def do_exit(self, line): print "Exiting..." return True
class InitShell(Cmd): """Ujuc computes init""" prompt = "psh >>" intro = "Welllllllllll...." path_home = os.environ['HOME'] path_pwd = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) def __init__(self): # 무조건 cmd 로 접근하여 작업을 하려면 해당 내용을 사용한다 # self.allow_cli_args = False Cmd.__init__(self, use_ipython=True) def do_system_pkg(self, arg): """ install system package """ sysname = platform.uname().system logging.debug(f"system is {sysname}") logging.info("install pkg") if sysname == "Darwin": subprocess.run(["xcode-select", "--install"]) subprocess.run(["sudo", "xcodebuild", "-license"]) output = subprocess.run(["which", "brew"], stdout=subprocess.PIPE, encoding='utf-8') logging.info(f"check brew {output}") if output is '': install_brew = subprocess.run([ "ruby", "-e", "\"$(curl -fsSL https://raw.githubusercontent.com/Homebrew" "/install/master/install)\"" ], stdout=subprocess.PIPE, encoding='utf-8') install_cask = subprocess.run(["brew", "tap", "caskroom/cask"], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(install_brew) logging.debug(install_cask) install_pkg = subprocess.run( ["brew", "install", "tmux", "tig", "zsh"], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(install_pkg) install_vim = subprocess.run([ "brew", "install", "vim", "--with-python3", "--with-override-system-vi" ], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(install_vim) install_font_hack = subprocess.run([ "brew", "cask", "install", "font-hack", "font-hack-nerd-font-mono" ], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(install_font_hack) elif sysname == "Linux": distro_name = distro.id() logging.debug(f"distro {distro_name}") if distro_name == "ubuntu": install_pkg = subprocess.run([ "sudo", "apt", "install", "-y", "vim", "tig", "tmux", "zsh", "openssh-server" ], stdout=subprocess.PIPE, encoding='utf-8') elif distro_name == "manjaro": install_pkg = subprocess.run( ["yaourt", "-S", "vim", "tig", "zsh", "openssh"], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(install_pkg) # todo: arch 용은 따로 만들어야될듯... (언젠가) @options([ make_option('--tmux', action="store_true", default=False), make_option('--tig', action="store_true", default=False), ]) def do_link_dotrc(self, arg, opts=None): """ Linked *rc file without vimrc """ logging.info("Start linking dotrc") if not opts.tmux and not opts.tig: self.symlink_rc("tmux.conf") self.symlink_rc("tigrc") elif opts.tmux: self.symlink_rc("tmux.conf") elif opts.tig: self.symlink_rc("tigrc") @options([ make_option('--zsh', action="store_true", default=False), make_option('--zplug', action="store_true", default=False), make_option('--config', action="store_true", default=False), ]) def do_zsh(self, arg, opts=None): """Install and configure zsh""" def install_zsh(): logging.info("install zsh") subprocess.run([ "curl", "-o", "/tmp/install.sh", "https://gist.githubusercontent.com/ujuc/" "0a27fd5c81a5f277f391e75683c469e8/raw/" "5c1b52db9362c36df5c9bae15921dbf8b5239866/install_oh-my-zsh.sh" ], stdout=subprocess.PIPE, encoding='utf-8') work_zsh = subprocess.run(["bash", f"/tmp/install.sh"], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(work_zsh) self.symlink_rc("zshrc") logging.info("installed oh-my-zsh") def install_zplug(): logging.info("install zplug") work_zplug = subprocess.run([ "git", "clone", "https://github.com/zplug/zplug", f"{self.path_home}/.zplug" ], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(work_zplug) logging.info("installed zplug") def config_zsh(): # todo: zsh 를 실행시키고 해당 값을 변경해줘야된다. logging.info("todo: configure zsh") logging.info("exit > zsh > zplug install > source ~/.zshrc") if not opts.zsh and not opts.zplug and not opts.config: install_zsh() install_zplug() config_zsh() elif opts.zsh: install_zsh() elif opts.zplug: install_zplug() elif opts.config: config_zsh() def do_powerline_font(self, arg): """install powerline font""" logging.info("install powerline font") subprocess.run(["bash", f"{self.path_pwd}/fonts/install.sh"]) def do_vim(self, arg): """Configure vim""" path_vim = f"{self.path_home}/.vim" os.mkdir(path_vim) os.mkdir(f"{path_vim}/bundle") os.mkdir(f"{path_vim}/vimundo") os.mkdir(f"{path_vim}/colors") logging.info("install vim-plug") work_vim_plug = subprocess.run([ "git", "clone", "https://github.com/junegunn/vim-plug.git", f"{path_vim}/autoload/" ], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(work_vim_plug) logging.info("installed vim-plug") logging.info("install amix/vimrc") subprocess.run([ "git", "clone", "https://github.com/amix/vimrc.git", f"{self.path_home}/.vim_runtime" ], stdout=subprocess.PIPE, encoding='utf-8') work_vimrc = subprocess.run( ["sh", f"{self.path_home}/.vim_runtime/install_awesome_vimrc.sh"], stdout=subprocess.PIPE, encoding='utf-8') logging.debug(work_vimrc) logging.info("installed amix/vimrc") logging.info("configure custom vimrc") os.symlink(f"{self.path_pwd}/vimrcs", f"{self.path_home}/.vim/vimrcs") self.symlink_rc("vimrc") # todo: YCM 추가 필요 # https://github.com/Valloric/YouCompleteMe.git subprocess.run(["vi", "+PlugInstall", "+qall"]) logging.info("configured custom vimrc") # todo: Git 부분을 shell 로 불러오지 안도록 하자. def do_git(self, arg): # todo: git config 부분 추가 subprocess.run(["git", "submodule", "init"]) subprocess.run(["git", "submodule", "update"]) def do_eof(self, arg): print("\n") logger.info("bye bye") return self._STOP_AND_EXIT def symlink_rc(self, file_name): try: os.lstat(f"{self.path_home}/.{file_name}") os.remove(f"{self.path_home}/.{file_name}") logging.info(f"Remove {file_name}") except FileNotFoundError: pass os.symlink(f"{self.path_pwd}/{file_name}", f"{self.path_home}/.{file_name}") logging.info(f"Linked {file_name}")
#def do_get(self,arg,opts=None): #for path in string.split(arg): #if not os.path.exists(path): #print self.colorize('Error: Path not exist','red') #continue #else: #for s in self._get_operation_list(opts): #s.download(path,targetpath=opts.desc_path if opts.desc_path else None) #def complete_get(self,text,line,begidx,endidx): #import readline #readline.set_completer_delims(' \t\n`~!@#$%^&*()-=+[{]}\\|;:\'",<>;?') #import glob #return glob.glob('%s*' % text) @options([make_option('-c', '--create', action='store_true', help='create piece'), make_option('-p', '--ploy', type='string', help='the ploy for choice servers'), make_option('-l', '--list', action='store_true', help='list piece'), make_option('-d', '--delete', action='store_true', help='delete piece'), make_option('-n', '--name', type='string', help='piece name')]) def do_piece(self, arg, opts=None): arg = ''.join(arg) if opts.create and opts.name and opts.ploy: piece = {'ploy': opts.ploy, 'createtime': time.ctime(), 'servers': []} if self.mode == Server: slist = self.mode.piece(opts.ploy) piece['servers'] = slist self.piecis[opts.name] = piece if slist:
class Shell(HistorySavingDefaultShell): '''A shell interface to trace functionality.''' def __init__(self, pager_path=None, files=None, verbose=True, clear_history=False): HistorySavingDefaultShell.__init__(self, clear_history=clear_history) self.tracer = TraceCore(libraries=BuiltInPackages, verbose=verbose, reader_class_name=config.cmd_reader_class) self.prompt = "trace> " self.files = files or [] self.last_exception = None self._verbose = verbose if pager_path: self.pager_path = pager_path self.output_file = PagerFilename else: self.pager_path = self.output_file = None def get_verbose(self): return self._verbose def set_verbose(self, is_verbose): self._verbose = self.tracer.verbose = is_verbose verbose = property(get_verbose, set_verbose) def do_quiet(self, args): '''Produces less output during processing.''' if self._verbose: self.set_verbose(False) msg("Will generate less output.") def do_verbose(self, args): '''Produces more output during processing.''' if not self._verbose: self.set_verbose(True) msg("Will generate more output.") def preloop(self): '''Executed before the command loop is entered.''' DefaultShell.preloop(self) # remove '-' (for option completion) and a number of shell separators (for path completion) # from the set of completer delimiters readline.set_completer_delims(re.sub(r'[-/*~\\]', '', readline.get_completer_delims())) def precmd(self, line): '''Executed before every command is interpreted.''' cleaned_line = line.strip() self._hist.append(cleaned_line) return cleaned_line def do_load(self, args): '''Loads filter modules given as package names.''' modules = args.split() old_modules = set(self.tracer.available_filters_dict.keys()) self.tracer.add_modules(modules) modules = set(self.tracer.available_filters_dict.keys()) added_modules = modules.difference(old_modules) if added_modules: msg("%s modules added:", len(added_modules)) for module in added_modules: msg("\t%s", module) else: msg("No modules added.") @options([ make_option('-l', '--long', action='store_true', dest='long', help='Show detailed summary', default=False), make_option('-s', '--sort-by', action='store', type='choice', choices=['name', 'module', 'opt', 'long-opt'], dest='sort_key', help='Display filters in a given sorted order', default='name')]) def do_list(self, args, opts): """Lists all loaded filters.""" self.tracer.list_filters(long=opts.long, filter_sort_key=opts.sort_key) def do_with(self, args): '''Changes or displays the working set.''' args = args.split() if args: self.files = [] for arg in args: # So that if arg is not a glob, it won't get removed # because it doesn't exist on the fs: globbed = list(flatten(glob.glob(arg))) if globbed: self.files += globbed else: self.files.append(arg) msg("Working set is: " + list_preview(self.files)) def get_filter_by_switch(self, switch_name): '''Retrieves the filter object based on its short or long form switch name.''' is_option_long_name = switch_name.startswith('--') for filter in self.tracer.available_filters_dict.values(): if is_option_long_name: if filter.long_opt == switch_name[2:]: return filter.__name__ else: if filter.opt == switch_name[1:]: return filter.__name__ err("No filter with switch %s found.", switch_name) return None def do_run(self, args): '''Runs the given filter with the given arguments on the working set.''' args = args.split() if not args: return filter_name = args.pop(0) if not filter_name: # no filter with the requested switch was found return if filter_name.startswith('--'): # a long option name was given filter_name = self.get_filter_by_switch(filter_name) elif filter_name.startswith('-'): # a short option name was given filter_name = self.get_filter_by_switch(filter_name) # Special case: for a one-arg filter any number of arguments are treated # as a single argument. if (filter_name in self.tracer and get_argcount_for_method(self.tracer[filter_name].__init__) == 1): filter_args = (' '.join(args), ) elif args is not None: filter_args = args else: filter_args = None def action(): self.tracer.run( [(filter_name, filter_args)], self.files ) print self.redirecting_stdout(action, filter_name, filter_args) def do_runmany(self, args): # XXX: HACK HACK HACK HACK filters = args.split() if not args: return def action(): self.tracer.run( [( filter_name, () ) for filter_name in filters], self.files ) print self.redirecting_stdout(action, '|'.join(filters), ()) def do_backtrace(self, args): '''Displays the exception backtrace for the last failed filter.''' if self.last_exception: sys.excepthook(*self.last_exception) do_bt = do_backtrace def create_pager_pipe(self): if not self.pager_path: raise RuntimeError('No pager was given.') if os.path.basename(self.pager_path) == 'less': params = (self.pager_path, '-R', '-') else: params = (self.pager_path, '-') return subprocess.Popen( params, stdin=subprocess.PIPE, stdout=None, stderr=None, shell=False) def do_into(self, args): '''Sets or displays the destination for filter output. The special filename StdoutFilename will redirect filter output to the console.''' def print_output_destination(): if self.output_file is None: msg("Filter output will be sent to the console.") elif self.output_file == PagerFilename: msg("Filter output will be paged with %s.", self.pager_path) else: msg("Filter output will be redirected to: %s", self.output_file) args = args.split(' ', 1) output_file = args[0] if output_file: if output_file == StdoutFilename: self.output_file = None else: # we will treat the value PagerFilename specially self.output_file = output_file print_output_destination() # report on output destination in any case def redirecting_stdout(self, action, filter_name, filter_args): '''Calls the given _action_ with stdout temporarily redirected to _self.output_file_ or a pager program.''' try: old_stdout = sys.stdout pipe = None if self.output_file == PagerFilename: pipe = self.create_pager_pipe() sys.stdout = pipe.stdin elif self.output_file: sys.stdout = open(self.output_file, 'w') action() if self.output_file == PagerFilename: if pipe: sys.stdout.close() # Signal EOF pipe.wait() except KeyboardInterrupt: if pipe: pipe.stdin.close() pipe.wait() msg("\nFilter run %s halted by user.", filter_run_name(filter_name, filter_args)) except Exception, e: if pipe: pipe.stdin.close() pipe.wait() msg("Filter run %s halted by framework:", filter_run_name(filter_name, filter_args)) # TODO: Exception no longer has a message field in Python 3.0, produces a DeprecationWarning # under Python 2.7 msg("\t%s (%s)", e.message, e.__class__.__name__) self.last_exception = sys.exc_info() finally:
class BaiduPan(Cmd): prompt = colored('dupan', 'yellow', attrs = ['bold']) + colored(' >> ', 'red', attrs = ['bold']) completekey = 'tab' editor = 'vim' timing = False debug = True download_root = ROOT cwd = '/' dirs = {} pcs = None def __init__(self): Cmd.__init__(self) try: import readline readline.set_completer_delims(' \t\n"') # initially it was ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?', but I dont want to break on too many except: pass @options([make_option('-u', '--username', help="specify username"), make_option('-p', '--password',help="specify password"), ]) def do_login(self, args, opts): print 'logging in, please wait ...' self.pcs = PCS(opts.username, opts.password, captcha_callback = handle_captcha) # self.pcs.get_fastest_pcs_server() res = {} for retry in range(3): res = json.loads(self.pcs.quota().content) if res['errno'] == 0: break else: res = {} time.sleep(retry+1) if res.get('errno') == 0: print 'Login success. storage used: %s/%s' % (readable_size(res['used']), readable_size(res['total'])) else: print 'login failed: %r' % res def do_cd(self, args): if type(args) == type([]): args = args[0] if not isinstance(args, basestring) or not args: print 'cd /path/to/dir' return if args.startswith('/'): self.cwd = args else: self.cwd = os.path.join(self.cwd, args) self.cwd = os.path.normpath(self.cwd) def do_timing(self, args): if not args: print 'timing on|off' return if args.lower() == 'on': self.timing = True elif args.lower() == 'off': self.timing = False else: print 'timing on|off' return def do_pwd(self, args): print self.cwd def do_saveto(self, args): path = args if not path: print 'current download root: %s' % self.download_root return if not path.startswith('/'): path = os.path.normpath(os.path.join(os.getcwd(), path)) self.download_root = path print 'will save to %s' % path def do_ls(self, args): if not self.pcs: print 'please login first' return if not args: path = self.cwd else: path = args path = os.path.normpath(os.path.join(self.cwd, path)) print path res = json.loads(self.pcs.list_files(path).content) # print json.dumps(res, indent = 4) ''' { "isdir": 1, "category": 6, "server_filename": "cs", "local_mtime": 1395372049, "server_ctime": 1395372049, "server_mtime": 1395372049, "fs_id": 640464281820244, "path": "/cs", "size": 0, "local_ctime": 1395372049 } ''' if res.get('errno', None) != 0: log('invalid response: %r' % res, 'yellow') return print 'total %d' % len(res.get('list', [])) content = [] cnt = 0 lst = res.get('list', []) idxsz = len(str(len(lst)-1)) sizes = [] sizesz = 0 for fsitem in lst: t = readable_size(fsitem.get('size')) if len(t) > sizesz: sizesz = len(t) sizes.append(t) for i, fsitem in enumerate(lst): print '[ %s ] %s %s %s %s' % (str(cnt).rjust(idxsz), fsitem.get('isdir', 0) and 'd' or '-', sizes[i].ljust(sizesz, ' '), datetime.datetime.fromtimestamp(fsitem.get('server_mtime', 0)).strftime('%Y-%m-%d_%H:%M:%S'), colored(fsitem.get('server_filename'), fsitem.get('isdir', 0) and 'cyan' or 'white', attrs = ['bold']) + (fsitem.get('isdir', 0) and '/' or '')) cnt += 1 content.append(fsitem.get('server_filename')) self.dirs[path] = lst @options([make_option('-i', '--index', help="the file index to delete, separate with comma, e.g. 3,5,2, also range supported, e.g. 1-4,5,7"), ]) def do_meta(self, args, opts): if not self.pcs: print 'please login first' return args = split_command_line(args) fps = [] if opts.index: if not self.dirs.get(self.cwd): print 'please use `ls` to list dir first to let me know which files you want to operate' return try: indexes = parse_index_param(opts.index, len(self.dirs.get(self.cwd))) fps = [self.dirs.get(self.cwd)[i]['server_filename'] for i in indexes] except Exception, ex: print ex return final = fps + args for path in final: path = os.path.normpath(os.path.join(self.cwd, path)) print path o = json.loads(self.pcs.meta([path]).content) if o.get('errno', None) != 0: print ('invalid request: %r' % o) return size = o['info'][0]['size'] info = o['info'][0] for k in info: print colored(k + ': ', 'cyan'), colored(info[k], 'white')
class BaseShell(AbstractShell): prompt = 'K:>' show_subcommands = ['exploits'] @cmd2.options([ cmd2.make_option('-l', '--list', action='store_true', help='Lists all known exploits'), cmd2.make_option('-i', '--info', action='store_true', help='Shows specific exploit info') ], arg_desc='\nRun/List/Describe exploit') def do_exploit(self, use_path, opts=None): loader = KModuleLoader() if opts.list: return loader.list_exploits() elif opts.info: if not use_path: return print_err('Specify exploit identifier') else: return loader.info_exploit(use_path[0]) if not use_path: return print_err('Specify exploit identifier') try: exploit_shell = ExploitShell(use_path[0]) exploit_shell.cmdloop() except core.errors.InvalidCommandArgumentError as err: print_err(str(err)) except core.errors.ModuleError as err: print_err(str(err)) def complete_exploit(self, text, line, begidx, endidx): cur_dir = os.getcwd() try: os.chdir(KreepConfig.exploits_path) before_arg = line.rfind(' ', 0, begidx) if before_arg == -1: return # arg not found fixed = line[before_arg + 1:begidx] # fixed portion of the arg arg = line[before_arg + 1:endidx] pattern = arg + '*' completions = [] # iter all exploits withing exploits_path for path in [match for match in glob.glob(pattern) ]: #if match.startswith(KreepConfig.exploits_path)]: if os.path.isdir( path) and '__pycache__' not in os.path.basename(path): path = path + '/' completions.append(path.replace(fixed, '', 1)) return completions finally: os.chdir(cur_dir) def emptyline(self): pass #def do_EOF(self, line): # return True def preloop(self): print('Ctrl-D or quit to exit environment\n')
opts.channel = self.default_channel \ if opts.channel is None else opts.channel opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key _grant_command_handler(opts.channel, opts.auth_key, opts.read, opts.write, opts.ttl, async=self.async) if opts.presence is True: _grant_command_handler(opts.channel + '-pnpres', opts.auth_key, opts.read, opts.write, opts.ttl, async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), make_option('-a', '--auth-key', dest="auth_key", action="store", help="Auth Key"), make_option('-t', '--ttl', action="store", default=5, help="TTL"), make_option('-p', '--presence', action="store_true", dest="presence", default=False, help="Revoke on presence channel ?") ]) def do_revoke(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key
#def do_get(self,arg,opts=None): #for path in string.split(arg): #if not os.path.exists(path): #print self.colorize('Error: Path not exist','red') #continue #else: #for s in self._get_operation_list(opts): #s.download(path,targetpath=opts.desc_path if opts.desc_path else None) #def complete_get(self,text,line,begidx,endidx): #import readline #readline.set_completer_delims(' \t\n`~!@#$%^&*()-=+[{]}\\|;:\'",<>;?') #import glob #return glob.glob('%s*' % text) @options([ make_option('-c', '--create', action='store_true', help='create piece'), make_option('-p', '--ploy', type='string', help='the ploy for choice servers'), make_option('-l', '--list', action='store_true', help='list piece'), make_option('-d', '--delete', action='store_true', help='delete piece'), make_option('-n', '--name', type='string', help='piece name') ]) def do_piece(self, arg, opts=None): arg = ''.join(arg) if opts.create and opts.name and opts.ploy: piece = { 'ploy': opts.ploy, 'createtime': time.ctime(),