add common execPythonScript method to rclexecm

This commit is contained in:
Jean-Francois Dockes 2020-04-07 10:09:09 +02:00
parent 32ebd65ba8
commit d3de1f0d6f
2 changed files with 15 additions and 11 deletions

View File

@ -32,6 +32,7 @@ import cmdtalk
PY3 = (sys.version > '3')
_mswindows = (sys.platform == "win32")
_execdir = os.path.dirname(sys.argv[0])
# Convert to bytes if not already such.
def makebytes(data):
@ -47,7 +48,7 @@ def subprocfile(fn):
# to convert.
# On Unix all list elements get converted to bytes in the C
# _posixsubprocess module, nothing to do.
if PY3 and _mswindows:
if PY3 and _mswindows and type(fn) != type(''):
return fn.decode('UTF-8')
else:
return fn
@ -239,7 +240,16 @@ def which(program):
return candidate
return None
# Execute Python script. cmd is a list with the script name as first elt.
def execPythonScript(icmd):
import subprocess
cmd = list(icmd)
if _mswindows:
if not os.path.isabs(cmd[0]):
cmd[0] = os.path.join(_execdir, cmd[0])
cmd = [sys.executable] + cmd
return subprocess.check_output(cmd)
# Temp dir helper
class SafeTmpDir:
def __init__(self, em):

View File

@ -36,7 +36,6 @@ from hwp5.transforms import BaseTransform
from hwp5.xmlmodel import Hwp5File as xml_Hwp5File
from hwp5.utils import cached_property
# Associate HTML meta names and hwp summaryinfo values
def metafields(summaryinfo):
yield(('Description', summaryinfo.subject + " " +
@ -49,8 +48,7 @@ def metafields(summaryinfo):
# Extractor class. We use hwp summaryinfo to extract metadata and code
# extracted from hwp.hwp5txt.py to extract the text.
class HWP5Dump(RclBaseHandler):
def __init__(self, em, td):
self.execdir = td
def __init__(self, em):
super(HWP5Dump, self).__init__(em)
def html_text(self, fn):
@ -83,13 +81,9 @@ class HWP5Dump(RclBaseHandler):
# the hwp5 module (no subproc). But this apparently mishandled
# tables. Switched to executing hwp5html instead. See 1st git
# version for the old approach.
cmd = [sys.executable, os.path.join(self.execdir, "hwp5html"),
"--html", fn]
html = subprocess.check_output(cmd)
return html
return rclexecm.execPythonScript(["hwp5html", "--html", fn])
if __name__ == '__main__':
execdir = os.path.dirname(sys.argv[0])
proto = rclexecm.RclExecM()
extract = HWP5Dump(proto, execdir)
extract = HWP5Dump(proto)
rclexecm.main(proto, extract)