Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcdb940

Browse files
committed
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
1 parent 8f81ef1 commit bcdb940

1 file changed

Lines changed: 44 additions & 20 deletions

File tree

Lib/commands.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
"""Execute shell commands via os.popen() and return status, output.
2+
3+
Interface 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+
11+
A trailing newline is removed from the output string.
12+
13+
Encapsulates 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+
122
# Module 'commands'
223
#
324
# Various tools for executing commands and looking at their output and status.
@@ -8,35 +29,38 @@
829
# Get 'ls -l' status for an object into a string
930
#
1031
def getstatus(file):
11-
return getoutput('ls -ld' + mkarg(file))
32+
"""Return output of "ls -ld <file>" in a string."""
33+
return getoutput('ls -ld' + mkarg(file))
1234

1335

1436
# Get the output from a shell command into a string.
1537
# The exit status is ignored; a trailing newline is stripped.
1638
# Assume the command will work with '{ ... ; } 2>&1' around it..
1739
#
1840
def getoutput(cmd):
19-
return getstatusoutput(cmd)[1]
41+
"""Return output (stdout or stderr) of executing cmd in a shell."""
42+
return getstatusoutput(cmd)[1]
2043

2144

2245
# Ditto but preserving the exit status.
2346
# Returns a pair (sts, output)
2447
#
2548
def getstatusoutput(cmd):
26-
import os
27-
pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
28-
text = pipe.read()
29-
sts = pipe.close()
30-
if sts == None: sts = 0
31-
if text[-1:] == '\n': text = text[:-1]
32-
return sts, text
49+
"""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
3357

3458

3559
# Make command argument from directory and pathname (prefix space, add quotes).
3660
#
3761
def mk2arg(head, x):
38-
import os
39-
return mkarg(os.path.join(head, x))
62+
import os
63+
return mkarg(os.path.join(head, x))
4064

4165

4266
# Make a shell command argument from a string.
@@ -47,12 +71,12 @@ def mk2arg(head, x):
4771
# with backslash.
4872
#
4973
def mkarg(x):
50-
if '\'' not in x:
51-
return ' \'' + x + '\''
52-
s = ' "'
53-
for c in x:
54-
if c in '\\$"`':
55-
s = s + '\\'
56-
s = s + c
57-
s = s + '"'
58-
return s
74+
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

0 commit comments

Comments
 (0)