|
| 1 | +import datetime |
| 2 | +import subprocess |
| 3 | +from subprocess import DEVNULL |
| 4 | + |
| 5 | +# git-archive substitution markers. When this file is written out by a `git |
| 6 | +# archive` command, these will be replaced by the short commit hash and the |
| 7 | +# commit date, respectively. |
| 8 | +VERSION_GIT_REF = "$Format:%h$" |
| 9 | +VERSION_GIT_DATE = "$Format:%ct$" |
| 10 | + |
| 11 | +# Fallback version in case we cannot derive the version. |
| 12 | +VERSION_UNKNOWN = "0+unknown" |
| 13 | + |
| 14 | + |
| 15 | +def fetch_git_ref(): |
| 16 | + return subprocess.check_output( # noqa: S603 |
| 17 | + ["git", "rev-parse", "--short", "HEAD"], # noqa: S607 |
| 18 | + stderr=DEVNULL, |
| 19 | + ).strip() |
| 20 | + |
| 21 | + |
| 22 | +def fetch_git_date(ref): |
| 23 | + output = subprocess.check_output( |
| 24 | + ["git", "show", "-s", "--format=%ct", ref] |
| 25 | + ) # noqa: S603, S607 |
| 26 | + return datetime.datetime.fromtimestamp(int(output)) # noqa: DTZ006 |
| 27 | + |
| 28 | + |
| 29 | +def fetch_git_dirty(): |
| 30 | + # Ensure git index is up-to-date first. This usually isn't necessary, but |
| 31 | + # can be needed inside a docker container where the index is out of date. |
| 32 | + subprocess.call(["git", "update-index", "-q", "--refresh"]) # noqa: S603, S607 |
| 33 | + dirty_tree = bool( |
| 34 | + subprocess.call(["git", "diff-files", "--quiet"]) |
| 35 | + ) # noqa: S603, S607 |
| 36 | + dirty_index = bool( |
| 37 | + subprocess.call( |
| 38 | + ["git", "diff-index", "--quiet", "--cached", "HEAD"] |
| 39 | + ) # noqa: S603, S607 |
| 40 | + ) |
| 41 | + return dirty_tree or dirty_index |
| 42 | + |
| 43 | + |
| 44 | +def git_version(): |
| 45 | + ref = fetch_git_ref() |
| 46 | + date = fetch_git_date(ref) |
| 47 | + dirty = fetch_git_dirty() |
| 48 | + return pep440_version(date, ref, dirty) |
| 49 | + |
| 50 | + |
| 51 | +def git_archive_version(): # pragma: no cover |
| 52 | + ref = VERSION_GIT_REF |
| 53 | + date = datetime.datetime.fromtimestamp(int(VERSION_GIT_DATE)) # noqa: DTZ006 |
| 54 | + return pep440_version(date, ref) |
| 55 | + |
| 56 | + |
| 57 | +def pep440_version(date, ref, dirty=False): # noqa: FBT002 |
| 58 | + """Build a PEP440-compliant version number from the passed information.""" |
| 59 | + return f"{date.strftime('%Y%m%d')}+g{ref}{'.dirty' if dirty else ''}" |
| 60 | + |
| 61 | + |
| 62 | +def get_version(): # pragma: no cover |
| 63 | + """Fetch the current application version.""" |
| 64 | + # First we try to retrieve the current application version from git. |
| 65 | + try: |
| 66 | + return git_version() |
| 67 | + except subprocess.CalledProcessError: |
| 68 | + pass |
| 69 | + |
| 70 | + # We are not in a git checkout or extracting the version from git failed, |
| 71 | + # so we attempt to read a version written into the header of this file by |
| 72 | + # `git archive`. |
| 73 | + if not VERSION_GIT_REF.startswith("$"): |
| 74 | + return git_archive_version() |
| 75 | + |
| 76 | + # If neither of these strategies work, we fall back to VERSION_UNKNOWN. |
| 77 | + return VERSION_UNKNOWN |
0 commit comments