@@ -5,7 +5,9 @@ package os
55// license that can be found in the LICENSE file.
66
77import (
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+ }
0 commit comments