+++ /dev/null
-"""Execute shell commands via os.popen() and return status, output.\r
-\r
-Interface summary:\r
-\r
- import commands\r
-\r
- outtext = commands.getoutput(cmd)\r
- (exitstatus, outtext) = commands.getstatusoutput(cmd)\r
- outtext = commands.getstatus(file) # returns output of "ls -ld file"\r
-\r
-A trailing newline is removed from the output string.\r
-\r
-Encapsulates the basic operation:\r
-\r
- pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')\r
- text = pipe.read()\r
- sts = pipe.close()\r
-\r
- [Note: it would be nice to add functions to interpret the exit status.]\r
-"""\r
-from warnings import warnpy3k\r
-warnpy3k("the commands module has been removed in Python 3.0; "\r
- "use the subprocess module instead", stacklevel=2)\r
-del warnpy3k\r
-\r
-__all__ = ["getstatusoutput","getoutput","getstatus"]\r
-\r
-# Module 'commands'\r
-#\r
-# Various tools for executing commands and looking at their output and status.\r
-#\r
-# NB This only works (and is only relevant) for UNIX.\r
-\r
-\r
-# Get 'ls -l' status for an object into a string\r
-#\r
-def getstatus(file):\r
- """Return output of "ls -ld <file>" in a string."""\r
- import warnings\r
- warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2)\r
- return getoutput('ls -ld' + mkarg(file))\r
-\r
-\r
-# Get the output from a shell command into a string.\r
-# The exit status is ignored; a trailing newline is stripped.\r
-# Assume the command will work with '{ ... ; } 2>&1' around it..\r
-#\r
-def getoutput(cmd):\r
- """Return output (stdout or stderr) of executing cmd in a shell."""\r
- return getstatusoutput(cmd)[1]\r
-\r
-\r
-# Ditto but preserving the exit status.\r
-# Returns a pair (sts, output)\r
-#\r
-def getstatusoutput(cmd):\r
- """Return (status, output) of executing cmd in a shell."""\r
- import os\r
- pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')\r
- text = pipe.read()\r
- sts = pipe.close()\r
- if sts is None: sts = 0\r
- if text[-1:] == '\n': text = text[:-1]\r
- return sts, text\r
-\r
-\r
-# Make command argument from directory and pathname (prefix space, add quotes).\r
-#\r
-def mk2arg(head, x):\r
- import os\r
- return mkarg(os.path.join(head, x))\r
-\r
-\r
-# Make a shell command argument from a string.\r
-# Return a string beginning with a space followed by a shell-quoted\r
-# version of the argument.\r
-# Two strategies: enclose in single quotes if it contains none;\r
-# otherwise, enclose in double quotes and prefix quotable characters\r
-# with backslash.\r
-#\r
-def mkarg(x):\r
- if '\'' not in x:\r
- return ' \'' + x + '\''\r
- s = ' "'\r
- for c in x:\r
- if c in '\\$"`':\r
- s = s + '\\'\r
- s = s + c\r
- s = s + '"'\r
- return s\r