python.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

getODCommand(execnm, args=None)

OpendTect command

appendDtectArgs(cmd, args=None)

Append OpendTect arguments

getEnvForOpendTect(args=None)

Environment for running OpendTect commands

getPythonExecNm()

Python executable name

getPythonCommand(scriptfile, posargs=None, dict=None, args=None)

Python command line

execCommand(cmd, background=False, env=None)

Command execution

isRunning(proc)

is the process running?

pauseProcess(proc)

Pause a running process

resumeProcess(proc)

Resume a paused process

printProcessTime(procnm, isstart, print_fn=std_msg, withprocline=True)

Print processing timestamp

kill(proc)

Terminate a process

startAndWait(cmd, env)

Run a command with subprocess

log_subprocess_output(pipe)

startDetached(cmd, env)

Run a command in the background with psutil

python.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']
python.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']
python.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.

python.odpy.oscommand.getPythonExecNm()

Python executable name

shortcut to sys.executable

python.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

python.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.

python.odpy.oscommand.isRunning(proc)

is the process running?

Parameters:
  • proc (psutil.Popen): Process to be checked

Returns:
  • bool: True if the process is running.

python.odpy.oscommand.pauseProcess(proc)

Pause a running process

Parameters:
  • proc (psutil.Popen): Process to be paused

Returns:
  • Set and return subprocess.Popen.returncode

python.odpy.oscommand.resumeProcess(proc)

Resume a paused process

Parameters:
  • proc (psutil.Popen): Process to be resumed

Returns:
  • Set and return subprocess.Popen.returncode

python.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
python.odpy.oscommand.kill(proc)

Terminate a process

Parameters:
  • proc (psutil.Popen): Object view of the process

Returns:
  • Set and return subprocess.Popen.returncode

python.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.

python.odpy.oscommand.log_subprocess_output(pipe)
python.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.