[project @ Better handling of configuring pipeline w/ clear success or failure. Loads...
[htsworkflow.git] / bin / config_pipeline.py
1 #!/usr/bin/python
2 import subprocess
3 import re
4 import os
5 import logging
6
7 logging.basicConfig(level=logging.DEBUG,
8                     format='%(asctime)s %(levelname)-8s %(message)s',
9                     datefmt='%a, %d %b %Y %H:%M:%S',
10                     filename='config_pipeline.log',
11                     filemode='w')
12
13 class ConfigInfo:
14   
15   def __init__(self):
16     self.run_path = None
17     self.bustard_path = None
18     self.config_filepath = None
19
20 #Info
21 s_start = re.compile('Starting Genome Analyzer Pipeline')
22 s_gerald = re.compile("[\S\s]+--GERALD[\S\s]+--make[\S\s]+")
23 s_generating = re.compile('Generating journals, Makefiles and parameter files')
24 s_seq_folder = re.compile('^Sequence folder: ')
25
26 #Errors
27 s_invalid_cmdline = re.compile('Usage:[\S\s]*goat_pipeline.py')
28
29 #Ignore
30 s_skip = re.compile('s_[0-8]_[0-9]+')
31
32 def handler(line, conf_info):
33   """
34   Processes each line of output from GOAT
35   and stores useful information using the logging module
36
37   Loads useful information into conf_info as well, for future
38   use outside the function.
39
40   returns True if found condition that signifies success.
41   """
42
43   # Irrelevant line
44   if s_skip.search(line):
45     pass
46   elif s_invalid_cmdline.search(line):
47     logging.error("Invalid commandline options!")
48   elif s_start.search(line):
49     logging.info('START: Configuring pipeline')
50   elif s_gerald.search(line):
51     logging.info('Running make now')
52   elif s_generating.search(line):
53     logging.info('Make files generted')
54     return True
55   elif s_seq_folder.search(line):
56     mo = s_seq_folder.search(line)
57     conf_info.bustard_path = line[mo.end():]
58     conf_info.run_path, temp = os.path.split(conf_info.bustard_path)
59   else:
60     logging.warning('How to handle: %s' % (line))
61
62   return False
63
64
65 def configure(conf_info):
66   """
67   Attempts to configure the GA pipeline using goat.
68
69   Uses logging module to store information about status.
70
71   returns True if configuration successful, otherwise False.
72   """
73   #ERROR Test:
74   #pipe = subprocess.Popen(['goat_pipeline.py',
75   #                         '--GERALD=config32bk.txt',
76   #                         '--make .',],
77   #                         #'.'],
78   #                        stdout=subprocess.PIPE,
79   #                        stderr=subprocess.PIPE)
80
81   #Not a test; actual run attempt.
82   pipe = subprocess.Popen(['goat_pipeline.py',
83                     '--GERALD=%s' % (conf_info.config_filepath),
84                            '--make',
85                            '.'],
86                           stdout=subprocess.PIPE,
87                           stderr=subprocess.PIPE)
88   
89   line = pipe.stdout.readline()
90
91   complete = False
92   while line != '':
93     if handler(line, conf_info):
94       complete = True
95     line = pipe.stdout.readline()
96
97   error_code = pipe.wait()
98   if error_code:
99     logging.error('Recieved error_code: %s' % (error_code))
100   else:
101     logging.info ('We are go for launch!')
102
103   #If log says complete and we don't have an
104   # error code (i.e. error_code == False)
105   status = complete is True and bool(error_code) is False
106
107   # If everything was successful, but for some reason
108   #  we didn't retrieve the path info, log it.
109   if status is True:
110     if conf_info.bustard_path is None or conf_info.run_path is None:
111       logging.error("Failed to retrieve run_path")
112       return False
113   
114   return status
115
116
117 if __name__ == '__main__':
118   ci = ConfigInfo()
119   ci.config_filepath = 'config32bk.txt'
120   
121   status = configure(ci)
122   if status:
123     print "Configure success"
124   else:
125     print "Configure failed"
126
127   print 'Run Dir:', ci.run_path
128   print 'Bustard Dir:', ci.bustard_path