summaryrefslogtreecommitdiff
path: root/Mailman/pythonlib
diff options
context:
space:
mode:
authorbwarsaw1999-01-09 06:14:15 +0000
committerbwarsaw1999-01-09 06:14:15 +0000
commit29d542c17acdee1b692dc99e3ed26cf2ae5c59b8 (patch)
tree4568717070102e22bf8c22cb5a35b9cdaf084600 /Mailman/pythonlib
parent3573bfdd17170362d1a2a9d2af1a2b0df16dad18 (diff)
downloadmailman-29d542c17acdee1b692dc99e3ed26cf2ae5c59b8.tar.gz
mailman-29d542c17acdee1b692dc99e3ed26cf2ae5c59b8.tar.zst
mailman-29d542c17acdee1b692dc99e3ed26cf2ae5c59b8.zip
Diffstat (limited to 'Mailman/pythonlib')
-rw-r--r--Mailman/pythonlib/getpass.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/Mailman/pythonlib/getpass.py b/Mailman/pythonlib/getpass.py
new file mode 100644
index 000000000..66b1aeebc
--- /dev/null
+++ b/Mailman/pythonlib/getpass.py
@@ -0,0 +1,107 @@
+"""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.
+
+ """
+
+ import sys
+ try:
+ fd = sys.stdin.fileno()
+ except:
+ return default_getpass(prompt)
+ try:
+ import termios, TERMIOS
+ except ImportError:
+ try:
+ import msvcrt
+ except ImportError:
+ return default_getpass(prompt)
+ else:
+ return win_getpass(prompt)
+
+ 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)
+ passwd = _raw_input(prompt)
+ 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
+ if c == '\003':
+ raise KeyboardInterrupt
+ if c == '\b':
+ pw = pw[:-1]
+ else:
+ pw = pw + c
+ msvcrt.putch('\r')
+ msvcrt.putch('\n')
+ return pw
+
+
+def default_getpass(prompt='Password: '):
+ 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
+
+
+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]