def build_template(self, job_header=None, prologue=None, epilogue=None): """ Build batch file template """ import os, stat from code_saturne.cs_exec_environment import append_shell_shebang, \ append_script_comment, prepend_path_command if not self.package: from code_saturne import cs_package self.package = cs_package.package() self.lines = [] append_shell_shebang(self.lines) # Add batch system info and user prologue if necessary if job_header: for line in job_header.split(os.linesep): self.lines.append(line) self.lines.append('') # Ensure switch to script directory append_script_comment(self.lines, 'Set working directory:' + os.linesep) self.lines.append('cd ' + enquote_arg(os.path.dirname(self.path))) self.lines.append('') if prologue: append_script_comment(self.lines, 'User prologue:' + os.linesep) for line in prologue.split(os.linesep): self.lines.append(line) self.lines.append('') # Add command to execute. append_script_comment(self.lines, 'Run command:' + os.linesep) exec_path = os.path.join(self.package.get_dir("bindir"), self.package.name) run_cmd = enquote_arg(exec_path) + ' run' self.cmd_name = self.package.name self.run_cmd_line_id = len(self.lines) self.lines.append(run_cmd) self.lines.append('') if epilogue: append_script_comment(self.lines, 'User epilogue:' + os.linesep) for line in epilogue.split(os.linesep): self.lines.append(line) self.lines.append('') self.save() st = os.stat(self.path) mode = st[stat.ST_MODE] os.chmod(self.path, mode | stat.S_IEXEC)
def run_studymanager_command(_c, _log, pythondir=None): """ Run command with arguments. Redirection of the stdout or stderr of the command. """ assert type(_c) == str or type(_c) == unicode log.debug("run_studymanager_command: %s" % _c) try: _log.seek(0, os.SEEK_END) except: _log.seek(0, 2) def __text(_t): return "\n\nExecution failed --> %s: %s" \ "\n - command: %s" \ "\n - directory: %s\n\n" % \ (_t, str(retcode), _c, os.getcwd()) _l = "" cmd = separate_args(_c) env = os.environ.copy() if pythondir: pythondir = enquote_arg(pythondir) pythonpath = pythondir + ':' + env.get("PYTHONPATH", '') env.update([("PYTHONPATH", pythonpath)]) try: t1 = time.time() retcode = run_command(cmd, stdout=_log, stderr=_log, env=env) t2 = time.time() if retcode < 0: _l = __text("command was terminated by signal") elif retcode > 0: _l = __text("command return") except OSError: import traceback import sys exc_info = sys.exc_info() bt = traceback.format_exception(*exc_info) for l in bt: _log.write(l) retcode = 0 del exc_info _l = __text("unknown command") t1 = 0. t2 = 0. _log.flush() if _l: _log.write(_l) return retcode, "%.2f" % (t2 - t1)
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 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 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)
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 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 slotBatchRunning(self): """ Launch Code_Saturne batch running. """ QDialog.accept(self) self.Apply() if self.case.isModified(): self.parent.fileSave() else: self.jmdl.save(param=self.case['xmlfile'], force=False) # Ensure code is run from a case subdirectory prv_dir = os.getcwd() os.chdir(self.case['data_path']) # Build command line rm_type = self.jmdl.batch.rm_type cmd = sys.argv[0] if not cmd: pkg = self.case['package'] bindir = pkg.get_dir('bindir') cmd = os.path.join(bindir, pkg.name) cmd = cs_exec_environment.enquote_arg(cmd) run_title = self.case.module_name() if rm_type == None: run_title += ' - Job Run' cmd += ' run' else: run_title += ' - Job Submission' cmd += ' submit' dlg = ListingDialogView(self.parent, self.case, run_title, cmd) dlg.show() os.chdir(prv_dir)
def main(argv, pkg): """ Main function. """ # Use alternate compute (back-end) package if defined batch = cs_batch.batch(pkg) submit_cmd = batch.submit_command_prefix() if not submit_cmd: submit_cmd = get_shell_type() runcase_path = process_cmd_line(argv, submit_cmd, pkg) if not runcase_path: return 1 scripts_dir = os.path.abspath(os.path.dirname(runcase_path)) runcase = cs_runcase.runcase(runcase_path) # Adjust run command for staging only run_args = runcase.get_run_args() for a in ['--stage', '--initialize', '--execute', '--finalize']: while a in run_args: run_args.remove(a) run_args.append('--stage') retcode, run_id, result_path = cs_run.run(run_args, pkg) # Now prepare runcase for submission if retcode != 0 or not result_path: err_str = ' run not staged due to prior error.' raise Exception(err_str) run_args = runcase.get_run_args() while '--stage' in run_args: run_args.remove(a) stages = [] for a in ['--initialize', '--execute', '--finalize']: if a in run_args: stages.append(a) if not stages: run_args.append('--initialize') run_args.append('--finalize') runcase.set_run_args(run_args) runcase.set_run_id(run_id=run_id) runcase_path = os.path.join(result_path, os.path.basename(runcase_path)) runcase.save(runcase_path) # Now submit case save_wd = os.getcwd() os.chdir(result_path) for a in argv: if os.path.isfile(a) and runcase_path: submit_cmd += ' ' + enquote_arg(runcase_path) runcase_path = None else: submit_cmd += ' ' + enquote_arg(a) return subprocess.call(submit_cmd, shell=True) os.chdir(save_wd)
def run_studymanager(pkg, options): """ Main function 1. parse the command line, 2. read the file of parameters 3. create all studies, 4. compile sources 5. run all cases 6. compare results 7. plot result 8. reporting by mail """ # Source environment if required before importing studymanager modules, as # it pulls Python packages such as matplotlib which may not be in # the standard path. from code_saturne.cs_exec_environment import set_modules, source_rcfile, enquote_arg set_modules(pkg) source_rcfile(pkg) from code_saturne.studymanager.cs_studymanager_study import Studies # Scripts exe = os.path.join(pkg.get_dir('bindir'), pkg.name) if sys.platform.startswith('win'): exe = exe + ".com" exe = enquote_arg(exe) dif = pkg.get_io_dump() for p in exe, dif: if not os.path.isfile(p): print("Error: executable %s not found." % p) if not sys.platform.startswith('win'): return 1 dif += " -d" # Read the file of parameters studies = Studies(pkg, options, exe, dif) if options.debug: print(" run_studymanager() >> Studies are initialized") if options.update_xml == False: os.chdir(studies.getDestination()) else: os.chdir(studies.getRepository()) # Print header studies.reporting(" -------------") studies.reporting(" Study Manager") studies.reporting(" -------------\n") studies.reporting(" Code name: " + pkg.name) studies.reporting(" Kernel version: " + pkg.version) studies.reporting(" Install directory: " + pkg.get_dir('exec_prefix')) studies.reporting(" File dump: " + dif) studies.reporting("\n Informations:") studies.reporting(" -------------\n") studies.reporting(" Date: " + datetime.now().strftime("%A %B %d %H:%M:%S %Y")) studies.reporting(" Platform: " + platform.platform()) studies.reporting(" Computer: " + platform.uname()[1] + " " + release()) studies.reporting(" Process Id: " + str(os.getpid())) studies.reporting(" User name: " + getpass.getuser()) studies.reporting(" Repository: " + studies.getRepository()) dest = studies.getDestination() studies.reporting(" Destination: " + dest) doc = os.path.join(dest, options.log_file) studies.reporting(" Ext. subprocesses logs: " + doc) studies.reporting("\n") # Update repository if needed if options.update: studies.updateRepository() # Update only xml data if needed if options.update_xml: studies.updateRepository(True) # Test sources compilation for all cases if options.test_compilation: studies.test_compilation() # Check if xml for result directories in the repository are OK if options.compare: studies.check_compare(destination=False) if options.post: studies.check_script(destination=False) studies.check_plots_and_input(destination=False) # Create all studies and all cases if options.compare or options.post or options.runcase: studies.create_studies() # Create dependency graph based on studies and all cases if options.create_graph: studies.create_graph() # Preprocessing and run all cases if options.debug: print(" run_studymanager() >> Starts running...") if options.runcase: studies.run() if options.debug: print(" run_studymanager() >> Exits runs") # Compare checkpoint files if options.compare: studies.check_compare() studies.compare() # Postprocess results and probes if options.post: checked_scripts = studies.check_script() if checked_scripts: studies.scripts() studies.check_plots_and_input() studies.postpro() studies.plot() studies.reporting("\n --------------------") studies.reporting(" End of Study Manager") studies.reporting(" --------------------") # Reporting - attached files are either pdf or # raw tex files if pdflatex is disabled attached_file = studies.build_reports("report_global", "report_detailed") if len(options.addresses.split()) > 0: send_report(pkg.code_name, studies.logs(), studies.getlabel(), options.addresses.split(), attached_file) return 0
def slotBatchRunning(self): """ Launch Code_Saturne batch running. """ QDialog.accept(self) self.Apply() if self.case.isModified(): self.parent.fileSave() # Ensure code is run from a case subdirectory prv_dir = os.getcwd() os.chdir(self.case['scripts_path']) # Do we have a mesh ? have_mesh = False node_ecs = self.case.xmlGetNode('solution_domain') if node_ecs.xmlGetNode('meshes_list'): if node_ecs.xmlGetNode('meshes_list').xmlGetNodeList('mesh'): have_mesh = True if node_ecs.xmlGetNode('mesh_input', 'path'): have_mesh = True if not have_mesh: title = self.tr("Warning") msg = self.tr("You have to select a mesh.\n\n") QMessageBox.information(self, title, msg) return # Verify if boundary condition definitions exist if self.case['run_type'] == 'standard': bd = LocalizationModel('BoundaryZone', self.case) if not bd.getZones(): if self.case['no_boundary_conditions'] == False: title = self.tr("Warning") msg = self.tr("No boundary definition declared.\n\n") QMessageBox.warning(self, title, msg) self.case['no_boundary_conditions'] = True # Build command line rm_type = self.jmdl.batch.rm_type batch = self.case['runcase'].path cmd = None run_title = None if rm_type == None: run_title = self.case.module_name() + ' - Job Run' cmd = cs_exec_environment.enquote_arg(batch) else: run_title = self.case.module_name() + ' - Job Submission' cmd = cs_exec_environment.enquote_arg(sys.argv[0]) \ + ' submit ' + cs_exec_environment.enquote_arg(batch) dlg = ListingDialogView(self.parent, self.case, run_title, cmd) dlg.show() os.chdir(prv_dir)