+++ /dev/null
-"""Utilities to get a password and/or the current user name.\r
-\r
-getpass(prompt[, stream]) - Prompt for a password, with echo turned off.\r
-getuser() - Get the user name from the environment or password database.\r
-\r
-GetPassWarning - This UserWarning is issued when getpass() cannot prevent\r
- echoing of the password contents while reading.\r
-\r
-On Windows, the msvcrt module will be used.\r
-On the Mac EasyDialogs.AskPassword is used, if available.\r
-\r
-"""\r
-\r
-# Authors: Piers Lauder (original)\r
-# Guido van Rossum (Windows support and cleanup)\r
-# Gregory P. Smith (tty support & GetPassWarning)\r
-\r
-import os, sys, warnings\r
-\r
-__all__ = ["getpass","getuser","GetPassWarning"]\r
-\r
-\r
-class GetPassWarning(UserWarning): pass\r
-\r
-\r
-def unix_getpass(prompt='Password: ', stream=None):\r
- """Prompt for a password, with echo turned off.\r
-\r
- Args:\r
- prompt: Written on stream to ask for the input. Default: 'Password: '\r
- stream: A writable file object to display the prompt. Defaults to\r
- the tty. If no tty is available defaults to sys.stderr.\r
- Returns:\r
- The seKr3t input.\r
- Raises:\r
- EOFError: If our input tty or stdin was closed.\r
- GetPassWarning: When we were unable to turn echo off on the input.\r
-\r
- Always restores terminal settings before returning.\r
- """\r
- fd = None\r
- tty = None\r
- try:\r
- # Always try reading and writing directly on the tty first.\r
- fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)\r
- tty = os.fdopen(fd, 'w+', 1)\r
- input = tty\r
- if not stream:\r
- stream = tty\r
- except EnvironmentError, e:\r
- # If that fails, see if stdin can be controlled.\r
- try:\r
- fd = sys.stdin.fileno()\r
- except (AttributeError, ValueError):\r
- passwd = fallback_getpass(prompt, stream)\r
- input = sys.stdin\r
- if not stream:\r
- stream = sys.stderr\r
-\r
- if fd is not None:\r
- passwd = None\r
- try:\r
- old = termios.tcgetattr(fd) # a copy to save\r
- new = old[:]\r
- new[3] &= ~termios.ECHO # 3 == 'lflags'\r
- tcsetattr_flags = termios.TCSAFLUSH\r
- if hasattr(termios, 'TCSASOFT'):\r
- tcsetattr_flags |= termios.TCSASOFT\r
- try:\r
- termios.tcsetattr(fd, tcsetattr_flags, new)\r
- passwd = _raw_input(prompt, stream, input=input)\r
- finally:\r
- termios.tcsetattr(fd, tcsetattr_flags, old)\r
- stream.flush() # issue7208\r
- except termios.error, e:\r
- if passwd is not None:\r
- # _raw_input succeeded. The final tcsetattr failed. Reraise\r
- # instead of leaving the terminal in an unknown state.\r
- raise\r
- # We can't control the tty or stdin. Give up and use normal IO.\r
- # fallback_getpass() raises an appropriate warning.\r
- del input, tty # clean up unused file objects before blocking\r
- passwd = fallback_getpass(prompt, stream)\r
-\r
- stream.write('\n')\r
- return passwd\r
-\r
-\r
-def win_getpass(prompt='Password: ', stream=None):\r
- """Prompt for password with echo off, using Windows getch()."""\r
- if sys.stdin is not sys.__stdin__:\r
- return fallback_getpass(prompt, stream)\r
- import msvcrt\r
- for c in prompt:\r
- msvcrt.putch(c)\r
- pw = ""\r
- while 1:\r
- c = msvcrt.getch()\r
- if c == '\r' or c == '\n':\r
- break\r
- if c == '\003':\r
- raise KeyboardInterrupt\r
- if c == '\b':\r
- pw = pw[:-1]\r
- else:\r
- pw = pw + c\r
- msvcrt.putch('\r')\r
- msvcrt.putch('\n')\r
- return pw\r
-\r
-\r
-def fallback_getpass(prompt='Password: ', stream=None):\r
- warnings.warn("Can not control echo on the terminal.", GetPassWarning,\r
- stacklevel=2)\r
- if not stream:\r
- stream = sys.stderr\r
- print >>stream, "Warning: Password input may be echoed."\r
- return _raw_input(prompt, stream)\r
-\r
-\r
-def _raw_input(prompt="", stream=None, input=None):\r
- # A raw_input() replacement that doesn't save the string in the\r
- # GNU readline history.\r
- if not stream:\r
- stream = sys.stderr\r
- if not input:\r
- input = sys.stdin\r
- prompt = str(prompt)\r
- if prompt:\r
- stream.write(prompt)\r
- stream.flush()\r
- # NOTE: The Python C API calls flockfile() (and unlock) during readline.\r
- line = input.readline()\r
- if not line:\r
- raise EOFError\r
- if line[-1] == '\n':\r
- line = line[:-1]\r
- return line\r
-\r
-\r
-def getuser():\r
- """Get the username from the environment or password database.\r
-\r
- First try various environment variables, then the password\r
- database. This works on Windows as long as USERNAME is set.\r
-\r
- """\r
-\r
- import os\r
-\r
- for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):\r
- user = os.environ.get(name)\r
- if user:\r
- return user\r
-\r
- # If this fails, the exception will "explain" why\r
- import pwd\r
- return pwd.getpwuid(os.getuid())[0]\r
-\r
-# Bind the name getpass to the appropriate function\r
-try:\r
- import termios\r
- # it's possible there is an incompatible termios from the\r
- # McMillan Installer, make sure we have a UNIX-compatible termios\r
- termios.tcgetattr, termios.tcsetattr\r
-except (ImportError, AttributeError):\r
- try:\r
- import msvcrt\r
- except ImportError:\r
- try:\r
- from EasyDialogs import AskPassword\r
- except ImportError:\r
- getpass = fallback_getpass\r
- else:\r
- getpass = AskPassword\r
- else:\r
- getpass = win_getpass\r
-else:\r
- getpass = unix_getpass\r