Fix error unclosed IO on external binary version query

This commit is contained in:
Ozzie Isaacs 2021-07-11 09:19:27 +02:00
parent a27314464a
commit 259ac94b93
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: if argument:
command.append(argument) command.append(argument)
try: try:
for line in process_wait(command): return process_wait(command, pattern=pattern).string
if re.search(pattern, line):
return line
except Exception as ex: except Exception as ex:
log.warning("%s: %s", path, ex) log.warning("%s: %s", path, ex)
return _EXECUTION_ERROR return _EXECUTION_ERROR

View File

@ -711,12 +711,11 @@ def check_unrar(unrarLocation):
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
unrarLocation = unrarLocation.encode(sys.getfilesystemencoding()) unrarLocation = unrarLocation.encode(sys.getfilesystemencoding())
unrarLocation = [unrarLocation] unrarLocation = [unrarLocation]
for lines in process_wait(unrarLocation): value = process_wait(unrarLocation, pattern='UNRAR (.*) freeware')
value = re.search('UNRAR (.*) freeware', lines, re.IGNORECASE) if value:
if value: version = value.group(1)
version = value.group(1) log.debug("unrar version %s", version)
log.debug("unrar version %s", version)
break
except (OSError, UnicodeDecodeError) as err: except (OSError, UnicodeDecodeError) as err:
log.debug_or_exception(err) log.debug_or_exception(err)
return _('Error excecuting UnRar') return _('Error excecuting UnRar')

View File

@ -20,7 +20,7 @@ from __future__ import division, print_function, unicode_literals
import sys import sys
import os import os
import subprocess import subprocess
import re
def process_open(command, quotes=(), env=None, sout=subprocess.PIPE, serr=subprocess.PIPE, newlines=True): 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 # 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 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. # Run command, wait for process to terminate, and return an iterator over lines of its output.
newlines = os.name != 'nt' newlines = os.name != 'nt'
ret_val = ""
p = process_open(command, serr=serr, newlines=newlines) p = process_open(command, serr=serr, newlines=newlines)
p.wait() p.wait()
for line in p.stdout.readlines(): for line in p.stdout.readlines():
if isinstance(line, bytes): if isinstance(line, bytes):
line = line.decode('utf-8') 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