Package pulp :: Package client :: Module pulpcli
[hide private]
[frames] | no frames]

Source Code for Module pulp.client.pulpcli

  1  # 
  2  # Pulp client utility 
  3  # Copyright (c) 2010 Red Hat, Inc. 
  4  # 
  5  # This software is licensed to you under the GNU General Public License, 
  6  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  7  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  8  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  9  # along with this software; if not, see 
 10  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 11  # 
 12  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 13  # granted to use or replicate Red Hat trademarks that are incorporated 
 14  # in this software or its documentation. 
 15  # 
 16   
 17  import os 
 18  import logging 
 19  import sys 
 20  import pkgutil 
 21  import pulp.client.utils as utils 
 22  from pulp.client.logutil import getLogger 
 23   
 24  import pulp.client.core as core 
 25   
 26  import gettext 
 27  _ = gettext.gettext 
 28  log = getLogger(__name__) 
 29   
30 -class PulpCore:
31 """ 32 A top level class to load modules dynamically from pulp.client.cores package 33 """
34 - def __init__(self):
35 self.cli_cores = {} 36 self.args = utils.findSysvArgs(sys.argv) 37 if len(self.args) > 1: 38 cls = self._load_core(self.args[1]) 39 if cls not in self._load_all_cores(): 40 print ("Invalid Command. Please see --help for valid modules") 41 sys.exit(0) 42 self.cli_cores[self.args[1]] = cls() 43 else: 44 for cls in self._load_all_cores(): 45 cmd = cls() 46 if cmd.name != "cli": 47 self.cli_cores[cmd.name] = cmd
48 49
50 - def _add_core(self, cmd):
51 self.cli_cores[cmd.name] = cmd
52
53 - def _load_core(self, core):
54 name = "core_" + core 55 mod = __import__('pulp.client.core.', globals(), locals(), [name]) 56 try: 57 submod = getattr(mod, name) 58 except AttributeError: 59 return None 60 return getattr(submod, core)
61
62 - def _load_all_cores(self):
63 pkgpth = os.path.dirname(core.__file__) 64 modules = [name for _, name, _ in pkgutil.iter_modules([pkgpth]) 65 if name.startswith("core_")] 66 cls = [] 67 for name in modules: 68 mod = __import__('pulp.client.core.', globals(), locals(), [name]) 69 submod = getattr(mod, name) 70 cls.append(getattr(submod, name.split("_")[-1])) 71 return cls
72
73 - def _usage(self):
74 print "\nUsage: %s -u <username> -p <password> MODULENAME --help\n" % os.path.basename(sys.argv[0]) 75 print "Supported modules:\n" 76 items = self.cli_cores.items() 77 for (name, cmd) in items: 78 print("\t%-14s %-25s" % (name, cmd.shortdesc)) 79 print("")
80
81 - def _find_best_match(self, args):
82 possiblecmd = utils.findSysvArgs(args) 83 if not possiblecmd: 84 return None 85 86 cmd = None 87 key = " ".join(possiblecmd) 88 if self.cli_cores.has_key(" ".join(possiblecmd)): 89 cmd = self.cli_cores[key] 90 i = -1 91 while cmd == None: 92 key = " ".join(possiblecmd[:i]) 93 if key is None or key == "": 94 break 95 if self.cli_cores.has_key(key): 96 cmd = self.cli_cores[key] 97 i -= 1 98 99 return cmd
100
101 - def main(self):
102 103 cmd = self._find_best_match(sys.argv[1:]) 104 if not cmd: 105 self._usage() 106 sys.exit(0) 107 108 cmd.main()
109 110 if __name__ == "__main__": 111 # TODO: Make logging configurable 112 logging.root.addHandler(logging.StreamHandler()) 113 logging.root.setLevel(logging.ERROR) 114 PulpCore().main() 115