Fix error unclosed IO on external binary version query

This commit is contained in:
Ozzie Isaacs 2021-07-11 09:19:46 +02:00
parent 87f07003f4
commit f8de7e75cc
3 changed files with 15 additions and 12 deletions

View File

@ -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

View File

@ -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')

View File

@ -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