]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/python/getpass.py
hgwebfs: write headers individually, so they are not limited by webfs iounit (thanks...
[plan9front.git] / sys / lib / python / getpass.py
1 """Utilities to get a password and/or the current user name.
2
3 getpass(prompt) - prompt for a password, with echo turned off
4 getuser() - get the user name from the environment or password database
5
6 On Windows, the msvcrt module will be used.
7 On the Mac EasyDialogs.AskPassword is used, if available.
8
9 """
10
11 # Authors: Piers Lauder (original)
12 #          Guido van Rossum (Windows support and cleanup)
13
14 import sys
15
16 __all__ = ["getpass","getuser"]
17
18 def unix_getpass(prompt='Password: ', stream=None):
19     """Prompt for a password, with echo turned off.
20     The prompt is written on stream, by default stdout.
21
22     Restore terminal settings at end.
23     """
24     if stream is None:
25         stream = sys.stdout
26
27     try:
28         fd = sys.stdin.fileno()
29     except:
30         return default_getpass(prompt)
31
32     old = termios.tcgetattr(fd)     # a copy to save
33     new = old[:]
34
35     new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
36     try:
37         termios.tcsetattr(fd, termios.TCSADRAIN, new)
38         passwd = _raw_input(prompt, stream)
39     finally:
40         termios.tcsetattr(fd, termios.TCSADRAIN, old)
41
42     stream.write('\n')
43     return passwd
44
45
46 def win_getpass(prompt='Password: ', stream=None):
47     """Prompt for password with echo off, using Windows getch()."""
48     if sys.stdin is not sys.__stdin__:
49         return default_getpass(prompt, stream)
50     import msvcrt
51     for c in prompt:
52         msvcrt.putch(c)
53     pw = ""
54     while 1:
55         c = msvcrt.getch()
56         if c == '\r' or c == '\n':
57             break
58         if c == '\003':
59             raise KeyboardInterrupt
60         if c == '\b':
61             pw = pw[:-1]
62         else:
63             pw = pw + c
64     msvcrt.putch('\r')
65     msvcrt.putch('\n')
66     return pw
67
68 def default_getpass(prompt='Password: ', stream=None):
69     try:
70         ctl = open("/dev/consctl", "w")
71         ctl.write("rawon")
72         ctl.flush()
73         buf = _raw_input(prompt, stream)
74         ctl.write("rawoff")
75         ctl.flush()
76         ctl.close()
77         return buf;
78     except:
79         buf = _raw_input(prompt, stream)
80     return buf
81
82 def _raw_input(prompt="", stream=None):
83     # A raw_input() replacement that doesn't save the string in the
84     # GNU readline history.
85     if stream is None:
86         stream = sys.stdout
87     prompt = str(prompt)
88     if prompt:
89         stream.write(prompt)
90         stream.flush()
91     line = sys.stdin.readline()
92     if not line:
93         raise EOFError
94     if line[-1] == '\n':
95         line = line[:-1]
96     return line
97
98
99 def getuser():
100     """Get the username from the environment or password database.
101
102     First try various environment variables, then the password
103     database.  This works on Windows as long as USERNAME is set.
104
105     """
106
107     import os
108
109     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
110         user = os.environ.get(name)
111         if user:
112             return user
113
114     # If this fails, the exception will "explain" why
115     import pwd
116     return pwd.getpwuid(os.getuid())[0]
117
118 # Bind the name getpass to the appropriate function
119 try:
120     import termios
121     # it's possible there is an incompatible termios from the
122     # McMillan Installer, make sure we have a UNIX-compatible termios
123     termios.tcgetattr, termios.tcsetattr
124 except (ImportError, AttributeError):
125     getpass = default_getpass
126 else:
127     getpass = unix_getpass