comparison nist_wrapper.py @ 9:8c20185752da

better logging
author pieter.lukasse@wur.nl
date Thu, 22 Jan 2015 22:14:23 +0100
parents 35e4707e0ac3
children bde3e4dc4476
comparison
equal deleted inserted replaced
8:35e4707e0ac3 9:8c20185752da
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 utils.log_message(log_file, "configuring NIST input...")
52 # remove old file: 52 # remove old file:
53 os.remove(new_nist_home+"/MSSEARCH/AUTOIMP.MSD") 53 os.remove(new_nist_home+"/MSSEARCH/AUTOIMP.MSD")
54 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:
55 if is_wine: 55 if is_wine:
56 text_file.write("C:\\NIST" + uuid_value + "\\MSSEARCH\\temp.msd") 56 text_file.write("C:\\NIST" + uuid_value + "\\MSSEARCH\\temp.msd")
69 replacement_text = "C:\\NIST" + uuid_value 69 replacement_text = "C:\\NIST" + uuid_value
70 70
71 # remove old file 71 # remove old file
72 os.remove(new_nist_home+"/MSSEARCH/nistms.INI") 72 os.remove(new_nist_home+"/MSSEARCH/nistms.INI")
73 # make new one 73 # make new one
74 log_file.write("configuring NIST ini...") 74 utils.log_message(log_file, "configuring NIST ini...")
75 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
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): 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):
77 for line in open(nist_ini_file): 77 for line in open(nist_ini_file):
78 if "Library Directory=" in line: 78 if "Library Directory=" in line:
79 line = "Library Directory="+ new_nist_home + "\\MSSEARCH\\\n" 79 line = "Library Directory="+ new_nist_home + "\\MSSEARCH\\\n"
114 114
115 exec_path = new_nist_home + "/MSSEARCH/nistms$.exe" 115 exec_path = new_nist_home + "/MSSEARCH/nistms$.exe"
116 116
117 pro = "" 117 pro = ""
118 if is_wine: 118 if is_wine:
119 log_file.write("calling wine with " + exec_path) 119 utils.log_message(log_file, "calling wine with " + exec_path)
120 print "calling wine with " + exec_path 120 print "calling wine with " + exec_path
121 cmd = ["wine "+ exec_path + " /INSTRUMENT /PAR=2"] 121 cmd = ["wine "+ exec_path + " /INSTRUMENT /PAR=2"]
122 # The os.setsid() is passed in the argument preexec_fn so 122 # The os.setsid() is passed in the argument preexec_fn so
123 # 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.
124 pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 124 pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
131 subprocess.call(cmd) 131 subprocess.call(cmd)
132 132
133 133
134 timeSleeping = 0 134 timeSleeping = 0
135 # monitor process by checking state file: 135 # monitor process by checking state file:
136 log_file.write("monitoring SRCREADY.TXT...") 136 utils.log_message(log_file, "monitoring SRCREADY.TXT...")
137 while True and timeSleeping < 20: 137 while True and timeSleeping < 20:
138 # check if SRCREADY.TXT is there already: 138 # check if SRCREADY.TXT is there already:
139 if os.path.exists(file_to_monitor): 139 if os.path.exists(file_to_monitor):
140 break 140 break
141 time.sleep(2) 141 time.sleep(2)
143 143
144 # kill process: 144 # kill process:
145 #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
146 #stays open...and orphan..killing it: 146 #stays open...and orphan..killing it:
147 147
148 log_file.write("killing wine process...") 148 utils.log_message(log_file, "killing wine process...")
149 if is_wine: 149 if is_wine:
150 # pid = utils.get_process_pid("nistms.exe") 150 # pid = utils.get_process_pid("nistms.exe")
151 # os.kill(pid, 9) 151 # os.kill(pid, 9)
152 os.killpg(pro.pid, 9) 152 os.killpg(pro.pid, 9)
153 else: 153 else:
303 is_wine = False 303 is_wine = False
304 if "wine" in nist_home_dir: 304 if "wine" in nist_home_dir:
305 is_wine = True 305 is_wine = True
306 306
307 uuid_value = str(uuid.uuid4()) 307 uuid_value = str(uuid.uuid4())
308 log_file = open(output_log_file,'w') 308
309
310 # prepare NIST environment for running: 309 # prepare NIST environment for running:
311 new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, log_file) 310 new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, output_log_file)
312 311
313 # run NIST search command: 312 # run NIST search command:
314 _run_NIST(new_nist_home, nist_output_file, is_wine, log_file) 313 _run_NIST(new_nist_home, nist_output_file, is_wine, output_log_file)
315 314
316 # write output tabular: 315 # write output tabular:
317 hits_dict = utils.get_nist_out_as_dict(nist_output_file) 316 hits_dict = utils.get_nist_out_as_dict(nist_output_file)
318 utils.save_dict_as_tsv(hits_dict, final_output_file) 317 utils.save_dict_as_tsv(hits_dict, final_output_file)
319 318
321 if len(sys.argv) > 6: 320 if len(sys.argv) > 6:
322 spectra_dict = utils.get_spectra_file_as_dict(spectrum_file) 321 spectra_dict = utils.get_spectra_file_as_dict(spectrum_file)
323 _create_html_report(output_html_report, output_html_report_files_path, hits_dict, spectra_dict) 322 _create_html_report(output_html_report, output_html_report_files_path, hits_dict, spectra_dict)
324 323
325 324
326 log_file.close()
327 #_save_data(enriched_data, headers, output_result) 325 #_save_data(enriched_data, headers, output_result)
328 326
329 seconds_end = int(round(time.time())) 327 seconds_end = int(round(time.time()))
330 print "Took " + str(seconds_end - seconds_start) + " seconds" 328 print "Took " + str(seconds_end - seconds_start) + " seconds"
331 329