Mercurial > cpython-lru_cache-weakref
annotate Lib/commands.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 000ae642f9b8 |
| children | 13846379347d |
| rev | line source |
|---|---|
| 5 | 1 # Module 'commands' |
| 2 # | |
| 3 # Various tools for executing commands and looking at their output and status. | |
| 774 | 4 # |
| 5 # NB This only works (and is only relevant) for UNIX. | |
| 5 | 6 |
| 7 | |
| 8 # Get 'ls -l' status for an object into a string | |
| 9 # | |
| 10 def getstatus(file): | |
| 11 return getoutput('ls -ld' + mkarg(file)) | |
| 12 | |
| 13 | |
| 14 # Get the output from a shell command into a string. | |
| 15 # The exit status is ignored; a trailing newline is stripped. | |
| 356 | 16 # Assume the command will work with '{ ... ; } 2>&1' around it.. |
| 5 | 17 # |
| 18 def getoutput(cmd): | |
| 19 return getstatusoutput(cmd)[1] | |
| 20 | |
| 21 | |
| 22 # Ditto but preserving the exit status. | |
| 23 # Returns a pair (sts, output) | |
| 24 # | |
| 25 def getstatusoutput(cmd): | |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
26 import os |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
27 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') |
| 356 | 28 text = pipe.read() |
| 29 sts = pipe.close() | |
| 559 | 30 if sts == None: sts = 0 |
| 31 if text[-1:] == '\n': text = text[:-1] | |
| 5 | 32 return sts, text |
| 33 | |
| 34 | |
| 35 # Make command argument from directory and pathname (prefix space, add quotes). | |
| 36 # | |
| 37 def mk2arg(head, x): | |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
38 import os |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
914
diff
changeset
|
39 return mkarg(os.path.join(head, x)) |
| 5 | 40 |
| 41 | |
| 42 # Make a shell command argument from a string. | |
|
2066
000ae642f9b8
Added clarifying comment
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
43 # 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
|
44 # version of the argument. |
| 5 | 45 # Two strategies: enclose in single quotes if it contains none; |
| 356 | 46 # otherwise, enclose in double quotes and prefix quotable characters |
| 5 | 47 # with backslash. |
| 48 # | |
| 49 def mkarg(x): | |
| 50 if '\'' not in x: | |
| 51 return ' \'' + x + '\'' | |
| 52 s = ' "' | |
| 53 for c in x: | |
|
914
74b716e9ac49
New modules mimetools and rfc822.
Guido van Rossum <guido@python.org>
parents:
774
diff
changeset
|
54 if c in '\\$"`': |
| 5 | 55 s = s + '\\' |
| 56 s = s + c | |
| 57 s = s + '"' | |
| 58 return s |
