def getPassword(prompt='Password: ', confirm=0, forceTTY=0, confirmPrompt='Confirm password: ', mismatchMessage="Passwords don't match."):
 isaTTY = (hasattr(sys.stdin, 'isatty') and sys.stdin.isatty())
 old = None
 try:
  if (not isaTTY):
   if forceTTY:
    try:
     old = (sys.stdin, sys.stdout)
     sys.stdin = sys.stdout = open('/dev/tty', 'r+')
    except:
     raise RuntimeError('Cannot obtain a TTY')
   else:
    password = sys.stdin.readline()
    if (password[(-1)] == '\n'):
     password = password[:(-1)]
    return password
  while 1:
   try1 = _getpass(prompt)
   if (not confirm):
    return try1
   try2 = _getpass(confirmPrompt)
   if (try1 == try2):
    return try1
   else:
    sys.stderr.write((mismatchMessage + '\n'))
 finally:
  if old:
   sys.stdin.close()
   (sys.stdin, sys.stdout) = old

def getPassword ( prompt = 'Password: ' , confirm = 0 , forceTTY = 0 , confirmPrompt = 'Confirm password: ' , mismatchMessage = "Passwords don't match." ) : isaTTY = ( hasattr ( sys . stdin , 'isatty' ) and sys . stdin . isatty ( ) ) old = None try : if ( not isaTTY ) : if forceTTY : try : old = ( sys . stdin , sys . stdout ) sys . stdin = sys . stdout = open ( '/dev/tty' , 'r+' ) except : raise RuntimeError ( 'Cannot obtain a TTY' ) else : password = sys . stdin . readline ( ) if ( password [ ( - 1 ) ] == '\n' ) : password = password [ : ( - 1 ) ] return password while 1 : try1 = _getpass ( prompt ) if ( not confirm ) : return try1 try2 = _getpass ( confirmPrompt ) if ( try1 == try2 ) : return try1 else : sys . stderr . write ( ( mismatchMessage + '\n' ) ) finally : if old : sys . stdin . close ( ) ( sys . stdin , sys . stdout ) = old
obtain a password by prompting or from stdin .