:py:mod:`python.odpy.oscommand` =============================== .. py:module:: python.odpy.oscommand .. autoapi-nested-parse:: Tools for managing OS commands Copyright (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt * AUTHOR : A. Huck * DATE : Apr 2019 General Example: How to launch the OpendTect software from python terminal >>> execnm = 'od_main' #execnm is executable name=od_main for the OpendTect software >>> cmds = getODCommand(execnm) ['C:\PROGRA~1\OPENDT~1\6683E8~1.0\bin\win64\Release\od_main'] >>> execpath = cmd[0] >>> odproc = execCommand(execpath, background=True, env=getEnvForOpendTect()) # execCommand executes the command in background mode and launches software getODCommand and execCommand wrapped into a function >>> def launchApp(execnm, args=None): return execCommand(getODCommand(execnm,args=args),env=getEnvForOpendTect()) >>> launchApp(od_DBMan) #od_DBMan is executable used by odpy.dbman Module Contents --------------- Functions ~~~~~~~~~ .. autoapisummary:: python.odpy.oscommand.getODCommand python.odpy.oscommand.appendDtectArgs python.odpy.oscommand.getEnvForOpendTect python.odpy.oscommand.getPythonExecNm python.odpy.oscommand.getPythonCommand python.odpy.oscommand.execCommand python.odpy.oscommand.isRunning python.odpy.oscommand.pauseProcess python.odpy.oscommand.resumeProcess python.odpy.oscommand.printProcessTime python.odpy.oscommand.kill python.odpy.oscommand.startAndWait python.odpy.oscommand.log_subprocess_output python.odpy.oscommand.startDetached .. py:function:: getODCommand(execnm, args=None) OpendTect command For a given OpendTect executable, get its full command line Parameters: * execnm (list): [Executable name] * args (dict, optional): Dictionary with the members 'dtectdata' and 'survey' as single element lists, and/or 'dtectexec' (see odpy.common.getODSoftwareDir) Returns: * list: OpendTect command line Notes: The two standard arguments providing the current survey location will be added to the command line arguments. Examples: >>> args = { 'dtectdata': ['D:\ODData'], 'survey': ['F3_Demo'] } >>> getODCommand( od_process_attrib, args=args ) ['C:\Program Files\OpendTect\6.6.0\bin\win64\Release\od_process_attrib', '--dataroot', 'D:\ODData', '--survey', 'F3_Demo'] .. py:function:: appendDtectArgs(cmd, args=None) Append OpendTect arguments Parameters: * cmd (list): List to which the returned elements will be added * arg (dict, optional): Dictionary with the members 'dtectdata' and 'survey' as single element lists, and/or 'dtectexec' (see odpy.getODSoftwareDir) Returns: * list: Input list with the added data root and survey directory name if provided Examples: >>> args = { 'dtectdata': ['D:\ODData'], 'survey': ['F3_Demo'] } >>> appendDtectArgs( list(), args=args ) ['--dataroot', 'D:\ODData', '--survey', 'F3_Demo'] .. py:function:: getEnvForOpendTect(args=None) Environment for running OpendTect commands Removes some of the environment set by conda, especially paths to Qt libraries, to remove conflicts with OpendTect libraries Parameters: * arg (dict, optional): Dictionary with the members 'dtectdata' and 'survey' as single element lists, and/or 'dtectexec' (see odpy.getODSoftwareDir) Returns: * An environment mapping that can be passed to subprocess.Popen or oscommand.execCommand. .. py:function:: getPythonExecNm() Python executable name shortcut to sys.executable .. py:function:: getPythonCommand(scriptfile, posargs=None, dict=None, args=None) Python command line Parameters: * scriptfile (str): Python script or executable * posargs (list, optional): Positional arguments for that script (default is None) * dict (str, optional): Value for the argument with key "--dict": String from which a json object can be parsed (default is None) * args (dict): Dictionary with the members 'dtectdata' and 'survey' as single element lists, and/or 'dtectexec' (see odpy.getODSoftwareDir). The dictionary may also contain the members 'proclog' and 'syslog' as strings, each being the full path to a log file. Returns: * list: Fully formed command line to be executed by the python executable .. py:function:: execCommand(cmd, background=False, env=None) Command execution Launches the execution of a command with its arguments. Parameters: * cmd (list): command to be executed with its arguments. * background (bool, optional): The command is forked if True (default is False). * env: subprocess.Popen Environment to be used to run the command Set to None of inherit the current process' environment Returns: * str: The stdout print of the subprocess.CompletedProcess (default) or a psutil.Popen object (background=True) Notes: This is an interface to the subprocess module. .. py:function:: isRunning(proc) is the process running? Parameters: * proc (psutil.Popen): Process to be checked Returns: * bool: True if the process is running. .. py:function:: pauseProcess(proc) Pause a running process Parameters: * proc (psutil.Popen): Process to be paused Returns: * Set and return subprocess.Popen.returncode .. py:function:: resumeProcess(proc) Resume a paused process Parameters: * proc (psutil.Popen): Process to be resumed Returns: * Set and return subprocess.Popen.returncode .. py:function:: printProcessTime(procnm, isstart, print_fn=std_msg, withprocline=True) Print processing timestamp Parameters: * procnm (str): Process name * isstart (bool): Status: Start or end of this process. * print_fn (function, optional): function to be used for the printing of the timestamp (default is odpy.std_msg, which default to stderr) * withprocline (bool, optional): Print the timestamp preceeded by a label string (default is True) Examples: >>> printProcessTime( 'od_process', True ) Process: 'od_process' Started: Mon 20 Apr 2020, 17:23:32 >>> printProcessTime( 'od_process', False ) Process: 'od_process' Finished: Mon 20 Apr 2020, 17:23:37 >>> printProcessTime( 'od_process', False, withprocline=False ) Finished: Mon 20 Apr 2020, 17:23:37 .. py:function:: kill(proc) Terminate a process Parameters: * proc (psutil.Popen): Object view of the process Returns: * Set and return subprocess.Popen.returncode .. py:function:: startAndWait(cmd, env) Run a command with subprocess Parameters: * cmd (list): command to be executed with its arguments. * env: see subprocess.Popen Returns: * str: stdout from the executed and retrieved subprocess.CompletedProcess Notes: * The command is executed by the run function of the subprocess module. * stdout is captured with a PIPE from the child process. * stderr is captured and forwarded to odpy.syslog_logger. .. py:function:: log_subprocess_output(pipe) .. py:function:: startDetached(cmd, env) Run a command in the background with psutil Parameters: * cmd (list): command to be executed with its arguments. * env: see subprocess.Popen Returns: psutil.Popen: Object view of the process Notes: * The command is executed by the Popen function of the psutil module. * Since the command is run as a daemon, it does not block the execution of the script. * stdout is captured and forwarded to odpy.proclog_logger. * stderr is captured and forwarded to odpy.proclog_logger.