/usr/lib/python2.7/dist-packages/code_saturne/cs_runcase.py is in code-saturne-data 4.3.3+repack-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2016 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------
import sys, os
try:
import ConfigParser # Python2
configparser = ConfigParser
except Exception:
import configparser # Python3
from cs_exec_environment import separate_args, assemble_args, enquote_arg, \
get_command_single_value, update_command_single_value
#===============================================================================
# Class used to manage runcase files
#===============================================================================
class runcase(object):
def __init__(self,
path,
package=None,
create_if_missing=True,
rebuild=False,
study_name=None,
case_name=None):
"""
Initialize runcase info object.
"""
self.path = path
self.package = package
try:
f = open(self.path, mode = 'r')
self.lines = f.readlines()
f.close()
if rebuild:
self.get_run_command()
command_line = self.lines[self.run_cmd_line_id]
self.build_template(package, study_name, case_name)
self.lines[self.run_cmd_line_id] = command_line
except IOError:
if create_if_missing or rebuild:
self.build_template(package, study_name, case_name)
else:
print("Error: can not open or read %s\n" % self.path)
sys.exit(1)
for i in range(len(self.lines)):
self.lines[i] = self.lines[i].rstrip()
self.get_run_command()
#---------------------------------------------------------------------------
def save(self, path=None):
"""
Save runcase, optionally to a different path.
"""
if (path):
self.path = path
f = open(self.path, mode = 'w')
for line in self.lines:
if line.startswith('export PATH='):
if self.package != None:
line = "export PATH=" + self.package.get_dir("bindir") + ":$PATH"
f.write(line + '\n')
f.close()
#---------------------------------------------------------------------------
def get_run_command(self):
"""
Determine the name of the main command of the runcase, and the associated
line in the file; this allows mixing Code_Saturne and NEPTUNE_CFD
cases in the same study.
"""
# Read the runcase script from the Repository.
self.cmd_name = None
self.run_cmd_line_id = -1
for i in range(len(self.lines) - 1, -1, -1):
line = self.lines[i]
# Skip comment and empty lines
if len(line) == 0:
continue
if line[0] == '#' or line[0:4] == 'rem ':
continue
j = line.find('#')
if j > -1:
line = line[0:j]
args = separate_args(line.rstrip())
if args.count('run') == 1:
if args.index('run') == 1: # "<package_name> run"
for name in ('code_saturne', 'neptune_cfd'):
if sys.platform.startswith('win'):
test_name = name
if args[0].find(test_name) == 0:
self.cmd_name = name
self.run_cmd_line_id = i
return
test_name = '\\' + name
if args[0].find(test_name) == 0:
self.cmd_name = name
self.run_cmd_line_id = i
return
# We should have exited before reaching this.
err_str = "Error: unable to determine the name of the script for " + self.path
raise ValueError(err_str)
#---------------------------------------------------------------------------
def get_run_args(self):
"""
Get the run command and arguments, as a list
"""
return separate_args(self.lines[self.run_cmd_line_id])
#---------------------------------------------------------------------------
def set_run_args(self, args):
"""
Set the run command and arguments from a list
"""
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def build_template(self, package=None, study_name=None, case_name=None):
"""
Build batch file template
"""
import os, stat
from cs_exec_environment import append_shell_shebang, \
append_script_comment, prepend_path_command
if not package:
import cs_package
package = cs_package.package()
self.lines = []
append_shell_shebang(self.lines)
# Add batch system info if necessary
batch_template = None
config = configparser.ConfigParser()
config.read(package.get_configfiles())
if config.has_option('install', 'batch'):
batch_template = config.get('install', 'batch')
if not os.path.isabs(batch_template):
batch_template = os.path.join(package.get_batchdir(),
'batch.' + batch_template)
fdt = open(batch_template, 'r')
import re, string
kwd1 = re.compile('nameandcase')
# Determine or build default names if required
if not case_name or not study_name:
topdir, scriptdir = os.path.split(os.path.split(self.path)[0])
if scriptdir == 'SCRIPTS':
studydir, casedir = os.path.split(topdir)
studydir = os.path.split(studydir)[1]
else:
casedir = ''
studydir = scriptdir
if not case_name:
if casedir:
case_name = casedir
else:
case_name = ''
if not study_name:
study_name = studydir
studycasename = study_name.lower() + case_name.lower()
# For some systems, names are limited to 15 caracters
studycasename = studycasename[:15]
for line in fdt:
line = line.rstrip()
line = re.sub(kwd1, studycasename, line)
self.lines.append(line)
fdt.close()
# Add command to execute.
append_script_comment(self.lines, 'Ensure the correct command is found:')
self.lines.append(prepend_path_command('PATH',
package.get_dir("bindir")))
self.lines.append('')
append_script_comment(self.lines, 'Run command:\n')
# On Linux systems, add a backslash to prevent aliases
if sys.platform.startswith('win'):
run_cmd = ''
else:
run_cmd = '\\'
run_cmd += package.name + ' run'
self.run_cmd_line_id = len(self.lines)
self.lines.append(run_cmd)
self.save()
st = os.stat(self.path)
mode = st[stat.ST_MODE]
os.chmod(self.path, mode | stat.S_IEXEC)
#---------------------------------------------------------------------------
def get_compute_build(self):
"""
Get the compute-build option in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
return get_command_single_value(args,
('--compute-build',
'--compute-build='))
#---------------------------------------------------------------------------
def set_compute_build(self, parameters):
"""
Set the compute-build option in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = update_command_single_value(separate_args(line),
('--compute-build',
'--compute-build='),
enquote_arg(parameters))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def get_coupling(self):
"""
Get the coupling option in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
return get_command_single_value(args,
('--coupling',))
#---------------------------------------------------------------------------
def set_coupling(self, coupling):
"""
Set the coupling option in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = update_command_single_value(separate_args(line),
('--coupling',),
enquote_arg(coupling))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def get_parameters(self):
"""
Get the parameters option in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
return get_command_single_value(args,
('--param', '--param=', '-p'))
#---------------------------------------------------------------------------
def set_parameters(self, parameters):
"""
Set the parameters option in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = update_command_single_value(separate_args(line),
('--param', '--param=', '-p'),
enquote_arg(parameters))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def get_nprocs(self):
"""
Get the nprocs option in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
return get_command_single_value(args,
('--nprocs', '--nprocs=', '-n'))
#---------------------------------------------------------------------------
def set_nprocs(self, parameters):
"""
Set the nprocs option in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = update_command_single_value(separate_args(line),
('--nprocs', '--nprocs=', '-n'),
enquote_arg(parameters))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def get_nthreads(self):
"""
Get the nthreads option in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
return get_command_single_value(args,
('--threads-per-task',
'--threads-per-task=',
'-nt'))
#---------------------------------------------------------------------------
def set_nthreads(self, parameters):
"""
Set the nthreads option in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = update_command_single_value(separate_args(line),
('--threads-per-task',
'--threads-per-task=',
'-nt'),
enquote_arg(parameters))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#---------------------------------------------------------------------------
def get_run_id(self):
"""
Get the run id, id_prefix, and id_suffix options in the run command
"""
args = separate_args(self.lines[self.run_cmd_line_id])
run_id = get_command_single_value(args, ('--id', '--id='))
run_id_prefix = get_command_single_value(args, ('--id-prefix',
'--id-prefix='))
run_id_suffix = get_command_single_value(args, ('--id-suffix',
'--id-suffix='))
return run_id, run_id_prefix, run_id_suffix
#---------------------------------------------------------------------------
def set_run_id(self, run_id=None, run_id_prefix=None, run_id_suffix=None):
"""
Set the run id, id_prefix, and id_suffix options in the run command
"""
line = self.lines[self.run_cmd_line_id]
args = separate_args(line)
if run_id != None:
args = update_command_single_value(args,
('--id', '--id='),
enquote_arg(run_id))
if run_id_prefix != None:
args = update_command_single_value(args,
('--id-prefix', '--id-prefix='),
enquote_arg(run_id_prefix))
if run_id_suffix != None:
args = update_command_single_value(args,
('--id-suffix', '--id-suffix='),
enquote_arg(run_id_suffix))
self.lines[self.run_cmd_line_id] = assemble_args(args)
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|