buildcfg.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import os
  2. import subprocess
  3. import bb.process
  4. def detect_revision(d):
  5. path = get_scmbasepath(d)
  6. return get_metadata_git_revision(path)
  7. def detect_branch(d):
  8. path = get_scmbasepath(d)
  9. return get_metadata_git_branch(path)
  10. def get_scmbasepath(d):
  11. return os.path.join(d.getVar('COREBASE'), 'meta')
  12. def get_metadata_git_branch(path):
  13. try:
  14. rev, _ = bb.process.run('git rev-parse --abbrev-ref HEAD', cwd=path)
  15. except (bb.process.ExecutionError, bb.process.NotFoundError):
  16. rev = '<unknown>'
  17. return rev.strip()
  18. def get_metadata_git_revision(path):
  19. try:
  20. rev, _ = bb.process.run('git rev-parse HEAD', cwd=path)
  21. except (bb.process.ExecutionError, bb.process.NotFoundError):
  22. rev = '<unknown>'
  23. return rev.strip()
  24. def get_metadata_git_toplevel(path):
  25. try:
  26. toplevel, _ = bb.process.run('git rev-parse --show-toplevel', cwd=path)
  27. except (bb.process.ExecutionError, bb.process.NotFoundError):
  28. return ""
  29. return toplevel.strip()
  30. def get_metadata_git_remotes(path):
  31. try:
  32. remotes_list, _ = bb.process.run('git remote', cwd=path)
  33. remotes = remotes_list.split()
  34. except (bb.process.ExecutionError, bb.process.NotFoundError):
  35. remotes = []
  36. return remotes
  37. def get_metadata_git_remote_url(path, remote):
  38. try:
  39. uri, _ = bb.process.run('git remote get-url {remote}'.format(remote=remote), cwd=path)
  40. except (bb.process.ExecutionError, bb.process.NotFoundError):
  41. return ""
  42. return uri.strip()
  43. def get_metadata_git_describe(path):
  44. try:
  45. describe, _ = bb.process.run('git describe --tags --dirty', cwd=path)
  46. except (bb.process.ExecutionError, bb.process.NotFoundError):
  47. return ""
  48. return describe.strip()
  49. def is_layer_modified(path):
  50. try:
  51. subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
  52. git diff --quiet --no-ext-diff
  53. git diff --quiet --no-ext-diff --cached""" % path,
  54. shell=True,
  55. stderr=subprocess.STDOUT)
  56. return ""
  57. except subprocess.CalledProcessError as ex:
  58. # Silently treat errors as "modified", without checking for the
  59. # (expected) return code 1 in a modified git repo. For example, we get
  60. # output and a 129 return code when a layer isn't a git repo at all.
  61. return " -- modified"
  62. def get_layer_revisions(d):
  63. layers = (d.getVar("BBLAYERS") or "").split()
  64. revisions = []
  65. for i in layers:
  66. revisions.append((i, os.path.basename(i), get_metadata_git_branch(i).strip(), get_metadata_git_revision(i), is_layer_modified(i)))
  67. return revisions