OILS / core / pyos.py View on Github | oilshell.org

290 lines, 136 significant
1"""
2pyos.py -- Wrappers for the operating system.
3
4Like py{error,util}.py, it won't be translated to C++.
5"""
6from __future__ import print_function
7
8from errno import EINTR
9import pwd
10import resource
11import select
12import sys
13import termios # for read -n
14import time
15
16from mycpp import iolib
17from mycpp import mops
18from mycpp.mylib import log
19
20import posix_ as posix
21from posix_ import WUNTRACED
22
23from typing import Optional, Tuple, List, Dict, cast, Any, TYPE_CHECKING
24if TYPE_CHECKING:
25 from core import error
26
27_ = log
28
29EOF_SENTINEL = 256 # bigger than any byte
30NEWLINE_CH = 10 # ord('\n')
31
32
33def FlushStdout():
34 # type: () -> Optional[error.IOError_OSError]
35 """Flush CPython buffers.
36
37 Return error because we call this in a C++ destructor, and those can't
38 throw exceptions.
39 """
40 err = None # type: Optional[error.IOError_OSError]
41 try:
42 sys.stdout.flush()
43 except (IOError, OSError) as e:
44 err = e
45 return err
46
47
48def WaitPid(waitpid_options):
49 # type: (int) -> Tuple[int, int]
50 """
51 Return value:
52 pid is 0 if WNOHANG passed, and nothing has changed state
53 status: value that can be parsed with WIFEXITED() etc.
54 """
55 try:
56 # Notes:
57 # - The arg -1 makes it like wait(), which waits for any process.
58 # - WUNTRACED is necessary to get stopped jobs. What about WCONTINUED?
59 # - We don't retry on EINTR, because the 'wait' builtin should be
60 # interruptible.
61 # - waitpid_options can be WNOHANG
62 pid, status = posix.waitpid(-1, WUNTRACED | waitpid_options)
63 except OSError as e:
64 if e.errno == EINTR and iolib.gSignalSafe.PollUntrappedSigInt():
65 raise KeyboardInterrupt()
66 return -1, e.errno
67
68 return pid, status
69
70
71class ReadError(Exception):
72 """Wraps errno returned by read().
73
74 Used by 'read' and 'mapfile' builtins.
75 """
76
77 def __init__(self, err_num):
78 # type: (int) -> None
79 self.err_num = err_num
80
81
82def Read(fd, n, chunks):
83 # type: (int, int, List[str]) -> Tuple[int, int]
84 """C-style wrapper around Python's posix.read() that uses return values
85 instead of exceptions for errors.
86
87 We will implement this directly in C++ and not use exceptions at all.
88
89 It reads n bytes from the given file descriptor and appends it to chunks.
90
91 Returns:
92 (-1, errno) on failure
93 (number of bytes read, 0) on success. Where 0 bytes read indicates EOF.
94 """
95 try:
96 chunk = posix.read(fd, n)
97 except OSError as e:
98 if e.errno == EINTR and iolib.gSignalSafe.PollUntrappedSigInt():
99 raise KeyboardInterrupt()
100 return -1, e.errno
101 else:
102 length = len(chunk)
103 if length:
104 chunks.append(chunk)
105 return length, 0
106
107
108def ReadByte(fd):
109 # type: (int) -> Tuple[int, int]
110 """Low-level interface that returns values rather than raising exceptions.
111
112 Used by _ReadUntilDelim() and _ReadLineSlowly().
113
114 Returns:
115 failure: (-1, errno) on failure
116 success: (ch integer value or EOF_SENTINEL, 0)
117 """
118 try:
119 b = posix.read(fd, 1)
120 except OSError as e:
121 if e.errno == EINTR and iolib.gSignalSafe.PollUntrappedSigInt():
122 raise KeyboardInterrupt()
123 return -1, e.errno
124 else:
125 if len(b):
126 return ord(b), 0
127 else:
128 return EOF_SENTINEL, 0
129
130
131def Environ():
132 # type: () -> Dict[str, str]
133 return posix.environ
134
135
136def Chdir(dest_dir):
137 # type: (str) -> int
138 """Returns 0 for success and nonzero errno for error."""
139 try:
140 posix.chdir(dest_dir)
141 except OSError as e:
142 return e.errno
143 return 0
144
145
146def GetMyHomeDir():
147 # type: () -> Optional[str]
148 """Get the user's home directory from the /etc/pyos.
149
150 Used by $HOME initialization in osh/state.py. Tilde expansion and
151 readline initialization use mem.GetValue('HOME').
152 """
153 uid = posix.getuid()
154 try:
155 e = pwd.getpwuid(uid)
156 except KeyError:
157 return None
158
159 return e.pw_dir
160
161
162def GetHomeDir(user_name):
163 # type: (str) -> Optional[str]
164 """For ~otheruser/src.
165
166 TODO: Should this be cached?
167 """
168 # http://linux.die.net/man/3/getpwnam
169 try:
170 e = pwd.getpwnam(user_name)
171 except KeyError:
172 return None
173
174 return e.pw_dir
175
176
177class PasswdEntry(object):
178
179 def __init__(self, pw_name, uid, gid):
180 # type: (str, int, int) -> None
181 self.pw_name = pw_name
182 self.pw_uid = uid
183 self.pw_gid = gid
184
185
186def GetAllUsers():
187 # type: () -> List[PasswdEntry]
188 users = [
189 PasswdEntry(u.pw_name, u.pw_uid, u.pw_gid) for u in pwd.getpwall()
190 ]
191 return users
192
193
194def GetUserName(uid):
195 # type: (int) -> str
196 try:
197 e = pwd.getpwuid(uid)
198 except KeyError:
199 return "<ERROR: Couldn't determine user name for uid %d>" % uid
200 else:
201 return e.pw_name
202
203
204def GetRLimit(res):
205 # type: (int) -> Tuple[mops.BigInt, mops.BigInt]
206 """
207 Raises IOError
208 """
209 soft, hard = resource.getrlimit(res)
210 return (mops.IntWiden(soft), mops.IntWiden(hard))
211
212
213def SetRLimit(res, soft, hard):
214 # type: (int, mops.BigInt, mops.BigInt) -> None
215 """
216 Raises IOError
217 """
218 resource.setrlimit(res, (soft.i, hard.i))
219
220
221def Time():
222 # type: () -> Tuple[float, float, float]
223 t = time.time() # calls gettimeofday() under the hood
224 u = resource.getrusage(resource.RUSAGE_SELF)
225 return t, u.ru_utime, u.ru_stime
226
227
228def PrintTimes():
229 # type: () -> None
230 utime, stime, cutime, cstime, elapsed = posix.times()
231 print("%dm%.3fs %dm%.3fs" %
232 (utime / 60, utime % 60, stime / 60, stime % 60))
233 print("%dm%.3fs %dm%.3fs" %
234 (cutime / 60, cutime % 60, cstime / 60, cstime % 60))
235
236
237# So builtin_misc.py doesn't depend on termios, which makes C++ translation
238# easier
239TERM_ICANON = termios.ICANON
240TERM_ECHO = termios.ECHO
241
242
243def PushTermAttrs(fd, mask):
244 # type: (int, int) -> Tuple[int, Any]
245 """Returns opaque type (void* in C++) to be reused in the PopTermAttrs()"""
246 # https://docs.python.org/2/library/termios.html
247 term_attrs = termios.tcgetattr(fd)
248
249 # Flip the bits in one field, e.g. ICANON to disable canonical (buffered)
250 # mode.
251 orig_local_modes = cast(int, term_attrs[3])
252 term_attrs[3] = orig_local_modes & mask
253
254 termios.tcsetattr(fd, termios.TCSANOW, term_attrs)
255 return orig_local_modes, term_attrs
256
257
258def PopTermAttrs(fd, orig_local_modes, term_attrs):
259 # type: (int, int, Any) -> None
260
261 term_attrs[3] = orig_local_modes
262 try:
263 termios.tcsetattr(fd, termios.TCSANOW, term_attrs)
264 except termios.error as e:
265 # Superficial fix for issue #1001. I'm not sure why we get errno.EIO,
266 # but we can't really handle it here. In C++ I guess we ignore the
267 # error.
268 pass
269
270
271def OsType():
272 # type: () -> str
273 """Compute $OSTYPE variable."""
274 return posix.uname()[0].lower()
275
276
277def InputAvailable(fd):
278 # type: (int) -> bool
279 # similar to lib/sh/input_avail.c in bash
280 # read, write, except
281 r, w, exc = select.select([fd], [], [fd], 0)
282 return len(r) != 0
283
284
285def MakeDirCacheKey(path):
286 # type: (str) -> Tuple[str, int]
287 """Returns a pair (path with last modified time) that can be used to cache
288 directory accesses."""
289 st = posix.stat(path)
290 return (path, int(st.st_mtime))