diff --git a/aexpect/client.py b/aexpect/client.py index fba78aa..696f7dc 100644 --- a/aexpect/client.py +++ b/aexpect/client.py @@ -47,7 +47,6 @@ from aexpect.utils import astring from aexpect.utils import data_factory -from aexpect.utils import genio from aexpect.utils import process as utils_process from aexpect.utils import path as utils_path from aexpect.utils import wait as utils_wait @@ -129,6 +128,7 @@ def __init__(self, command=None, a_id=None, auto_close=False, echo=False, """ self.a_id = a_id or data_factory.generate_random_string(8) self.log_file = None + self.log_file_fd = None self.closed = False if encoding is None: self.encoding = locale.getpreferredencoding() @@ -610,17 +610,20 @@ def set_output_prefix(self, output_prefix): """ self.output_prefix = output_prefix - def set_log_file(self, filename): + def set_log_file(self, filepath, open_fd=False): """ Set a log file name for this tail instance. - :param filename: Base name of the log. + :param filepath: complete file name and path of the log. + :param open_fd: whether to also open the log file """ - self.log_file = filename + self.log_file = filepath + if open_fd: + self.log_file_fd = open(filepath, encoding="utf-8") # pylint: disable=R1732 def _close_log_file(self): - if self.log_file is not None: - genio.close_log_file(self.log_file) + if self.log_file_fd is not None: + self.log_file_fd.close() def _tail(self): # speed optimization pylint: disable=too-many-branches,too-many-statements diff --git a/aexpect/remote.py b/aexpect/remote.py index 0b7ff59..a20d088 100644 --- a/aexpect/remote.py +++ b/aexpect/remote.py @@ -46,7 +46,6 @@ import logging import time import re -import os import pipes from aexpect.client import Expect @@ -414,7 +413,6 @@ def remote_login(client, host, port, username, password, prompt, linesep="\n", output_params = () if log_filename: output_params = (log_filename,) - log_filename = os.path.basename(log_filename) if verbose: LOG.debug("Login command: '%s'", cmd) diff --git a/aexpect/utils/genio.py b/aexpect/utils/genio.py deleted file mode 100644 index 5bf6fa0..0000000 --- a/aexpect/utils/genio.py +++ /dev/null @@ -1,34 +0,0 @@ -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# -# See LICENSE for more details. - -""" -Naive module that keeps tacks of some opened files and somehow manages them. -""" - -import os - -# This variable is used from Avocado-vt -_open_log_files = {} # pylint: disable=C0103 - - -def close_log_file(filename): - """ - This closes all files that use the same base name "filename". - """ - - remove = [] - for log_file, log_fd in _open_log_files.items(): - if os.path.basename(log_file) == filename: - log_fd.close() - remove.append(log_file) - if remove: - for key_to_remove in remove: - _open_log_files.pop(key_to_remove)