From d3de1f0d6f660ecc54a21d20d0d586b86a840166 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 7 Apr 2020 10:09:09 +0200 Subject: [PATCH] add common execPythonScript method to rclexecm --- src/filters/rclexecm.py | 14 ++++++++++++-- src/filters/rclhwp.py | 12 +++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/filters/rclexecm.py b/src/filters/rclexecm.py index 95eea9ca..5007df9e 100644 --- a/src/filters/rclexecm.py +++ b/src/filters/rclexecm.py @@ -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): diff --git a/src/filters/rclhwp.py b/src/filters/rclhwp.py index 15fedaf7..bb682dd7 100755 --- a/src/filters/rclhwp.py +++ b/src/filters/rclhwp.py @@ -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)