Mercurial > cpython-lru_cache-weakref
annotate Lib/commands.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | a36fa2ce61b1 |
| children | b36cb4602e21 |
| 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 """ |
|
13846379347d
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents:
2066
diff
changeset
|
21 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
22 __all__ = ["getstatusoutput","getoutput","getstatus"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
23 |
| 5 | 24 # Module 'commands' |
| 25 # | |
| 26 # Various tools for executing commands and looking at their output and status. | |
| 774 | 27 # |
| 28 # NB This only works (and is only relevant) for UNIX. | |
| 5 | 29 |
| 30 | |
| 31 # Get 'ls -l' status for an object into a string | |
| 32 # | |
| 33 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
|
34 """Return output of "ls -ld <file>" in a string.""" |
|
41171
dba5a33a0e79
Deprecate commands.getstatus().
Georg Brandl <georg@python.org>
parents:
16070
diff
changeset
|
35 import warnings |
|
53903
a36fa2ce61b1
Merged revisions 72458 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
47339
diff
changeset
|
36 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
|
37 return getoutput('ls -ld' + mkarg(file)) |
| 5 | 38 |
| 39 | |
| 40 # Get the output from a shell command into a string. | |
| 41 # The exit status is ignored; a trailing newline is stripped. | |
| 356 | 42 # Assume the command will work with '{ ... ; } 2>&1' around it.. |
| 5 | 43 # |
| 44 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
|
45 """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
|
46 return getstatusoutput(cmd)[1] |
| 5 | 47 |
| 48 | |
| 49 # Ditto but preserving the exit status. | |
| 50 # Returns a pair (sts, output) | |
| 51 # | |
| 52 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
|
53 """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
|
54 import os |
|
13846379347d
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents:
2066
diff
changeset
|
55 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
|
56 text = pipe.read() |
|
13846379347d
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents:
2066
diff
changeset
|
57 sts = pipe.close() |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
7755
diff
changeset
|
58 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
|
59 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
|
60 return sts, text |
| 5 | 61 |
| 62 | |
| 63 # Make command argument from directory and pathname (prefix space, add quotes). | |
| 64 # | |
| 65 def mk2arg(head, x): | |
|
47336
2f9319169fec
warn about some members of the commands module
Benjamin Peterson <benjamin@python.org>
parents:
41171
diff
changeset
|
66 from warnings import warnpy3k |
|
47339
c4a458ecd2ed
take Brett's advice on a few warnings
Benjamin Peterson <benjamin@python.org>
parents:
47336
diff
changeset
|
67 warnpy3k("In 3.x, mk2arg has been removed.") |
|
5741
13846379347d
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents:
2066
diff
changeset
|
68 import os |
|
13846379347d
Added docstrings by Sue Williams, re-indented to 4 spaces / level.
Fred Drake <fdrake@acm.org>
parents:
2066
diff
changeset
|
69 return mkarg(os.path.join(head, x)) |
| 5 | 70 |
| 71 | |
| 72 # Make a shell command argument from a string. | |
|
2066
000ae642f9b8
Added clarifying comment
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
73 # 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
|
74 # version of the argument. |
| 5 | 75 # Two strategies: enclose in single quotes if it contains none; |
| 356 | 76 # otherwise, enclose in double quotes and prefix quotable characters |
| 5 | 77 # with backslash. |
| 78 # | |
| 79 def mkarg(x): | |
|
47336
2f9319169fec
warn about some members of the commands module
Benjamin Peterson <benjamin@python.org>
parents:
41171
diff
changeset
|
80 from warnings import warnpy3k |
|
47339
c4a458ecd2ed
take Brett's advice on a few warnings
Benjamin Peterson <benjamin@python.org>
parents:
47336
diff
changeset
|
81 warnpy3k("in 3.x, mkarg has been removed.") |
|
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 |
