blob: ee4db8595acc457135e142d2d505ba5b1707a6ab [file] [log] [blame]
Fred Drakebcdb9401997-06-12 16:17:00 +00001"""Execute shell commands via os.popen() and return status, output.
2
3Interface summary:
Tim Peters88869f92001-01-14 23:36:06 +00004
Fred Drakebcdb9401997-06-12 16:17:00 +00005 import commands
Tim Peters88869f92001-01-14 23:36:06 +00006
Fred Drakebcdb9401997-06-12 16:17:00 +00007 outtext = commands.getoutput(cmd)
8 (exitstatus, outtext) = commands.getstatusoutput(cmd)
9 outtext = commands.getstatus(file) # returns output of "ls -ld file"
10
11A trailing newline is removed from the output string.
12
13Encapsulates the basic operation:
Tim Peters88869f92001-01-14 23:36:06 +000014
Fred Drakebcdb9401997-06-12 16:17:00 +000015 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 Rossum0368b722007-05-11 16:50:42 +000022__all__ = ["getstatusoutput", "getoutput"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000023
Guido van Rossumc6360141990-10-13 19:23:40 +000024# Module 'commands'
25#
26# Various tools for executing commands and looking at their output and status.
Guido van Rossume58f98b1992-03-31 19:02:55 +000027#
28# NB This only works (and is only relevant) for UNIX.
Guido van Rossumc6360141990-10-13 19:23:40 +000029
Guido van Rossumc6360141990-10-13 19:23:40 +000030
Guido van Rossumc6360141990-10-13 19:23:40 +000031# Get the output from a shell command into a string.
32# The exit status is ignored; a trailing newline is stripped.
Guido van Rossum48154be1991-08-16 13:23:29 +000033# Assume the command will work with '{ ... ; } 2>&1' around it..
Guido van Rossumc6360141990-10-13 19:23:40 +000034#
35def getoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000036 """Return output (stdout or stderr) of executing cmd in a shell."""
37 return getstatusoutput(cmd)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +000038
39
40# Ditto but preserving the exit status.
41# Returns a pair (sts, output)
42#
43def getstatusoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000044 """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 Drake8152d322000-12-12 23:20:45 +000049 if sts is None: sts = 0
Fred Drakebcdb9401997-06-12 16:17:00 +000050 if text[-1:] == '\n': text = text[:-1]
51 return sts, text
Guido van Rossumc6360141990-10-13 19:23:40 +000052
53
Guido van Rossumc6360141990-10-13 19:23:40 +000054# Make command argument from directory and pathname (prefix space, add quotes).
55#
56def mk2arg(head, x):
Fred Drakebcdb9401997-06-12 16:17:00 +000057 import os
58 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000059
60
61# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000062# Return a string beginning with a space followed by a shell-quoted
63# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000064# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000065# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000066# with backslash.
67#
68def mkarg(x):
Fred Drakebcdb9401997-06-12 16:17:00 +000069 if '\'' not in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 return ' \'' + x + '\''
Fred Drakebcdb9401997-06-12 16:17:00 +000071 s = ' "'
72 for c in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000073 if c in '\\$"`':
74 s = s + '\\'
75 s = s + c
Fred Drakebcdb9401997-06-12 16:17:00 +000076 s = s + '"'
77 return s