-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathos.go
576 lines (498 loc) · 15.3 KB
/
os.go
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package os implements the Python os module.
package os
import (
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"github.com/go-python/gpython/py"
)
var (
osSep = py.String("/")
osName = py.String("posix")
osPathsep = py.String(":")
osLinesep = py.String("\n")
osDefpath = py.String(":/bin:/usr/bin")
osDevnull = py.String("/dev/null")
osAltsep py.Object = py.None
)
func initGlobals() {
switch runtime.GOOS {
case "android":
osName = py.String("java")
case "windows":
osSep = py.String(`\`)
osName = py.String("nt")
osPathsep = py.String(";")
osLinesep = py.String("\r\n")
osDefpath = py.String(`C:\bin`)
osDevnull = py.String("nul")
osAltsep = py.String("/")
}
}
func init() {
initGlobals()
methods := []*py.Method{
py.MustNewMethod("_exit", _exit, 0, "Immediate program termination."),
py.MustNewMethod("close", closefd, 0, closefd_doc),
py.MustNewMethod("fdopen", fdopen, 0, fdopen_doc),
py.MustNewMethod("getcwd", getCwd, 0, "Get the current working directory"),
py.MustNewMethod("getcwdb", getCwdb, 0, "Get the current working directory in a byte slice"),
py.MustNewMethod("chdir", chdir, 0, "Change the current working directory"),
py.MustNewMethod("getenv", getenv, 0, "Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are str."),
py.MustNewMethod("getpid", getpid, 0, "Return the current process id."),
py.MustNewMethod("listdir", listDir, 0, listDir_doc),
py.MustNewMethod("makedirs", makedirs, 0, makedirs_doc),
py.MustNewMethod("mkdir", mkdir, 0, mkdir_doc),
py.MustNewMethod("putenv", putenv, 0, "Set the environment variable named key to the string value."),
py.MustNewMethod("remove", remove, 0, remove_doc),
py.MustNewMethod("removedirs", removedirs, 0, removedirs_doc),
py.MustNewMethod("rmdir", rmdir, 0, rmdir_doc),
py.MustNewMethod("system", system, 0, "Run shell commands, prints stdout directly to default"),
py.MustNewMethod("unsetenv", unsetenv, 0, "Unset (delete) the environment variable named key."),
}
globals := py.StringDict{
"error": py.OSError,
"environ": getEnvVariables(),
"sep": osSep,
"name": osName,
"curdir": py.String("."),
"pardir": py.String(".."),
"extsep": py.String("."),
"altsep": osAltsep,
"pathsep": osPathsep,
"linesep": osLinesep,
"defpath": osDefpath,
"devnull": osDevnull,
}
py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "os",
Doc: "Miscellaneous operating system interfaces",
},
Methods: methods,
Globals: globals,
})
}
// getEnvVariables returns the dictionary of environment variables.
func getEnvVariables() py.StringDict {
vs := os.Environ()
dict := py.NewStringDictSized(len(vs))
for _, evar := range vs {
key_value := strings.SplitN(evar, "=", 2) // returns a []string containing [key,value]
dict.M__setitem__(py.String(key_value[0]), py.String(key_value[1]))
}
return dict
}
const closefd_doc = `Close a file descriptor`
func closefd(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pyfd py.Object
)
err := py.ParseTupleAndKeywords(args, kwargs, "i", []string{"fd"}, &pyfd)
if err != nil {
return nil, err
}
var (
fd = uintptr(pyfd.(py.Int))
name = strconv.Itoa(int(fd))
)
f := os.NewFile(fd, name)
if f == nil {
return nil, py.ExceptionNewf(py.OSError, "Bad file descriptor")
}
err = f.Close()
if err != nil {
return nil, err
}
return py.None, nil
}
const fdopen_doc = `# Supply os.fdopen()`
func fdopen(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pyfd py.Object
pymode py.Object = py.String("r")
pybuffering py.Object = py.Int(-1)
pyencoding py.Object = py.None
)
err := py.ParseTupleAndKeywords(
args, kwargs,
"i|s#is#", []string{"fd", "mode", "buffering", "encoding"},
&pyfd, &pymode, &pybuffering, &pyencoding,
)
if err != nil {
return nil, err
}
// FIXME(sbinet): handle buffering
// FIXME(sbinet): handle encoding
var (
fd = uintptr(pyfd.(py.Int))
name = strconv.Itoa(int(fd))
mode string
)
switch v := pymode.(type) {
case py.String:
mode = string(v)
case py.Bytes:
mode = string(v)
}
perm, _, _, err := py.FileModeFrom(mode)
if err != nil {
return nil, err
}
f := os.NewFile(fd, name)
if f == nil {
return nil, py.ExceptionNewf(py.OSError, "Bad file descriptor")
}
return &py.File{f, perm}, nil
}
// getCwd returns the current working directory.
func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
dir, err := os.Getwd()
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "Unable to get current working directory.")
}
return py.String(dir), nil
}
// getCwdb returns the current working directory as a byte list.
func getCwdb(self py.Object, args py.Tuple) (py.Object, error) {
dir, err := os.Getwd()
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "Unable to get current working directory.")
}
return py.Bytes(dir), nil
}
// chdir changes the current working directory to the provided path.
func chdir(self py.Object, args py.Tuple) (py.Object, error) {
if len(args) == 0 {
return nil, py.ExceptionNewf(py.TypeError, "Missing required argument 'path' (pos 1)")
}
dir, ok := args[0].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected, not "+args[0].Type().Name)
}
err := os.Chdir(string(dir))
if err != nil {
return nil, py.ExceptionNewf(py.NotADirectoryError, "Couldn't change cwd; "+err.Error())
}
return py.None, nil
}
// getenv returns the value of the environment variable key.
// If no such environment variable exists and a default value was provided, that value is returned.
func getenv(self py.Object, args py.Tuple) (py.Object, error) {
if len(args) < 1 {
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'name:str'")
}
k, ok := args[0].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
}
v, ok := os.LookupEnv(string(k))
if ok {
return py.String(v), nil
}
if len(args) == 2 {
return args[1], nil
}
return py.None, nil
}
// getpid returns the current process id.
func getpid(self py.Object, args py.Tuple) (py.Object, error) {
return py.Int(os.Getpid()), nil
}
const listDir_doc = `
Return a list containing the names of the files in the directory.
path can be specified as either str, bytes. If path is bytes, the filenames
returned will also be bytes; in all other circumstances
the filenames returned will be str.
If path is None, uses the path='.'.
The list is in arbitrary order. It does not include the special
entries '.' and '..' even if they are present in the directory.
`
func listDir(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
path py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs, "|z*:listdir", []string{"path"}, &path)
if err != nil {
return nil, err
}
if path == py.None {
cwd, err := os.Getwd()
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "cannot get cwd, error %s", err.Error())
}
path = py.String(cwd)
}
dirName := ""
returnsBytes := false
switch v := path.(type) {
case py.String:
dirName = string(v)
case py.Bytes:
dirName = string(v)
returnsBytes = true
default:
return nil, py.ExceptionNewf(py.TypeError, "str or bytes expected, not %T", path)
}
dirEntries, err := os.ReadDir(dirName)
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "cannot read directory %s, error %s", dirName, err.Error())
}
result := py.NewListSized(len(dirEntries))
for i, dirEntry := range dirEntries {
if returnsBytes {
result.Items[i] = py.Bytes(dirEntry.Name())
} else {
result.Items[i] = py.String(dirEntry.Name())
}
}
return result, nil
}
const makedirs_doc = `makedirs(name [, mode=0o777][, exist_ok=False])
Super-mkdir; create a leaf directory and all intermediate ones. Works like
mkdir, except that any intermediate path segment (not just the rightmost)
will be created if it does not exist. If the target directory already
exists, raise an OSError if exist_ok is False. Otherwise no exception is
raised. This is recursive.`
func makedirs(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pypath py.Object
pymode py.Object = py.Int(0o777)
pyok py.Object = py.False
)
err := py.ParseTupleAndKeywords(
args, kwargs,
"s#|ip:makedirs", []string{"path", "mode", "exist_ok"},
&pypath, &pymode, &pyok,
)
if err != nil {
return nil, err
}
var (
path = ""
mode = os.FileMode(pymode.(py.Int))
)
switch v := pypath.(type) {
case py.String:
path = string(v)
case py.Bytes:
path = string(v)
}
if pyok.(py.Bool) == py.False {
// check if leaf exists.
_, err := os.Stat(path)
// FIXME(sbinet): handle other errors.
if err == nil {
return nil, py.ExceptionNewf(py.FileExistsError, "File exists: '%s'", path)
}
}
err = os.MkdirAll(path, mode)
if err != nil {
return nil, err
}
return py.None, nil
}
const mkdir_doc = `Create a directory.
If dir_fd is not None, it should be a file descriptor open to a directory,
and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.
The mode argument is ignored on Windows.`
func mkdir(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pypath py.Object
pymode py.Object = py.Int(511)
pydirfd py.Object = py.None
)
err := py.ParseTupleAndKeywords(
args, kwargs,
"s#|ii:mkdir", []string{"path", "mode", "dir_fd"},
&pypath, &pymode, &pydirfd,
)
if err != nil {
return nil, err
}
var (
path = ""
mode = os.FileMode(pymode.(py.Int))
)
switch v := pypath.(type) {
case py.String:
path = string(v)
case py.Bytes:
path = string(v)
}
if pydirfd != py.None {
// FIXME(sbinet)
return nil, py.ExceptionNewf(py.NotImplementedError, "mkdir(dir_fd=XXX) not implemented")
}
err = os.Mkdir(path, mode)
if err != nil {
return nil, err
}
return py.None, nil
}
// putenv sets the value of an environment variable named by the key.
func putenv(self py.Object, args py.Tuple) (py.Object, error) {
if len(args) != 2 {
return nil, py.ExceptionNewf(py.TypeError, "missing required arguments: 'key:str' and 'value:str'")
}
k, ok := args[0].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
}
v, ok := args[1].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 2), not "+args[1].Type().Name)
}
err := os.Setenv(string(k), string(v))
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "Unable to set enviroment variable")
}
return py.None, nil
}
// Unset (delete) the environment variable named key.
func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
if len(args) != 1 {
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'key:str'")
}
k, ok := args[0].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
}
err := os.Unsetenv(string(k))
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "Unable to unset enviroment variable")
}
return py.None, nil
}
// os._exit() immediate program termination; unlike sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never return
if len(args) == 0 {
os.Exit(0)
}
arg, ok := args[0].(py.Int)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "expected int (pos 1), not "+args[0].Type().Name)
}
os.Exit(int(arg))
return nil, nil
}
const remove_doc = `Remove a file (same as unlink()).
If dir_fd is not None, it should be a file descriptor open to a directory,
and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.`
func remove(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pypath py.Object
pydir py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs, "s#|i:remove", []string{"path", "dir_fd"}, &pypath, &pydir)
if err != nil {
return nil, err
}
if pydir != py.None {
// FIXME(sbinet) ?
return nil, py.ExceptionNewf(py.NotImplementedError, "remove(dir_fd=XXX) not implemented")
}
var name string
switch v := pypath.(type) {
case py.String:
name = string(v)
case py.Bytes:
name = string(v)
}
err = os.Remove(name)
if err != nil {
return nil, err
}
return py.None, nil
}
const removedirs_doc = `removedirs(name)
Super-rmdir; remove a leaf directory and all empty intermediate
ones. Works like rmdir except that, if the leaf directory is
successfully removed, directories corresponding to rightmost path
segments will be pruned away until either the whole path is
consumed or an error occurs. Errors during this latter phase are
ignored -- they generally mean that a directory was not empty.`
func removedirs(self py.Object, args py.Tuple) (py.Object, error) {
var pypath py.Object
err := py.ParseTuple(args, "s#:rmdir", &pypath)
if err != nil {
return nil, err
}
var name string
switch v := pypath.(type) {
case py.String:
name = string(v)
case py.Bytes:
name = string(v)
}
err = os.RemoveAll(name)
if err != nil {
return nil, err
}
return py.None, nil
}
const rmdir_doc = `Remove a directory.
If dir_fd is not None, it should be a file descriptor open to a directory,
and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.`
func rmdir(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pypath py.Object
pydir py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs, "s#|i:rmdir", []string{"path", "dir_fd"}, &pypath, &pydir)
if err != nil {
return nil, err
}
if pydir != py.None {
// FIXME(sbinet) ?
return nil, py.ExceptionNewf(py.NotImplementedError, "rmdir(dir_fd=XXX) not implemented")
}
var name string
switch v := pypath.(type) {
case py.String:
name = string(v)
case py.Bytes:
name = string(v)
}
err = os.Remove(name)
if err != nil {
return nil, err
}
return py.None, nil
}
// os.system(command string) this function runs a shell command and directs the output to standard output.
func system(self py.Object, args py.Tuple) (py.Object, error) {
if len(args) != 1 {
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'command:str'")
}
arg, ok := args[0].(py.String)
if !ok {
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
}
var command *exec.Cmd
if runtime.GOOS != "windows" {
command = exec.Command("/bin/sh", "-c", string(arg))
} else {
command = exec.Command("cmd.exe", string(arg))
}
outb, err := command.CombinedOutput() // - commbinedoutput to get both stderr and stdout -
if err != nil {
return nil, py.ExceptionNewf(py.OSError, err.Error())
}
ok = py.Println(self, string(outb))
if !ok {
return py.Int(1), nil
}
return py.Int(0), nil
}