"""Utilities to get a password and/or the current user name.
getpass(prompt) - prompt for a password, with echo turned off
getuser() - get the user name from the environment or password database
On Windows, the msvcrt module will be used.
On the Mac EasyDialogs.AskPassword is used, if available.
# Authors: Piers Lauder (original)
# Guido van Rossum (Windows support and cleanup)
__all__
= ["getpass","getuser"]
def unix_getpass(prompt
='Password: '):
"""Prompt for a password, with echo turned off.
Restore terminal settings at end.
return default_getpass(prompt
)
old
= termios
.tcgetattr(fd
) # a copy to save
new
[3] = new
[3] & ~termios
.ECHO
# 3 == 'lflags'
termios
.tcsetattr(fd
, termios
.TCSADRAIN
, new
)
passwd
= _raw_input(prompt
)
termios
.tcsetattr(fd
, termios
.TCSADRAIN
, old
)
def win_getpass(prompt
='Password: '):
"""Prompt for password with echo off, using Windows getch()."""
if sys
.stdin
is not sys
.__stdin
__:
return default_getpass(prompt
)
if c
== '\r' or c
== '\n':
def default_getpass(prompt
='Password: '):
print "Warning: Problem with getpass. Passwords may be echoed."
return _raw_input(prompt
)
def _raw_input(prompt
=""):
# A raw_input() replacement that doesn't save the string in the
line
= sys
.stdin
.readline()
"""Get the username from the environment or password database.
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
for name
in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user
= os
.environ
.get(name
)
# If this fails, the exception will "explain" why
return pwd
.getpwuid(os
.getuid())[0]
# Bind the name getpass to the appropriate function
# it's possible there is an incompatible termios from the
# McMillan Installer, make sure we have a UNIX-compatible termios
termios
.tcgetattr
, termios
.tcsetattr
except (ImportError, AttributeError):
from EasyDialogs
import AskPassword
getpass
= default_getpass