|
| 1 | +""" |
| 2 | +This module provides a way to retrieve version information about conda-metadata-app. |
| 3 | +""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import subprocess |
| 7 | + |
| 8 | +from pydantic import BaseModel, Field |
| 9 | +from streamlit.logger import get_logger |
| 10 | + |
| 11 | +VERSION_INFO_FILE = "version_info.json" |
| 12 | + |
| 13 | +LOGGER = get_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class VersionInfo(BaseModel): |
| 17 | + git_hash: str = Field(pattern=r"^[0-9a-f]{40}$") |
| 18 | + timestamp: datetime.datetime |
| 19 | + |
| 20 | + @property |
| 21 | + def short_git_hash(self): |
| 22 | + return self.git_hash[:7] |
| 23 | + |
| 24 | + def timestamp_string(self): |
| 25 | + return self.timestamp.strftime("%Y-%m-%d %H:%M:%S") |
| 26 | + |
| 27 | + def __str__(self): |
| 28 | + return f"{self.short_git_hash} ({self.timestamp_string()})" |
| 29 | + |
| 30 | + |
| 31 | +def get_version_info_from_git() -> VersionInfo: |
| 32 | + """ |
| 33 | + Returns version information from the `.git` directory. |
| 34 | + This requires you to have the `git` command line tool installed. |
| 35 | +
|
| 36 | + You should only use this function from a Streamlit Cloud deployment or |
| 37 | + during the Docker build process, as neither git nor the `.git` directory |
| 38 | + will be available in the final Docker image! |
| 39 | +
|
| 40 | + :raises FileNotFoundError: If git is not available. |
| 41 | + :raises subprocess.CalledProcessError: If the git command fails. |
| 42 | + """ |
| 43 | + git_info = subprocess.check_output( |
| 44 | + [ |
| 45 | + "git", |
| 46 | + "--no-pager", |
| 47 | + "log", |
| 48 | + "-1", |
| 49 | + "--pretty=format:%H-%cd", |
| 50 | + "--date=unix", |
| 51 | + ], |
| 52 | + text=True, |
| 53 | + ).strip() |
| 54 | + |
| 55 | + git_hash, timestamp = git_info.split("-") |
| 56 | + |
| 57 | + return VersionInfo( |
| 58 | + git_hash=git_hash, |
| 59 | + timestamp=datetime.datetime.fromtimestamp(int(timestamp), datetime.UTC), |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def save_version_info_from_git() -> None: |
| 64 | + """ |
| 65 | + Saves the version information from git to version_info.json. |
| 66 | + See the `get_version_info_from_git` function for more information. |
| 67 | + """ |
| 68 | + version_info = get_version_info_from_git() |
| 69 | + |
| 70 | + with open(VERSION_INFO_FILE, "w") as f: |
| 71 | + f.write(version_info.model_dump_json(indent=2)) |
| 72 | + |
| 73 | + |
| 74 | +def get_version_info() -> VersionInfo | None: |
| 75 | + """ |
| 76 | + Get the version information of the current app installation. |
| 77 | + This works as follows: |
| 78 | + 1. Retrieve the version information from the `version_info.json` file (relevant for Docker image). |
| 79 | + 2. If the file does not exist, retrieve the version information from git (relevant for Streamlit Cloud). |
| 80 | +
|
| 81 | + :return: The version information or None if it could not be retrieved. |
| 82 | + """ |
| 83 | + try: |
| 84 | + with open(VERSION_INFO_FILE) as f: |
| 85 | + return VersionInfo.model_validate_json(f.read()) |
| 86 | + except FileNotFoundError: |
| 87 | + pass |
| 88 | + |
| 89 | + # fall back to retrieving the version information from git |
| 90 | + try: |
| 91 | + return get_version_info_from_git() |
| 92 | + except (FileNotFoundError, subprocess.CalledProcessError): |
| 93 | + LOGGER.warning( |
| 94 | + "Unable to retrieve version information from git after finding that version.info.json is not available.", |
| 95 | + exc_info=True, |
| 96 | + ) |
| 97 | + return None |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + save_version_info_from_git() |
0 commit comments