__init__.py 3.0 KB

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