Skip to content

Commit

Permalink
Simplify spawn's logfile closing from using globals to other projects
Browse files Browse the repository at this point in the history
Arbitrary functions can be used as closing hooks for the spawns
and we only need to provide a minimal logfile closing hook and
functionality letting the caller design their own hooks of any
further complexity.

Signed-off-by: Plamen Dimitrov <[email protected]>
  • Loading branch information
pevogam committed Oct 16, 2023
1 parent b24c729 commit 92ac265
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 42 deletions.
15 changes: 9 additions & 6 deletions aexpect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Check warning

Code scanning / CodeQL

File is not always closed Warning

File is opened but is not closed.

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

Expand Down
2 changes: 0 additions & 2 deletions aexpect/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import logging
import time
import re
import os
import pipes

from aexpect.client import Expect
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 0 additions & 34 deletions aexpect/utils/genio.py

This file was deleted.

0 comments on commit 92ac265

Please sign in to comment.