Initial port to python3
[htsworkflow.git] / htsworkflow / pipelines / configure_run.py
index 0c1dc8b0ad44bc2d8eb92cf6f27d3f89b73ad609..02d0adfc1619035a007849b0e7e4e24b81254e67 100644 (file)
@@ -1,20 +1,25 @@
 #!/usr/bin/python
+__docformat__ = "restructuredtext en"
+
 import subprocess
 import logging
 import time
 import re
 import os
 
-from htsworkflow.pipelines.retrieve_config import getCombinedOptions, saveConfigFile
-from htsworkflow.pipelines.retrieve_config import FlowCellNotFound, WebError404
+from htsworkflow.pipelines.retrieve_config import \
+     CONFIG_SYSTEM, CONFIG_USER, \
+     FlowCellNotFound, getCombinedOptions, saveConfigFile, WebError404
 from htsworkflow.pipelines.genome_mapper import DuplicateGenome, getAvailableGenomes, constructMapperDict
 from htsworkflow.pipelines.run_status import GARunStatus
 
 from pyinotify import WatchManager, ThreadedNotifier
 from pyinotify import EventsCodes, ProcessEvent
 
+LOGGER = logging.getLogger(__name__)
+
 class ConfigInfo:
-  
+
   def __init__(self):
     #run_path = firecrest analysis directory to run analysis from
     self.run_path = None
@@ -67,12 +72,12 @@ class RunEvent(ProcessEvent):
     self._ci = conf_info
 
     ProcessEvent.__init__(self)
-    
+
 
   def process_IN_CREATE(self, event):
     fullpath = os.path.join(event.path, event.name)
     if s_finished.search(fullpath):
-      logging.info("File Found: %s" % (fullpath))
+      LOGGER.info("File Found: %s" % (fullpath))
 
       if s_firecrest_finished.search(fullpath):
         self.run_status_dict['firecrest'] = True
@@ -96,7 +101,7 @@ class RunEvent(ProcessEvent):
       self._ci.status.updateBustard(event.name)
     elif s_firecrest_all.search(fullpath):
       self._ci.status.updateFirecrest(event.name)
-      
+
     #print "Create: %s" % (os.path.join(event.path, event.name))
 
   def process_IN_DELETE(self, event):
@@ -206,19 +211,19 @@ def config_stdout_handler(line, conf_info):
 
   # Detect invalid command-line arguments
   elif s_invalid_cmdline.search(line):
-    logging.error("Invalid commandline options!")
+    LOGGER.error("Invalid commandline options!")
 
   # Detect starting of configuration
   elif s_start.search(line):
-    logging.info('START: Configuring pipeline')
+    LOGGER.info('START: Configuring pipeline')
 
   # Detect it made it past invalid arguments
   elif s_gerald.search(line):
-    logging.info('Running make now')
+    LOGGER.info('Running make now')
 
   # Detect that make files have been generated (based on output)
   elif s_generating.search(line):
-    logging.info('Make files generted')
+    LOGGER.info('Make files generted')
     return True
 
   # Capture run directory
@@ -235,17 +240,17 @@ def config_stdout_handler(line, conf_info):
 
       conf_info.bustard_path = firecrest_bustard
       conf_info.run_path = firecrest
-    
+
     #Standard output handling
     else:
-      print 'Sequence line:', line
+      print('Sequence line:', line)
       mo = s_seq_folder.search(line)
       conf_info.bustard_path = line[mo.end():]
       conf_info.run_path, temp = os.path.split(conf_info.bustard_path)
 
   # Log all other output for debugging purposes
   else:
-    logging.warning('CONF:?: %s' % (line))
+    LOGGER.warning('CONF:?: %s' % (line))
 
   return False
 
@@ -268,29 +273,29 @@ def config_stderr_handler(line, conf_info):
 
   # Detect invalid species directory error
   if s_species_dir_err.search(line):
-    logging.error(line)
+    LOGGER.error(line)
     return RUN_ABORT
   # Detect goat_pipeline.py traceback
   elif s_goat_traceb.search(line):
-    logging.error("Goat config script died, traceback in debug output")
+    LOGGER.error("Goat config script died, traceback in debug output")
     return RUN_ABORT
   # Detect indication of successful configuration (from stderr; odd, but ok)
   elif s_stderr_taskcomplete.search(line):
-    logging.info('Configure step successful (from: stderr)')
+    LOGGER.info('Configure step successful (from: stderr)')
     return True
   # Detect missing cycles
   elif s_missing_cycles.search(line):
 
     # Only display error once
     if not SUPPRESS_MISSING_CYCLES:
-      logging.error("Missing cycles detected; Not all cycles copied?")
-      logging.debug("CONF:STDERR:MISSING_CYCLES: %s" % (line))
+      LOGGER.error("Missing cycles detected; Not all cycles copied?")
+      LOGGER.debug("CONF:STDERR:MISSING_CYCLES: %s" % (line))
       SUPPRESS_MISSING_CYCLES = True
     return RUN_ABORT
-  
+
   # Log all other output as debug output
   else:
-    logging.debug('CONF:STDERR:?: %s' % (line))
+    LOGGER.debug('CONF:STDERR:?: %s' % (line))
 
   # Neutral (not failure; nor success)
   return False
@@ -331,19 +336,19 @@ def pipeline_stderr_handler(line, conf_info):
   if pl_stderr_ignore(line):
     pass
   elif s_make_error.search(line):
-    logging.error("make error detected; run failed")
+    LOGGER.error("make error detected; run failed")
     return RUN_FAILED
   elif s_no_gnuplot.search(line):
-    logging.error("gnuplot not found")
+    LOGGER.error("gnuplot not found")
     return RUN_FAILED
   elif s_no_convert.search(line):
