mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4736 Instance.py renames
This commit is contained in:
@@ -33,33 +33,33 @@ class Instance:
|
|||||||
return s.connect_ex(('localhost', port)) == 0
|
return s.connect_ex(('localhost', port)) == 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def launch(resInsightExecutable = '', console = False, launchPort = -1, commandLineParameters=[]):
|
def launch(resinsight_executable = '', console = False, launch_port = -1, command_line_parameters=[]):
|
||||||
""" Launch a new Instance of ResInsight. This requires the environment variable
|
""" Launch a new Instance of ResInsight. This requires the environment variable
|
||||||
RESINSIGHT_EXECUTABLE to be set or the parameter resInsightExecutable to be provided.
|
RESINSIGHT_EXECUTABLE to be set or the parameter resinsight_executable to be provided.
|
||||||
The RESINSIGHT_GRPC_PORT environment variable can be set to an alternative port number.
|
The RESINSIGHT_GRPC_PORT environment variable can be set to an alternative port number.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
resInsightExecutable (str): Path to a valid ResInsight executable. If set
|
resinsight_executable (str): Path to a valid ResInsight executable. If set
|
||||||
will take precedence over what is provided in the RESINSIGHT_EXECUTABLE
|
will take precedence over what is provided in the RESINSIGHT_EXECUTABLE
|
||||||
environment variable.
|
environment variable.
|
||||||
console (bool): If True, launch as console application, without GUI.
|
console (bool): If True, launch as console application, without GUI.
|
||||||
launchPort(int): If -1 will use the default port of 50051 or look for RESINSIGHT_GRPC_PORT
|
launch_port(int): If -1 will use the default port of 50051 or look for RESINSIGHT_GRPC_PORT
|
||||||
if anything else, ResInsight will try to launch with this port
|
if anything else, ResInsight will try to launch with this port
|
||||||
commandLineParameters(list): Additional command line parameters as string entries in the list.
|
command_line_parameters(list): Additional command line parameters as string entries in the list.
|
||||||
Returns:
|
Returns:
|
||||||
Instance: an instance object if it worked. None if not.
|
Instance: an instance object if it worked. None if not.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
port = 50051
|
port = 50051
|
||||||
portEnv = os.environ.get('RESINSIGHT_GRPC_PORT')
|
port_env = os.environ.get('RESINSIGHT_GRPC_PORT')
|
||||||
if portEnv:
|
if port_env:
|
||||||
port = int(portEnv)
|
port = int(port_env)
|
||||||
if launchPort is not -1:
|
if launch_port is not -1:
|
||||||
port = launchPort
|
port = launch_port
|
||||||
|
|
||||||
if not resInsightExecutable:
|
if not resinsight_executable:
|
||||||
resInsightExecutable = os.environ.get('RESINSIGHT_EXECUTABLE')
|
resinsight_executable = os.environ.get('RESINSIGHT_EXECUTABLE')
|
||||||
if not resInsightExecutable:
|
if not resinsight_executable:
|
||||||
print('ERROR: Could not launch ResInsight because the environment variable'
|
print('ERROR: Could not launch ResInsight because the environment variable'
|
||||||
' RESINSIGHT_EXECUTABLE is not set')
|
' RESINSIGHT_EXECUTABLE is not set')
|
||||||
return None
|
return None
|
||||||
@@ -68,12 +68,12 @@ class Instance:
|
|||||||
port += 1
|
port += 1
|
||||||
|
|
||||||
print('Port ' + str(port))
|
print('Port ' + str(port))
|
||||||
print('Trying to launch', resInsightExecutable)
|
print('Trying to launch', resinsight_executable)
|
||||||
|
|
||||||
if isinstance(commandLineParameters, str):
|
if isinstance(command_line_parameters, str):
|
||||||
commandLineParameters = [str]
|
command_line_parameters = [str]
|
||||||
|
|
||||||
parameters = ["ResInsight", "--server", str(port)] + commandLineParameters
|
parameters = ["ResInsight", "--server", str(port)] + command_line_parameters
|
||||||
if console:
|
if console:
|
||||||
print("Launching as console app")
|
print("Launching as console app")
|
||||||
parameters.append("--console")
|
parameters.append("--console")
|
||||||
@@ -82,14 +82,14 @@ class Instance:
|
|||||||
for i in range(0, len(parameters)):
|
for i in range(0, len(parameters)):
|
||||||
parameters[i] = str(parameters[i])
|
parameters[i] = str(parameters[i])
|
||||||
|
|
||||||
pid = os.spawnv(os.P_NOWAIT, resInsightExecutable, parameters)
|
pid = os.spawnv(os.P_NOWAIT, resinsight_executable, parameters)
|
||||||
if pid:
|
if pid:
|
||||||
instance = Instance(port=port, launched=True)
|
instance = Instance(port=port, launched=True)
|
||||||
return instance
|
return instance
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def find(startPort = 50051, endPort = 50071):
|
def find(start_port = 50051, end_port = 50071):
|
||||||
""" Search for an existing Instance of ResInsight by testing ports.
|
""" Search for an existing Instance of ResInsight by testing ports.
|
||||||
|
|
||||||
By default we search from port 50051 to 50071 or if the environment
|
By default we search from port 50051 to 50071 or if the environment
|
||||||
@@ -97,27 +97,27 @@ class Instance:
|
|||||||
RESINSIGHT_GRPC_PORT to RESINSIGHT_GRPC_PORT+20
|
RESINSIGHT_GRPC_PORT to RESINSIGHT_GRPC_PORT+20
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
startPort (int): start searching from this port
|
start_port (int): start searching from this port
|
||||||
endPort (int): search up to but not including this port
|
end_port (int): search up to but not including this port
|
||||||
"""
|
"""
|
||||||
portEnv = os.environ.get('RESINSIGHT_GRPC_PORT')
|
port_env = os.environ.get('RESINSIGHT_GRPC_PORT')
|
||||||
if portEnv:
|
if port_env:
|
||||||
startPort = int(portEnv)
|
start_port = int(port_env)
|
||||||
endPort = startPort + 20
|
end_port = start_port + 20
|
||||||
|
|
||||||
for tryPort in range(startPort, endPort):
|
for try_port in range(start_port, end_port):
|
||||||
if Instance.__is_port_in_use(tryPort):
|
if Instance.__is_port_in_use(try_port):
|
||||||
return Instance(port=tryPort)
|
return Instance(port=try_port)
|
||||||
|
|
||||||
print('Error: Could not find any ResInsight instances responding between ports ' + str(startPort) + ' and ' + str(endPort))
|
print('Error: Could not find any ResInsight instances responding between ports ' + str(start_port) + ' and ' + str(end_port))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __checkVersion(self):
|
def __check_version(self):
|
||||||
try:
|
try:
|
||||||
majorVersionOk = self.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
|
major_version_ok = self.major_version() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
|
||||||
minorVersionOk = self.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
|
minor_version_ok = self.minor_version() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
|
||||||
return True, majorVersionOk and minorVersionOk
|
return True, major_version_ok and minor_version_ok
|
||||||
except grpc.RpcError as e:
|
except:
|
||||||
return False, False
|
return False, False
|
||||||
|
|
||||||
def __init__(self, port = 50051, launched = False):
|
def __init__(self, port = 50051, launched = False):
|
||||||
@@ -135,25 +135,25 @@ class Instance:
|
|||||||
# Main version check package
|
# Main version check package
|
||||||
self.app = self.app = App_pb2_grpc.AppStub(self.channel)
|
self.app = self.app = App_pb2_grpc.AppStub(self.channel)
|
||||||
|
|
||||||
connectionOk = False
|
connection_ok = False
|
||||||
versionOk = False
|
version_ok = False
|
||||||
|
|
||||||
if self.launched:
|
if self.launched:
|
||||||
for i in range(0, 10):
|
for i in range(0, 10):
|
||||||
connectionOk, versionOk = self.__checkVersion()
|
connection_ok, version_ok = self.__check_version()
|
||||||
if connectionOk:
|
if connection_ok:
|
||||||
break
|
break
|
||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
else:
|
else:
|
||||||
connectionOk, versionOk = self.__checkVersion()
|
connection_ok, version_ok = self.__check_version()
|
||||||
|
|
||||||
if not connectionOk:
|
if not connection_ok:
|
||||||
if self.launched:
|
if self.launched:
|
||||||
raise Exception('Error: Could not connect to resinsight at ', location, ' after trying 10 times with 1 second apart')
|
raise Exception('Error: Could not connect to resinsight at ', location, ' after trying 10 times with 1 second apart')
|
||||||
else:
|
else:
|
||||||
raise Exception('Error: Could not connect to resinsight at ', location)
|
raise Exception('Error: Could not connect to resinsight at ', location)
|
||||||
exit(1)
|
exit(1)
|
||||||
if not versionOk:
|
if not version_ok:
|
||||||
raise Exception('Error: Wrong Version of ResInsight at ', location)
|
raise Exception('Error: Wrong Version of ResInsight at ', location)
|
||||||
|
|
||||||
# Service packages
|
# Service packages
|
||||||
@@ -163,34 +163,34 @@ class Instance:
|
|||||||
path = os.getcwd()
|
path = os.getcwd()
|
||||||
self.commands.set_start_dir(path=path)
|
self.commands.set_start_dir(path=path)
|
||||||
|
|
||||||
def __versionMessage(self):
|
def __version_message(self):
|
||||||
return self.app.GetVersion(Empty())
|
return self.app.GetVersion(Empty())
|
||||||
|
|
||||||
def majorVersion(self):
|
def major_version(self):
|
||||||
"""Get an integer with the major version number"""
|
"""Get an integer with the major version number"""
|
||||||
return self.__versionMessage().major_version
|
return self.__version_message().major_version
|
||||||
|
|
||||||
def minorVersion(self):
|
def minor_version(self):
|
||||||
"""Get an integer with the minor version number"""
|
"""Get an integer with the minor version number"""
|
||||||
return self.__versionMessage().minor_version
|
return self.__version_message().minor_version
|
||||||
|
|
||||||
def patchVersion(self):
|
def patch_version(self):
|
||||||
"""Get an integer with the patch version number"""
|
"""Get an integer with the patch version number"""
|
||||||
return self.__versionMessage().patch_version
|
return self.__version_message().patch_version
|
||||||
|
|
||||||
def versionString(self):
|
def version_string(self):
|
||||||
"""Get a full version string, i.e. 2019.04.01"""
|
"""Get a full version string, i.e. 2019.04.01"""
|
||||||
return str(self.majorVersion()) + "." + str(self.minorVersion()) + "." + str(self.patchVersion())
|
return str(self.major_version()) + "." + str(self.minor_version()) + "." + str(self.patch_version())
|
||||||
|
|
||||||
def exit(self):
|
def exit(self):
|
||||||
"""Tell ResInsight instance to quit"""
|
"""Tell ResInsight instance to quit"""
|
||||||
print("Telling ResInsight to Exit")
|
print("Telling ResInsight to Exit")
|
||||||
return self.app.Exit(Empty())
|
return self.app.Exit(Empty())
|
||||||
|
|
||||||
def isConsole(self):
|
def is_console(self):
|
||||||
"""Returns true if the connected ResInsight instance is a console app"""
|
"""Returns true if the connected ResInsight instance is a console app"""
|
||||||
return self.app.GetRuntimeInfo(Empty()).app_type == App_pb2.ApplicationTypeEnum.Value('CONSOLE_APPLICATION')
|
return self.app.GetRuntimeInfo(Empty()).app_type == App_pb2.ApplicationTypeEnum.Value('CONSOLE_APPLICATION')
|
||||||
|
|
||||||
def isGui(self):
|
def is_gui(self):
|
||||||
"""Returns true if the connected ResInsight instance is a GUI app"""
|
"""Returns true if the connected ResInsight instance is a GUI app"""
|
||||||
return self.app.GetRuntimeInfo(Empty()).app_type == App_pb2.ApplicationTypeEnum.Value('GUI_APPLICATION')
|
return self.app.GetRuntimeInfo(Empty()).app_type == App_pb2.ApplicationTypeEnum.Value('GUI_APPLICATION')
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ import rips
|
|||||||
|
|
||||||
resinsight = rips.Instance.find()
|
resinsight = rips.Instance.find()
|
||||||
if resinsight is not None:
|
if resinsight is not None:
|
||||||
print(resinsight.versionString())
|
print(resinsight.version_string())
|
||||||
print("Is this a console run?", resinsight.isConsole())
|
print("Is this a console run?", resinsight.is_console())
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Load ResInsight Processing Server Client Library
|
# Load ResInsight Processing Server Client Library
|
||||||
import rips
|
import rips
|
||||||
# Launch ResInsight with last project file and a Window size of 600x1000 pixels
|
# Launch ResInsight with last project file and a Window size of 600x1000 pixels
|
||||||
resinsight = rips.Instance.launch(commandLineParameters=['--last', '--size', 600, 1000])
|
resinsight = rips.Instance.launch(command_line_parameters=['--last', '--size', 600, 1000])
|
||||||
# Get a list of all cases
|
# Get a list of all cases
|
||||||
cases = resinsight.project.cases()
|
cases = resinsight.project.cases()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user