blob: d0e8dd5fe930105085a283e7aa38dc00e1582a99 [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"""
Georg Brandl208b00d2009-04-25 15:11:29 +000021from warnings import warnpy3k
22warnpy3k("the commands module has been removed in Python 3.0; "
23 "use the subprocess module instead", stacklevel=2)
24del warnpy3k
Fred Drakebcdb9401997-06-12 16:17:00 +000025
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000026__all__ = ["getstatusoutput","getoutput","getstatus"]
27
Guido van Rossumc6360141990-10-13 19:23:40 +000028# Module 'commands'
29#
30# Various tools for executing commands and looking at their output and status.
Guido van Rossume58f98b1992-03-31 19:02:55 +000031#
32# NB This only works (and is only relevant) for UNIX.
Guido van Rossumc6360141990-10-13 19:23:40 +000033
Guido van Rossumc6360141990-10-13 19:23:40 +000034
35# Get 'ls -l' status for an object into a string
36#
37def getstatus(file):
Fred Drakebcdb9401997-06-12 16:17:00 +000038 """Return output of "ls -ld <file>" in a string."""
Georg Brandl8044e5b2007-03-13 21:32:01 +000039 import warnings
Philip Jenveyd846f1d2009-05-08 02:28:39 +000040 warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2)
Fred Drakebcdb9401997-06-12 16:17:00 +000041 return getoutput('ls -ld' + mkarg(file))
Guido van Rossumc6360141990-10-13 19:23:40 +000042
43
44# Get the output from a shell command into a string.
45# The exit status is ignored; a trailing newline is stripped.
Guido van Rossum48154be1991-08-16 13:23:29 +000046# Assume the command will work with '{ ... ; } 2>&1' around it..
Guido van Rossumc6360141990-10-13 19:23:40 +000047#
48def getoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000049 """Return output (stdout or stderr) of executing cmd in a shell."""
50 return getstatusoutput(cmd)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +000051
52
53# Ditto but preserving the exit status.
54# Returns a pair (sts, output)
55#
56def getstatusoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000057 """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()
Fred Drake8152d322000-12-12 23:20:45 +000062 if sts is None: sts = 0
Fred Drakebcdb9401997-06-12 16:17:00 +000063 if text[-1:] == '\n': text = text[:-1]
64 return sts, text
Guido van Rossumc6360141990-10-13 19:23:40 +000065
66
Guido van Rossumc6360141990-10-13 19:23:40 +000067# Make command argument from directory and pathname (prefix space, add quotes).
68#
69def mk2arg(head, x):
Fred Drakebcdb9401997-06-12 16:17:00 +000070 import os
71 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000072
73
74# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000075# Return a string beginning with a space followed by a shell-quoted
76# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000077# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000078# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000079# with backslash.
80#
81def mkarg(x):
Fred Drakebcdb9401997-06-12 16:17:00 +000082 if '\'' not in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000083 return ' \'' + x + '\''
Fred Drakebcdb9401997-06-12 16:17:00 +000084 s = ' "'
85 for c in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000086 if c in '\\$"`':
87 s = s + '\\'
88 s = s + c
Fred Drakebcdb9401997-06-12 16:17:00 +000089 s = s + '"'
90 return s