3 # Various tools for executing commands and looking at their output and status.
5 # NB This only works (and is only relevant) for UNIX.
8 # Get 'ls -l' status for an object into a string
11 return getoutput('ls -ld' + mkarg(file))
14 # Get the output from a shell command into a string.
15 # The exit status is ignored; a trailing newline is stripped.
16 # Assume the command will work with '{ ... ; } 2>&1' around it..
19 return getstatusoutput(cmd
)[1]
22 # Ditto but preserving the exit status.
23 # Returns a pair (sts, output)
25 def getstatusoutput(cmd
):
27 pipe
= os
.popen('{ ' + cmd
+ '; } 2>&1', 'r')
30 if sts
== None: sts
= 0
31 if text
[-1:] == '\n': text
= text
[:-1]
35 # Make command argument from directory and pathname (prefix space, add quotes).
39 return mkarg(os
.path
.join(head
, x
))
42 # Make a shell command argument from a string.
43 # Two strategies: enclose in single quotes if it contains none;
44 # otherwise, enclose in double quotes and prefix quotable characters
49 return ' \'' + x
+ '\''