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