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