Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
commands Namespace Reference

Functions

def getstatus
 
def getoutput
 
def getstatusoutput
 
def mk2arg
 
def mkarg
 

Variables

list __all__ = ["getstatusoutput","getoutput","getstatus"]
 

Detailed Description

Execute shell commands via os.popen() and return status, output.

Interface summary:

   import commands

   outtext = commands.getoutput(cmd)
   (exitstatus, outtext) = commands.getstatusoutput(cmd)
   outtext = commands.getstatus(file)  # returns output of "ls -ld file"

A trailing newline is removed from the output string.

Encapsulates the basic operation:

  pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
  text = pipe.read()
  sts = pipe.close()

 [Note:  it would be nice to add functions to interpret the exit status.]

Function Documentation

def commands.getoutput (   cmd)
Return output (stdout or stderr) of executing cmd in a shell.

Definition at line 42 of file commands.py.

42 
43 def getoutput(cmd):
44  """Return output (stdout or stderr) of executing cmd in a shell."""
45  return getstatusoutput(cmd)[1]
46 
47 
48 # Ditto but preserving the exit status.
49 # Returns a pair (sts, output)
#
def commands.getstatus (   file)
Return output of "ls -ld <file>" in a string.

Definition at line 33 of file commands.py.

33 
34 def getstatus(file):
35  """Return output of "ls -ld <file>" in a string."""
36  return getoutput('ls -ld' + mkarg(file))
37 
38 
39 # Get the output from a shell command into a string.
40 # The exit status is ignored; a trailing newline is stripped.
41 # Assume the command will work with '{ ... ; } 2>&1' around it..
#
def commands.getstatusoutput (   cmd)
Return (status, output) of executing cmd in a shell.

Definition at line 50 of file commands.py.

50 
51 def getstatusoutput(cmd):
52  """Return (status, output) of executing cmd in a shell."""
53  import os
54  pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
55  text = pipe.read()
56  sts = pipe.close()
57  if sts is None: sts = 0
58  if text[-1:] == '\n': text = text[:-1]
59  return sts, text
60 
61 
62 # Make command argument from directory and pathname (prefix space, add quotes).
#
def commands.mk2arg (   head,
  x 
)

Definition at line 63 of file commands.py.

References mkarg().

63 
64 def mk2arg(head, x):
65  import os
66  return mkarg(os.path.join(head, x))
67 
68 
69 # Make a shell command argument from a string.
70 # Return a string beginning with a space followed by a shell-quoted
71 # version of the argument.
72 # Two strategies: enclose in single quotes if it contains none;
73 # otherwise, enclose in double quotes and prefix quotable characters
74 # with backslash.
#
def commands.mkarg (   x)

Definition at line 75 of file commands.py.

75 
76 def mkarg(x):
77  if '\'' not in x:
78  return ' \'' + x + '\''
79  s = ' "'
80  for c in x:
81  if c in '\\$"`':
82  s = s + '\\'
83  s = s + c
84  s = s + '"'
85  return s

Variable Documentation

list __all__ = ["getstatusoutput","getoutput","getstatus"]

Definition at line 22 of file commands.py.