Package gmisclib :: Module do_exec
[frames] | no frames]

Source Code for Module gmisclib.do_exec

 1  """Execute a *nix command  and capture the output.""" 
 2   
 3  import os 
 4  import re 
 5   
 6  __version__ = "$Revision: 1.5 $" 
 7  _comment = re.compile("\\s*#"); 
 8   
 9   
10 -def getall(s):
11 """Read a list of lines from a process, after 12 dropping junk like comments (beginning with whitespace then '#') 13 or blank lines. 14 Raises an exception if if the process returns 15 a line beginning with 'ERR:' 16 The argument s is a string containing the command and its 17 arguments. S is normally passed to a shell.""" 18 19 pipe = os.popen(s, 'r') 20 21 if pipe is None: 22 raise RuntimeError, 'Cannot spawn pipe for {%s}'%s 23 24 x = [] 25 while 1: 26 line = pipe.readline(); 27 if line == '' : 28 break 29 if line.startswith('#'): 30 continue 31 cs = _comment.search(line) 32 if cs is not None : # Strip off the comment. 33 line = cs.string[:cs.start()] 34 line = line.rstrip() 35 if line == '': 36 continue 37 if line.startswith('ERR:'): 38 raise RuntimeError, '%s from {%s}' % (line, s) 39 x.append(line.strip()) 40 41 sts = pipe.close() 42 if sts is None: sts = 0 43 if sts != 0 : 44 raise RuntimeError, \ 45 'spawned command fails with %d from {%s}' % (sts, s) 46 return x
47 48
49 -def get(s):
50 """Read a single line from a process, after 51 dropping junk like comments (#) or blank lines. 52 Returns a trouble indication if the process returns 53 'ERR:' """ 54 x = getall(s) 55 if len(x) < 1: 56 raise RuntimeError, 'no output fron {%s}' % s 57 return x[0]
58