python::GBSUtilities Namespace Reference


Functions

def RemoveChildProcesses
def ParseCommaSepList
def ParseEnvStr
def ProcessSandboxSetup


Function Documentation

def python::GBSUtilities::ParseCommaSepList (   s,
  l 
)

Parse comma separated list string s (with possibly embedded \,) and append into a list l

Definition at line 53 of file GBSUtilities.py.

00053                           :
00054 
00055     """Parse comma separated list string s (with possibly embedded \,) and append into a list l"""
00056     if not s: return l
00057     part_arg = ""
00058     for arg in s.split(','):
00059         if part_arg: arg = part_arg + arg
00060         part_arg = ""
00061         if arg[-1] == "\\": part_arg = arg[:-1] + ','
00062         else:               l.append(arg)
00063     if part_arg: l.append(part_arg)
00064     return l
00065 
def ParseEnvStr(s):

def python::GBSUtilities::ParseEnvStr (   s  ) 

Parse environment string: 'var1=val1,var2=val2...' into dictionary and return.

If parse error, dictionary will be empty

Definition at line 66 of file GBSUtilities.py.

00066                   :
00067 
00068     """Parse environment string: 'var1=val1,var2=val2...' into dictionary and return.
00069 
00070     If parse error, dictionary will be empty"""
00071 
00072     d = {}
00073     l = []
00074     ParseCommaSepList(s,l)
00075     for entry in l:
00076         mo = re.search(r"^\s*(\S+)\s*=(.*)$",entry)
00077         if not mo: return None
00078         d[mo.group(1)] = mo.group(2)
00079     return d
00080 
def ProcessSandboxSetup(obj,in_str,sb_type):

def python::GBSUtilities::ProcessSandboxSetup (   obj,
  in_str,
  sb_type 
)

Support setting of input and output sandbox lists for Task, Jobs and Protojobs objects.

Args are:-
  obj         Object whose sandbox is being setup
  in_str     Comma separated list of sandbox files
  sb_type     Sandbox type: either "input" or "output"

Returns:
  ok          Flag, True if O.K.
  out_str     The string to be stored ( but only if ok is True)
  

Definition at line 81 of file GBSUtilities.py.

00081                                            :
00082 
00083     """Support setting of input and output sandbox lists for Task, Jobs and Protojobs objects.
00084 
00085     Args are:-
00086       obj         Object whose sandbox is being setup
00087       in_str     Comma separated list of sandbox files
00088       sb_type     Sandbox type: either "input" or "output"
00089 
00090     Returns:
00091       ok          Flag, True if O.K.
00092       out_str     The string to be stored ( but only if ok is True)
00093       """
00094 
00095     #  Form file list and validate it.
00096 
00097     l = []
00098     ParseCommaSepList(in_str,l)
00099     out_str = ""
00100     err_msg = ""
00101     for file in l:
00102         if sb_type == "input":
00103             if not os.path.isfile(file):
00104                 if not err_msg: err_msg = "Cannot find input sandbox file(s):"
00105                 err_msg += " '" + file + "'"
00106             else:
00107                 if out_str: out_str += ","
00108                 out_str += os.path.split(file)[1]
00109         else:
00110             if re.search(r"/",file):
00111                 if not err_msg: err_msg = "Output sandbox file(s) contains directory:"
00112                 err_msg += " '" + file + "'"
00113             else:
00114                 if out_str: out_str += ","
00115                 out_str += file
00116     if err_msg:
00117         print err_msg
00118         return (False,"")
00119 
00120     #  For input sandbox, except for GBSProtoJob objects, create or wipe
00121     #  sandbox directory and copy files.
00122 
00123     if sb_type != "input" or obj.GetType() == "GBSProtoJob": return (True,out_str)
00124     sbox_dir = obj.GetStoreLocation("child_dir") + "/InputSandbox"
00125     if not os.path.isdir(sbox_dir):
00126         try:
00127             os.makedirs(sbox_dir,0755)
00128         except OSError:
00129                 print "Unable to create directory: " + sbox_dir
00130                 return (False,"")
00131     else:
00132         os.system("rm -f " + sbox_dir + "/*")
00133 
00134     for file in l:
00135         print "Copying " + file + " -> " + sbox_dir
00136         if os.system("cp " + file + " " + sbox_dir):
00137             print "Failed to copy file!"
00138             return (False,"")
00139     
00140     return (True,out_str)
00141             
            

def python::GBSUtilities::RemoveChildProcesses (  ) 

Delete all child processes starting with those without children and working up.

Definition at line 10 of file GBSUtilities.py.

00010                           :
00011     """Delete all child processes starting with those without children and working up."""
00012     killed_process = 1
00013     my_pid = os.getpid()
00014     print "Debug: Removing child processes. Parent process is " + str(my_pid)
00015     while killed_process:  #Continue until fail to kill any process
00016         #  Collect a list of children and their parents
00017         killed_process = 0
00018         parents   = {}  # Hash: pid -> parent pid
00019         children  = {}  # Hash: pid -> list of child pids
00020         time_cmds = {}  # Hash: pid -> stime,cmd
00021         ps_cmd = "ps -o pid,ppid,stime,cmd"
00022         inp = os.popen(ps_cmd,"r")
00023         for line in inp:
00024             if re.search(ps_cmd,line): continue #Skip process running ps!
00025             print "Debug PS line: ",line,
00026             mo = re.search(r"(\d+)\s+(\d+)\s+(.*)",line)
00027             if mo:
00028                 (pid,ppid,cmd)      = mo.groups()
00029                 parents[int(pid)]   = int(ppid)
00030                 time_cmds[int(pid)] = cmd
00031         inp.close()
00032         for child,parent in parents.iteritems():
00033             antecedent = parent
00034             while parents.has_key(antecedent):
00035                 if children.has_key(antecedent): children[antecedent].append(child)
00036                 else: children[antecedent] = [child]
00037                 antecedent = parents[antecedent]
00038         # For the current process, attempt to kill any children without children
00039         if not children.has_key(my_pid): break
00040         for child in children[my_pid]:
00041             if children.has_key(child): continue # Skip children with children
00042             #  Attempt to kill twice.  Use /proc/pid/status to check if successful
00043             if os.path.isfile('/proc/' + str(child) + '/status'):
00044                 print "Attempting to kill " + str(child) + " " + time_cmds[child]
00045                 os.kill(child,signal.SIGTERM)
00046                 time.sleep(1)
00047                 if os.path.isfile('/proc/' + str(child) + '/status'): os.kill(child,signal.SIGKILL)
00048                 time.sleep(1)
00049                 if os.path.isfile('/proc/' + str(child) + '/status'): print "Failed to kill " + str(child)
00050                 else: killed_process = 1
00051 
00052 
def ParseCommaSepList(s,l):


Generated on Mon Feb 18 14:42:03 2008 for gbs by  doxygen 1.5.4