__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 log_file_path(builddir, stage, logtofile=True):
  11. """Return path to log file"""
  12. return "{}-{}.log".format(builddir, stage) if logtofile else None
  13. def open_log_file(builddir, stage, logtofile=True):
  14. """
  15. Open a file for logging and return its handler.
  16. If logtofile is True, returns sys.stdout. Otherwise opens a file
  17. with a suitable name in the build directory.
  18. """
  19. return open(log_file_path(builddir, stage, logtofile), 'a+') if logtofile else sys.stdout
  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+b") 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 run_cmd_on_host(builddir, cmd):
  44. """Call subprocess.check_output and return the text output."""
  45. out = subprocess.check_output(cmd,
  46. stderr=open(os.devnull, "w"),
  47. cwd=builddir,
  48. env={"LANG": "C"},
  49. universal_newlines=True)
  50. return out
  51. def get_elf_arch_tag(builddir, prefix, fpath, tag):
  52. """
  53. Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
  54. Example:
  55. >>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
  56. 'bin/busybox', 'Tag_CPU_arch')
  57. v5TEJ
  58. >>>
  59. """
  60. cmd = ["host/bin/{}-readelf".format(prefix),
  61. "-A", os.path.join("target", fpath)]
  62. out = run_cmd_on_host(builddir, cmd)
  63. regexp = re.compile(r"^ {}: (.*)$".format(tag))
  64. for line in out.splitlines():
  65. m = regexp.match(line)
  66. if not m:
  67. continue
  68. return m.group(1)
  69. return None
  70. def get_file_arch(builddir, prefix, fpath):
  71. return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
  72. def get_elf_prog_interpreter(builddir, prefix, fpath):
  73. """
  74. Runs the cross readelf on 'fpath' to extract the program interpreter
  75. name and returns it.
  76. Example:
  77. >>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
  78. 'arm-linux-gnueabihf',
  79. 'bin/busybox')
  80. /lib/ld-linux-armhf.so.3
  81. >>>
  82. """
  83. cmd = ["host/bin/{}-readelf".format(prefix),
  84. "-l", os.path.join("target", fpath)]
  85. out = run_cmd_on_host(builddir, cmd)
  86. regexp = re.compile(r"^ *\[Requesting program interpreter: (.*)\]$")
  87. for line in out.splitlines():
  88. m = regexp.match(line)
  89. if not m:
  90. continue
  91. return m.group(1)
  92. return None
  93. def img_round_power2(img):
  94. """
  95. Rounds up the size of an image file to the next power of 2
  96. """
  97. sz = os.stat(img).st_size
  98. pow2 = 1
  99. while pow2 < sz:
  100. pow2 = pow2 << 1
  101. with open(img, 'ab') as f:
  102. f.truncate(pow2)