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

Source Code for Module pulp.client.core.basecore

  1  # 
  2  # Base class inherited by all cores 
  3  # 
  4  # Copyright (c) 2010 Red Hat, Inc. 
  5  # 
  6  # This software is licensed to you under the GNU General Public License, 
  7  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  8  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  9  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 10  # along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 12  # 
 13  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 14  # granted to use or replicate Red Hat trademarks that are incorporated 
 15  # in this software or its documentation. 
 16  # 
 17   
 18  import os 
 19  import sys 
 20  from optparse import OptionParser 
 21  import pulp.client.utils as utils 
 22  import pulp.client.auth_utils as auth_utils 
 23   
24 -class BaseCore(object):
25 """ Base class for all sub-calls. """
26 - def __init__(self, name="cli", usage=None, shortdesc=None, 27 description=None):
28 self.shortdesc = shortdesc 29 if shortdesc is not None and description is None: 30 description = shortdesc 31 self.debug = 0 32 self.setup_option_parser(usage, description, False) 33 self.generate_options() 34 self._add_common_options() 35 self.name = name 36 self.username = None 37 self.password = None 38 self.cert_filename = None 39 self.key_filename = None
40
41 - def setup_option_parser(self, usage, description, skip_actions):
42 self.usage = "usage: %prog -u <username> -p <password> " + usage 43 self.parser = OptionParser(usage=self._usage_str(skip_actions), 44 description=description)
45 46
47 - def _add_common_options(self):
48 """ Common options to all modules. """ 49 help = "username for access to Pulp." 50 help = help + " Default user admin is included with base install." 51 self.parser.add_option("-u", "--username", dest="username", 52 help=help) 53 help = "password for access to Pulp." 54 self.parser.add_option("-p", "--password", dest="password", 55 help=help)
56 57
58 - def _get_action(self):
59 """ Validate the arguments passed in and determine what action to take """ 60 action = None 61 possiblecmd = utils.findSysvArgs(sys.argv) 62 if len(possiblecmd) > 2: 63 action = possiblecmd[2] 64 elif len(possiblecmd) == 2 and possiblecmd[1] == self.name: 65 self._usage() 66 sys.exit(0) 67 else: 68 return None 69 if action not in self.actions.keys(): 70 self._usage() 71 sys.exit(0) 72 return action
73
74 - def generate_options(self):
75 pass
76
77 - def _usage_str(self, skip_actions):
78 retval = self.usage.replace("%prog", os.path.basename(sys.argv[0])) + "\n" 79 if (not skip_actions): 80 retval = retval + "Supported Actions:\n" 81 items = self.actions.items() 82 items.sort() 83 for (name, cmd) in items: 84 retval = retval + "\t%-14s %-25s\n" % (name, cmd) 85 return retval
86
87 - def _usage_old(self):
88 print "\nUsage: %s MODULENAME ACTION [options] --help\n" % os.path.basename(sys.argv[0]) 89 print "Supported Actions:\n" 90 items = self.actions.items() 91 items.sort() 92 for (name, cmd) in items: 93 print("\t%-14s %-25s" % (name, cmd)) 94 print("")
95
96 - def _usage(self):
97 print self._usage_str(False)
98
99 - def _do_core(self):
100 # Subclass required to implement to actually execute the indicated command 101 pass
102
103 - def load_server(self):
104 # Subclass required to implement to setup Connection to Pulp server 105 pass
106
107 - def main(self):
108 (self.options, self.args) = self.parser.parse_args() 109 self.username = self.options.username 110 self.password = self.options.password 111 112 # It looks like this main method is only called by pulp-admin, so it should 113 # be safe to hook in the admin certificates here 114 cert_filename, key_filename = auth_utils.admin_cert_paths() 115 if os.path.exists(cert_filename): 116 self.cert_filename = cert_filename 117 self.key_filename = key_filename 118 119 self.load_server() 120 self._do_core()
121
122 -def systemExit(code, msgs=None):
123 "Exit with a code and optional message(s). Saved a few lines of code." 124 125 if msgs: 126 if type(msgs) not in [type([]), type(())]: 127 msgs = (msgs, ) 128 for msg in msgs: 129 sys.stderr.write(unicode(msg).encode("utf-8") + '\n') 130 sys.exit(code)
131