blob: d19aa1a480ac3374cbc333f132c60fcca8043b61 [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."""
Guido van Rossumd8faa362007-04-27 19:54:29 +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):
Fred Drakebcdb9401997-06-12 16:17:00 +000066 import os
67 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000068
69
70# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000071# Return a string beginning with a space followed by a shell-quoted
72# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000073# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000074# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000075# with backslash.
76#
77def mkarg(x):
Fred Drakebcdb9401997-06-12 16:17:00 +000078 if '\'' not in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 return ' \'' + x + '\''
Fred Drakebcdb9401997-06-12 16:17:00 +000080 s = ' "'
81 for c in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000082 if c in '\\$"`':
83 s = s + '\\'
84 s = s + c
Fred Drakebcdb9401997-06-12 16:17:00 +000085 s = s + '"'
86 return s