blob: 567248e405690f932ed4bad8a0a4ef97de28ad5b [file] [log] [blame]
Fred Drakebcdb9401997-06-12 16:17:00 +00001"""Execute shell commands via os.popen() and return status, output.
2
3Interface summary:
4
5 import commands
6
7 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:
14
15 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 Rossumc6360141990-10-13 19:23:40 +000022# Module 'commands'
23#
24# Various tools for executing commands and looking at their output and status.
Guido van Rossume58f98b1992-03-31 19:02:55 +000025#
26# NB This only works (and is only relevant) for UNIX.
Guido van Rossumc6360141990-10-13 19:23:40 +000027
Guido van Rossumc6360141990-10-13 19:23:40 +000028
29# Get 'ls -l' status for an object into a string
30#
31def getstatus(file):
Fred Drakebcdb9401997-06-12 16:17:00 +000032 """Return output of "ls -ld <file>" in a string."""
33 return getoutput('ls -ld' + mkarg(file))
Guido van Rossumc6360141990-10-13 19:23:40 +000034
35
36# Get the output from a shell command into a string.
37# The exit status is ignored; a trailing newline is stripped.
Guido van Rossum48154be1991-08-16 13:23:29 +000038# Assume the command will work with '{ ... ; } 2>&1' around it..
Guido van Rossumc6360141990-10-13 19:23:40 +000039#
40def getoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000041 """Return output (stdout or stderr) of executing cmd in a shell."""
42 return getstatusoutput(cmd)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +000043
44
45# Ditto but preserving the exit status.
46# Returns a pair (sts, output)
47#
48def getstatusoutput(cmd):
Fred Drakebcdb9401997-06-12 16:17:00 +000049 """Return (status, output) of executing cmd in a shell."""
50 import os
51 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
52 text = pipe.read()
53 sts = pipe.close()
54 if sts == None: sts = 0
55 if text[-1:] == '\n': text = text[:-1]
56 return sts, text
Guido van Rossumc6360141990-10-13 19:23:40 +000057
58
Guido van Rossumc6360141990-10-13 19:23:40 +000059# Make command argument from directory and pathname (prefix space, add quotes).
60#
61def mk2arg(head, x):
Fred Drakebcdb9401997-06-12 16:17:00 +000062 import os
63 return mkarg(os.path.join(head, x))
Guido van Rossumc6360141990-10-13 19:23:40 +000064
65
66# Make a shell command argument from a string.
Guido van Rossume1130a41995-01-04 19:20:00 +000067# Return a string beginning with a space followed by a shell-quoted
68# version of the argument.
Guido van Rossumc6360141990-10-13 19:23:40 +000069# Two strategies: enclose in single quotes if it contains none;
Guido van Rossum48154be1991-08-16 13:23:29 +000070# otherwise, enclose in double quotes and prefix quotable characters
Guido van Rossumc6360141990-10-13 19:23:40 +000071# with backslash.
72#
73def mkarg(x):
Fred Drakebcdb9401997-06-12 16:17:00 +000074 if '\'' not in x:
75 return ' \'' + x + '\''
76 s = ' "'
77 for c in x:
78 if c in '\\$"`':
79 s = s + '\\'
80 s = s + c
81 s = s + '"'
82 return s