Source code for panpython.sdk.pandat_utility.pandat_run


"""
"Pandat Utility: Pandat Run" Module
===================================
A core module calling Pandat.exe
"""


import time
import sys
import subprocess
from subprocess import call


[docs]class WinCmdProcess_2: """ Run a batchfile calculation from Pandat console mode. Parameters ---------- time_limit_in_min: float, default=15.0 time limit (minutes) for an exe run Examples -------- >>> from panpython.sdk.pandat_utility.pandat_run import WinCmdProcess_2 >>> m_pandat_process: WinCmdProcess_2 = WinCmdProcess_2(time_limit_in_min=15.0) >>> run_list: str = '\"' + 'pandat.exe' + '\" \"' + 'example_batchfile.pbfx' + '\" \"0\"' >>> m_pandat_process.run_silent(run_list=run_list, ... task_id='0') """ def __init__(self, time_limit_in_min=15.0): """ Initialize an instance handling a windows exe in cmd Parameters ---------- time_limit_in_min: float, optional time limit for an exe run Returns ------- """ self.elapsed_time: float = 0.0 self.time_limit_in_min: float = time_limit_in_min return
[docs] def run_silent(self, run_list, task_id, mute=False, console_output="NULL") -> str: """ Run exe in silent mode Parameters ---------- run_list: str the command list to run task_id: str an task id from calling to track mute: bool muting std output from Pandat run console_output: str For saving the console output of Pandat run if needed. Defaults to "NULL" which wont save any file. Otherwise, name of file to store the console output should be given. Returns ------- A string containing runtime info """ start_time = time.time() pandat_time_limit = self.time_limit_in_min * 60.0 # 10.0 # print('pandat_time_limit = ', pandat_time_limit) is_win32: bool = 'win32' in str(sys.platform).lower() # print('IS_WIN32 = ', IS_WIN32) kwargs = {} if is_win32: startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE kwargs['startupinfo'] = startupinfo kwargs['timeout'] = pandat_time_limit if console_output != "NULL" : try: # Open a file to write the console output file_pointer = open(console_output, "w"); kwargs['stdout'] = file_pointer except FileNotFoundError: print("Warning:run_silent:Could not open console_output={} for writing".format(console_output)) rlt = '' try: call(run_list, **kwargs) time_cost_str = '{:.2f}'.format(time.time() - start_time) if 'stdout' in kwargs: kwargs['stdout'].close(); if is_win32: rlt = '>\trun Pandat with id ' + str(task_id) + ' [time cost = ' + time_cost_str + ' s]' if not mute: print(rlt) except subprocess.TimeoutExpired: rlt = '>>> Pandat timeout (more than ' + str(pandat_time_limit) + ' s)! with ' + str(task_id) if not mute: print(rlt) self.elapsed_time += time.time() - start_time return rlt
[docs] def get_elapsed_time(self) -> float: """ Return elapsed time of exe Parameters ---------- Returns ------- float elapsed time of running Pandat.exe """ return self.elapsed_time