annotate Lib/commands.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
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:
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
4
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
5 import commands
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
6
5741
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:
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
14
5741
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 """
53409
1467cbc223e6 #5841: add deprecation py3k warning and notice in the docs for commands module.
Georg Brandl <georg@python.org>
parents: 47339
diff changeset
21 from warnings import warnpy3k
1467cbc223e6 #5841: add deprecation py3k warning and notice in the docs for commands module.
Georg Brandl <georg@python.org>
parents: 47339
diff changeset
22 warnpy3k("the commands module has been removed in Python 3.0; "
1467cbc223e6 #5841: add deprecation py3k warning and notice in the docs for commands module.
Georg Brandl <georg@python.org>
parents: 47339
diff changeset
23 "use the subprocess module instead", stacklevel=2)
1467cbc223e6 #5841: add deprecation py3k warning and notice in the docs for commands module.
Georg Brandl <georg@python.org>
parents: 47339
diff changeset
24 del warnpy3k
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
25
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
26 __all__ = ["getstatusoutput","getoutput","getstatus"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
27
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Module 'commands'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # 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
31 #
7c8abce19798 path -> posixpath
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
32 # NB This only works (and is only relevant) for UNIX.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
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 # Get 'ls -l' status for an object into a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 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
38 """Return output of "ls -ld <file>" in a string."""
41171
dba5a33a0e79 Deprecate commands.getstatus().
Georg Brandl <georg@python.org>
parents: 16070
diff changeset
39 import warnings
53903
a36fa2ce61b1 Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents: 47339
diff changeset
40 warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2)
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
41 return getoutput('ls -ld' + mkarg(file))
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
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 # Get the output from a shell command into a string.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # 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
46 # Assume the command will work with '{ ... ; } 2>&1' around it..
5
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 getoutput(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 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
50 return getstatusoutput(cmd)[1]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # Ditto but preserving the exit status.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # Returns a pair (sts, output)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 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
57 """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
58 import os
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
59 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
60 text = pipe.read()
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
61 sts = pipe.close()
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
62 if sts is None: sts = 0
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
63 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
64 return sts, text
5
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # Make command argument from directory and pathname (prefix space, add quotes).
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 import os
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
71 return mkarg(os.path.join(head, x))
5
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # Make a shell command argument from a string.
2066
000ae642f9b8 Added clarifying comment
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
75 # 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
76 # version of the argument.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 # 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
78 # otherwise, enclose in double quotes and prefix quotable characters
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # with backslash.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 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
82 if '\'' not in x:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7722
diff changeset
83 return ' \'' + x + '\''
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
84 s = ' "'
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
85 for c in x:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7722
diff changeset
86 if c in '\\$"`':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7722
diff changeset
87 s = s + '\\'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7722
diff changeset
88 s = s + c
5741
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
89 s = s + '"'
13846379347d Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents: 2066
diff changeset
90 return s