__init__.py 2.8 KB

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