symbian-qemu-0.9.1-12/python-2.6.1/Lib/commands.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 """Execute shell commands via os.popen() and return status, output.
       
     2 
       
     3 Interface summary:
       
     4 
       
     5        import commands
       
     6 
       
     7        outtext = commands.getoutput(cmd)
       
     8        (exitstatus, outtext) = commands.getstatusoutput(cmd)
       
     9        outtext = commands.getstatus(file)  # returns output of "ls -ld file"
       
    10 
       
    11 A trailing newline is removed from the output string.
       
    12 
       
    13 Encapsulates the basic operation:
       
    14 
       
    15       pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
       
    16       text = pipe.read()
       
    17       sts = pipe.close()
       
    18 
       
    19  [Note:  it would be nice to add functions to interpret the exit status.]
       
    20 """
       
    21 
       
    22 __all__ = ["getstatusoutput","getoutput","getstatus"]
       
    23 
       
    24 # Module 'commands'
       
    25 #
       
    26 # Various tools for executing commands and looking at their output and status.
       
    27 #
       
    28 # NB This only works (and is only relevant) for UNIX.
       
    29 
       
    30 
       
    31 # Get 'ls -l' status for an object into a string
       
    32 #
       
    33 def getstatus(file):
       
    34     """Return output of "ls -ld <file>" in a string."""
       
    35     import warnings
       
    36     warnings.warn("commands.getstatus() is deprecated", DeprecationWarning)
       
    37     return getoutput('ls -ld' + mkarg(file))
       
    38 
       
    39 
       
    40 # Get the output from a shell command into a string.
       
    41 # The exit status is ignored; a trailing newline is stripped.
       
    42 # Assume the command will work with '{ ... ; } 2>&1' around it..
       
    43 #
       
    44 def getoutput(cmd):
       
    45     """Return output (stdout or stderr) of executing cmd in a shell."""
       
    46     return getstatusoutput(cmd)[1]
       
    47 
       
    48 
       
    49 # Ditto but preserving the exit status.
       
    50 # Returns a pair (sts, output)
       
    51 #
       
    52 def getstatusoutput(cmd):
       
    53     """Return (status, output) of executing cmd in a shell."""
       
    54     import os
       
    55     pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
       
    56     text = pipe.read()
       
    57     sts = pipe.close()
       
    58     if sts is None: sts = 0
       
    59     if text[-1:] == '\n': text = text[:-1]
       
    60     return sts, text
       
    61 
       
    62 
       
    63 # Make command argument from directory and pathname (prefix space, add quotes).
       
    64 #
       
    65 def mk2arg(head, x):
       
    66     from warnings import warnpy3k
       
    67     warnpy3k("In 3.x, mk2arg has been removed.")
       
    68     import os
       
    69     return mkarg(os.path.join(head, x))
       
    70 
       
    71 
       
    72 # Make a shell command argument from a string.
       
    73 # Return a string beginning with a space followed by a shell-quoted
       
    74 # version of the argument.
       
    75 # Two strategies: enclose in single quotes if it contains none;
       
    76 # otherwise, enclose in double quotes and prefix quotable characters
       
    77 # with backslash.
       
    78 #
       
    79 def mkarg(x):
       
    80     from warnings import warnpy3k
       
    81     warnpy3k("in 3.x, mkarg has been removed.")
       
    82     if '\'' not in x:
       
    83         return ' \'' + x + '\''
       
    84     s = ' "'
       
    85     for c in x:
       
    86         if c in '\\$"`':
       
    87             s = s + '\\'
       
    88         s = s + c
       
    89     s = s + '"'
       
    90     return s