-    logging.error("imagemagick's convert command not found")
+    LOGGER.error("imagemagick's convert command not found")
     return RUN_FAILED
   elif s_no_ghostscript.search(line):
-    logging.error("ghostscript not found")
+    LOGGER.error("ghostscript not found")
     return RUN_FAILED
   else:
-    logging.debug('PIPE:STDERR:?: %s' % (line))
+    LOGGER.debug('PIPE:STDERR:?: %s' % (line))
 
   return False
 
@@ -365,24 +370,24 @@ def retrieve_config(conf_info, flowcell, cfg_filepath, genome_dir):
   options = getCombinedOptions()
 
   if options.url is None:
-    logging.error("~/.ga_frontend.conf or /etc/ga_frontend/ga_frontend.conf" \
-                  " missing base_host_url option")
+    LOGGER.error("%s or %s missing base_host_url option" % \
+                  (CONFIG_USER, CONFIG_SYSTEM))
     return False
 
   try:
     saveConfigFile(flowcell, options.url, cfg_filepath)
     conf_info.config_filepath = cfg_filepath
-  except FlowCellNotFound, e:
-    logging.error(e)
+  except FlowCellNotFound as e:
+    LOGGER.error(e)
     return False
-  except WebError404, e:
-    logging.error(e)
+  except WebError404 as e:
+    LOGGER.error(e)
     return False
-  except IOError, e:
-    logging.error(e)
+  except IOError as e:
+    LOGGER.error(e)
     return False
-  except Exception, e:
-    logging.error(e)
+  except Exception as e:
+    LOGGER.error(e)
     return False
 
   f = open(cfg_filepath, 'r')
@@ -392,14 +397,14 @@ def retrieve_config(conf_info, flowcell, cfg_filepath, genome_dir):
   genome_dict = getAvailableGenomes(genome_dir)
   mapper_dict = constructMapperDict(genome_dict)
 
-  logging.debug(data)
+  LOGGER.debug(data)
 
   f = open(cfg_filepath, 'w')
   f.write(data % (mapper_dict))
   f.close()
-  
+
   return True
-  
+
 
 
 def configure(conf_info):
@@ -445,27 +450,26 @@ def configure(conf_info):
 
   fout = open(stdout_filepath, 'w')
   ferr = open(stderr_filepath, 'w')
-  
+
   pipe = subprocess.Popen(['goat_pipeline.py',
-                    '--GERALD=%s' % (conf_info.config_filepath),
-                           #'--tiles=s_4_0100,s_4_0101,s_4_0102,s_4_0103,s_4_0104',
+                           '--GERALD=%s' % (conf_info.config_filepath),
                            '--make',
                            conf_info.analysis_dir],
-                          stdout=fout,
-                          stderr=ferr)
+                           stdout=fout,
+                           stderr=ferr)
 
-  print "Configuring pipeline: %s" % (time.ctime())
+  print("Configuring pipeline: %s" % (time.ctime()))
   error_code = pipe.wait()
 
   # Clean up
   fout.close()
   ferr.close()
-  
-  
+
+
   ##################
   # Process stdout
   fout = open(stdout_filepath, 'r')
-  
+
   stdout_line = fout.readline()
 
   complete = False
@@ -480,9 +484,9 @@ def configure(conf_info):
 
   #error_code = pipe.wait()
   if error_code:
-    logging.error('Recieved error_code: %s' % (error_code))
+    LOGGER.error('Recieved error_code: %s' % (error_code))
   else:
-    logging.info('We are go for launch!')
+    LOGGER.info('We are go for launch!')
 
   #Process stderr
   ferr = open(stderr_filepath, 'r')
@@ -505,8 +509,8 @@ def configure(conf_info):
   # 1) The stdout completed without error
   # 2) The program exited with status 0
   # 3) No errors found in stdout
-  print '#Expect: True, False, True, True'
-  print complete, bool(error_code), abort != RUN_ABORT, stderr_success is True
+  print('#Expect: True, False, True, True')
+  print(complete, bool(error_code), abort != RUN_ABORT, stderr_success is True)
   status = complete is True and \
            bool(error_code) is False and \
            abort != RUN_ABORT and \
@@ -516,9 +520,9 @@ def configure(conf_info):
   #  we didn't retrieve the path info, log it.
   if status is True:
     if conf_info.bustard_path is None or conf_info.run_path is None:
-      logging.error("Failed to retrieve run_path")
+      LOGGER.error("Failed to retrieve run_path")
       return False
-  
+
   return status
 
 
@@ -528,7 +532,7 @@ def run_pipeline(conf_info):
   """
   # Fail if the run_path doesn't actually exist
   if not os.path.exists(conf_info.run_path):
-    logging.error('Run path does not exist: %s' \
+    LOGGER.error('Run path does not exist: %s' \
               % (conf_info.run_path))
     return False
 
@@ -548,8 +552,8 @@ def run_pipeline(conf_info):
   wdd = wm.add_watch(conf_info.run_path, mask, rec=True)
 
   # Log pipeline starting
-  logging.info('STARTING PIPELINE @ %s' % (time.ctime()))
-  
+  LOGGER.info('STARTING PIPELINE @ %s' % (time.ctime()))
+
   # Start the pipeline (and hide!)
   #pipe = subprocess.Popen(['make',
   #                         '-j8',
@@ -588,9 +592,9 @@ def run_pipeline(conf_info):
   ferr.close()
 
   # Finished file check!
-  print 'RUN SUCCESS CHECK:'
-  for key, value in event.run_status_dict.items():
-    print '  %s: %s' % (key, value)
+  print('RUN SUCCESS CHECK:')
+  for key, value in list(event.run_status_dict.items()):
+    print('  %s: %s' % (key, value))
 
   dstatus = event.run_status_dict