changeset 16:c21e96bb68c8

added clean-up after execution
author pieter.lukasse@wur.nl
date Fri, 23 Jan 2015 14:57:55 +0100
parents 76d6d8a5fa29
children 82368bd06e1d
files nist_wrapper.py utils.py
diffstat 2 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/nist_wrapper.py	Fri Jan 23 14:27:04 2015 +0100
+++ b/nist_wrapper.py	Fri Jan 23 14:57:55 2015 +0100
@@ -85,6 +85,12 @@
     
     return new_nist_home
 
+def _clean_up_NIST(new_nist_home):
+    '''
+    remove folder
+    '''
+    utils.remove_dir(new_nist_home)
+
 def _run_NIST(new_nist_home, output_file, is_wine, log_file, job_size):
     '''
       - run : (wine) new_nist_home/MSSEARCH/nistms$.exe /INSTRUMENT /PAR=2
@@ -98,13 +104,6 @@
     # to avoid conflicts in the orphan process killing (see end of this method), we will 
     # only run NIST again after previous nistms.exe process has been killed:
     # TODO : solution is currently only for wine (in the windows context the solution is not there yet, but parallel calls are not expected as in windows we only run tests one by one for now)
-#     if is_wine:
-#         while True:
-#             # check if process exists. If not, break loop and continue
-#             pid = utils.get_process_pid("nistms.exe")
-#             if pid == -1:
-#                 break
-#             time.sleep(2)
     
     
     # remove old file, if it is there:
@@ -154,7 +153,7 @@
     if is_wine:
 #         pid = utils.get_process_pid("nistms.exe")
 #         os.kill(pid, 9)
-        os.killpg(pro.pid, 9)    
+        os.killpg(pro.pid, 9)
     else:
         # windows case:
         proc_name = "nistms.exe"
@@ -318,6 +317,9 @@
     job_size = len(spectra_dict)
     _run_NIST(new_nist_home, nist_output_file, is_wine, output_log_file, job_size)
 
+    # clean-up NIST environment:
+    _clean_up_NIST(new_nist_home)
+    
     # write output tabular:
     hits_dict = utils.get_nist_out_as_dict(nist_output_file) 
     utils.save_dict_as_tsv(hits_dict, final_output_file)
--- a/utils.py	Fri Jan 23 14:27:04 2015 +0100
+++ b/utils.py	Fri Jan 23 14:57:55 2015 +0100
@@ -6,11 +6,25 @@
 import shutil
 import subprocess
 import csv
+import os
+import stat
 from collections import OrderedDict
     
 def copy_dir(src, dst):
     shutil.copytree(src, dst)
 
+def _del_rw(action, name, exc):
+    '''
+    ensures the read only files are set to read/write 
+    and then deletes them
+    '''
+    os.chmod(name, stat.S_IWRITE)
+    os.remove(name)
+
+def remove_dir(src):
+    shutil.rmtree(src, onerror=_del_rw)
+
+
 def log_message(log_file, log_message):
     with open(log_file, "a") as text_file:
         text_file.write(log_message + "\n")