0001 """Utilities to get a password and/or the current user name. 0002 0003 getpass(prompt) - prompt for a password, with echo turned off 0004 getuser() - get the user name from the environment or password database 0005 0006 On Windows, the msvcrt module will be used. 0007 On the Mac EasyDialogs.AskPassword is used, if available. 0008 0009 """ 0010 0011 # Authors: Piers Lauder (original) 0012 # Guido van Rossum (Windows support and cleanup) 0013 0014 import sys 0015 0016 __all__ = ["getpass","getuser"] 0017 0018 def unix_getpass(prompt='Password: '): 0019 """Prompt for a password, with echo turned off. 0020 0021 Restore terminal settings at end. 0022 """ 0023 0024 try: 0025 fd = sys.stdin.fileno() 0026 except: 0027 return default_getpass(prompt) 0028 0029 old = termios.tcgetattr(fd) # a copy to save 0030 new = old[:] 0031 0032 new[3] = new[3] & ~termios.ECHO # 3 == 'lflags' 0033 try: 0034 termios.tcsetattr(fd, termios.TCSADRAIN, new) 0035 passwd = _raw_input(prompt) 0036 finally: 0037 termios.tcsetattr(fd, termios.TCSADRAIN, old) 0038 0039 sys.stdout.write('\n') 0040 return passwd 0041 0042 0043 def win_getpass(prompt='Password: '): 0044 """Prompt for password with echo off, using Windows getch().""" 0045 if sys.stdin is not sys.__stdin__: 0046 return default_getpass(prompt) 0047 import msvcrt 0048 for c in prompt: 0049 msvcrt.putch(c) 0050 pw = "" 0051 while 1: 0052 c = msvcrt.getch() 0053 if c == '\r' or c == '\n': 0054 break 0055 if c == '\003': 0056 raise KeyboardInterrupt 0057 if c == '\b': 0058 pw = pw[:-1] 0059 else: 0060 pw = pw + c 0061 msvcrt.putch('\r') 0062 msvcrt.putch('\n') 0063 return pw 0064 0065 0066 def default_getpass(prompt='Password: '): 0067 print "Warning: Problem with getpass. Passwords may be echoed." 0068 return _raw_input(prompt) 0069 0070 0071 def _raw_input(prompt=""): 0072 # A raw_input() replacement that doesn't save the string in the 0073 # GNU readline history. 0074 prompt = str(prompt) 0075 if prompt: 0076 sys.stdout.write(prompt) 0077 line = sys.stdin.readline() 0078 if not line: 0079 raise EOFError 0080 if line[-1] == '\n': 0081 line = line[:-1] 0082 return line 0083 0084 0085 def getuser(): 0086 """Get the username from the environment or password database. 0087 0088 First try various environment variables, then the password 0089 database. This works on Windows as long as USERNAME is set. 0090 0091 """ 0092 0093 import os 0094 0095 for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): 0096 user = os.environ.get(name) 0097 if user: 0098 return user 0099 0100 # If this fails, the exception will "explain" why 0101 import pwd 0102 return pwd.getpwuid(os.getuid())[0] 0103 0104 # Bind the name getpass to the appropriate function 0105 try: 0106 import termios 0107 # it's possible there is an incompatible termios from the 0108 # McMillan Installer, make sure we have a UNIX-compatible termios 0109 termios.tcgetattr, termios.tcsetattr 0110 except (ImportError, AttributeError): 0111 try: 0112 import msvcrt 0113 except ImportError: 0114 try: 0115 from EasyDialogs import AskPassword 0116 except ImportError: 0117 getpass = default_getpass 0118 else: 0119 getpass = AskPassword 0120 else: 0121 getpass = win_getpass 0122 else: 0123 getpass = unix_getpass 0124
Generated by PyXR 0.9.4