7 from urllib2 import urlopen, HTTPError, URLError
9 ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
11 def open_log_file(builddir, stage, logtofile=True):
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.
18 fhandle = open("{}-{}.log".format(builddir, stage), 'a+')
23 def filepath(relpath):
24 return os.path.join(os.getcwd(), "support/testing", relpath)
26 def download(dldir, filename):
27 finalpath = os.path.join(dldir, filename)
28 if os.path.exists(finalpath):
31 if not os.path.exists(dldir):
34 tmpfile = tempfile.mktemp(dir=dldir)
35 print "Downloading to {}".format(tmpfile)
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:
45 print "Renaming from %s to %s" % (tmpfile, finalpath)
46 os.rename(tmpfile, finalpath)
49 def get_elf_arch_tag(builddir, prefix, fpath, tag):
51 Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
53 >>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
54 'bin/busybox', 'Tag_CPU_arch')
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)
69 def get_file_arch(builddir, prefix, fpath):
70 return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
72 def get_elf_prog_interpreter(builddir, prefix, fpath):
74 Runs the cross readelf on 'fpath' to extract the program interpreter
77 >>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
78 'arm-linux-gnueabihf',
80 /lib/ld-linux-armhf.so.3
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)