annotate Lib/commands.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 13846379347d
children 67318d3fa6dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
1 """Execute shell commands via os.popen() and return status, output.
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
2
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
3 Interface summary:
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
4
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
5 import commands
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
6
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
7 outtext = commands.getoutput(cmd)
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
8 (exitstatus, outtext) = commands.getstatusoutput(cmd)
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
9 outtext = commands.getstatus(file) # returns output of "ls -ld file"
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
10
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
11 A trailing newline is removed from the output string.
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
12
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
13 Encapsulates the basic operation:
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
14
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
15 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
16 text = pipe.read()
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
17 sts = pipe.close()
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
18
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
19 [Note: it would be nice to add functions to interpret the exit status.]
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
20 """
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
21
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # Module 'commands'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # Various tools for executing commands and looking at their output and status.
774
7c8abce19798 path -> posixpath
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
25 #
7c8abce19798 path -> posixpath
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
26 # NB This only works (and is only relevant) for UNIX.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # Get 'ls -l' status for an object into a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def getstatus(file):
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
32 """Return output of "ls -ld <file>" in a string."""
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
33 return getoutput('ls -ld' + mkarg(file))
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # Get the output from a shell command into a string.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # The exit status is ignored; a trailing newline is stripped.
356
ab05efed03e9 Now uses pipes.
Guido van Rossum <guido@python.org>
parents: 30
diff changeset
38 # Assume the command will work with '{ ... ; } 2>&1' around it..
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 def getoutput(cmd):
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
41 """Return output (stdout or stderr) of executing cmd in a shell."""
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
42 return getstatusoutput(cmd)[1]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # Ditto but preserving the exit status.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # Returns a pair (sts, output)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def getstatusoutput(cmd):
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
49 """Return (status, output) of executing cmd in a shell."""
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
50 import os
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
51 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
52 text = pipe.read()
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
53 sts = pipe.close()
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
54 if sts == None: sts = 0
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
55 if text[-1:] == '\n': text = text[:-1]
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
56 return sts, text
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # Make command argument from directory and pathname (prefix space, add quotes).
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def mk2arg(head, x):
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
62 import os
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
63 return mkarg(os.path.join(head, x))
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # Make a shell command argument from a string.
2066
000ae642f9b8 Added clarifying comment
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
67 # Return a string beginning with a space followed by a shell-quoted
000ae642f9b8 Added clarifying comment
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
68 # version of the argument.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # Two strategies: enclose in single quotes if it contains none;
356
ab05efed03e9 Now uses pipes.
Guido van Rossum <guido@python.org>
parents: 30
diff changeset
70 # otherwise, enclose in double quotes and prefix quotable characters
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # with backslash.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def mkarg(x):
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
74 if '\'' not in x:
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
75 return ' \'' + x + '\''
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
76 s = ' "'
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
77 for c in x:
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
78 if c in '\\$"`':
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
79 s = s + '\\'
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
80 s = s + c
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
81 s = s + '"'
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
82 return s