Spoolwatch function for getting cycle number from Recipe*.xml file.
[htsworkflow.git] / gaworkflow / automation / spoolwatcher.py
index ba566cbfa04957e69c813302aa9e701fa0952a9d..d50eefbd6ea9b3cf11b2575ce7181d642b282b8b 100644 (file)
@@ -4,6 +4,9 @@ import os
 import re
 import sys
 import time
+import glob
+
+
 
 # this uses pyinotify
 import pyinotify
@@ -12,6 +15,53 @@ from pyinotify import EventsCodes
 from benderjab import rpc
 
 
+s_cycles = re.compile('No. Cycles: (?P<cycles>[0-9]+)')
+
+def get_cycles(run_dir="."):
+  """
+  Find the number of cycles from the Recipe*.xml file found in run_dir.
+  """
+  
+  file_path_list = glob.glob(os.path.join(run_dir, "Recipe*.xml"))
+
+  # Error handling
+  if len(file_path_list) == 0:
+    msg = "Recipe xml file not found."
+    raise IOError, msg
+
+  elif len(file_path_list) > 1:
+    msg = "%s Recipe files found, expected 1." % (len(file_path_list))
+    raise ValueError, msg
+
+  f = open(file_path_list[0], 'r')
+
+  #Find the line of the file with the cycle number in it.
+  for line in f:
+    mo = s_cycles.search(line)
+    if mo:
+      break
+
+  f.close()
+
+  # Process the line with the cycle number in it.
+  cycle_num = int(mo.group('cycles'))
+
+  return cycle_num
+
+
+
+class WatcherEvents(object):
+    # two events need to be tracked
+    # one to send startCopy
+    # one to send OMG its broken
+    # OMG its broken needs to stop when we've seen enough
+    #  cycles
+    # this should be per runfolder. 
+    # read the xml files 
+    def __init__(self):
+        pass
+        
+
 class Handler(pyinotify.ProcessEvent):
     def __init__(self, watchmanager, bot):
         self.last_event_time = None
@@ -112,7 +162,7 @@ class SpoolWatcher(rpc.XmlRpcBot):
         if self.wdd is not None:
             logging.debug("disabling watch")
             logging.debug(str(self.wdd))
-            self.wm.rm_watch(self.wdd)
+            self.wm.rm_watch(self.wdd.values())
             self.wdd = None
             
     def process_notify(self, *args):
@@ -193,4 +243,4 @@ def main(args=None):
     
 if __name__ == "__main__":
     sys.exit(main(sys.argv[1:]))
-    
\ No newline at end of file
+