get_app_version.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from __future__ import unicode_literals
  2. import os
  3. import subprocess
  4. from pelican import signals
  5. def app_version(generator):
  6. def minimal_env_cmd(cmd):
  7. # make minimal environment
  8. env = {}
  9. for k in ['SYSTEMROOT', 'PATH']:
  10. v = os.environ.get(k)
  11. if v is not None:
  12. env[k] = v
  13. env['LANGUAGE'] = 'C'
  14. env['LANG'] = 'C'
  15. env['LC_ALL'] = 'C'
  16. out = subprocess.Popen(
  17. cmd, stdout=subprocess.PIPE, env=env).communicate()[0]
  18. return out
  19. try:
  20. # version
  21. describe = minimal_env_cmd(["git", "describe", "--always"])
  22. git_revision = describe.strip().decode('ascii')
  23. # branch
  24. branch = minimal_env_cmd(["git", "branch"])
  25. git_branch = branch.strip().decode('ascii').replace('* ', '')
  26. except OSError:
  27. git_revision = "Unknown"
  28. git_branch = "Unknown"
  29. generator.context['CURRENT_VERSION'] = git_revision
  30. generator.context['CURRENT_BRANCH'] = git_branch
  31. def register():
  32. signals.generator_init.connect(app_version)