comparison nist_wrapper.py @ 7:165c22633081

adding log
author pieter.lukasse@wur.nl
date Thu, 22 Jan 2015 22:06:00 +0100
parents e19a6fbcf1db
children 35e4707e0ac3
comparison
equal deleted inserted replaced
6:e19a6fbcf1db 7:165c22633081
20 __copyright__ = "Copyright, 2015" 20 __copyright__ = "Copyright, 2015"
21 __license__ = "Apache v2" 21 __license__ = "Apache v2"
22 22
23 23
24 24
25 def _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine): 25 def _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, log_file):
26 ''' 26 '''
27 executes the following steps: 27 executes the following steps:
28 - copy nist_home_dir folder to nist_home_dir+_uuid 28 - copy nist_home_dir folder to nist_home_dir+_uuid
29 - copy spectrum_file.msp to ~/.wine/drive_c/NIST_uid 29 - copy spectrum_file.msp to ~/.wine/drive_c/NIST_uid
30 - creates nist_home_dir+_uuid/MSSEARCH/AUTOIMP.MSD -> pointing to C:\NIST_uid\MSSEARCH\temp.msd (in case of is_wine) or to nist_home_dir+_uuidM\SSEARCH\temp.msd 30 - creates nist_home_dir+_uuid/MSSEARCH/AUTOIMP.MSD -> pointing to C:\NIST_uid\MSSEARCH\temp.msd (in case of is_wine) or to nist_home_dir+_uuidM\SSEARCH\temp.msd
46 new_nist_home = nist_home_dir+uuid_value 46 new_nist_home = nist_home_dir+uuid_value
47 utils.copy_dir(nist_home_dir, new_nist_home) 47 utils.copy_dir(nist_home_dir, new_nist_home)
48 48
49 utils.copy_file(spectrum_file, new_nist_home+"/spectrum_file.msp") 49 utils.copy_file(spectrum_file, new_nist_home+"/spectrum_file.msp")
50 50
51 log_file.write("configuring NIST input...")
51 # remove old file: 52 # remove old file:
52 os.remove(new_nist_home+"/MSSEARCH/AUTOIMP.MSD") 53 os.remove(new_nist_home+"/MSSEARCH/AUTOIMP.MSD")
53 with open(new_nist_home + "/MSSEARCH/AUTOIMP.MSD", "a") as text_file: 54 with open(new_nist_home + "/MSSEARCH/AUTOIMP.MSD", "a") as text_file:
54 if is_wine: 55 if is_wine:
55 text_file.write("C:\\NIST" + uuid_value + "\\MSSEARCH\\temp.msd") 56 text_file.write("C:\\NIST" + uuid_value + "\\MSSEARCH\\temp.msd")
68 replacement_text = "C:\\NIST" + uuid_value 69 replacement_text = "C:\\NIST" + uuid_value
69 70
70 # remove old file 71 # remove old file
71 os.remove(new_nist_home+"/MSSEARCH/nistms.INI") 72 os.remove(new_nist_home+"/MSSEARCH/nistms.INI")
72 # make new one 73 # make new one
74 log_file.write("configuring NIST ini...")
73 o = open(new_nist_home+"/MSSEARCH/nistms.INI","a") #open for append 75 o = open(new_nist_home+"/MSSEARCH/nistms.INI","a") #open for append
74 # TODO : this loop/replace below is a bit limited to specific variables...either test different NIST versions or make more generic (harder in case of wine, or we need extra "home in .INI file" parameter): 76 # TODO : this loop/replace below is a bit limited to specific variables...either test different NIST versions or make more generic (harder in case of wine, or we need extra "home in .INI file" parameter):
75 for line in open(nist_ini_file): 77 for line in open(nist_ini_file):
76 if "Library Directory=" in line: 78 if "Library Directory=" in line:
77 line = "Library Directory="+ new_nist_home + "\\MSSEARCH\\\n" 79 line = "Library Directory="+ new_nist_home + "\\MSSEARCH\\\n"
81 o.write(line) 83 o.write(line)
82 o.close() 84 o.close()
83 85
84 return new_nist_home 86 return new_nist_home
85 87
86 def _run_NIST(new_nist_home, output_file, is_wine): 88 def _run_NIST(new_nist_home, output_file, is_wine, log_file):
87 ''' 89 '''
88 - run : (wine) new_nist_home/MSSEARCH/nistms$.exe /INSTRUMENT /PAR=2 90 - run : (wine) new_nist_home/MSSEARCH/nistms$.exe /INSTRUMENT /PAR=2
89 - monitor : new_nist_home/MSSEARCH/SRCREADY.TXT for content = "1" 91 - monitor : new_nist_home/MSSEARCH/SRCREADY.TXT for content = "1"
90 - when ready: 92 - when ready:
91 > copy SRCRESLT.TXT to output_file 93 > copy SRCRESLT.TXT to output_file
112 114
113 exec_path = new_nist_home + "/MSSEARCH/nistms$.exe" 115 exec_path = new_nist_home + "/MSSEARCH/nistms$.exe"
114 116
115 pro = "" 117 pro = ""
116 if is_wine: 118 if is_wine:
119 log_file.write("calling wine with " + exec_path)
117 print "calling wine with " + exec_path 120 print "calling wine with " + exec_path
118 cmd = ["wine "+ exec_path + " /INSTRUMENT /PAR=2"] 121 cmd = ["wine "+ exec_path + " /INSTRUMENT /PAR=2"]
119 # The os.setsid() is passed in the argument preexec_fn so 122 # The os.setsid() is passed in the argument preexec_fn so
120 # it's run after the fork() and before exec() to run the shell. 123 # it's run after the fork() and before exec() to run the shell.
121 pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 124 pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
128 subprocess.call(cmd) 131 subprocess.call(cmd)
129 132
130 133
131 timeSleeping = 0 134 timeSleeping = 0
132 # monitor process by checking state file: 135 # monitor process by checking state file:
136 log_file.write("monitoring SRCREADY.TXT...")
133 while True and timeSleeping < 20: 137 while True and timeSleeping < 20:
134 # check if SRCREADY.TXT is there already: 138 # check if SRCREADY.TXT is there already:
135 if os.path.exists(file_to_monitor): 139 if os.path.exists(file_to_monitor):
136 break 140 break
137 time.sleep(2) 141 time.sleep(2)
139 143
140 # kill process: 144 # kill process:
141 #p.terminate() - not needed, nistm$ will terminate...nistms.exe is the one that 145 #p.terminate() - not needed, nistm$ will terminate...nistms.exe is the one that
142 #stays open...and orphan..killing it: 146 #stays open...and orphan..killing it:
143 147
148 log_file.write("killing wine process...")
144 if is_wine: 149 if is_wine:
145 # pid = utils.get_process_pid("nistms.exe") 150 # pid = utils.get_process_pid("nistms.exe")
146 # os.kill(pid, 9) 151 # os.kill(pid, 9)
147 os.killpg(pro.pid, 9) 152 os.killpg(pro.pid, 9)
148 else: 153 else:
285 nist_home_dir = sys.argv[1] 290 nist_home_dir = sys.argv[1]
286 nist_ini_file = sys.argv[2] 291 nist_ini_file = sys.argv[2]
287 spectrum_file = sys.argv[3] 292 spectrum_file = sys.argv[3]
288 nist_output_file = sys.argv[4] 293 nist_output_file = sys.argv[4]
289 final_output_file = sys.argv[5] 294 final_output_file = sys.argv[5]
295 output_log_file = sys.argv[6]
290 # html report pars: 296 # html report pars:
291 output_html_report = None 297 output_html_report = None
292 output_html_report_files_path = None 298 output_html_report_files_path = None
293 if len(sys.argv) > 6: 299 if len(sys.argv) > 7:
294 output_html_report = sys.argv[6] 300 output_html_report = sys.argv[7]
295 output_html_report_files_path = sys.argv[7] 301 output_html_report_files_path = sys.argv[8]
296 302
297 is_wine = False 303 is_wine = False
298 if "wine" in nist_home_dir: 304 if "wine" in nist_home_dir:
299 is_wine = True 305 is_wine = True
300 306
301 uuid_value = str(uuid.uuid4()) 307 uuid_value = str(uuid.uuid4())
308 log_file = open(output_log_file,'w')
302 309
303 # prepare NIST environment for running: 310 # prepare NIST environment for running:
304 new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine) 311 new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, log_file)
305 312
306 # run NIST search command: 313 # run NIST search command:
307 _run_NIST(new_nist_home, nist_output_file, is_wine) 314 _run_NIST(new_nist_home, nist_output_file, is_wine)
308 315
309 # write output tabular: 316 # write output tabular:
314 if len(sys.argv) > 6: 321 if len(sys.argv) > 6:
315 spectra_dict = utils.get_spectra_file_as_dict(spectrum_file) 322 spectra_dict = utils.get_spectra_file_as_dict(spectrum_file)
316 _create_html_report(output_html_report, output_html_report_files_path, hits_dict, spectra_dict) 323 _create_html_report(output_html_report, output_html_report_files_path, hits_dict, spectra_dict)
317 324
318 325
326 log_file.close()
319 #_save_data(enriched_data, headers, output_result) 327 #_save_data(enriched_data, headers, output_result)
320 328
321 seconds_end = int(round(time.time())) 329 seconds_end = int(round(time.time()))
322 print "Took " + str(seconds_end - seconds_start) + " seconds" 330 print "Took " + str(seconds_end - seconds_start) + " seconds"
323 331