Commit cec7a481 authored by Renato Bellotti's avatar Renato Bellotti
Browse files

Improved code readibility

parent 68c546d5
......@@ -9,28 +9,41 @@ Script that launches OPAL simulations
"""
import sys,os,shutil,glob
import subprocess
import sys
import os
import shutil
import glob
from simulation import Simulation
from opaldict import OpalDict
def getPaths(path, pattern, name):
result = glob.glob(os.path.join(path,pattern))
def getTemplatePaths(parentDir, pattern):
'''
Return the paths to template files.
The template files are files in the parent directory
that follow the given pattern.
Returns
=======
The paths to the template files.
'''
result = glob.glob(os.path.join(parentDir, pattern))
if not result:
print('No '+name+' file ('+pattern+') found')
print(f'No template file ({pattern}) found')
sys.exit()
return result
def getBaseName(inputfilePath):
templates = getPaths(inputfilePath, '*.tmpl', 'template')
templates = getTemplatePaths(inputfilePath, '*.tmpl')
name = templates[0].split('/')[-1][:-5] #NOTE: choose first (alphanumeric order) *.tmpl file by default
if os.path.isfile(os.path.join('.',name+'.data')):
# NOTE: choose first (alphanumeric order) *.tmpl file by default
name = templates[0].split('/')[-1][:-5]
if os.path.isfile(os.path.join('.', name + '.data')):
return name
print('Template and data filename do not match, '+name+'.data expected')
print(f'Template and data filename do not match, {name}.data expected')
sys.exit()
......@@ -57,8 +70,9 @@ def printUsage():
print("Important: runOPAL is currently not compatible with the command SAMPLE")
def checkCompat(tmplFile, incompatible): #NOTE: SAMPLE command not compatible with runOPAL (issue #8)
templateFile = open(tmplFile,'r')
# NOTE: SAMPLE command not compatible with runOPAL (issue #8)
def checkCompat(tmplFile, incompatible):
templateFile = open(tmplFile, 'r')
for line in templateFile:
if line.startswith('//'):
continue
......@@ -70,9 +84,9 @@ def checkCompat(tmplFile, incompatible): #NOTE: SAMPLE command not compatible wi
def traverseRanges(list, opaldict, args, doNobatch):
"""
Traverse all possible combinations of range variable values. Start simulation
once all range variables are fixed to a value. A list entry has the following
structure:
Traverse all possible combinations of range variable values.
Start simulation once all range variables are fixed to a value.
A list entry has the following structure:
['name of var', start_value, end_value, step_value]
"""
head = list[0]
......@@ -82,11 +96,12 @@ def traverseRanges(list, opaldict, args, doNobatch):
step = head[1][2]
qid = -1
if curval > endval:
print('range is empty, start value',curval,'needs to be higher than end value',endval)
print('range is empty, start value', curval,
'needs to be higher than end value', endval)
while curval <= endval:
opaldict[head[0]] = curval
if len(tail) == 0:
#run simulation
# run simulation
sim = Simulation(opaldict)
qid = sim.run(*args)
if doNobatch:
......@@ -107,7 +122,7 @@ def main(argv):
doTest = False
doKeep = False
doNobatch = False
doOptimize = True #NOTE: this flag is opposite of --noopt
doOptimize = True # NOTE: this flag is opposite of --noopt
queue = ""
info = 6
hypert = 0
......@@ -139,7 +154,7 @@ def main(argv):
elif arg.startswith("--hypert"):
hypert = int(arg.split("=")[1])
elif arg.startswith("-"):
print(arg,'is not a valid option, see --help for the available options')
print(arg, 'is not a valid option, see --help for the available options')
exit()
# safety check
......@@ -149,37 +164,37 @@ def main(argv):
# determine what kind of job should be ran, simulation by default
if doOptimize and os.environ.get('OPTIMIZER'):
if quiet == False:
if not quiet:
print('job type: OPTIMIZATION')
inputfilePath = os.environ.get('OPTIMIZER')
if not (inputfilePath and glob.glob(os.path.join(inputfilePath,'*.tmpl'))):
if quiet == False:
if not (inputfilePath and glob.glob(os.path.join(inputfilePath, '*.tmpl'))):
if not quiet:
print('job type: SIMULATION')
doOptimize = False
if os.environ.get('TEMPLATES'):
inputfilePath = os.environ.get('TEMPLATES')
elif (glob.glob(os.path.join('.','*.tmpl'))):
elif (glob.glob(os.path.join('.', '*.tmpl'))):
inputfilePath = '../'
else:
print('Template file unknown -> exiting ...')
sys.exit()
#check that tmpl and data files can be found or guessed
# check that tmpl and data files can be found or guessed
if not baseFileName:
baseFileName = getBaseName(inputfilePath)
elif not os.path.isfile(os.path.join(inputfilePath,baseFileName+'.tmpl')):
print(baseFileName+'.tmpl cannot be found! Check if it exists in '+inputfilePath)
elif not os.path.isfile(os.path.join(inputfilePath, baseFileName+'.tmpl')):
print(f'{baseFileName}.tmpl cannot be found! Check if it exists in {inputfilePath}')
sys.exit()
if quiet == False:
if not quiet:
print('baseFileName = '+baseFileName)
dataFile = baseFileName + '.data'
tmplFile = os.path.join(inputfilePath,baseFileName+'.tmpl')
tmplFile = os.path.join(inputfilePath, baseFileName + '.tmpl')
oinpFile = baseFileName + '.in' # the resulting OPAL input file
checkCompat(tmplFile, ['SAMPLE']) # check compatibility
#create the dictionary
# create the dictionary
opaldict = OpalDict(dataFile)
# check if template values must be changed
# if so add update the dictionary with the default values
......@@ -188,16 +203,18 @@ def main(argv):
if not opaldict.hasRanges():
sim = Simulation(opaldict)
qid = sim.run(N, baseFileName, inputfilePath, tmplFile, oinpFile, doTest, doKeep, doNobatch, doOptimize, info, queue, hypert, quiet)
qid = sim.run(N, baseFileName, inputfilePath, tmplFile, oinpFile,
doTest, doKeep, doNobatch, doOptimize,
info, queue, hypert, quiet)
if doNobatch:
if quiet == False:
print( "... finished!\n")
if not quiet:
print("... finished!\n")
#else:
# print( "SGE-ID= {}\n".format(qid))
else:
ranges = opaldict.Range()
#create range toplevel dir
# create range toplevel dir
dirname = baseFileName
for p in opaldict.uservars:
dirname += "_" + str(p[0]) + "=" + str(p[1])
......@@ -206,9 +223,9 @@ def main(argv):
# If there's already a directory remove it...
if os.path.isdir(dirname):
if doKeep:
print( 'KEEP existing directory ', dirname)
print('KEEP existing directory ', dirname)
else:
print( 'REMOVE existing directory', dirname)
print('REMOVE existing directory', dirname)
shutil.rmtree(dirname)
# create directory and change to the directory
os.mkdir(dirname)
......@@ -218,8 +235,10 @@ def main(argv):
os.chdir(dirname)
print(ranges)
#run simulations of all possible combinations
args = [N, baseFileName, inputfilePath, tmplFile, oinpFile, doTest, doKeep, doNobatch, doOptimize, info, queue, hypert, quiet]
# run simulations of all possible combinations
args = [N, baseFileName, inputfilePath, tmplFile, oinpFile,
doTest, doKeep, doNobatch, doOptimize,
info, queue, hypert, quiet]
traverseRanges(list(ranges.items()), opaldict, args, doNobatch)
# clean up
......@@ -227,6 +246,6 @@ def main(argv):
os.chdir("..")
#call main
# call main
if __name__ == "__main__":
main(sys.argv[1:])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment