]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - support/testing/infra/__init__.py
27e2a2708db04dfc1968df74d1b3355d40065dff
[coffee/buildroot.git] / support / testing / infra / __init__.py
1 import contextlib
2 import os
3 import re
4 import sys
5 import tempfile
6 import subprocess
7 from urllib2 import urlopen, HTTPError, URLError
8
9 ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
10
11 def open_log_file(builddir, stage, logtofile=True):
12     """
13     Open a file for logging and return its handler.
14     If logtofile is True, returns sys.stdout. Otherwise opens a file
15     with a suitable name in the build directory.
16     """
17     if logtofile:
18         fhandle = open("{}-{}.log".format(builddir, stage), 'a+')
19     else:
20         fhandle = sys.stdout
21     return fhandle
22
23 def filepath(relpath):
24     return os.path.join(os.getcwd(), "support/testing", relpath)
25
26 def download(dldir, filename):
27     finalpath = os.path.join(dldir, filename)
28     if os.path.exists(finalpath):
29         return finalpath
30
31     if not os.path.exists(dldir):
32         os.makedirs(dldir)
33
34     tmpfile = tempfile.mktemp(dir=dldir)
35     print "Downloading to {}".format(tmpfile)
36
37     try:
38         url_fh = urlopen(os.path.join(ARTIFACTS_URL, filename))
39         with open(tmpfile, "w+") as tmpfile_fh:
40             tmpfile_fh.write(url_fh.read())
41     except (HTTPError, URLError), err:
42         os.unlink(tmpfile)
43         raise err
44
45     print "Renaming from %s to %s" % (tmpfile, finalpath)
46     os.rename(tmpfile, finalpath)
47     return finalpath
48
49 def get_elf_arch_tag(builddir, prefix, fpath, tag):
50     """
51     Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
52     Example:
53     >>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
54                          'bin/busybox', 'Tag_CPU_arch')
55     v5TEJ
56     >>>
57     """
58     cmd = ["host/bin/{}-readelf".format(prefix),
59            "-A", os.path.join("target", fpath)]
60     out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
61     regexp = re.compile("^  {}: (.*)$".format(tag))
62     for line in out.splitlines():
63         m = regexp.match(line)
64         if not m:
65             continue
66         return m.group(1)
67     return None
68
69 def get_file_arch(builddir, prefix, fpath):
70     return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
71
72 def get_elf_prog_interpreter(builddir, prefix, fpath):
73     """
74     Runs the cross readelf on 'fpath' to extract the program interpreter
75     name and returns it.
76     Example:
77     >>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
78                                  'arm-linux-gnueabihf',
79                                  'bin/busybox')
80     /lib/ld-linux-armhf.so.3
81     >>>
82     """
83     cmd = ["host/bin/{}-readelf".format(prefix),
84            "-l", os.path.join("target", fpath)]
85     out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
86     regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
87     for line in out.splitlines():
88         m = regexp.match(line)
89         if not m:
90             continue
91         return m.group(1)
92     return None