1998-04-10 04:37:16 +08:00
|
|
|
"""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
|
|
|
|
|
|
|
|
Authors: Piers Lauder (original)
|
|
|
|
Guido van Rossum (Windows support and cleanup)
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def getpass(prompt='Password: '):
|
|
|
|
"""Prompt for a password, with echo turned off.
|
|
|
|
|
|
|
|
Restore terminal settings at end.
|
|
|
|
|
|
|
|
On Windows, this calls win_getpass(prompt) which uses the
|
|
|
|
msvcrt module to get the same effect.
|
1999-02-11 22:41:46 +08:00
|
|
|
|
|
|
|
On the Mac EasyDialogs.AskPassword is used, if available.
|
1998-04-10 04:37:16 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
1998-09-22 10:38:42 +08:00
|
|
|
try:
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
except:
|
|
|
|
return default_getpass(prompt)
|
1998-04-10 04:37:16 +08:00
|
|
|
try:
|
|
|
|
import termios, TERMIOS
|
|
|
|
except ImportError:
|
1998-04-14 04:22:21 +08:00
|
|
|
try:
|
|
|
|
import msvcrt
|
|
|
|
except ImportError:
|
1999-02-11 22:41:46 +08:00
|
|
|
try:
|
|
|
|
from EasyDialogs import AskPassword
|
|
|
|
except ImportError:
|
|
|
|
return default_getpass(prompt)
|
|
|
|
else:
|
|
|
|
return AskPassword(prompt)
|
1998-04-14 04:22:21 +08:00
|
|
|
else:
|
|
|
|
return win_getpass(prompt)
|
1998-04-10 04:37:16 +08:00
|
|
|
|
|
|
|
old = termios.tcgetattr(fd) # a copy to save
|
|
|
|
new = old[:]
|
|
|
|
|
|
|
|
new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags'
|
|
|
|
try:
|
|
|
|
termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
|
1998-07-29 03:28:43 +08:00
|
|
|
passwd = _raw_input(prompt)
|
1998-04-10 04:37:16 +08:00
|
|
|
finally:
|
|
|
|
termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
|
|
|
|
|
|
|
|
sys.stdout.write('\n')
|
|
|
|
return passwd
|
|
|
|
|
|
|
|
|
|
|
|
def win_getpass(prompt='Password: '):
|
|
|
|
"""Prompt for password with echo off, using Windows getch()."""
|
|
|
|
import msvcrt
|
|
|
|
for c in prompt:
|
|
|
|
msvcrt.putch(c)
|
|
|
|
pw = ""
|
|
|
|
while 1:
|
|
|
|
c = msvcrt.getch()
|
|
|
|
if c == '\r' or c == '\n':
|
|
|
|
break
|
1998-06-12 22:28:38 +08:00
|
|
|
if c == '\003':
|
|
|
|
raise KeyboardInterrupt
|
1998-04-10 04:37:16 +08:00
|
|
|
if c == '\b':
|
|
|
|
pw = pw[:-1]
|
|
|
|
else:
|
|
|
|
pw = pw + c
|
|
|
|
msvcrt.putch('\r')
|
|
|
|
msvcrt.putch('\n')
|
|
|
|
return pw
|
|
|
|
|
|
|
|
|
1998-04-14 04:22:21 +08:00
|
|
|
def default_getpass(prompt='Password: '):
|
1998-07-29 03:28:43 +08:00
|
|
|
return _raw_input(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
def _raw_input(prompt=""):
|
|
|
|
# A raw_input() replacement that doesn't save the string in the
|
|
|
|
# GNU readline history.
|
|
|
|
import sys
|
|
|
|
prompt = str(prompt)
|
|
|
|
if prompt:
|
|
|
|
sys.stdout.write(prompt)
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line[-1] == '\n':
|
|
|
|
line = line[:-1]
|
|
|
|
return line
|
1998-04-14 04:22:21 +08:00
|
|
|
|
|
|
|
|
1998-04-10 04:37:16 +08:00
|
|
|
def getuser():
|
|
|
|
"""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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
|
|
|
|
user = os.environ.get(name)
|
|
|
|
if user:
|
|
|
|
return user
|
|
|
|
|
|
|
|
# If this fails, the exception will "explain" why
|
|
|
|
import pwd
|
|
|
|
return pwd.getpwuid(os.getuid())[0]
|