From bf522ad3e0c52cdb69b406226840d870ff4f2766 Mon Sep 17 00:00:00 2001 From: Chris Laplante Date: Fri, 7 Jun 2019 14:24:03 -0400 Subject: 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. Signed-off-by: Chris Laplante Signed-off-by: Richard Purdie --- lib/bb/build.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'lib/bb/build.py') diff --git a/lib/bb/build.py b/lib/bb/build.py index a0a764a7c..85ad8ea68 100644 --- a/lib/bb/build.py +++ b/lib/bb/build.py @@ -163,12 +163,35 @@ class LogTee(object): def __repr__(self): return ''.format(self.name) + def flush(self): 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 -# 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. # def exec_func(func, d, dirs = None, pythonexception=False): @@ -375,9 +398,9 @@ exit $ret cmd = [fakerootcmd, runfile] if bb.msg.loggerDefaultVerbose: - logfile = LogTee(logger, sys.stdout) + logfile = LogTee(logger, StdoutNoopContextManager()) else: - logfile = sys.stdout + logfile = StdoutNoopContextManager() progress = d.getVarFlag(func, 'progress') if progress: @@ -433,7 +456,7 @@ exit $ret bb.debug(2, "Executing shell function %s" % func) 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)]) except bb.process.CmdError: logfn = d.getVar('BB_LOGFILE') -- cgit 1.2.3-korg