1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
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/"
@contextlib.contextmanager
def smart_open(filename=None):
"""
Return a file-like object that can be written to using the 'with'
keyword, as in the example:
with infra.smart_open("test.log") as outfile:
outfile.write("Hello, world!\n")
"""
if filename and filename != '-':
fhandle = open(filename, 'a+')
else:
fhandle = sys.stdout
try:
yield fhandle
finally:
if fhandle is not sys.stdout:
fhandle.close()
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):
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
|