__init__.py 2.8 KB

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