return '<LogTee {0}>'.format(self.name)
-def exec_func(func, d, dirs = None, logfile = NULL):
+def exec_func(func, d, dirs = None):
"""Execute an BB 'function'"""
body = data.getVar(func, d)
with bb.utils.fileslocked(lockfiles):
if ispython:
- exec_func_python(func, d, runfile, logfile, cwd=adir)
+ exec_func_python(func, d, runfile, cwd=adir)
else:
- exec_func_shell(func, d, runfile, logfile, cwd=adir, fakeroot=fakeroot)
+ exec_func_shell(func, d, runfile, cwd=adir, fakeroot=fakeroot)
_functionfmt = """
def {function}(d):
{function}(d)
"""
logformatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
-def exec_func_python(func, d, runfile, logfile, cwd=None):
+def exec_func_python(func, d, runfile, cwd=None):
"""Execute a python BB 'function'"""
bbfile = d.getVar('FILE', True)
if cwd:
os.chdir(cwd)
- handler = logging.StreamHandler(logfile)
- handler.setFormatter(logformatter)
- bblogger.addHandler(handler)
-
try:
comp = utils.better_compile(code, func, bbfile)
utils.better_exec(comp, {"d": d}, code, bbfile)
raise FuncFailed(func, None)
finally:
- bblogger.removeHandler(handler)
os.chdir(olddir)
-def exec_func_shell(function, d, runfile, logfile, cwd=None, fakeroot=False):
+def exec_func_shell(function, d, runfile, cwd=None, fakeroot=False):
"""Execute a shell function from the metadata
Note on directory behavior. The 'dirs' varflag should contain a list
cmd = runfile
if logger.isEnabledFor(logging.DEBUG):
- logfile = LogTee(logger, logfile)
+ logfile = LogTee(logger, sys.stdout)
+ else:
+ logfile = sys.stdout
try:
bb.process.run(cmd, cwd=cwd, shell=False, stdin=NULL, log=logfile)
except bb.process.CmdError:
- raise FuncFailed(function, logfile.name)
+ logfn = d.getVar('BB_LOGFILE', True)
+ raise FuncFailed(function, logfn)
def _task_data(fn, task, d):
localdata = data.createCopy(d)
prefuncs = localdata.getVarFlag(task, 'prefuncs', expand=True)
postfuncs = localdata.getVarFlag(task, 'postfuncs', expand=True)
- logfile = open(logfn, 'w')
+ # Handle logfiles
+ si = file('/dev/null', 'r')
+ try:
+ logfile = file(logfn, 'w')
+ except OSError:
+ logger.exception("Opening log file '%s'", logfn)
+ pass
+
+ # Dup the existing fds so we dont lose them
+ osi = [os.dup(sys.stdin.fileno()), sys.stdin.fileno()]
+ oso = [os.dup(sys.stdout.fileno()), sys.stdout.fileno()]
+ ose = [os.dup(sys.stderr.fileno()), sys.stderr.fileno()]
+
+ # Replace those fds with our own
+ os.dup2(si.fileno(), osi[1])
+ os.dup2(logfile.fileno(), oso[1])
+ os.dup2(logfile.fileno(), ose[1])
+
+ # Ensure python logging goes to the logfile
+ handler = logging.StreamHandler(logfile)
+ handler.setFormatter(logformatter)
+ bblogger.addHandler(handler)
+
+ localdata.setVar('BB_LOGFILE', logfn)
+
event.fire(TaskStarted(task, localdata), localdata)
try:
for func in (prefuncs or '').split():
- exec_func(func, localdata, logfile=logfile)
- exec_func(task, localdata, logfile=logfile)
+ exec_func(func, localdata)
+ exec_func(task, localdata)
for func in (postfuncs or '').split():
- exec_func(func, localdata, logfile=logfile)
+ exec_func(func, localdata)
except FuncFailed as exc:
logger.error(str(exc))
event.fire(TaskFailed(task, logfn, localdata), localdata)
return 1
finally:
+ sys.stdout.flush()
+ sys.stderr.flush()
+
+ bblogger.removeHandler(handler)
+
+ # Restore the backup fds
+ os.dup2(osi[0], osi[1])
+ os.dup2(oso[0], oso[1])
+ os.dup2(ose[0], ose[1])
+
+ # Close the backup fds
+ os.close(osi[0])
+ os.close(oso[0])
+ os.close(ose[0])
+ si.close()
+
logfile.close()
if os.path.exists(logfn) and os.path.getsize(logfn) == 0:
logger.debug(2, "Zero size logfn %s, removing", logfn)