You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

  1. import subprocess
  2. import shutil
  3. import os
  4. from . import __version__
  5. cmd_env = {
  6. "PATH": os.environ["PATH"],
  7. "HOME": os.environ["HOME"],
  8. "LANG": "C",
  9. "LC_ALL": "C",
  10. }
  11. def run(cmd):
  12. return subprocess.check_output(cmd, stderr=subprocess.DEVNULL, env=cmd_env)
  13. if (os.path.exists("../.git") or os.path.exists(".git")) and shutil.which("git"):
  14. try:
  15. git_revision = run(["git", "rev-parse", "HEAD"]).strip().decode("ascii")
  16. git_revision_url = f"https://github.com/maunium/stickerpicker/commit/{git_revision}"
  17. git_revision = git_revision[:8]
  18. except (subprocess.SubprocessError, OSError):
  19. git_revision = "unknown"
  20. git_revision_url = None
  21. try:
  22. git_tag = run(["git", "describe", "--exact-match", "--tags"]).strip().decode("ascii")
  23. except (subprocess.SubprocessError, OSError):
  24. git_tag = None
  25. else:
  26. git_revision = "unknown"
  27. git_revision_url = None
  28. git_tag = None
  29. git_tag_url = (f"https://github.com/maunium/stickerpicker/releases/tag/{git_tag}"
  30. if git_tag else None)
  31. if git_tag and __version__ == git_tag[1:].replace("-", ""):
  32. version = __version__
  33. linkified_version = f"[{version}]({git_tag_url})"
  34. else:
  35. if not __version__.endswith("+dev"):
  36. __version__ += "+dev"
  37. version = f"{__version__}.{git_revision}"
  38. if git_revision_url:
  39. linkified_version = f"{__version__}.[{git_revision}]({git_revision_url})"
  40. else:
  41. linkified_version = version