[2] | 1 | """Execute shell commands via os.popen() and return status, output.
|
---|
| 2 |
|
---|
| 3 | Interface summary:
|
---|
| 4 |
|
---|
| 5 | import commands
|
---|
| 6 |
|
---|
| 7 | outtext = commands.getoutput(cmd)
|
---|
| 8 | (exitstatus, outtext) = commands.getstatusoutput(cmd)
|
---|
| 9 | outtext = commands.getstatus(file) # returns output of "ls -ld file"
|
---|
| 10 |
|
---|
| 11 | A trailing newline is removed from the output string.
|
---|
| 12 |
|
---|
| 13 | Encapsulates the basic operation:
|
---|
| 14 |
|
---|
| 15 | pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
|
---|
| 16 | text = pipe.read()
|
---|
| 17 | sts = pipe.close()
|
---|
| 18 |
|
---|
| 19 | [Note: it would be nice to add functions to interpret the exit status.]
|
---|
| 20 | """
|
---|
[391] | 21 | from warnings import warnpy3k
|
---|
| 22 | warnpy3k("the commands module has been removed in Python 3.0; "
|
---|
| 23 | "use the subprocess module instead", stacklevel=2)
|
---|
| 24 | del warnpy3k
|
---|
[2] | 25 |
|
---|
| 26 | __all__ = ["getstatusoutput","getoutput","getstatus"]
|
---|
| 27 |
|
---|
| 28 | # Module 'commands'
|
---|
| 29 | #
|
---|
| 30 | # Various tools for executing commands and looking at their output and status.
|
---|
| 31 | #
|
---|
| 32 | # NB This only works (and is only relevant) for UNIX.
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | # Get 'ls -l' status for an object into a string
|
---|
| 36 | #
|
---|
| 37 | def getstatus(file):
|
---|
| 38 | """Return output of "ls -ld <file>" in a string."""
|
---|
| 39 | import warnings
|
---|
| 40 | warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2)
|
---|
| 41 | return getoutput('ls -ld' + mkarg(file))
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | # Get the output from a shell command into a string.
|
---|
| 45 | # The exit status is ignored; a trailing newline is stripped.
|
---|
| 46 | # Assume the command will work with '{ ... ; } 2>&1' around it..
|
---|
| 47 | #
|
---|
| 48 | def getoutput(cmd):
|
---|
| 49 | """Return output (stdout or stderr) of executing cmd in a shell."""
|
---|
| 50 | return getstatusoutput(cmd)[1]
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | # Ditto but preserving the exit status.
|
---|
| 54 | # Returns a pair (sts, output)
|
---|
| 55 | #
|
---|
| 56 | def getstatusoutput(cmd):
|
---|
| 57 | """Return (status, output) of executing cmd in a shell."""
|
---|
| 58 | import os
|
---|
| 59 | pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
|
---|
| 60 | text = pipe.read()
|
---|
| 61 | sts = pipe.close()
|
---|
| 62 | if sts is None: sts = 0
|
---|
| 63 | if text[-1:] == '\n': text = text[:-1]
|
---|
| 64 | return sts, text
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | # Make command argument from directory and pathname (prefix space, add quotes).
|
---|
| 68 | #
|
---|
| 69 | def mk2arg(head, x):
|
---|
| 70 | import os
|
---|
| 71 | return mkarg(os.path.join(head, x))
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | # Make a shell command argument from a string.
|
---|
| 75 | # Return a string beginning with a space followed by a shell-quoted
|
---|
| 76 | # version of the argument.
|
---|
| 77 | # Two strategies: enclose in single quotes if it contains none;
|
---|
| 78 | # otherwise, enclose in double quotes and prefix quotable characters
|
---|
| 79 | # with backslash.
|
---|
| 80 | #
|
---|
| 81 | def mkarg(x):
|
---|
| 82 | if '\'' not in x:
|
---|
| 83 | return ' \'' + x + '\''
|
---|
| 84 | s = ' "'
|
---|
| 85 | for c in x:
|
---|
| 86 | if c in '\\$"`':
|
---|
| 87 | s = s + '\\'
|
---|
| 88 | s = s + c
|
---|
| 89 | s = s + '"'
|
---|
| 90 | return s
|
---|