support/testing: simplify logging by keeping the log file open

We currently call infra.smart_open() to open log files each time we
need to write to them.

Opening the file once in the constructor of Builder and Emulator and
writing to it whenever needed is simpler and slightly more efficient.

Remove smart_open and instead create a new open_log_file() function
which just opens the logfile. Also let it compute the filename, in
order to simplify even further the Builder and Emulator code.

Signed-off-by: Luca Ceresoli <luca@lucaceresoli.net>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
This commit is contained in:
Luca Ceresoli 2017-05-10 23:33:46 +02:00 committed by Thomas Petazzoni
parent fa3c5cad44
commit d332f2c521
3 changed files with 15 additions and 33 deletions

View File

@ -8,24 +8,17 @@ from urllib2 import urlopen, HTTPError, URLError
ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/" ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
@contextlib.contextmanager def open_log_file(builddir, stage, logtofile=True):
def smart_open(filename=None):
""" """
Return a file-like object that can be written to using the 'with' Open a file for logging and return its handler.
keyword, as in the example: If logtofile is True, returns sys.stdout. Otherwise opens a file
with infra.smart_open("test.log") as outfile: with a suitable name in the build directory.
outfile.write("Hello, world!\n")
""" """
if filename and filename != '-': if logtofile:
fhandle = open(filename, 'a+') fhandle = open("{}-{}.log".format(builddir, stage), 'a+')
else: else:
fhandle = sys.stdout fhandle = sys.stdout
return fhandle
try:
yield fhandle
finally:
if fhandle is not sys.stdout:
fhandle.close()
def filepath(relpath): def filepath(relpath):
return os.path.join(os.getcwd(), "support/testing", relpath) return os.path.join(os.getcwd(), "support/testing", relpath)

View File

@ -8,16 +8,12 @@ class Builder(object):
def __init__(self, config, builddir, logtofile): def __init__(self, config, builddir, logtofile):
self.config = config self.config = config
self.builddir = builddir self.builddir = builddir
self.logtofile = logtofile self.logfile = infra.open_log_file(builddir, "build", logtofile)
def build(self): def build(self):
if not os.path.isdir(self.builddir): if not os.path.isdir(self.builddir):
os.makedirs(self.builddir) os.makedirs(self.builddir)
log = "{}-build.log".format(self.builddir)
if not self.logtofile:
log = None
config_file = os.path.join(self.builddir, ".config") config_file = os.path.join(self.builddir, ".config")
with open(config_file, "w+") as cf: with open(config_file, "w+") as cf:
cf.write(self.config) cf.write(self.config)
@ -25,14 +21,12 @@ class Builder(object):
cmd = ["make", cmd = ["make",
"O={}".format(self.builddir), "O={}".format(self.builddir),
"olddefconfig"] "olddefconfig"]
with infra.smart_open(log) as log_fh: ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
ret = subprocess.call(cmd, stdout=log_fh, stderr=log_fh)
if ret != 0: if ret != 0:
raise SystemError("Cannot olddefconfig") raise SystemError("Cannot olddefconfig")
cmd = ["make", "-C", self.builddir] cmd = ["make", "-C", self.builddir]
with infra.smart_open(log) as log_fh: ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile)
ret = subprocess.call(cmd, stdout=log_fh, stderr=log_fh)
if ret != 0: if ret != 0:
raise SystemError("Build failed") raise SystemError("Build failed")

View File

@ -14,9 +14,7 @@ class Emulator(object):
self.__tn = None self.__tn = None
self.downloaddir = downloaddir self.downloaddir = downloaddir
self.log = "" self.log = ""
self.log_file = "{}-run.log".format(builddir) self.logfile = infra.open_log_file(builddir, "run", logtofile)
if logtofile is None:
self.log_file = None
# Start Qemu to boot the system # Start Qemu to boot the system
# #
@ -72,9 +70,8 @@ class Emulator(object):
if kernel_cmdline: if kernel_cmdline:
qemu_cmd += ["-append", " ".join(kernel_cmdline)] qemu_cmd += ["-append", " ".join(kernel_cmdline)]
with infra.smart_open(self.log_file) as lfh: self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
lfh.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd)) self.qemu = subprocess.Popen(qemu_cmd, stdout=self.logfile, stderr=self.logfile)
self.qemu = subprocess.Popen(qemu_cmd, stdout=lfh, stderr=lfh)
# Wait for the telnet port to appear and connect to it. # Wait for the telnet port to appear and connect to it.
while True: while True:
@ -88,8 +85,7 @@ class Emulator(object):
def __read_until(self, waitstr, timeout=5): def __read_until(self, waitstr, timeout=5):
data = self.__tn.read_until(waitstr, timeout) data = self.__tn.read_until(waitstr, timeout)
self.log += data self.log += data
with infra.smart_open(self.log_file) as lfh: self.logfile.write(data)
lfh.write(data)
return data return data
def __write(self, wstr): def __write(self, wstr):
@ -100,8 +96,7 @@ class Emulator(object):
def login(self, password=None): def login(self, password=None):
self.__read_until("buildroot login:", 10) self.__read_until("buildroot login:", 10)
if "buildroot login:" not in self.log: if "buildroot login:" not in self.log:
with infra.smart_open(self.log_file) as lfh: self.logfile.write("==> System does not boot")
lfh.write("==> System does not boot")
raise SystemError("System does not boot") raise SystemError("System does not boot")
self.__write("root\n") self.__write("root\n")