New: SalomeManager

Mod: docs
New: bashrc example
This commit is contained in:
L-Nafaryus 2021-08-25 17:00:55 +05:00
parent 3f8d798965
commit 8b70f273db
No known key found for this signature in database
GPG Key ID: C76D8DCD2727DBB7
8 changed files with 103 additions and 60 deletions

3
INSTALL.rst Normal file
View File

@ -0,0 +1,3 @@
Installation
============

View File

@ -27,7 +27,7 @@ env.update(
env.update(
logger_name = "anisotropy",
db_path = env["BUILD"],
salome_port = 2810,
salome_timeout = 15 * 60,
openfoam_template = os.path.join(env["ROOT"], "anisotropy/openfoam/template")
)

View File

@ -74,7 +74,7 @@ class Anisotropy(object):
}
try:
versions["Salome"] = salomepl.utils.version()
versions["Salome"] = salomepl.utils.SalomeManager().version()
versions["OpenFOAM"] = openfoam.version()
except Exception:
@ -230,20 +230,25 @@ class Anisotropy(object):
:return: Process output, error messages and returncode
:rtype: tuple(str, str, int)
"""
scriptpath = os.path.join(self.env["ROOT"], "anisotropy/core/cli.py")
port = 2900
p = self.params["structure"]
return salomepl.utils.runSalome(
self.env["salome_port"],
scriptpath,
self.env["ROOT"],
scriptpath = os.path.join(self.env["ROOT"], "anisotropy/core/cli.py")
salomeargs = [
"computemesh",
p["type"], p["direction"], p["theta"],
p["type"],
p["direction"],
p["theta"],
]
manager = salomepl.utils.SalomeManager()
return manager.execute(
scriptpath,
*salomeargs,
timeout = self.env["salome_timeout"],
root = self.env["ROOT"],
logpath = os.path.join(self.env["LOG"], "salome.log")
)
def genmesh(self):
"""Computes a mesh on shape

View File

@ -2,76 +2,105 @@
# This file is part of anisotropy.
# License: GNU GPL version 3, see the file "LICENSE" for details.
#import salome
import subprocess
import logging
import sys, os
def hasDesktop() -> bool:
return salome.sg.hasDesktop()
import re
class SalomeNotFound(Exception):
pass
def version() -> str:
if os.environ.get("SALOME_PATH"):
cmd = os.path.join(os.environ["SALOME_PATH"], "salome")
class SalomeManager(object):
def __init__(self):
self.__port = None
self.__lastproc = None
else:
raise(SalomeNotFound("Can't find salome executable."))
proc = subprocess.Popen(
[ cmd, "--version" ],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, err = proc.communicate()
return str(out, "utf-8").strip().split(" ")[-1]
def runSalome(port: int, scriptpath: str, root: str, *args, logpath: str = None) -> int:
# ISSUE: salome removes commas from string list
if os.environ.get("SALOME_PATH"):
cmd = [ os.path.join(os.environ["SALOME_PATH"], "salome") ]
else:
raise(SalomeNotFound("Can't find salome executable."))
if not logpath:
logpath = "/tmp/salome.log"
#fullargs = list(args)
args = list(args)
args.insert(1, root)
fmtargs = "args:{}".format(",".join([ '"{}"'.format(str(arg)) for arg in args ]))
cmdargs = [
"start", "-t",
"--shutdown-servers=1",
"--port", str(port),
scriptpath,
fmtargs
]
cmd.extend(cmdargs)
def runner(self, cmdargs: list, **kwargs):
timeout = kwargs.pop("timeout") if kwargs.get("timeout") else None
try:
with subprocess.Popen(
cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
) as proc, open(logpath, "wb") as logfile:
cmdargs,
stdout = kwargs.pop("stdout") if kwargs.get("stdout") else subprocess.PIPE,
stderr = kwargs.pop("stdout") if kwargs.get("stderr") else subprocess.PIPE,
**kwargs
) as proc:
self.__lastproc = proc
out, err = proc.communicate(timeout = timeout)
logfile = open(logpath, "wb")
for line in proc.stdout:
logfile.write(line)
out, err = proc.communicate()
if err:
logfile.write(err)
except FileNotFoundError:
raise SalomeNotFound()
return out, err, proc.returncode
def port(self) -> int:
out, err, returncode = self.runner(["salome", "start", "--print-port"], text = True)
if returncode == 0:
reg = re.search("(?!port:)([0-9]+)", out)
if reg:
return int(reg.group())
return 2810
def version(self) -> int:
out, err, returncode = self.runner(["salome", "--version"], text = True)
return out.strip().split(" ")[-1]
def kill(self, port: int = None):
return self.runner(["salome", "kill", str(self.__port or port)])
def execute(self, scriptpath: str, *args, root: str = None, logpath: str = None, timeout: int = None, **kwargs):
if not root:
root = os.environ["HOME"]
# ISSUE: salome removes commas from string list
args = list(args)
args.insert(1, root)
salomeargs = "args:"
salomeargs += ",".join([ '"{}"'.format(str(arg)) for arg in args ])
if kwargs:
salomeargs += "," + ",".join([ '{}="{}"'.format(k, v) for k, v in kwargs.items() ])
###
self.__port = self.port()
cmd = [
"salome",
"start",
"-t",
"--shutdown-servers=1",
"--port", str(self.__port),
scriptpath,
salomeargs
]
try:
out, err, returncode = self.runner(cmd, timeout = timeout)
except subprocess.TimeoutExpired:
lastproc = self.__lastproc
self.kill()
out, err = lastproc.communicate()
returncode = lastproc.returncode
if logpath:
with open(os.path.join(logpath, "salome.log"), "wb") as io:
io.write(out)
io.write(err)
return str(out, "utf-8"), str(err, "utf-8"), returncode

2
conf/bashrc Normal file
View File

@ -0,0 +1,2 @@
export PATH="${PATH}:${HOME}/programs/salome/SALOME-9.7.0-MPI"
source "${HOME}/programs/OpenFOAM/OpenFOAM-v2012/etc/bashrc"

3
docs/source/INSTALL.rst Normal file
View File

@ -0,0 +1,3 @@
Installation
============

View File

@ -5,7 +5,7 @@ Subpackages
-----------
.. toctree::
:maxdepth: 4
:maxdepth: 1
anisotropy.core
anisotropy.openfoam

View File

@ -10,7 +10,8 @@ Welcome to anisotropy's documentation!
:maxdepth: 2
:caption: Contents:
modules
INSTALL
anisotropy
.. include:: ../../README.rst