Skip to content
Snippets Groups Projects
Commit d70c2f52 authored by Alex Buts's avatar Alex Buts
Browse files

Re #11886 This should fix the issue.

parent 9c50ef2d
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@ class UserProperties(object):
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 @@ class UserProperties(object):
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 @@ class UserProperties(object):
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 @@ class MantidConfigDirectInelastic(object):
# 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 @@ class MantidConfigDirectInelastic(object):
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 <source_mod_date:
return True
elif targ_mod_date == source_mod_date:
if time_targ<time_source:
return True
else:
return False
else:
return False
#
def copy_reduction_sample(self,InstrName,CycleID,rb_folder):
""" Method copies sample reduction script from user script repository
to user folder.
......@@ -210,9 +240,11 @@ class MantidConfigDirectInelastic(object):
target_file = self._target_reduction_file(InstrName,CycleID)
full_target = os.path.join(rb_folder,target_file)
# already have target file
if os.path.isfile(full_target):
# already have target file or modified by user
if not self.script_need_replacing(full_source,full_target):
return
if os.path.isfile(full_target):
os.remove(full_target)
shutil.copyfile(full_source,full_target)
os.chmod(full_target,0777)
if platform.system() != 'Windows':
......@@ -381,8 +413,3 @@ class MantidConfigDirectInelastic(object):
os.system('chown -R {0}:{0} {1}'.format(self._fedid,config_file_name))
def copy_and_overwrite(from_path, to_path):
if os.path.exists(to_path):
shutil.rmtree(to_path)
shutil.copytree(from_path, to_path)
import os
#os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
os.environ["PATH"] = r"c:/Mantid/Code/builds/br_master/bin/Release;"+os.environ["PATH"]
import unittest
import shutil
import datetime
......@@ -51,7 +51,7 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
if not os.path.exists(self.userRootDir):
os.makedirs(self.userRootDir)
def makeFakeSourceReductionFile(self,mcf):
def makeFakeSourceReductionFile(self,mcf,contents=None):
instr_name = mcf._user.get_last_instrument()
......@@ -61,10 +61,13 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
file_name = mcf._sample_reduction_file(instr_name)
full_file = os.path.join(file_path,file_name)
if not os.path.isfile(full_file):
fh=open(full_file,'w')
fh.write('#Test reduction file')
fh.close()
if os.path.isfile(full_file):
os.remove(full_file)
fh=open(full_file,'w')
fh.write('#Test reduction file\n')
fh.write('Contents={0}'.format(contents))
fh.close()
return full_file
def tearDown(self):
......@@ -181,7 +184,8 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
rbnum2='RB1999000'
targetDir = config['defaultsave.directory']
targetDir = self.get_save_dir()
rbdir2 = os.path.join(targetDir,self.userID,rbnum2)
if not os.path.exists(rbdir2):
os.makedirs(rbdir2)
......@@ -200,7 +204,7 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
mcf.init_user(self.userID,user)
self.makeFakeSourceReductionFile(mcf)
fake_source=self.makeFakeSourceReductionFile(mcf)
self.assertEqual(len(mcf._dynamic_configuration),6)
self.assertEqual(mcf._dynamic_configuration[1],'default.instrument=MERLIN')
......@@ -226,7 +230,7 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
user1.set_user_properties('MARI','20000124','CYCLE20001',rbdir2)
mcf.init_user(user1ID,user1)
self.makeFakeSourceReductionFile(mcf)
source_file = self.makeFakeSourceReductionFile(mcf)
mcf.generate_config()
self.assertEqual(len(mcf._dynamic_configuration),6)
......@@ -234,6 +238,20 @@ class ISISDirectInelasticConfigTest(unittest.TestCase):
config_file = os.path.join(self.userRootDir,'.mantid','Mantid.user.properties')
self.assertTrue(os.path.exists(os.path.join(user1RootDir,'.mantid')))
self.assertTrue(os.path.exists(config_file))
#
# Check sample reduction file
#
full_rb_path = rbdir2
cycle_id = user1.get_last_cycleID()
instr = user1.get_last_instrument()
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
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment