__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ARTIFACTS_URL = "http://autobuild.buildroot.net/artefacts/"
  9. @contextlib.contextmanager
  10. def smart_open(filename=None):
  11. """
  12. Return a file-like object that can be written to using the 'with'
  13. keyword, as in the example:
  14. with infra.smart_open("test.log") as outfile:
  15. outfile.write("Hello, world!\n")
  16. """
  17. if filename and filename != '-':
  18. fhandle = open(filename, 'a+')
  19. else:
  20. fhandle = sys.stdout
  21. try:
  22. yield fhandle
  23. finally:
  24. if fhandle is not sys.stdout:
  25. fhandle.close()
  26. def filepath(relpath):
  27. return os.path.join(os.getcwd(), "support/testing", relpath)
  28. def download(dldir, filename):
  29. finalpath = os.path.join(dldir, filename)
  30. if os.path.exists(finalpath):
  31. return finalpath
  32. if not os.path.exists(dldir):
  33. os.makedirs(dldir)
  34. tmpfile = tempfile.mktemp(dir=dldir)
  35. print "Downloading to {}".format(tmpfile)
  36. try:
  37. url_fh = urlopen(os.path.join(ARTIFACTS_URL, filename))
  38. with open(tmpfile, "w+") as tmpfile_fh:
  39. tmpfile_fh.write(url_fh.read())
  40. except (HTTPError, URLError), err:
  41. os.unlink(tmpfile)
  42. raise err
  43. print "Renaming from %s to %s" % (tmpfile, finalpath)
  44. os.rename(tmpfile, finalpath)
  45. return finalpath
  46. def get_elf_arch_tag(builddir, prefix, fpath, tag):
  47. """
  48. Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
  49. Example:
  50. >>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
  51. 'bin/busybox', 'Tag_CPU_arch')
  52. v5TEJ
  53. >>>
  54. """
  55. cmd = ["host/usr/bin/{}-readelf".format(prefix),
  56. "-A", os.path.join("target", fpath)]
  57. out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
  58. regexp = re.compile("^ {}: (.*)$".format(tag))
  59. for line in out.splitlines():
  60. m = regexp.match(line)
  61. if not m:
  62. continue
  63. return m.group(1)
  64. return None
  65. def get_file_arch(builddir, prefix, fpath):
  66. return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
  67. def get_elf_prog_interpreter(builddir, prefix, fpath):
  68. cmd = ["host/usr/bin/{}-readelf".format(prefix),
  69. "-l", os.path.join("target", fpath)]
  70. out = subprocess.check_output(cmd, cwd=builddir, env={"LANG": "C"})
  71. regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
  72. for line in out.splitlines():
  73. m = regexp.match(line)
  74. if not m:
  75. continue
  76. return m.group(1)
  77. return None