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