Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 1 | """Execute shell commands via os.popen() and return status, output. |
| 2 | |
| 3 | Interface summary: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 4 | |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 5 | import commands |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 6 | |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 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: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 14 | |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 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 | """ |
| 21 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 22 | __all__ = ["getstatusoutput","getoutput","getstatus"] |
| 23 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 24 | # Module 'commands' |
| 25 | # |
| 26 | # Various tools for executing commands and looking at their output and status. |
Guido van Rossum | e58f98b | 1992-03-31 19:02:55 +0000 | [diff] [blame] | 27 | # |
| 28 | # NB This only works (and is only relevant) for UNIX. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 30 | |
| 31 | # Get 'ls -l' status for an object into a string |
| 32 | # |
| 33 | def getstatus(file): |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 34 | """Return output of "ls -ld <file>" in a string.""" |
| 35 | return getoutput('ls -ld' + mkarg(file)) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | # Get the output from a shell command into a string. |
| 39 | # The exit status is ignored; a trailing newline is stripped. |
Guido van Rossum | 48154be | 1991-08-16 13:23:29 +0000 | [diff] [blame] | 40 | # Assume the command will work with '{ ... ; } 2>&1' around it.. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 41 | # |
| 42 | def getoutput(cmd): |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 43 | """Return output (stdout or stderr) of executing cmd in a shell.""" |
| 44 | return getstatusoutput(cmd)[1] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | # Ditto but preserving the exit status. |
| 48 | # Returns a pair (sts, output) |
| 49 | # |
| 50 | def getstatusoutput(cmd): |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 51 | """Return (status, output) of executing cmd in a shell.""" |
| 52 | import os |
| 53 | pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') |
| 54 | text = pipe.read() |
| 55 | sts = pipe.close() |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 56 | if sts is None: sts = 0 |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 57 | if text[-1:] == '\n': text = text[:-1] |
| 58 | return sts, text |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 59 | |
| 60 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 61 | # Make command argument from directory and pathname (prefix space, add quotes). |
| 62 | # |
| 63 | def mk2arg(head, x): |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 64 | import os |
| 65 | return mkarg(os.path.join(head, x)) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | # Make a shell command argument from a string. |
Guido van Rossum | e1130a4 | 1995-01-04 19:20:00 +0000 | [diff] [blame] | 69 | # Return a string beginning with a space followed by a shell-quoted |
| 70 | # version of the argument. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 71 | # Two strategies: enclose in single quotes if it contains none; |
Guido van Rossum | 48154be | 1991-08-16 13:23:29 +0000 | [diff] [blame] | 72 | # otherwise, enclose in double quotes and prefix quotable characters |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 73 | # with backslash. |
| 74 | # |
| 75 | def mkarg(x): |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 76 | if '\'' not in x: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | return ' \'' + x + '\'' |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 78 | s = ' "' |
| 79 | for c in x: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | if c in '\\$"`': |
| 81 | s = s + '\\' |
| 82 | s = s + c |
Fred Drake | bcdb940 | 1997-06-12 16:17:00 +0000 | [diff] [blame] | 83 | s = s + '"' |
| 84 | return s |