kumquat-buildroot/support/testing/infra/__init__.py
Ricardo Martincoski 4a40d36f13 support/testing: switch to Python 3 only
Python 2.7 will not be maintained past 2020.

Many scripts on the tree are used during the build and should keep
Python 2 compatibility for a while.
This is not the case for the runtime test infra. It's meant to be run in
modern distros only, so it can safely switch to support Python 3 only.

An advantage of this approach is to have less scenarios to test in.
Otherwise every change to the test infra or runtime tests would need to
be tested against both versions of the interpreter, increasing the
effort of the developers, to ensure the compatibility to Python 2 was
not broken.

In order to accomplish the change to Python 3:
 - change the shebang for run-tests;
 - use Python 3 urllib as a drop-in replacement for Python 2 urllib2;
 - when writing the downloaded binary files, explicitly open the output
   file as binary;
 - when subprocess is used to retrieve the text output from commands,
   explicitly ask for text output. For this, use 'universal_newlines'
   because 'text' was added only on Python 3.7;
 - when pexpect is used to retrieve the text output from qemu or git,
   explicitly ask for text output using 'encoding';
 - the code using csv currently follows the example in the documentation
   for the Python 2 module, change it to follow the example in the
   documentation for the Python 3 module;
 - fix the relative import for test_git.py to be Python 3 compliant.

Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
Cc: Arnout Vandecappelle <arnout@mind.be>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Cc: Yann E. MORIN <yann.morin.1998@free.fr>
Tested-by: Romain Naour <romain.naour@smile.fr>
Tested-by: Nicolas Carrier <nicolas.carrier@orolia.com>
Signed-off-by: Nicolas Carrier <nicolas.carrier@orolia.com>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2019-10-28 22:14:07 +01:00

115 lines
3.4 KiB
Python

import os
import re
import sys
import tempfile
import subprocess
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
BASE_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../../.."))
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 basepath(relpath=""):
"""Return the absolute path for a file or directory relative to the Buildroot top directory."""
return os.path.join(BASE_DIR, relpath)
def filepath(relpath):
return os.path.join(BASE_DIR, "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+b") as tmpfile_fh:
tmpfile_fh.write(url_fh.read())
except (HTTPError, URLError) as err:
os.unlink(tmpfile)
raise err
print("Renaming from {} to {}".format(tmpfile, finalpath))
os.rename(tmpfile, finalpath)
return finalpath
def run_cmd_on_host(builddir, cmd):
"""Call subprocess.check_output and return the text output."""
out = subprocess.check_output(cmd,
stderr=open(os.devnull, "w"),
cwd=builddir,
env={"LANG": "C"},
universal_newlines=True)
return out
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/bin/{}-readelf".format(prefix),
"-A", os.path.join("target", fpath)]
out = run_cmd_on_host(builddir, cmd)
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/bin/{}-readelf".format(prefix),
"-l", os.path.join("target", fpath)]
out = run_cmd_on_host(builddir, cmd)
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