blob: 287aa27f4dd3d1fb10a5daae57df0600c3d0743a [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
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000022__all__ = ["getstatusoutput","getoutput","getstatus"]
23
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
31# Get 'ls -l' status for an object into a string
32#
33def getstatus(file):
Fred Drakebcdb9401997-06-12 16:17:00 +000034 """Return output of "ls -ld <file>" in a string."""
Georg Brandl8044e5b2007-03-13 21:32:01 +000035 import warnings
36 warnings.warn("commands.getstatus() is deprecated", DeprecationWarning)
Fred Drakebcdb9401997-06-12 16:17:00 +000037 return getoutput('ls -ld' + mkarg(file))
Guido van Rossumc6360141990-10-13 19:23:40 +000038
39
40# Get the output from a shell command into a string.
41# The exit status is ignored; a trailing newline is stripped.
Guido van Rossum48154be1991-08-16 13:23:29 +000042# Assume the command will work with '{ ... ; } 2>&1' around it..
Guido van Rossumc6360141990-10-13 19:23:40 +000043#
44def getoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000045 """Return output (stdout or stderr) of executing cmd in a shell."""
46 return getstatusoutput(cmd)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +000047
48
49# Ditto but preserving the exit status.
50# Returns a pair (sts, output)
51#
52def getstatusoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000053 """Return (status, output) of executing cmd in a shell."""
54 import os
55 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
56 text = pipe.read()
57 sts = pipe.close()
Fred Drake8152d322000-12-12 23:20:45 +000058 if sts is None: sts = 0
Fred Drakebcdb9401997-06-12 16:17:00 +000059 if text[-1:] == '\n': text = text[:-1]
60 return sts, text
Guido van Rossumc6360141990-10-13 19:23:40 +000061
62
Guido van Rossumc6360141990-10-13 19:23:40 +000063# Make command argument from directory and pathname (prefix space, add quotes).
64#
65def mk2arg(head, x):
Benjamin Peterson3aa84a72008-05-26 19:41:53 +000066 from warnings import warnpy3k
Benjamin Peterson9171bed2008-05-26 20:41:45 +000067 warnpy3k("In 3.x, mk2arg has been removed.")
Fred Drakebcdb9401997-06-12 16:17:00 +000068 import os
69 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000070
71
72# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000073# Return a string beginning with a space followed by a shell-quoted
74# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000075# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000076# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000077# with backslash.
78#
79def mkarg(x):
Benjamin Peterson3aa84a72008-05-26 19:41:53 +000080 from warnings import warnpy3k
Benjamin Peterson9171bed2008-05-26 20:41:45 +000081 warnpy3k("in 3.x, mkarg has been removed.")
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