__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 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. return out
  50. def get_elf_arch_tag(builddir, prefix, fpath, tag):
  51. """
  52. Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
  53. Example:
  54. >>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
  55. 'bin/busybox', 'Tag_CPU_arch')
  56. v5TEJ
  57. >>>
  58. """
  59. cmd = ["host/bin/{}-readelf".format(prefix),
  60. "-A", os.path.join("target", fpath)]
  61. out = run_cmd_on_host(builddir, cmd)
  62. regexp = re.compile("^ {}: (.*)$".format(tag))
  63. for line in out.splitlines():
  64. m = regexp.match(line)
  65. if not m:
  66. continue
  67. return m.group(1)
  68. return None
  69. def get_file_arch(builddir, prefix, fpath):
  70. return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
  71. def get_elf_prog_interpreter(builddir, prefix, fpath):
  72. """
  73. Runs the cross readelf on 'fpath' to extract the program interpreter
  74. name and returns it.
  75. Example:
  76. >>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
  77. 'arm-linux-gnueabihf',
  78. 'bin/busybox')
  79. /lib/ld-linux-armhf.so.3
  80. >>>
  81. """
  82. cmd = ["host/bin/{}-readelf".format(prefix),
  83. "-l", os.path.join("target", fpath)]
  84. out = run_cmd_on_host(builddir, cmd)
  85. regexp = re.compile("^ *\[Requesting program interpreter: (.*)\]$")
  86. for line in out.splitlines():
  87. m = regexp.match(line)
  88. if not m:
  89. continue
  90. return m.group(1)
  91. return None