get_app_version.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import unicode_literals
  2. from subprocess import (
  3. call,
  4. STDOUT
  5. )
  6. from ..version import __version__
  7. import os
  8. import subprocess
  9. def app_version():
  10. def minimal_env_cmd(cmd):
  11. # make minimal environment
  12. env = {}
  13. for k in ['SYSTEMROOT', 'PATH']:
  14. v = os.environ.get(k)
  15. if v is not None:
  16. env[k] = v
  17. env['LANGUAGE'] = 'C'
  18. env['LANG'] = 'C'
  19. env['LC_ALL'] = 'C'
  20. out = subprocess.Popen(
  21. cmd, stdout=subprocess.PIPE, env=env).communicate()[0]
  22. return out
  23. subst_list = {
  24. "version": __version__,
  25. "branch": None,
  26. "commit": None
  27. }
  28. if os.system("command -v git > /dev/null 2>&1") != 0:
  29. subst_list
  30. else:
  31. if call(["git", "branch"], stderr=STDOUT,
  32. stdout=open(os.devnull, 'w')) != 0:
  33. subst_list
  34. else:
  35. # version
  36. describe = minimal_env_cmd(["git", "describe", "--always"])
  37. git_revision = describe.strip().decode('ascii')
  38. # branch
  39. branch = minimal_env_cmd(["git", "branch"])
  40. git_branch = branch.strip().decode('ascii').replace('* ', '')
  41. subst_list = {
  42. "version": __version__,
  43. "branch": git_branch,
  44. "commit": git_revision
  45. }
  46. return subst_list
  47. if __name__ == "__main__":
  48. app_version()