Skip to content

Commit

Permalink
Re #11728 Copied special sample file to users directory
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed May 12, 2015
1 parent f32cd96 commit 409d20c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/scripts/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@ set ( TEST_PY_FILES
test/DirectReductionHelpersTest.py
test/DirectPropertyManagerTest.py
test/DirectEnergyConversionTest.py
test/ISISDirecInelasticConfigTest.py
test/IndirectApplyCorrectionsTest.py
test/IndirectCommonTests.py
test/ReductionWrapperTest.py
Expand Down
61 changes: 48 additions & 13 deletions Code/Mantid/scripts/Inelastic/Direct/ISISDirecInelasticConfig.py
Expand Up @@ -116,8 +116,22 @@ def __init__(self,mantid='/opt/Mantid/',home='/home/',\
self._map_mask_folder = str(map_mask_folder)
# check if all necessary server folders specified as class parameters are present
self._check_server_folders_present()
#
# Static Parts of dynamic contents of Mantid configuration file
self._root_data_folder='/archive' # root folder for all experimental results -- particular one will depend on
# instrument and cycle number.
# the common part of all strings, generated dynamically as function of input class parameters.
self._dynamic_options_base = ['default.facility=ISIS']
# Path to python scripts, defined and used by mantid wrt to Mantid Root (this path may be version specific)
self._python_mantid_path = ['scripts/Calibration/','scripts/Examples/','scripts/Interface/','scripts/Vates/']
# Static paths to user scripts, defined wrt script repository root
self._python_user_scripts = set(['direct_inelastic/ISIS/qtiGenie/'])
# File name, used as source of reduction scripts for particular instrument
self._sample_reduction_file = lambda InstrName : '{0}Reduction_Sample.py'.format(InstrName)
# File name, used as target for copying to user folder for user to deploy as the base for his reduction script
self._target_reduction_file = lambda InstrName,cycleID : '{0}Reduction_{1}_{2}.py'.format(InstrName,cycleID[0],cycleID[1])

# Convid contents
# Static contents of the Mantid Config file
self._header = ("# This file can be used to override any properties for this installation.\n"
"# Any properties found in this file will override any that are found in the Mantid.Properties file\n"
"# As this file will not be replaced with further installations of Mantid it is a safe place to put\n"
Expand Down Expand Up @@ -152,14 +166,7 @@ def __init__(self,mantid='/opt/Mantid/',home='/home/',\
"#MantidOptions.ReusePlotInstances=Off\n\n"
"## Uncomment to disable use of OpenGL to render unwrapped instrument views\n"
"#MantidOptions.InstrumentView.UseOpenGL=Off\n")
#
self._root_data_folder='/archive'
# the common part of all strings, generated dynamically as function of input class parameters.
self._dynamic_options_base = ['default.facility=ISIS']
# Path to python scripts, defined and used by mantid wrt to Mantid Root (this path may be version specific)
self._python_mantid_path = ['scripts/Calibration/','scripts/Examples/','scripts/Interface/','scripts/Vates/']
# Static paths to user scripts, defined wrt script repository root
self._python_user_scripts = set(['direct_inelastic/ISIS/qtiGenie/'])

# Methods, which build & verify various parts of Mantid configuration
self._dynamic_options = [self._set_default_inst,
self._set_script_repo, # this would be necessary to have on an Instrument scientist account, disabled on generic setup
Expand All @@ -185,6 +192,30 @@ def config_need_replacing(self,config_file_name):
else:
return False

def copy_reduction_sample(self,InstrName,CycleID,rb_folder):
""" Method copies sample reduction script from user script repository
to user folder.
"""

source_file = self._sample_reduction_file(InstrName)

source_path = os.path.join(self._script_repo,'direct_inelastic',InstrName.upper())
full_source = os.path.join(source_path,source_file)

if not os.path.isfile(full_source):
return

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):
return
shutil.copyfile(full_source,full_target)
os.chmod(full_target,0777)
if platform.system() != 'Windows':
os.system('chown '+self._fedid+':'+self._fedid+' '+full_target)


def get_data_folder_name(self,instr,cycle_ID):
"""Method to generate a data folder from instrument name and the cycle start date
(cycle ID)
Expand Down Expand Up @@ -269,11 +300,11 @@ def _def_python_search_path(self):
path +=';'+os.path.join(self._mantid_path,part)

# define and append user scrips search path
python_path = copy.deepcopy(self._python_user_scripts)
user_path_part = copy.deepcopy(self._python_user_scripts)
for instr in self._user.instrument.values():
python_path.add(os.path.join('direct_inelastic/',instr.upper()+'/'))
for part in python_path:
path +=';'+os.path.join(self._script_repo,part)
user_path_part.add(os.path.join('direct_inelastic',instr.upper()))
for part in user_path_part:
path +=';'+os.path.join(self._script_repo,part)+'/'

self._dynamic_configuration.append('pythonscripts.directories=' + path)
#
Expand Down Expand Up @@ -325,6 +356,10 @@ def generate_config(self):
if platform.system() != 'Windows':
os.system('chown -R '+self._fedid+':'+self._fedid+' '+config_path)

InstrName = self._user.get_last_instrument()
cycleID = self._user.get_last_cycleID()
rb_folder = self._user.get_last_rbdir()
self.copy_reduction_sample(InstrName,cycleID,rb_folder)

def _write_user_config_file(self,config_file_name):
"""Write existing dynamic configuration from memory to
Expand Down
28 changes: 27 additions & 1 deletion Code/Mantid/scripts/test/ISISDirecInelasticConfigTest.py
@@ -1,5 +1,5 @@
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
Expand Down Expand Up @@ -40,6 +40,22 @@ def setUp(self):
if not os.path.exists(self.userRootDir):
os.makedirs(self.userRootDir)

def makeFakeSourceReductionFile(self,mcf):

instr_name = mcf._user.get_last_instrument()

file_path = os.path.join(self.UserScriptRepoDir,'direct_inelastic',instr_name.upper())
if not os.path.exists(file_path):
os.makedirs(file_path)

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()


def tearDown(self):
# Clean-up user's folder structure
if os.path.exists(self.rbdir):
Expand Down Expand Up @@ -114,6 +130,7 @@ def test_build_config(self):


mcf.init_user(self.userID,user)
self.makeFakeSourceReductionFile(mcf)

self.assertEqual(len(mcf._dynamic_configuration),6)

Expand All @@ -123,6 +140,13 @@ def test_build_config(self):
self.assertTrue(os.path.exists(os.path.join(self.userRootDir,'.mantid')))
self.assertTrue(os.path.exists(config_file))

cur_cycleID = mcf._user.get_last_cycleID()
instr = mcf._user.get_last_instrument()
targ_file = mcf._target_reduction_file(instr,cur_cycleID)

reduction_file = os.path.join(mcf._user.get_last_rbdir(),targ_file)
self.assertTrue(os.path.isfile(reduction_file))

self.assertFalse(mcf.config_need_replacing(config_file))
start_date = user.get_start_date()
date_in_apast=datetime.date(start_date.year,start_date.month,start_date.day-1)
Expand All @@ -143,6 +167,7 @@ def test_build_3Experiments_config(self):
user = UserProperties()
user.set_user_properties(self.instrument,self.start_date,self.cycle,self.rbdir)


rbnum2='RB1999000'
targetDir = config['defaultsave.directory']
rbdir2 = os.path.join(targetDir,self.userID,rbnum2)
Expand All @@ -162,6 +187,7 @@ def test_build_3Experiments_config(self):


mcf.init_user(self.userID,user)
self.makeFakeSourceReductionFile(mcf)

self.assertEqual(len(mcf._dynamic_configuration),6)
self.assertEqual(mcf._dynamic_configuration[1],'default.instrument=MERLIN')
Expand Down

0 comments on commit 409d20c

Please sign in to comment.