This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy path__init__.py
More file actions
207 lines (162 loc) · 5.3 KB
/
__init__.py
File metadata and controls
207 lines (162 loc) · 5.3 KB
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
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Miscellaneous operating system interfaces."""
# pylint: disable=g-multiple-import
from '__go__/io/ioutil' import ReadDir
from '__go__/os' import (Chdir, Chmod, Environ, Getpid as getpid, Getwd, Pipe,
ProcAttr, Remove, StartProcess, Stat, Stdout, Stdin,
Stderr, Mkdir)
from '__go__/path/filepath' import Separator
from '__go__/grumpy' import (NewFileFromFD, StartThread, ToNative)
from '__go__/reflect' import MakeSlice
from '__go__/runtime' import GOOS
from '__go__/syscall' import (Close, SYS_FCNTL, Syscall, F_GETFD, Wait4,
WaitStatus, WNOHANG)
from '__go__/sync' import WaitGroup
from '__go__/time' import Second
import _syscall
from os import path
import stat as stat_module
import sys
sep = chr(Separator)
error = OSError # pylint: disable=invalid-name
curdir = '.'
name = 'posix'
environ = {}
for var in Environ():
k, v = var.split('=', 1)
environ[k] = v
def mkdir(path, mode=0o777):
err = Mkdir(path, mode)
if err:
raise OSError(err.Error())
def chdir(path):
err = Chdir(path)
if err:
raise OSError(err.Error())
def chmod(filepath, mode):
# TODO: Support mode flags other than perms.
err = Chmod(filepath, stat(filepath).st_mode & ~0o777 | mode & 0o777)
if err:
raise OSError(err.Error())
def close(fd):
err = Close(fd)
if err:
raise OSError(err.Error())
def fdopen(fd, mode='r'): # pylint: disable=unused-argument
# Ensure this is a valid file descriptor to match CPython behavior.
_, _, err = Syscall(SYS_FCNTL, fd, F_GETFD, 0)
if err:
raise OSError(err.Error())
return NewFileFromFD(fd, None)
def listdir(p):
files, err = ReadDir(p)
if err:
raise OSError(err.Error())
return [x.Name() for x in files]
def getcwd():
dir, err = Getwd()
if err:
raise OSError(err.Error())
return dir
class _Popen(object):
def __init__(self, command, mode):
self.mode = mode
self.result = None
self.r, self.w, err = Pipe()
if err:
raise OSError(err.Error())
attr = ProcAttr.new()
# Create a slice using a reflect.Type returned by ToNative.
# TODO: There should be a cleaner way to create slices in Python.
files_type = ToNative(__frame__(), attr.Files).Type()
files = MakeSlice(files_type, 3, 3).Interface()
if self.mode == 'r':
fd = self.r.Fd()
files[0], files[1], files[2] = Stdin, self.w, Stderr
elif self.mode == 'w':
fd = self.w.Fd()
files[0], files[1], files[2] = self.r, Stdout, Stderr
else:
raise ValueError('invalid popen mode: %r', self.mode)
attr.Files = files
# TODO: There should be a cleaner way to create slices in Python.
args_type = ToNative(__frame__(), StartProcess).Type().In(1)
args = MakeSlice(args_type, 3, 3).Interface()
shell = environ['SHELL']
args[0] = shell
args[1] = '-c'
args[2] = command
self.proc, err = StartProcess(shell, args, attr)
if err:
raise OSError(err.Error())
self.wg = WaitGroup.new()
self.wg.Add(1)
StartThread(self._thread_func)
self.file = NewFileFromFD(fd, self.close)
def _thread_func(self):
self.result = self.proc.Wait()
if self.mode == 'r':
self.w.Close()
self.wg.Done()
def close(self, _):
if self.mode == 'w':
self.w.Close()
self.wg.Wait()
state, err = self.result
if err:
raise OSError(err.Error())
return state.Sys()
def popen(command, mode='r'):
return _Popen(command, mode).file
def remove(filepath):
if stat_module.S_ISDIR(stat(filepath).st_mode):
raise OSError('Operation not permitted: ' + filepath)
err = Remove(filepath)
if err:
raise OSError(err.Error())
def rmdir(filepath):
if not stat_module.S_ISDIR(stat(filepath).st_mode):
raise OSError('Operation not permitted: ' + filepath)
err = Remove(filepath)
if err:
raise OSError(err.Error())
class StatResult(object):
def __init__(self, info):
self._info = info
def st_mode(self):
# TODO: This is an incomplete mode flag. It should include S_IFDIR, etc.
return self._info.Mode()
# TODO: Make this a decorator once they're implemented.
st_mode = property(st_mode)
def st_mtime(self):
return float(self._info.ModTime().UnixNano()) / Second
# TODO: Make this a decorator once they're implemented.
st_mtime = property(st_mtime)
def st_size(self):
return self._info.Size()
# TODO: Make this a decorator once they're implemented.
st_size = property(st_size)
def stat(filepath):
info, err = Stat(filepath)
if err:
raise OSError(err.Error())
return StatResult(info)
unlink = remove
def waitpid(pid, options):
status = WaitStatus.new()
_syscall.invoke(Wait4, pid, status, options, None)
return pid, _encode_wait_result(status)
def _encode_wait_result(status):
return status.Signal() | (status.ExitStatus() << 8)