Skip to content

Commit 1d03c4c

Browse files
committed
added os.system()
1 parent fdec59c commit 1d03c4c

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

main

90.5 KB
Binary file not shown.

os/os.module.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package os
55
// license that can be found in the LICENSE file.
66

77
import (
8+
"fmt"
89
"os"
10+
"os/exec"
911
"reflect"
1012
"strings"
1113

@@ -22,6 +24,7 @@ func init() {
2224
py.MustNewMethod("putenv", putenv, 0, "Set the environment variable named key to the string value."),
2325
py.MustNewMethod("unsetenv", unsetenv, 0, "Unset (delete) the environment variable named key."),
2426
py.MustNewMethod("_exit", _exit, 0, "Immediate program termination."),
27+
py.MustNewMethod("system", system, 0, "Run shell commands, prints stdout directly to deault"),
2528
}
2629

2730
globals := py.StringDict{
@@ -192,3 +195,30 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
192195
return nil, nil
193196
}
194197
}
198+
199+
// os.system(command string) this function runs a shell command and directs the output to standard output.
200+
func system(self py.Object, args py.Tuple) (py.Object, error) {
201+
if len(args) == 0 {
202+
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 or more arguments all of type string")
203+
} else {
204+
var cargs []string
205+
if reflect.TypeOf(args[0]).String() == "py.String" {
206+
_carg, err := py.ReprAsString(args[0])
207+
if err != nil {
208+
return nil, py.ExceptionNewf(py.TypeError, "Unable to parse string") // this will never execute
209+
}
210+
carg := strings.ReplaceAll(_carg, "'", "") // required
211+
212+
cargs = strings.Split(carg, " ")
213+
} else {
214+
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 or more arguments all of type string")
215+
}
216+
command := exec.Command(cargs[0])
217+
outb, err := command.Output()
218+
if err != nil {
219+
return nil, py.ExceptionNewf(py.OSError, err.Error())
220+
}
221+
fmt.Println(string(outb))
222+
return py.Int(0), nil
223+
}
224+
}

os/os.test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23

34
class bcolors:
45
HEADER = '\033[95m'
@@ -22,6 +23,7 @@ class bcolors:
2223
print('getenv("TEST_VAR"):', os.getenv("TEST_VAR")) # get TEST_VAR value
2324
print('unsetenv("TEST_VAR"):', os.unsetenv("TEST_VAR")) # unset TEST_VAR
2425
print('getenv("TEST_VAR"):',os.getenv("TEST_VAR")) # get TEST_VAR after it has been deleted
26+
print('system("ls .")', os.system("ls ."))
2527
print()
2628
print("--------------")
2729
print()
@@ -106,6 +108,18 @@ def _fail(test_name):
106108
_fail('os.getenv("TEST_VAR") == None')
107109
failed+=1
108110

111+
print(bcolors.ENDC+bcolors.BOLD+bcolors.HEADER+"Please check if $HOME will be outputted...")
112+
print(bcolors.ENDC)
113+
114+
if os.system("pwd") == 0:
115+
_pass('os.system("pwd")')
116+
passed_tests+=1
117+
else:
118+
_fail('os.system("pwd")')
119+
failed+=1
120+
121+
time.sleep(2)
122+
109123
print()
110124
print(bcolors.ENDC+bcolors.BOLD+bcolors.HEADER+"----- Results -----")
111125
print(bcolors.OKGREEN+bcolors.BOLD, "PASSED: "+str(passed_tests)+" out of "+str(passed_tests+failed))

0 commit comments

Comments
 (0)