Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python2....
File: getpass.py
"""Utilities to get a password and/or the current user name.
[0] Fix | Delete
[1] Fix | Delete
getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
[2] Fix | Delete
getuser() - Get the user name from the environment or password database.
[3] Fix | Delete
[4] Fix | Delete
GetPassWarning - This UserWarning is issued when getpass() cannot prevent
[5] Fix | Delete
echoing of the password contents while reading.
[6] Fix | Delete
[7] Fix | Delete
On Windows, the msvcrt module will be used.
[8] Fix | Delete
On the Mac EasyDialogs.AskPassword is used, if available.
[9] Fix | Delete
[10] Fix | Delete
"""
[11] Fix | Delete
[12] Fix | Delete
# Authors: Piers Lauder (original)
[13] Fix | Delete
# Guido van Rossum (Windows support and cleanup)
[14] Fix | Delete
# Gregory P. Smith (tty support & GetPassWarning)
[15] Fix | Delete
[16] Fix | Delete
import os, sys, warnings
[17] Fix | Delete
[18] Fix | Delete
__all__ = ["getpass","getuser","GetPassWarning"]
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class GetPassWarning(UserWarning): pass
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
def unix_getpass(prompt='Password: ', stream=None):
[25] Fix | Delete
"""Prompt for a password, with echo turned off.
[26] Fix | Delete
[27] Fix | Delete
Args:
[28] Fix | Delete
prompt: Written on stream to ask for the input. Default: 'Password: '
[29] Fix | Delete
stream: A writable file object to display the prompt. Defaults to
[30] Fix | Delete
the tty. If no tty is available defaults to sys.stderr.
[31] Fix | Delete
Returns:
[32] Fix | Delete
The seKr3t input.
[33] Fix | Delete
Raises:
[34] Fix | Delete
EOFError: If our input tty or stdin was closed.
[35] Fix | Delete
GetPassWarning: When we were unable to turn echo off on the input.
[36] Fix | Delete
[37] Fix | Delete
Always restores terminal settings before returning.
[38] Fix | Delete
"""
[39] Fix | Delete
fd = None
[40] Fix | Delete
tty = None
[41] Fix | Delete
try:
[42] Fix | Delete
# Always try reading and writing directly on the tty first.
[43] Fix | Delete
fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
[44] Fix | Delete
tty = os.fdopen(fd, 'w+', 1)
[45] Fix | Delete
input = tty
[46] Fix | Delete
if not stream:
[47] Fix | Delete
stream = tty
[48] Fix | Delete
except EnvironmentError, e:
[49] Fix | Delete
# If that fails, see if stdin can be controlled.
[50] Fix | Delete
try:
[51] Fix | Delete
fd = sys.stdin.fileno()
[52] Fix | Delete
except (AttributeError, ValueError):
[53] Fix | Delete
passwd = fallback_getpass(prompt, stream)
[54] Fix | Delete
input = sys.stdin
[55] Fix | Delete
if not stream:
[56] Fix | Delete
stream = sys.stderr
[57] Fix | Delete
[58] Fix | Delete
if fd is not None:
[59] Fix | Delete
passwd = None
[60] Fix | Delete
try:
[61] Fix | Delete
old = termios.tcgetattr(fd) # a copy to save
[62] Fix | Delete
new = old[:]
[63] Fix | Delete
new[3] &= ~termios.ECHO # 3 == 'lflags'
[64] Fix | Delete
tcsetattr_flags = termios.TCSAFLUSH
[65] Fix | Delete
if hasattr(termios, 'TCSASOFT'):
[66] Fix | Delete
tcsetattr_flags |= termios.TCSASOFT
[67] Fix | Delete
try:
[68] Fix | Delete
termios.tcsetattr(fd, tcsetattr_flags, new)
[69] Fix | Delete
passwd = _raw_input(prompt, stream, input=input)
[70] Fix | Delete
finally:
[71] Fix | Delete
termios.tcsetattr(fd, tcsetattr_flags, old)
[72] Fix | Delete
stream.flush() # issue7208
[73] Fix | Delete
except termios.error, e:
[74] Fix | Delete
if passwd is not None:
[75] Fix | Delete
# _raw_input succeeded. The final tcsetattr failed. Reraise
[76] Fix | Delete
# instead of leaving the terminal in an unknown state.
[77] Fix | Delete
raise
[78] Fix | Delete
# We can't control the tty or stdin. Give up and use normal IO.
[79] Fix | Delete
# fallback_getpass() raises an appropriate warning.
[80] Fix | Delete
del input, tty # clean up unused file objects before blocking
[81] Fix | Delete
passwd = fallback_getpass(prompt, stream)
[82] Fix | Delete
[83] Fix | Delete
stream.write('\n')
[84] Fix | Delete
return passwd
[85] Fix | Delete
[86] Fix | Delete
[87] Fix | Delete
def win_getpass(prompt='Password: ', stream=None):
[88] Fix | Delete
"""Prompt for password with echo off, using Windows getch()."""
[89] Fix | Delete
if sys.stdin is not sys.__stdin__:
[90] Fix | Delete
return fallback_getpass(prompt, stream)
[91] Fix | Delete
import msvcrt
[92] Fix | Delete
for c in prompt:
[93] Fix | Delete
msvcrt.putch(c)
[94] Fix | Delete
pw = ""
[95] Fix | Delete
while 1:
[96] Fix | Delete
c = msvcrt.getch()
[97] Fix | Delete
if c == '\r' or c == '\n':
[98] Fix | Delete
break
[99] Fix | Delete
if c == '\003':
[100] Fix | Delete
raise KeyboardInterrupt
[101] Fix | Delete
if c == '\b':
[102] Fix | Delete
pw = pw[:-1]
[103] Fix | Delete
else:
[104] Fix | Delete
pw = pw + c
[105] Fix | Delete
msvcrt.putch('\r')
[106] Fix | Delete
msvcrt.putch('\n')
[107] Fix | Delete
return pw
[108] Fix | Delete
[109] Fix | Delete
[110] Fix | Delete
def fallback_getpass(prompt='Password: ', stream=None):
[111] Fix | Delete
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
[112] Fix | Delete
stacklevel=2)
[113] Fix | Delete
if not stream:
[114] Fix | Delete
stream = sys.stderr
[115] Fix | Delete
print >>stream, "Warning: Password input may be echoed."
[116] Fix | Delete
return _raw_input(prompt, stream)
[117] Fix | Delete
[118] Fix | Delete
[119] Fix | Delete
def _raw_input(prompt="", stream=None, input=None):
[120] Fix | Delete
# A raw_input() replacement that doesn't save the string in the
[121] Fix | Delete
# GNU readline history.
[122] Fix | Delete
if not stream:
[123] Fix | Delete
stream = sys.stderr
[124] Fix | Delete
if not input:
[125] Fix | Delete
input = sys.stdin
[126] Fix | Delete
prompt = str(prompt)
[127] Fix | Delete
if prompt:
[128] Fix | Delete
stream.write(prompt)
[129] Fix | Delete
stream.flush()
[130] Fix | Delete
# NOTE: The Python C API calls flockfile() (and unlock) during readline.
[131] Fix | Delete
line = input.readline()
[132] Fix | Delete
if not line:
[133] Fix | Delete
raise EOFError
[134] Fix | Delete
if line[-1] == '\n':
[135] Fix | Delete
line = line[:-1]
[136] Fix | Delete
return line
[137] Fix | Delete
[138] Fix | Delete
[139] Fix | Delete
def getuser():
[140] Fix | Delete
"""Get the username from the environment or password database.
[141] Fix | Delete
[142] Fix | Delete
First try various environment variables, then the password
[143] Fix | Delete
database. This works on Windows as long as USERNAME is set.
[144] Fix | Delete
[145] Fix | Delete
"""
[146] Fix | Delete
[147] Fix | Delete
import os
[148] Fix | Delete
[149] Fix | Delete
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
[150] Fix | Delete
user = os.environ.get(name)
[151] Fix | Delete
if user:
[152] Fix | Delete
return user
[153] Fix | Delete
[154] Fix | Delete
# If this fails, the exception will "explain" why
[155] Fix | Delete
import pwd
[156] Fix | Delete
return pwd.getpwuid(os.getuid())[0]
[157] Fix | Delete
[158] Fix | Delete
# Bind the name getpass to the appropriate function
[159] Fix | Delete
try:
[160] Fix | Delete
import termios
[161] Fix | Delete
# it's possible there is an incompatible termios from the
[162] Fix | Delete
# McMillan Installer, make sure we have a UNIX-compatible termios
[163] Fix | Delete
termios.tcgetattr, termios.tcsetattr
[164] Fix | Delete
except (ImportError, AttributeError):
[165] Fix | Delete
try:
[166] Fix | Delete
import msvcrt
[167] Fix | Delete
except ImportError:
[168] Fix | Delete
try:
[169] Fix | Delete
from EasyDialogs import AskPassword
[170] Fix | Delete
except ImportError:
[171] Fix | Delete
getpass = fallback_getpass
[172] Fix | Delete
else:
[173] Fix | Delete
getpass = AskPassword
[174] Fix | Delete
else:
[175] Fix | Delete
getpass = win_getpass
[176] Fix | Delete
else:
[177] Fix | Delete
getpass = unix_getpass
[178] Fix | Delete
[179] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function