blob: cfbb541cfd61ac27993b7755f3e2126d81b136b7 [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."""
35 return getoutput('ls -ld' + mkarg(file))
Guido van Rossumc6360141990-10-13 19:23:40 +000036
37
38# Get the output from a shell command into a string.
39# The exit status is ignored; a trailing newline is stripped.
Guido van Rossum48154be1991-08-16 13:23:29 +000040# Assume the command will work with '{ ... ; } 2>&1' around it..
Guido van Rossumc6360141990-10-13 19:23:40 +000041#
42def getoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000043 """Return output (stdout or stderr) of executing cmd in a shell."""
44 return getstatusoutput(cmd)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +000045
46
47# Ditto but preserving the exit status.
48# Returns a pair (sts, output)
49#
50def getstatusoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000051 """Return (status, output) of executing cmd in a shell."""
52 import os
53 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
54 text = pipe.read()
55 sts = pipe.close()
Fred Drake8152d322000-12-12 23:20:45 +000056 if sts is None: sts = 0
Fred Drakebcdb9401997-06-12 16:17:00 +000057 if text[-1:] == '\n': text = text[:-1]
58 return sts, text
Guido van Rossumc6360141990-10-13 19:23:40 +000059
60
Guido van Rossumc6360141990-10-13 19:23:40 +000061# Make command argument from directory and pathname (prefix space, add quotes).
62#
63def mk2arg(head, x):
Fred Drakebcdb9401997-06-12 16:17:00 +000064 import os
65 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000066
67
68# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000069# Return a string beginning with a space followed by a shell-quoted
70# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000071# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000072# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000073# with backslash.
74#
75def mkarg(x):
Fred Drakebcdb9401997-06-12 16:17:00 +000076 if '\'' not in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 return ' \'' + x + '\''
Fred Drakebcdb9401997-06-12 16:17:00 +000078 s = ' "'
79 for c in x:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 if c in '\\$"`':
81 s = s + '\\'
82 s = s + c
Fred Drakebcdb9401997-06-12 16:17:00 +000083 s = s + '"'
84 return s