mirror of
https://git.yoctoproject.org/poky
synced 2026-09-19 09:49:33 +02:00
bitbake: build/progress: use context managers for progress handlers
It seems context management support was half-implemented, but never finished. For example, LogTee has __enter__ and __exit__ but they haven't been exercised until now. (Bitbake rev: bf522ad3e0c52cdb69b406226840d870ff4f2766) Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
committed by
Richard Purdie
parent
575ea5532f
commit
7687469590
@@ -163,12 +163,35 @@ class LogTee(object):
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<LogTee {0}>'.format(self.name)
|
return '<LogTee {0}>'.format(self.name)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
self.outfile.flush()
|
self.outfile.flush()
|
||||||
|
|
||||||
|
|
||||||
|
class StdoutNoopContextManager:
|
||||||
|
"""
|
||||||
|
This class acts like sys.stdout, but adds noop __enter__ and __exit__ methods.
|
||||||
|
"""
|
||||||
|
def __enter__(self):
|
||||||
|
return sys.stdout
|
||||||
|
|
||||||
|
def __exit__(self, *exc_info):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def write(self, string):
|
||||||
|
return sys.stdout.write(string)
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return sys.stdout.name
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# pythonexception allows the python exceptions generated to be raised
|
# pythonexception allows the python exceptions generated to be raised
|
||||||
# as the real exceptions (not FuncFailed) and without a backtrace at the
|
# as the real exceptions (not FuncFailed) and without a backtrace at the
|
||||||
# origin of the failure.
|
# origin of the failure.
|
||||||
#
|
#
|
||||||
def exec_func(func, d, dirs = None, pythonexception=False):
|
def exec_func(func, d, dirs = None, pythonexception=False):
|
||||||
@@ -375,9 +398,9 @@ exit $ret
|
|||||||
cmd = [fakerootcmd, runfile]
|
cmd = [fakerootcmd, runfile]
|
||||||
|
|
||||||
if bb.msg.loggerDefaultVerbose:
|
if bb.msg.loggerDefaultVerbose:
|
||||||
logfile = LogTee(logger, sys.stdout)
|
logfile = LogTee(logger, StdoutNoopContextManager())
|
||||||
else:
|
else:
|
||||||
logfile = sys.stdout
|
logfile = StdoutNoopContextManager()
|
||||||
|
|
||||||
progress = d.getVarFlag(func, 'progress')
|
progress = d.getVarFlag(func, 'progress')
|
||||||
if progress:
|
if progress:
|
||||||
@@ -433,7 +456,7 @@ exit $ret
|
|||||||
bb.debug(2, "Executing shell function %s" % func)
|
bb.debug(2, "Executing shell function %s" % func)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(os.devnull, 'r+') as stdin:
|
with open(os.devnull, 'r+') as stdin, logfile:
|
||||||
bb.process.run(cmd, shell=False, stdin=stdin, log=logfile, extrafiles=[(fifo,readfifo)])
|
bb.process.run(cmd, shell=False, stdin=stdin, log=logfile, extrafiles=[(fifo,readfifo)])
|
||||||
except bb.process.CmdError:
|
except bb.process.CmdError:
|
||||||
logfn = d.getVar('BB_LOGFILE')
|
logfn = d.getVar('BB_LOGFILE')
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import time
|
|||||||
import inspect
|
import inspect
|
||||||
import bb.event
|
import bb.event
|
||||||
import bb.build
|
import bb.build
|
||||||
|
from bb.build import StdoutNoopContextManager
|
||||||
|
|
||||||
class ProgressHandler(object):
|
class ProgressHandler(object):
|
||||||
"""
|
"""
|
||||||
@@ -27,7 +28,14 @@ class ProgressHandler(object):
|
|||||||
if outfile:
|
if outfile:
|
||||||
self._outfile = outfile
|
self._outfile = outfile
|
||||||
else:
|
else:
|
||||||
self._outfile = sys.stdout
|
self._outfile = StdoutNoopContextManager()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self._outfile.__enter__()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *excinfo):
|
||||||
|
self._outfile.__exit__(*excinfo)
|
||||||
|
|
||||||
def _fire_progress(self, taskprogress, rate=None):
|
def _fire_progress(self, taskprogress, rate=None):
|
||||||
"""Internal function to fire the progress event"""
|
"""Internal function to fire the progress event"""
|
||||||
@@ -147,6 +155,12 @@ class MultiStageProgressReporter(object):
|
|||||||
self._stage_total = None
|
self._stage_total = None
|
||||||
self._callers = []
|
self._callers = []
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *excinfo):
|
||||||
|
pass
|
||||||
|
|
||||||
def _fire_progress(self, taskprogress):
|
def _fire_progress(self, taskprogress):
|
||||||
bb.event.fire(bb.build.TaskProgress(taskprogress), self._data)
|
bb.event.fire(bb.build.TaskProgress(taskprogress), self._data)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user