From f8de7e75cc88264d3b1a8ee23cf3e49de0f5794d Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 11 Jul 2021 09:19:46 +0200 Subject: [PATCH] Fix error unclosed IO on external binary version query --- cps/converter.py | 4 +--- cps/helper.py | 11 +++++------ cps/subproc_wrapper.py | 12 +++++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cps/converter.py b/cps/converter.py index 2ff73666..f37168c7 100644 --- a/cps/converter.py +++ b/cps/converter.py @@ -39,9 +39,7 @@ def _get_command_version(path, pattern, argument=None): if argument: command.append(argument) try: - for line in process_wait(command): - if re.search(pattern, line): - return line + return process_wait(command, pattern=pattern).string except Exception as ex: log.warning("%s: %s", path, ex) return _EXECUTION_ERROR diff --git a/cps/helper.py b/cps/helper.py index d567e9b3..b6ea6760 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -711,12 +711,11 @@ def check_unrar(unrarLocation): if sys.version_info < (3, 0): unrarLocation = unrarLocation.encode(sys.getfilesystemencoding()) unrarLocation = [unrarLocation] - for lines in process_wait(unrarLocation): - value = re.search('UNRAR (.*) freeware', lines, re.IGNORECASE) - if value: - version = value.group(1) - log.debug("unrar version %s", version) - break + value = process_wait(unrarLocation, pattern='UNRAR (.*) freeware') + if value: + version = value.group(1) + log.debug("unrar version %s", version) + except (OSError, UnicodeDecodeError) as err: log.debug_or_exception(err) return _('Error excecuting UnRar') diff --git a/cps/subproc_wrapper.py b/cps/subproc_wrapper.py index c6c65851..27375686 100644 --- a/cps/subproc_wrapper.py +++ b/cps/subproc_wrapper.py @@ -20,7 +20,7 @@ from __future__ import division, print_function, unicode_literals import sys import os import subprocess - +import re def process_open(command, quotes=(), env=None, sout=subprocess.PIPE, serr=subprocess.PIPE, newlines=True): # Linux py2.7 encode as list without quotes no empty element for parameters @@ -44,12 +44,18 @@ def process_open(command, quotes=(), env=None, sout=subprocess.PIPE, serr=subpro return subprocess.Popen(exc_command, shell=False, stdout=sout, stderr=serr, universal_newlines=newlines, env=env) # nosec -def process_wait(command, serr=subprocess.PIPE): +def process_wait(command, serr=subprocess.PIPE, pattern=""): # Run command, wait for process to terminate, and return an iterator over lines of its output. newlines = os.name != 'nt' + ret_val = "" p = process_open(command, serr=serr, newlines=newlines) p.wait() for line in p.stdout.readlines(): if isinstance(line, bytes): line = line.decode('utf-8') - yield line + match = re.search(pattern, line, re.IGNORECASE) + if match and ret_val == "": + ret_val = match + p.stdout.close() + p.stderr.close() + return ret_val