From: Gilles Thouenon Date: Tue, 3 Oct 2023 07:35:06 +0000 (+0200) Subject: Fix new pylint-3.0.0 detected issues X-Git-Tag: 9.0.0~107 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=transportpce.git;a=commitdiff_plain;h=65f292021789e99cbf735b83537ca80554a75ba8 Fix new pylint-3.0.0 detected issues Refactor wait_until_log_contains method of test_utils taking advantage of the with block in order to remove the finally block. That simplifies the return flow. JIRA: TRNSPRTPCE-764 Signed-off-by: Gilles Thouenon Change-Id: I8b3fa004eddc9e8688640205490c385645f4d6e3 --- diff --git a/tests/transportpce_tests/common/test_utils.py b/tests/transportpce_tests/common/test_utils.py index 8ba812204..888a8e91f 100644 --- a/tests/transportpce_tests/common/test_utils.py +++ b/tests/transportpce_tests/common/test_utils.py @@ -273,34 +273,29 @@ def wait_until_log_contains(log_file, regexp, time_to_wait=60): # pylint: disable=lost-exception # pylint: disable=consider-using-with stringfound = False - filefound = False line = None try: with TimeOut(seconds=time_to_wait): while not os.path.exists(log_file): time.sleep(0.2) - filelogs = open(log_file, 'r', encoding='utf-8') - filelogs.seek(0, 2) - filefound = True - print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True) - compiled_regexp = re.compile(regexp) - while True: - line = filelogs.readline() - if compiled_regexp.search(line): - print('Pattern found!', end=' ') - stringfound = True - break - if not line: - time.sleep(0.1) + with open(log_file, 'r', encoding='utf-8') as filelogs: + filelogs.seek(0, 2) + print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True) + compiled_regexp = re.compile(regexp) + while True: + line = filelogs.readline() + if compiled_regexp.search(line): + print('Pattern found!', end=' ') + stringfound = True + break + if not line: + time.sleep(0.1) + return stringfound except TimeoutError: print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True) + return stringfound except PermissionError: print('Permission Error when trying to access the log file', end=' ... ', flush=True) - finally: - if filefound: - filelogs.close() - else: - print('log file does not exist or is not accessible... ', flush=True) return stringfound