__init__.py 3.4 KB

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