Package backend :: Package common :: Module cli
[hide private]
[frames] | no frames]

Source Code for Module backend.common.cli

 1  # 
 2  # Copyright (c) 2012--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  try: 
19      #  python 2 
20      import xmlrpclib 
21  except ImportError: 
22      #  python3 
23      import xmlrpc.client as xmlrpclib  # pylint: disable=F0401 
24   
25   
26 -def getUsernamePassword(cmdlineUsername, cmdlinePassword):
27 """ 28 Returns a username and password (either by returning the ones passed as 29 args, or the user's input 30 """ 31 if cmdlineUsername and cmdlinePassword: 32 return cmdlineUsername, cmdlinePassword 33 34 username = cmdlineUsername 35 password = cmdlinePassword 36 37 # Read the username, if not already specified 38 tty = open("/dev/tty", "r+") 39 while not username: 40 tty.write("Username: ") 41 try: 42 username = tty.readline() 43 except KeyboardInterrupt: 44 tty.write("\n") 45 sys.exit(0) 46 if username is None: 47 # EOF 48 tty.write("\n") 49 sys.exit(0) 50 username = username.strip() 51 if username: 52 break 53 54 # Now read the password 55 while not password: 56 try: 57 password = getpass.getpass("Password: ") 58 except KeyboardInterrupt: 59 tty.write("\n") 60 sys.exit(0) 61 tty.close() 62 return username, password
63 64
65 -def xmlrpc_login(client, username, password, verbose=0):
66 """ 67 Authenticate Session call 68 """ 69 if verbose: 70 print("...logging in to server...") 71 72 try: 73 sessionkey = client.auth.login(username, password) 74 except xmlrpclib.Fault: 75 e = sys.exc_info()[1] 76 sys.stderr.write("Error: %s\n" % e.faultString) 77 sys.exit(-1) 78 79 return sessionkey
80 81
82 -def xmlrpc_logout(client, session_key, verbose=0):
83 """ 84 End Authentication call 85 """ 86 if verbose: 87 print("...logging out of server...") 88 89 client.auth.logout(session_key)
90