anisotropy/salomepl/utils.py

73 lines
1.6 KiB
Python
Raw Normal View History

2021-03-24 21:27:12 +05:00
#import salome
2021-03-22 15:44:22 +05:00
import subprocess
import logging
2021-05-17 00:39:07 +05:00
import sys, os
2021-03-18 17:29:30 +05:00
def hasDesktop() -> bool:
return salome.sg.hasDesktop()
2021-03-19 21:59:30 +05:00
class SalomeNotFound(Exception):
pass
2021-03-19 21:59:30 +05:00
def version() -> str:
if os.environ.get("SALOME_PATH"):
cmd = os.path.join(os.environ["SALOME_PATH"], "salome")
else:
raise(SalomeNotFound("Can't find salome executable."))
proc = subprocess.Popen(
[ cmd, "--version" ],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, err = proc.communicate()
2021-04-14 15:13:41 +05:00
return str(out, "utf-8").strip().split(" ")[-1]
2021-04-14 15:13:41 +05:00
def runSalome(port: int, scriptpath: str, root: str, logpath: str = None, *args) -> int:
2021-04-14 15:13:41 +05:00
if os.environ.get("SALOME_PATH"):
2021-08-02 16:36:06 +05:00
cmd = [ os.path.join(os.environ["SALOME_PATH"], "salome") ]
else:
raise(SalomeNotFound("Can't find salome executable."))
2021-03-24 21:27:12 +05:00
if not logpath:
logpath = "/tmp/salome.log"
2021-03-19 21:59:30 +05:00
2021-07-26 18:00:23 +05:00
fullargs = list(args)
fullargs.extend([ root, logpath ])
fmtargs = "args:{}".format(", ".join([ str(arg) for arg in args ]))
cmdargs = [
2021-07-26 18:00:23 +05:00
"start", "-t",
"--shutdown-servers=1",
"--port", str(port),
scriptpath,
fmtargs
]
2021-03-19 21:59:30 +05:00
2021-08-02 16:36:06 +05:00
cmd.extend(cmdargs)
with subprocess.Popen(
2021-08-02 16:36:06 +05:00
cmd,
2021-03-22 22:49:56 +05:00
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
) as proc, open(logpath, "wb") as logfile:
2021-03-19 21:59:30 +05:00
logfile = open(logpath, "wb")
for line in proc.stdout:
logfile.write(line)
2021-03-19 21:59:30 +05:00
2021-08-02 16:36:06 +05:00
out, err = proc.communicate()
2021-03-19 21:59:30 +05:00
if err:
logfile.write(err)
return out, err, proc.returncode
2021-03-19 21:59:30 +05:00