odpy.oscommand
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
|
OpendTect command |
|
Append OpendTect arguments |
|
Environment for running OpendTect commands |
Python executable name |
|
|
Python command line |
|
Command execution |
|
is the process running? |
|
Pause a running process |
|
Resume a paused process |
|
Print processing timestamp |
|
Terminate a process |
|
Run a command with subprocess |
|
|
|
Run a command in the background with psutil |
- odpy.oscommand.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']
- odpy.oscommand.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']
- odpy.oscommand.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.
- odpy.oscommand.getPythonExecNm()
Python executable name
shortcut to sys.executable
- odpy.oscommand.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
- odpy.oscommand.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.
- odpy.oscommand.isRunning(proc)
is the process running?
- Parameters:
proc (psutil.Popen): Process to be checked
- Returns:
bool: True if the process is running.
- odpy.oscommand.pauseProcess(proc)
Pause a running process
- Parameters:
proc (psutil.Popen): Process to be paused
- Returns:
Set and return subprocess.Popen.returncode
- odpy.oscommand.resumeProcess(proc)
Resume a paused process
- Parameters:
proc (psutil.Popen): Process to be resumed
- Returns:
Set and return subprocess.Popen.returncode
- odpy.oscommand.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
- odpy.oscommand.kill(proc)
Terminate a process
- Parameters:
proc (psutil.Popen): Object view of the process
- Returns:
Set and return subprocess.Popen.returncode
- odpy.oscommand.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.
- odpy.oscommand.log_subprocess_output(pipe)
- odpy.oscommand.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.