|
14 | 14 | import sys |
15 | 15 |
|
16 | 16 | def unix_getpass(prompt='Password: '): |
17 | | - """Prompt for a password, with echo turned off. |
| 17 | + """Prompt for a password, with echo turned off. |
18 | 18 |
|
19 | | - Restore terminal settings at end. |
20 | | - """ |
| 19 | + Restore terminal settings at end. |
| 20 | + """ |
21 | 21 |
|
22 | | - try: |
23 | | - fd = sys.stdin.fileno() |
24 | | - except: |
25 | | - return default_getpass(prompt) |
| 22 | + try: |
| 23 | + fd = sys.stdin.fileno() |
| 24 | + except: |
| 25 | + return default_getpass(prompt) |
26 | 26 |
|
27 | | - getpass = default_getpass |
28 | | - old = termios.tcgetattr(fd) # a copy to save |
29 | | - new = old[:] |
| 27 | + getpass = default_getpass |
| 28 | + old = termios.tcgetattr(fd) # a copy to save |
| 29 | + new = old[:] |
30 | 30 |
|
31 | | - new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags' |
32 | | - try: |
33 | | - termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new) |
34 | | - passwd = _raw_input(prompt) |
35 | | - finally: |
36 | | - termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old) |
| 31 | + new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags' |
| 32 | + try: |
| 33 | + termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new) |
| 34 | + passwd = _raw_input(prompt) |
| 35 | + finally: |
| 36 | + termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old) |
37 | 37 |
|
38 | | - sys.stdout.write('\n') |
39 | | - return passwd |
| 38 | + sys.stdout.write('\n') |
| 39 | + return passwd |
40 | 40 |
|
41 | 41 |
|
42 | 42 | def win_getpass(prompt='Password: '): |
43 | | - """Prompt for password with echo off, using Windows getch().""" |
44 | | - import msvcrt |
45 | | - for c in prompt: |
46 | | - msvcrt.putch(c) |
47 | | - pw = "" |
48 | | - while 1: |
49 | | - c = msvcrt.getch() |
50 | | - if c == '\r' or c == '\n': |
51 | | - break |
52 | | - if c == '\003': |
53 | | - raise KeyboardInterrupt |
54 | | - if c == '\b': |
55 | | - pw = pw[:-1] |
56 | | - else: |
57 | | - pw = pw + c |
58 | | - msvcrt.putch('\r') |
59 | | - msvcrt.putch('\n') |
60 | | - return pw |
| 43 | + """Prompt for password with echo off, using Windows getch().""" |
| 44 | + import msvcrt |
| 45 | + for c in prompt: |
| 46 | + msvcrt.putch(c) |
| 47 | + pw = "" |
| 48 | + while 1: |
| 49 | + c = msvcrt.getch() |
| 50 | + if c == '\r' or c == '\n': |
| 51 | + break |
| 52 | + if c == '\003': |
| 53 | + raise KeyboardInterrupt |
| 54 | + if c == '\b': |
| 55 | + pw = pw[:-1] |
| 56 | + else: |
| 57 | + pw = pw + c |
| 58 | + msvcrt.putch('\r') |
| 59 | + msvcrt.putch('\n') |
| 60 | + return pw |
61 | 61 |
|
62 | 62 |
|
63 | 63 | def default_getpass(prompt='Password: '): |
64 | | - print "Warning: Problem with getpass. Passwords may be echoed." |
65 | | - return _raw_input(prompt) |
| 64 | + print "Warning: Problem with getpass. Passwords may be echoed." |
| 65 | + return _raw_input(prompt) |
66 | 66 |
|
67 | 67 |
|
68 | 68 | def _raw_input(prompt=""): |
69 | | - # A raw_input() replacement that doesn't save the string in the |
70 | | - # GNU readline history. |
71 | | - import sys |
72 | | - prompt = str(prompt) |
73 | | - if prompt: |
74 | | - sys.stdout.write(prompt) |
75 | | - line = sys.stdin.readline() |
76 | | - if not line: |
77 | | - raise EOFError |
78 | | - if line[-1] == '\n': |
79 | | - line = line[:-1] |
80 | | - return line |
| 69 | + # A raw_input() replacement that doesn't save the string in the |
| 70 | + # GNU readline history. |
| 71 | + import sys |
| 72 | + prompt = str(prompt) |
| 73 | + if prompt: |
| 74 | + sys.stdout.write(prompt) |
| 75 | + line = sys.stdin.readline() |
| 76 | + if not line: |
| 77 | + raise EOFError |
| 78 | + if line[-1] == '\n': |
| 79 | + line = line[:-1] |
| 80 | + return line |
81 | 81 |
|
82 | 82 |
|
83 | 83 | def getuser(): |
84 | | - """Get the username from the environment or password database. |
| 84 | + """Get the username from the environment or password database. |
85 | 85 |
|
86 | | - First try various environment variables, then the password |
87 | | - database. This works on Windows as long as USERNAME is set. |
| 86 | + First try various environment variables, then the password |
| 87 | + database. This works on Windows as long as USERNAME is set. |
88 | 88 |
|
89 | | - """ |
| 89 | + """ |
90 | 90 |
|
91 | | - import os |
| 91 | + import os |
92 | 92 |
|
93 | | - for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
94 | | - user = os.environ.get(name) |
95 | | - if user: |
96 | | - return user |
| 93 | + for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
| 94 | + user = os.environ.get(name) |
| 95 | + if user: |
| 96 | + return user |
97 | 97 |
|
98 | | - # If this fails, the exception will "explain" why |
99 | | - import pwd |
100 | | - return pwd.getpwuid(os.getuid())[0] |
| 98 | + # If this fails, the exception will "explain" why |
| 99 | + import pwd |
| 100 | + return pwd.getpwuid(os.getuid())[0] |
101 | 101 |
|
102 | 102 | # Bind the name getpass to the appropriate function |
103 | 103 | try: |
104 | | - import termios, TERMIOS |
| 104 | + import termios, TERMIOS |
105 | 105 | except ImportError: |
106 | | - try: |
107 | | - import msvcrt |
108 | | - except ImportError: |
109 | | - try: |
110 | | - from EasyDialogs import AskPassword |
111 | | - except ImportError: |
112 | | - getpass = default_getpass |
113 | | - else: |
114 | | - getpass = AskPassword |
115 | | - else: |
116 | | - getpass = win_getpass |
| 106 | + try: |
| 107 | + import msvcrt |
| 108 | + except ImportError: |
| 109 | + try: |
| 110 | + from EasyDialogs import AskPassword |
| 111 | + except ImportError: |
| 112 | + getpass = default_getpass |
| 113 | + else: |
| 114 | + getpass = AskPassword |
| 115 | + else: |
| 116 | + getpass = win_getpass |
117 | 117 | else: |
118 | | - getpass = unix_getpass |
119 | | - |
| 118 | + getpass = unix_getpass |
0 commit comments