rev_info.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import datetime
  2. import subprocess
  3. def git_short_rev():
  4. try:
  5. return subprocess.check_output([
  6. 'git',
  7. 'rev-parse',
  8. '--short',
  9. 'HEAD',
  10. ]).decode('utf-8').strip()
  11. except Exception:
  12. raise RuntimeError("Could not read git revision. Make sure you have git installed and you're working with a git clone of the repository.")
  13. def current_date():
  14. return datetime.date.today().strftime('%Y-%m-%d')
  15. def git_date(short=True):
  16. try:
  17. iso = subprocess.check_output([
  18. 'git',
  19. 'log',
  20. '-1',
  21. '--format=%ci',
  22. 'HEAD',
  23. ]).decode('utf-8').strip()
  24. if short:
  25. return iso.split(' ')[0]
  26. else:
  27. return iso
  28. except Exception:
  29. raise RuntimeError("Could not read git commit date. Make sure you have git installed and you're working with a git clone of the repository.")
  30. def git_release_version(search_prefix):
  31. try:
  32. tags = subprocess.check_output([
  33. 'git',
  34. 'tag',
  35. '--points-at',
  36. 'HEAD',
  37. ]).decode('utf-8').splitlines()
  38. for tag in tags:
  39. if tag.startswith(search_prefix):
  40. return tag[len(search_prefix):]
  41. return None
  42. except Exception:
  43. raise RuntimeError("Could not read git release tags. Make sure you have git installed and you're working with a git clone of the repository.")