From d70c2f52194964e7862378d01034d3c31f088ec1 Mon Sep 17 00:00:00 2001 From: Alex Buts Date: Tue, 2 Jun 2015 17:41:31 +0100 Subject: [PATCH 1/3] Re #11886 This should fix the issue. --- .../Direct/ISISDirecInelasticConfig.py | 53 ++++++++++++++----- .../test/ISISDirecInelasticConfigTest.py | 36 +++++++++---- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py index 8403930a7c1a..6238f99449e7 100644 --- a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py +++ b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py @@ -18,7 +18,7 @@ def __init__(self): self.cycle_IDlist={} self.start_dates = {} self._recent_dateID=None - +# def set_user_properties(self,instrument,start_date,cycle,rb_folder): """Define the information, user office provides about user. The info has the form: instrument -- string with full instrument name @@ -44,26 +44,28 @@ def set_user_properties(self,instrument,start_date,cycle,rb_folder): max_date = a_date else: self._recent_dateID = recent_date_id +# def get_start_date(self): """Last start date""" if self._recent_dateID: return self.start_dates[self._recent_dateID] else: raise RuntimeError("User's experiment date is not defined. User undefined") +# def get_last_instrument(self): """return instrument used in last actual experiment""" if self._recent_dateID: return self.instrument[self._recent_dateID] else: raise RuntimeError("User's experiment date is not defined. User undefined") - +# def get_last_rbdir(self): """return rb folder used in last actual instrument""" if self._recent_dateID: return self.rb_dir[self._recent_dateID] else: raise RuntimeError("User's experiment date is not defined. User undefined") - +# def get_last_cycleID(self): """return last cycle the user is participating""" if self._recent_dateID: @@ -81,7 +83,9 @@ def check_input(self,instrument,start_date,cycle,rb_folder): raise RuntimeError("Cycle {0} should have form CYCLEYYYYN where N-- the cycle's number in a year but it is not".format(cycle)) if not (os.path.exists(rb_folder) and os.path.isdir(rb_folder)): raise RuntimeError("Folder {0} have to exist".format(rb_folder)) - +# +#--------------------------------------------------------------------# +# class MantidConfigDirectInelastic(object): """Class describes Mantid server specific user's configuration, necessary for Direct Inelastic reduction and analysis to work @@ -179,8 +183,11 @@ def __init__(self,mantid='/opt/Mantid/',home='/home/',\ # Its contents is generated by _init_config method from server and user specific # input parameters together. self._dynamic_configuration = None + # Unconditionally rewrite Mantid Configuration self._force_change_config = False - + # Unconditionally rewrite copy of sample reduction script + self._force_change_script = False +# def config_need_replacing(self,config_file_name): """Method specifies conditions when existing configuration file should be replaced""" if self._force_change_config: @@ -194,7 +201,30 @@ def config_need_replacing(self,config_file_name): return True else: return False - +# + def script_need_replacing(self,source_script_name,target_script_name): + """Method specifies conditions when existing reduction file should be replaced + by sample file + """ + if self._force_change_script: + return True + # missing file should always be replaced + if not os.path.isfile(target_script_name): + return True + time_targ = os.path.getmtime(target_script_name) + targ_mod_date = date.fromtimestamp(time_targ) + time_source = os.path.getmtime(source_script_name) + source_mod_date = date.fromtimestamp(time_source) + if targ_mod_date Date: Tue, 2 Jun 2015 18:36:25 +0100 Subject: [PATCH 2/3] Re #11886 Do not replace old configs and Pylint warnings --- .../Direct/ISISDirecInelasticConfig.py | 31 ++++++++++++------- .../test/ISISDirecInelasticConfigTest.py | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py index 6238f99449e7..9bffe08d63d2 100644 --- a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py +++ b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py @@ -3,7 +3,7 @@ import shutil import re import copy -from datetime import date,time +from datetime import date # the list of instruments this configuration is applicable to INELASTIC_INSTRUMENTS = ['MAPS','LET','MERLIN','MARI','HET'] @@ -76,11 +76,14 @@ def get_last_cycleID(self): def check_input(self,instrument,start_date,cycle,rb_folder): """Verify that input is correct""" if not instrument in INELASTIC_INSTRUMENTS: - raise RuntimeError("Instrument {0} has to be one of ISIS inelastic instruments".format(instrument)) + raise RuntimeError("Instrument {0} has to be one of "\ + "ISIS inelastic instruments".format(instrument)) if not (isinstance(start_date,str) and len(start_date) == 8): - raise RuntimeError("Experiment start date {0} should be defined as a sting in the form YYYYMMDD but it is not".format(start_date)) + raise RuntimeError("Experiment start date {0} should be defined as"\ + " a sting in the form YYYYMMDD but it is not".format(start_date)) if not (isinstance(cycle,str) and len(cycle) == 10 and re.match('^CYCLE',cycle)) : - raise RuntimeError("Cycle {0} should have form CYCLEYYYYN where N-- the cycle's number in a year but it is not".format(cycle)) + raise RuntimeError("Cycle {0} should have form CYCLEYYYYN where "\ + "N-- the cycle's number in a year but it is not".format(cycle)) if not (os.path.exists(rb_folder) and os.path.isdir(rb_folder)): raise RuntimeError("Folder {0} have to exist".format(rb_folder)) # @@ -211,17 +214,21 @@ def script_need_replacing(self,source_script_name,target_script_name): # missing file should always be replaced if not os.path.isfile(target_script_name): return True + # if user already started -- do not replace his config + #except when changes are forced + now_is = date.today() + start_date = self._user.get_start_date() + if now_is >start_date: + return False + # time_targ = os.path.getmtime(target_script_name) - targ_mod_date = date.fromtimestamp(time_targ) + time_source = os.path.getmtime(source_script_name) - source_mod_date = date.fromtimestamp(time_source) - if targ_mod_date Date: Wed, 3 Jun 2015 10:02:45 +0100 Subject: [PATCH 3/3] Re #11886 Should fix unit test on Unix and better logic behind replacing sample file. --- .../Direct/ISISDirecInelasticConfig.py | 34 +++++++++---------- .../test/ISISDirecInelasticConfigTest.py | 13 ++++--- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py index 9bffe08d63d2..2ac4f3b5b7db 100644 --- a/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py +++ b/Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py @@ -4,6 +4,7 @@ import re import copy from datetime import date +import time # the list of instruments this configuration is applicable to INELASTIC_INSTRUMENTS = ['MAPS','LET','MERLIN','MARI','HET'] @@ -207,34 +208,26 @@ def config_need_replacing(self,config_file_name): # def script_need_replacing(self,source_script_name,target_script_name): """Method specifies conditions when existing reduction file should be replaced - by sample file + by a sample file. """ if self._force_change_script: return True - # missing file should always be replaced + # non-existing file should always be replaced if not os.path.isfile(target_script_name): return True - # if user already started -- do not replace his config - #except when changes are forced - now_is = date.today() + #Always replace sample file if it has not been touched start_date = self._user.get_start_date() - if now_is >start_date: - return False - # - time_targ = os.path.getmtime(target_script_name) - - time_source = os.path.getmtime(source_script_name) - # we may want better granularity in a future -# source_mod_date = date.fromtimestamp(time_source) -# targ_mod_date = date.fromtimestamp(time_targ) - if time_targ < time_source: + # this time is set up to the file, copied from the repository + sample_file_time = time.mktime(start_date.timetuple()) + targ_file_time = os.path.getmtime(target_script_name) + if sample_file_time == targ_file_time: return True - else: + else: # somebody have modified the target file. Leave it alone return False # def copy_reduction_sample(self,InstrName,CycleID,rb_folder): - """ Method copies sample reduction script from user script repository - to user folder. + """Method copies sample reduction script from user script repository + to user folder. """ source_file = self._sample_reduction_file(InstrName) @@ -254,8 +247,13 @@ def copy_reduction_sample(self,InstrName,CycleID,rb_folder): os.remove(full_target) shutil.copyfile(full_source,full_target) os.chmod(full_target,0777) + if platform.system() != 'Windows': os.system('chown '+self._fedid+':'+self._fedid+' '+full_target) + # Set up the file creation and modification dates to the users start date + start_date = self._user.get_start_date() + file_time = time.mktime(start_date.timetuple()) + os.utime(full_target,(file_time,file_time)) def get_data_folder_name(self,instr,cycle_ID): diff --git a/Code/Mantid/scripts/test/ISISDirecInelasticConfigTest.py b/Code/Mantid/scripts/test/ISISDirecInelasticConfigTest.py index b0bc01897dee..a05404a48040 100644 --- a/Code/Mantid/scripts/test/ISISDirecInelasticConfigTest.py +++ b/Code/Mantid/scripts/test/ISISDirecInelasticConfigTest.py @@ -227,7 +227,7 @@ def test_build_3Experiments_config(self): os.makedirs(user1RootDir) # user1 = UserProperties() - user1.set_user_properties('MARI','39990124','CYCLE39991',rbdir2) + user1.set_user_properties('MARI','20990124','CYCLE20991',rbdir2) mcf.init_user(user1ID,user1) source_file = self.makeFakeSourceReductionFile(mcf) @@ -247,11 +247,14 @@ def test_build_3Experiments_config(self): target_file = mcf._target_reduction_file(instr,cycle_id) full_target_file = os.path.join(full_rb_path,target_file) self.assertTrue(os.path.exists(full_target_file)) - # target file does not need replacing + # Fresh target file should always be replaced + self.assertTrue(mcf.script_need_replacing(source_file,full_target_file)) + # modify target file access time: + access_time = os.path.getmtime(full_target_file) + now = time.time() + os.utime(full_target_file,(access_time ,now)) + # should not replace modified target file self.assertFalse(mcf.script_need_replacing(source_file,full_target_file)) - # make new fake configuration file - new_source = self.makeFakeSourceReductionFile(mcf,'New') - self.assertTrue(mcf.script_need_replacing(new_source,full_target_file)) #-------------------------------------------------------------------- # clean up