From ad688c00db2c05718c0fb4cea39fbbb8eb2ae805 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Husiaty=C5=84ski?= Date: Wed, 13 Feb 2008 19:02:34 +0100 Subject: [PATCH] Some new bug fixes. --- plugin/abs.py | 44 +++++++++++++++++++++++++++ pyshell | 2 +- pyshell.py | 96 +++++++++++++++++++++++++++++++++++++++++++---------------- showinfo.py | 4 ++- 4 files changed, 119 insertions(+), 27 deletions(-) diff --git a/plugin/abs.py b/plugin/abs.py index 7f967c6..6189842 100644 --- a/plugin/abs.py +++ b/plugin/abs.py @@ -1,11 +1,16 @@ import string import os import os.path +import sys + + class abs(object): """Do some stuff with ABS""" def __init__(self, put): self.put = put + self.compile_cmd = "makepkg -f " + self.install_cmd = "pacman -U " self.search_path = "/var/abs" def do_search(self, pkgname, *ignore): @@ -24,3 +29,42 @@ class abs(object): self.put(root) except IOError: pass + + def do_compile(self, pkgname, *ignore): + for root, dirs, files in os.walk(self.search_path): + for d in dirs: + if d == pkgname: + path = os.path.join(root, d) + self.put("==> " + path, "GREEN", True) + os.system("cd %s && %s " % (path, self.compile_cmd)) + return + self.put("PKGBUILD not found.") + + def do_install(self, pkgname, *ignore): + pkg_list = [] + for root, dirs, files in os.walk(self.search_path): + for d in dirs: + if d == pkgname: + path = os.path.join(root, d) + for pkg in os.listdir(path): + if pkg.endswith("pkg.tar.gz"): + pkg_list.append(pkg) + if len(pkg_list) == 0: + self.put("No package found. Try to compile it first.") + elif len(pkg_list) == 1: + os.system("cd %s && %s %s" % \ + (path, self.install_cmd, pkg_list[0])) + else: + self.put("More than one package found. Please choose one:") + for numb, pkg in enumerate(pkg_list): + self.put(str(numb) + pkg) + pkg_numb = int(raw_input()) + if pkg_numb > len(pkg_list): + sys.exit("Blah! Error!") + os.system("cd %s && %s %s" % \ + (path, self.install_cmd, pkg_list[pkg_numb])) + return + + + + diff --git a/pyshell b/pyshell index 85c34fe..15797d3 100755 --- a/pyshell +++ b/pyshell @@ -1,6 +1,6 @@ #!/bin/bash -SHELL_FILE=aur-shell.py +SHELL_FILE=pyshell.py # cd SCRIPT_PATH if [ $# -eq 0 ] diff --git a/pyshell.py b/pyshell.py index ba87ac6..6d439f5 100644 --- a/pyshell.py +++ b/pyshell.py @@ -23,24 +23,67 @@ __version__ = "0.1" class AurShell(cmd.Cmd): - """Interactive shell framework. + """ + Interactive shell framework. To use it, write some modules, becouse by default it's very poor shell. """ def __init__(self, init_message=""): cmd.Cmd.__init__(self) self.put = Put() + # prefix for method callable by shell + self.cmdprefix = "do_" self.prompt = conf.shell_prompt - if init_message: - self.intro = init_message - else: - self.intro = "\n\tWelcome to aurShell v%(__version__)s\n" % globals() - self.commands = plugload.load(conf.modules_path) + self.commands = self.load_plugins(conf.modules_path) # load history, create empty file if history doesn't exist if not os.path.isfile(conf.history_file): open(conf.history_file, "w").close() readline.read_history_file(conf.history_file) readline.set_history_length(conf.history_length) + if init_message: + self.intro = init_message + else: + self.intro = "\n\tWelcome to aurShell v%(__version__)s\n" % globals() + + + def load_plugins(self, modules_path): + """Load all commands modules from modules_path direcotory.""" + command_modules = [] + # adding plugins path to sys.path + if modules_path not in sys.path: + sys.path.append(modules_path) + # importing all modules from modules_path + for module_name in os.listdir(modules_path): + module_path = os.path.abspath( + string.join(modules_path, module_name)) + (module_name, module_extension) = os.path.splitext(module_name) + # if it shouldn't be load + if os.path.islink(module_path) or \ + module_path == __file__ or \ + module_extension != ".py": + continue + for key in sys.modules.keys(): + if key == module_name: + del sys.modules[key] + module = __import__(module_name) + command_modules.append({"obj": module_name, "name": module,}) + # search for class in each module + module_objs = {} + for module in command_modules: + for obj in dir(module["name"]): + # TODO + # better class serch 2008-01-27 16:56:42 + if not obj.startswith("_"): + try: + # check if it's method + x = getattr(module['name'], obj)(self.put) + # create instance for each class + # as agrument give it Put() class instance + module_objs[obj] = \ + getattr(module['name'], obj)(self.put) + except TypeError: + pass + return module_objs def default(self, cmd=None): """Run commands. @@ -64,28 +107,31 @@ class AurShell(cmd.Cmd): # for only one argument, try to run __call__() method with # no arguments elif "__call__" in dir(self.commands[cmd[0]]): - a = self.put(getattr(self.commands[cmd[0]], "__call__")()) - if a: - self.put(a) + getattr(self.commands[cmd[0]], "__call__")() else: self.put("%s : bad usege. Try to run help." % cmd[0]) # if command was called with arguments elif len(cmd) > 1: + cmd[1] = self.cmdprefix + cmd[1] if not cmd[0] in self.commands.keys(): self.put("%s : command not found." % cmd[0]) - # if method named arg[1] exist in class arg[0], - # then try to run in + # if method named arg[1] exist in class arg[0], try to run it elif cmd[1] in dir(self.commands[cmd[0]]): - a = self.put(getattr(self.commands[cmd[0]], cmd[1])(cmd[2:])) - if a: - self.put(a) + #print dir(self.commands[cmd[0]]) + try: + getattr(self.commands[cmd[0]], cmd[1])(*cmd[2:]) + except TypeError: + # show __doc__ if exist + doc = getattr(self.commands[cmd[0]], cmd[1]) + if doc.__doc__: + self.put(doc.__doc__) + else: + self.put("%s : bad usage" % cmd[1][3:]) # if there's no such method arg[1] in class arg[0], # try to run class.__call__(args..) else: try: - a = self.commands[cmd[0]](*cmd[1:]) - if a: - self.put(a) + self.commands[cmd[0]](*cmd[1:]) except TypeError: # object is not callable self.put("%s : bad usage" % cmd[0]) @@ -93,7 +139,7 @@ class AurShell(cmd.Cmd): def completenames(self, text, *ignored): """Complete commands""" - dotext = 'do_'+text + dotext = self.cmdprefix + text # local methods local_cmd_list = [a[3:] + " " for a in self.get_names() if a.startswith(dotext)] # + all metrods from modules @@ -103,19 +149,19 @@ class AurShell(cmd.Cmd): def completedefault(self, text, line, begidx, endidx): """Complete commands argument""" + dotext = self.cmdprefix + text line = line.split() # if only commands was given if len(line) == 1: - cmds = [a + " " for a in dir(self.commands[line[0]]) if not - a.startswith("__")] - return cmds - # if command and argument begin was given + cmds = [a[3:] + " " for a in dir(self.commands[line[0]]) \ + if a.startswith(dotext)] elif len(line) == 2: - return [a + " " for a in dir(self.commands[line[0]]) if - a.startswith(line[1])] + cmds = [a[3:] + " " for a in dir(self.commands[line[0]]) \ + if a.startswith(dotext)] # else don't complete (or should I?) else: - return [] + cmds = [] + return cmds def do_help(self, arg): diff --git a/showinfo.py b/showinfo.py index 6bffc20..dc8f20a 100644 --- a/showinfo.py +++ b/showinfo.py @@ -20,7 +20,7 @@ class Put(object): else: self.color(message, color, newline) - + def normal(self, message="", **args): """Default message printer""" if not args: @@ -28,10 +28,12 @@ class Put(object): else: print("%s %s" % (message, " ".join(args))) + def noline(self, message): """Print message, but don't end it with new line character""" print message, + def color(self, msg, color=None, newline=False): """Print message with colors By default it doesn't end with new line character. -- 2.11.4.GIT