-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathpyos.py
335 lines (257 loc) · 8.8 KB
/
pyos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""
pyos.py -- Wrappers for the operating system.
Like py{error,util}.py, it won't be translated to C++.
"""
from __future__ import print_function
from errno import EINTR
import pwd
import resource
import signal
import select
import termios # for read -n
import time
from core import pyutil
from core.pyerror import log
import posix_ as posix
from typing import Optional, Tuple, List, Dict, cast, Any, TYPE_CHECKING
if TYPE_CHECKING:
from core.comp_ui import _IDisplay
from osh.builtin_process import _TrapHandler
EOF_SENTINEL = 256 # bigger than any byte
NEWLINE_CH = 10 # ord('\n')
class ReadError(Exception):
"""Wraps errno returned by read(). Used by 'read' and 'mapfile' builtins.
"""
def __init__(self, err_num):
# type: (int) -> None
self.err_num = err_num
def Read(fd, n, buf):
# type: (int, int, List[str]) -> Tuple[int, int]
"""
C-style wrapper around Python's posix.read() that uses return values instead
of exceptions for errors. We will implement this directly in C++ and not use
exceptions at all.
It reads n bytes from the given file descriptor and appends it to buf.
Returns:
(-1, errno) on failure
(number of bytes read, 0) on success. Where 0 bytes read indicates EOF.
"""
try:
chunk = posix.read(fd, n)
except OSError as e:
return -1, e.errno
else:
buf.append(chunk)
return len(chunk), 0
def ReadByte(fd):
# type: (int) -> Tuple[int, int]
"""
Another low level interface with a return value interface. Used by
_ReadUntilDelim() and _ReadLineSlowly().
Returns:
failure: (-1, errno) on failure
success: (ch integer value or EOF_SENTINEL, 0)
"""
try:
b = posix.read(fd, 1)
except OSError as e:
return -1, e.errno
else:
if len(b):
return ord(b), 0
else:
return EOF_SENTINEL, 0
def ReadLine():
# type: () -> str
"""Read a line from stdin.
This is a SLOW PYTHON implementation taht calls read(0, 1) too many times. I
tried to write libc.stdin_readline() which uses the getline() function, but
somehow that makes spec/oil-builtins.test.sh fail. We use Python's
f.readline() in frontend/reader.py FileLineReader with f == stdin.
So I think the buffers get confused:
- Python buffers for sys.stdin.readline()
- libc buffers for getline()
- no buffers when directly issuing read(0, 1) calls, which ReadByte() does
TODO: Add keep_newline arg
"""
ch_array = [] # type: List[int]
while True:
ch, err_num = ReadByte(0)
if ch < 0:
if err_num == EINTR:
# Instead of retrying, return EOF, which is what libc.stdin_readline()
# did. I think this interface is easier with getline().
# This causes 'read --line' to return status 1.
return ''
else:
raise ReadError(err_num)
elif ch == EOF_SENTINEL:
break
else:
ch_array.append(ch)
# TODO: Add option to omit newline
if ch == NEWLINE_CH:
break
return pyutil.ChArrayToString(ch_array)
def Environ():
# type: () -> Dict[str, str]
return posix.environ
def Chdir(dest_dir):
# type: (str) -> int
"""Returns 0 for success and nonzero errno for error."""
try:
posix.chdir(dest_dir)
except OSError as e:
return e.errno
return 0
def GetMyHomeDir():
# type: () -> Optional[str]
"""Get the user's home directory from the /etc/pyos.
Used by $HOME initialization in osh/state.py. Tilde expansion and readline
initialization use mem.GetValue('HOME').
"""
uid = posix.getuid()
try:
e = pwd.getpwuid(uid)
except KeyError:
return None
return e.pw_dir
def GetHomeDir(user_name):
# type: (str) -> Optional[str]
"""
For ~otheruser/src. TODO: Should this be cached?
"""
# http://linux.die.net/man/3/getpwnam
try:
e = pwd.getpwnam(user_name)
except KeyError:
return None
return e.pw_dir
def GetUserName(uid):
# type: (int) -> str
try:
e = pwd.getpwuid(uid)
except KeyError:
return "<ERROR: Couldn't determine user name for uid %d>" % uid
else:
return e.pw_name
def Time():
# type: () -> Tuple[float, float, float]
t = time.time() # calls gettimeofday() under the hood
u = resource.getrusage(resource.RUSAGE_SELF)
return t, u.ru_utime, u.ru_stime
def PrintTimes():
# type: () -> None
utime, stime, cutime, cstime, elapsed = posix.times()
print("%dm%1.3fs %dm%1.3fs" % (utime / 60, utime % 60, stime / 60, stime % 60))
print("%dm%1.3fs %dm%1.3fs" % (cutime / 60, cutime % 60, cstime / 60, cstime % 60))
# So builtin_misc.py doesn't depend on termios, which makes C++ translation
# easier
TERM_ICANON = termios.ICANON
TERM_ECHO = termios.ECHO
class TermState(object):
"""
TODO: Make this into a context manager which is a C++ destructor?
"""
def __init__(self, fd, mask):
# type: (int, int) -> None
self.fd = fd
# silly way to make a copy
# https://docs.python.org/2/library/termios.html
self.orig_attrs = termios.tcgetattr(fd)
term_attrs = termios.tcgetattr(fd)
a3 = cast(int, term_attrs[3])
# Disable canonical (buffered) mode. See `man termios` for an extended
# discussion.
term_attrs[3] = a3 & mask
termios.tcsetattr(self.fd, termios.TCSANOW, term_attrs)
def Restore(self):
# type: () -> None
try:
termios.tcsetattr(self.fd, termios.TCSANOW, self.orig_attrs)
except termios.error as e:
# Superficial fix for issue #1001. I'm not sure why we get errno.EIO,
# but we can't really handle it here. In C++ I guess we ignore the
# error.
pass
def OsType():
# type: () -> str
""" Compute $OSTYPE variable """
return posix.uname()[0].lower()
def InputAvailable(fd):
# type: (int) -> bool
# similar to lib/sh/input_avail.c in bash
# read, write, except
r, w, exc = select.select([fd], [], [fd], 0)
return len(r) != 0
UNTRAPPED_SIGWINCH = -1
class SigwinchHandler(object):
"""Wrapper to call user handler."""
def __init__(self, display, sig_state):
# type: (_IDisplay, SignalState) -> None
self.display = display
self.sig_state = sig_state
self.user_handler = None # type: _TrapHandler
def __call__(self, sig_num, unused_frame):
# type: (int, Any) -> None
"""For Python's signal module."""
# SENTINEL for UNTRAPPED SIGWINCH. If it's trapped, self.user_handler
# will overwrite it with signal.SIGWINCH.
self.sig_state.last_sig_num = UNTRAPPED_SIGWINCH
self.display.OnWindowChange()
if self.user_handler:
self.user_handler(sig_num, unused_frame)
def SignalState_AfterForkingChild():
# type: () -> None
"""Not a member of SignalState since we didn't do dependency injection."""
# Respond to Ctrl-\ (core dump)
signal.signal(signal.SIGQUIT, signal.SIG_DFL)
# Python sets SIGPIPE handler to SIG_IGN by default. Child processes
# shouldn't have this.
# https://docs.python.org/2/library/signal.html
# See Python/pythonrun.c.
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
# Child processes should get Ctrl-Z.
signal.signal(signal.SIGTSTP, signal.SIG_DFL)
class SignalState(object):
"""All changes to global signal state go through this object."""
def __init__(self):
# type: () -> None
self.sigwinch_handler = None # type: SigwinchHandler
self.last_sig_num = 0 # MUTABLE GLOBAL, for interrupted 'wait'
def InitShell(self):
# type: () -> None
"""Always called when initializing the shell process."""
pass
def InitInteractiveShell(self, display):
# type: (_IDisplay) -> None
"""Called when initializing an interactive shell."""
# The shell itself should ignore Ctrl-\.
signal.signal(signal.SIGQUIT, signal.SIG_IGN)
# This prevents Ctrl-Z from suspending OSH in interactive mode.
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
# Register a callback to receive terminal width changes.
# NOTE: In line_input.c, we turned off rl_catch_sigwinch.
# This is ALWAYS on, which means that it can cause EINTR, and wait() and
# read() have to handle it
self.sigwinch_handler = SigwinchHandler(display, self)
signal.signal(signal.SIGWINCH, self.sigwinch_handler)
def AddUserTrap(self, sig_num, handler):
# type: (int, Any) -> None
"""For user-defined handlers registered with the 'trap' builtin."""
if sig_num == signal.SIGWINCH:
assert self.sigwinch_handler is not None
self.sigwinch_handler.user_handler = handler
else:
signal.signal(sig_num, handler)
# TODO: SIGINT is similar: set a flag, then optionally call user _TrapHandler
def RemoveUserTrap(self, sig_num):
# type: (int) -> None
"""For user-defined handlers registered with the 'trap' builtin."""
# Restore default
if sig_num == signal.SIGWINCH:
assert self.sigwinch_handler is not None
self.sigwinch_handler.user_handler = None
else:
signal.signal(sig_num, signal.SIG_DFL)
# TODO: SIGINT is similar: set a flag, then optionally call user _TrapHandler