kumquat-buildroot/support/testing/infra/__init__.py
Luca Ceresoli d332f2c521 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>
2017-05-17 21:59:58 +02:00

93 lines
2.8 KiB
Python

import contextlib
import os
import re
import sys
import tempfile
import subprocess
from urllib2 import urlopen, HTTPError, URLError
ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
def open_log_file(builddir, stage, logtofile=True):
"""
Open a file for logging and return its handler.
If logtofile is True, returns sys.stdout. Otherwise opens a file
with a suitable name in the build directory.
"""
if logtofile:
fhandle = open("{}-{}.log".format(builddir, stage), 'a+')
else:
fhandle = sys.stdout
return fhandle
def filepath(relpath):
return os.path.join(os.getcwd(), "support/testing", relpath)
def download(dldir, filename):
finalpath = os.path.join(dldir, filename)
if os.path.exists(finalpath):
return finalpath
if not os.path.exists(dldir):
os.makedirs(dldir)
tmpfile = tempfile.mktemp(dir=dldir)
print "Downloading to {}".format(tmpfile)
try:
url_fh = urlopen(os.path.join(ARTIFACTS_URL, filename))
with open(tmpfile, "w+") as tmpfile_fh:
tmpfile_fh.write(url_fh.read())
except (HTTPError, URLError), err:
os.unlink(tmpfile)
raise err
print "Renaming from %s to %s" % (tmpfile, finalpath)
os.rename(tmpfile, finalpath)
return finalpath
def get_elf_arch_tag(builddir, prefix, fpath, tag):
"""
Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
Example:
>>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
'bin/busybox', 'Tag_CPU_arch')
v5TEJ
>>>
"""
cmd = ["host/usr/bin/{}-readelf".format(prefix),
"-A", os.path.join("target", fpath)]
out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
regexp = re.compile("^ {}: (.*)$".format(tag))
for line in out.splitlines():
m = regexp.match(line)
if not m:
continue
return m.group(1)
return None
def get_file_arch(builddir, prefix, fpath):
return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
def get_elf_prog_interpreter(builddir, prefix, fpath):
"""
Runs the cross readelf on 'fpath' to extract the program interpreter
name and returns it.
Example:
>>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
'arm-linux-gnueabihf',
'bin/busybox')
/lib/ld-linux-armhf.so.3
>>>
"""
cmd = ["host/usr/bin/{}-readelf".format(prefix),
"-l", os.path.join("target", fpath)]
out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
for line in out.splitlines():
m = regexp.match(line)
if not m:
continue
return m.group(1)
return None