Package config_common :: Module handler_base
[hide private]
[frames] | no frames]

Source Code for Module config_common.handler_base

 1  # 
 2  # Copyright (c) 2008--2016 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15   
16  import sys 
17  import getpass 
18  from optparse import OptionParser, Option 
19   
20  from config_common import rhn_log 
21  from config_common import cfg_exceptions 
22  from config_common import local_config 
23  from rhn.i18n import bstr 
24   
25 -class HandlerBase:
26 _options_table = [] 27 _option_parser_class = OptionParser 28 _usage_options = "[options]" 29 _option_class = Option
30 - def __init__(self, args, repository, mode=None, exec_name=None):
31 self.repository = repository 32 self.set_mode(mode) 33 self.set_exec_name(exec_name) 34 self.options, self.args = self._parse_args(args)
35
36 - def set_mode(self, mode):
37 self.mode = mode
38
39 - def set_exec_name(self, exec_name):
40 self.exec_name = exec_name
41
42 - def _prog(self):
43 return "%s %s" % (sys.argv[0], self.mode or "<unknown>")
44
45 - def _parse_args(self, args):
46 # Parses the arguments and returns a tuple (options, args) 47 usage = " ".join(["%prog", self.mode, self._usage_options]) 48 self._parser = self._option_parser_class( 49 option_list=self._options_table, 50 usage=usage) 51 return self._parser.parse_args(args)
52
53 - def usage(self):
54 return self._parser.print_help()
55
56 - def authenticate(self, username=None, password=None):
57 # entry point for repository authentication 58 59 try: 60 self.repository.login() 61 except cfg_exceptions.InvalidSession: 62 if not username : 63 username=local_config.get('username') 64 if not password : 65 password=local_config.get('password') 66 67 if not password : 68 (username, password) = self.get_auth_info(username) 69 try: 70 self.repository.login(username=username, password=password) 71 except cfg_exceptions.InvalidSession: 72 e = sys.exc_info()[1] 73 rhn_log.die(1, "Session error: %s\n" % e)
74
75 - def get_auth_info(self, username=None):
76 if username is None: 77 username = self._read_username() 78 79 password = getpass.getpass() 80 81 return (username, password)
82
83 - def _read_username(self):
84 tty = open("/dev/tty", "rb+", buffering=0) 85 tty.write(bstr("Username: ")) 86 try: 87 username = tty.readline() 88 except KeyboardInterrupt: 89 tty.write(bstr("\n")) 90 sys.exit(0) 91 if username is None: 92 # EOF 93 tty.write(bstr("\n")) 94 sys.exit(0) 95 return username.strip()
96