Mercurial > cpython-lru_cache-weakref
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 |
| 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 | 22 # Module 'commands' |
| 23 # | |
| 24 # Various tools for executing commands and looking at their output and status. | |
| 774 | 25 # |
| 26 # NB This only works (and is only relevant) for UNIX. | |
| 5 | 27 |
| 28 | |
| 29 # Get 'ls -l' status for an object into a string | |
| 30 # | |
| 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 | 34 |
| 35 | |
| 36 # Get the output from a shell command into a string. | |
| 37 # The exit status is ignored; a trailing newline is stripped. | |
| 356 | 38 # Assume the command will work with '{ ... ; } 2>&1' around it.. |
| 5 | 39 # |
| 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 | 43 |
| 44 | |
| 45 # Ditto but preserving the exit status. | |
| 46 # Returns a pair (sts, output) | |
| 47 # | |
| 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 | 57 |
| 58 | |
| 59 # Make command argument from directory and pathname (prefix space, add quotes). | |
| 60 # | |
| 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 | 64 |
| 65 | |
| 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 | 69 # Two strategies: enclose in single quotes if it contains none; |
| 356 | 70 # otherwise, enclose in double quotes and prefix quotable characters |
| 5 | 71 # with backslash. |
| 72 # | |
| 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 |
