Mercurial > cpython-lru_cache-weakref
annotate Lib/commands.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| 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 | 28 # Module 'commands' |
| 29 # | |
| 30 # Various tools for executing commands and looking at their output and status. | |
| 774 | 31 # |
| 32 # NB This only works (and is only relevant) for UNIX. | |
| 5 | 33 |
| 34 | |
| 35 # Get 'ls -l' status for an object into a string | |
| 36 # | |
| 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 | 42 |
| 43 | |
| 44 # Get the output from a shell command into a string. | |
| 45 # The exit status is ignored; a trailing newline is stripped. | |
| 356 | 46 # Assume the command will work with '{ ... ; } 2>&1' around it.. |
| 5 | 47 # |
| 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 | 51 |
| 52 | |
| 53 # Ditto but preserving the exit status. | |
| 54 # Returns a pair (sts, output) | |
| 55 # | |
| 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 | 65 |
| 66 | |
| 67 # Make command argument from directory and pathname (prefix space, add quotes). | |
| 68 # | |
| 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 | 72 |
| 73 | |
| 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 | 77 # Two strategies: enclose in single quotes if it contains none; |
| 356 | 78 # otherwise, enclose in double quotes and prefix quotable characters |
| 5 | 79 # with backslash. |
| 80 # | |
| 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 |
