# HG changeset patch # User pieter.lukasse@wur.nl # Date 1421961263 -3600 # Node ID 8c20185752dae0b4f1f7fd091ef0b2b244b64aa1 # Parent 35e4707e0ac367fc3fdf4a26c8532b869784b706 better logging diff -r 35e4707e0ac3 -r 8c20185752da nist_wrapper.py --- a/nist_wrapper.py Thu Jan 22 22:07:40 2015 +0100 +++ b/nist_wrapper.py Thu Jan 22 22:14:23 2015 +0100 @@ -48,7 +48,7 @@ utils.copy_file(spectrum_file, new_nist_home+"/spectrum_file.msp") - log_file.write("configuring NIST input...") + utils.log_message(log_file, "configuring NIST input...") # remove old file: os.remove(new_nist_home+"/MSSEARCH/AUTOIMP.MSD") with open(new_nist_home + "/MSSEARCH/AUTOIMP.MSD", "a") as text_file: @@ -71,7 +71,7 @@ # remove old file os.remove(new_nist_home+"/MSSEARCH/nistms.INI") # make new one - log_file.write("configuring NIST ini...") + utils.log_message(log_file, "configuring NIST ini...") o = open(new_nist_home+"/MSSEARCH/nistms.INI","a") #open for append # 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): for line in open(nist_ini_file): @@ -116,7 +116,7 @@ pro = "" if is_wine: - log_file.write("calling wine with " + exec_path) + utils.log_message(log_file, "calling wine with " + exec_path) print "calling wine with " + exec_path cmd = ["wine "+ exec_path + " /INSTRUMENT /PAR=2"] # The os.setsid() is passed in the argument preexec_fn so @@ -133,7 +133,7 @@ timeSleeping = 0 # monitor process by checking state file: - log_file.write("monitoring SRCREADY.TXT...") + utils.log_message(log_file, "monitoring SRCREADY.TXT...") while True and timeSleeping < 20: # check if SRCREADY.TXT is there already: if os.path.exists(file_to_monitor): @@ -145,7 +145,7 @@ #p.terminate() - not needed, nistm$ will terminate...nistms.exe is the one that #stays open...and orphan..killing it: - log_file.write("killing wine process...") + utils.log_message(log_file, "killing wine process...") if is_wine: # pid = utils.get_process_pid("nistms.exe") # os.kill(pid, 9) @@ -305,13 +305,12 @@ is_wine = True uuid_value = str(uuid.uuid4()) - log_file = open(output_log_file,'w') - + # prepare NIST environment for running: - new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, log_file) + new_nist_home = _prepare_NIST(uuid_value, nist_home_dir, nist_ini_file, spectrum_file, is_wine, output_log_file) # run NIST search command: - _run_NIST(new_nist_home, nist_output_file, is_wine, log_file) + _run_NIST(new_nist_home, nist_output_file, is_wine, output_log_file) # write output tabular: hits_dict = utils.get_nist_out_as_dict(nist_output_file) @@ -323,7 +322,6 @@ _create_html_report(output_html_report, output_html_report_files_path, hits_dict, spectra_dict) - log_file.close() #_save_data(enriched_data, headers, output_result) seconds_end = int(round(time.time())) diff -r 35e4707e0ac3 -r 8c20185752da utils.py --- a/utils.py Thu Jan 22 22:07:40 2015 +0100 +++ b/utils.py Thu Jan 22 22:14:23 2015 +0100 @@ -11,7 +11,10 @@ def copy_dir(src, dst): shutil.copytree(src, dst) - +def log_message(log_file, log_message): + with open(log_file, "a") as text_file: + text_file.write(log_message + "\n") + def copy_file(src, dst): shutil.copy(src, dst)