From: Guy Rutenberg Date: Sat, 11 Jul 2015 15:52:14 +0000 (+0300) Subject: lyxpak.py: Use `subprocess` for spawning scripts. X-Git-Tag: 2.2.0alpha1~391 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=be41ccd4d43a47570e2c53481361593b9e453069;p=features.git lyxpak.py: Use `subprocess` for spawning scripts. `os.popen` is deprecated since Python-2.6. Also, this fixes the handling of files with quotes in their names. The patch requires Python >= 2.7. Furthermore, the patch executes the lyx2lyx script with the same interpreter used for it, to maintain compatibility. I also removed some lines related to Python-2.4, as it is no longer supported anyway. --- diff --git a/lib/scripts/lyxpak.py b/lib/scripts/lyxpak.py index 9a56bf03c1..bf66c70b6c 100755 --- a/lib/scripts/lyxpak.py +++ b/lib/scripts/lyxpak.py @@ -16,10 +16,13 @@ # line options, however. import gzip, os, re, string, sys -if sys.version_info < (2, 4, 0): - from sets import Set as set from getopt import getopt from cStringIO import StringIO +import subprocess + +# The path to the current python executable. sys.executable may fail, so in +# this case we revert to simply calling "python" from the path. +PYTHON_BIN = sys.executable if sys.executable else "python" running_on_windows = (os.name == 'nt') @@ -69,13 +72,6 @@ def gzopen(file, mode): return open(unicode(file, 'utf-8'), mode) -def run_cmd(cmd): - handle = os.popen(cmd, 'r') - cmd_stdout = handle.read() - cmd_status = handle.close() - return cmd_status, cmd_stdout - - def find_exe(candidates, extlist, path): for prog in candidates: for directory in path: @@ -98,6 +94,7 @@ def gather_files(curfile, incfiles, lyx2lyx): " Recursively gather files." curdir = os.path.dirname(abspath(curfile)) is_lyxfile = re_lyxfile.search(curfile) + if is_lyxfile: if running_on_windows: # os.popen cannot cope with unicode arguments and we cannot be @@ -106,14 +103,16 @@ def gather_files(curfile, incfiles, lyx2lyx): tmp = NamedTemporaryFile(delete=False) tmp.close() copyfile(unicode(curfile, 'utf-8'), tmp.name) - lyx2lyx_cmd = 'python "%s" "%s"' % (lyx2lyx, tmp.name) - l2l_status, l2l_stdout = run_cmd(lyx2lyx_cmd) + try: + l2l_stdout = subprocess.check_output([PYTHON_BIN, lyx2lyx, tmp.name]) + except subprocess.CalledProcessError: + error('%s failed to convert "%s"' % (lyx2lyx, curfile)) os.unlink(tmp.name) else: - lyx2lyx_cmd = 'python "%s" "%s"' % (lyx2lyx, curfile) - l2l_status, l2l_stdout = run_cmd(lyx2lyx_cmd) - if l2l_status != None: - error('%s failed to convert "%s"' % (lyx2lyx, curfile)) + try: + l2l_stdout = subprocess.check_output([PYTHON_BIN, lyx2lyx, curfile]) + except subprocess.CalledProcessError: + error('%s failed to convert "%s"' % (lyx2lyx, curfile)) if l2l_stdout.startswith("\x1f\x8b"): l2l_stdout = gzip.GzipFile("", "r", 0, StringIO(l2l_stdout)).read() lines = l2l_stdout.splitlines() @@ -222,8 +221,9 @@ def find_lyx2lyx(progloc, path): lyx_exe, full_path = find_exe(["lyxc", "lyx"], extlist, path) if lyx_exe == None: error('Cannot find the LyX executable in the path.') - cmd_status, cmd_stdout = run_cmd("%s -version 2>&1" % lyx_exe) - if cmd_status != None: + try: + cmd_stdout = subprocess.check_output([lyx_exe, '-version'], stderr=subprocess.STDOUT) + except subprocess.CalledProcessError: error('Cannot query LyX about the lyx2lyx script.') re_msvc = re.compile(r'^(\s*)(Host type:)(\s+)(win32)$') re_sysdir = re.compile(r'^(\s*)(LyX files dir:)(\s+)(\S+)$')