From cafe68f30ec230ef4c1e040ad227923072e5d8f2 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 28 Feb 2021 21:20:42 +0100 Subject: [PATCH] Move LogPipe directly into process.py, we don't need to split that stuff on different files --- moulinette/utils/process.py | 35 +++++++++++++++++++++++++++++++++-- moulinette/utils/stream.py | 36 ------------------------------------ 2 files changed, 33 insertions(+), 38 deletions(-) delete mode 100644 moulinette/utils/stream.py diff --git a/moulinette/utils/process.py b/moulinette/utils/process.py index a759e2e0..40b94358 100644 --- a/moulinette/utils/process.py +++ b/moulinette/utils/process.py @@ -9,8 +9,6 @@ import queue # process.quote syntax to access this module ! from shlex import quote -from .stream import LogPipe - quote # This line is here to avoid W0611 PEP8 error (see comments above) # Prevent to import subprocess only for common classes @@ -94,6 +92,39 @@ def call_async_output(args, callback, **kwargs): return p.poll() +class LogPipe(threading.Thread): + # Adapted from https://codereview.stackexchange.com/a/17959 + def __init__(self, log_callback, queue): + """Setup the object with a logger and a loglevel + and start the thread + """ + threading.Thread.__init__(self) + self.daemon = False + self.log_callback = log_callback + + self.fdRead, self.fdWrite = os.pipe() + self.pipeReader = os.fdopen(self.fdRead) + + self.queue = queue + + self.start() + + def fileno(self): + """Return the write file descriptor of the pipe""" + return self.fdWrite + + def run(self): + """Run the thread, logging everything.""" + for line in iter(self.pipeReader.readline, ""): + self.queue.put((self.log_callback, line.strip("\n"))) + + self.pipeReader.close() + + def close(self): + """Close the write end of the pipe.""" + os.close(self.fdWrite) + + # Call multiple commands ----------------------------------------------- diff --git a/moulinette/utils/stream.py b/moulinette/utils/stream.py deleted file mode 100644 index c3f1840f..00000000 --- a/moulinette/utils/stream.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import threading - -class LogPipe(threading.Thread): - # Adapted from https://codereview.stackexchange.com/a/17959 - def __init__(self, log_callback, queue): - """Setup the object with a logger and a loglevel - and start the thread - """ - threading.Thread.__init__(self) - self.daemon = False - self.log_callback = log_callback - - self.fdRead, self.fdWrite = os.pipe() - self.pipeReader = os.fdopen(self.fdRead) - - self.queue = queue - - self.start() - - def fileno(self): - """Return the write file descriptor of the pipe""" - return self.fdWrite - - def run(self): - """Run the thread, logging everything.""" - for line in iter(self.pipeReader.readline, ""): - self.queue.put((self.log_callback, line.strip("\n"))) - - self.pipeReader.close() - - def close(self): - """Close the write end of the pipe.""" - os.close(self.fdWrite) - -