Static strings that describe the version of the pip package.
| 101 | |
| 102 | |
| 103 | class Version: |
| 104 | """Static strings that describe the version of the pip package.""" |
| 105 | |
| 106 | # Cached values returned by the properties. |
| 107 | __root_dir_attr: Optional[str] = None |
| 108 | __string_attr: Optional[str] = None |
| 109 | __git_hash_attr: Optional[str] = None |
| 110 | |
| 111 | @classmethod |
| 112 | def _root_dir(cls) -> str: |
| 113 | """The path to the root of the git repo.""" |
| 114 | if cls.__root_dir_attr is None: |
| 115 | # This setup.py file lives in the root of the repo. |
| 116 | cls.__root_dir_attr = str(Path(__file__).parent.resolve()) |
| 117 | return str(cls.__root_dir_attr) |
| 118 | |
| 119 | @classmethod |
| 120 | def git_hash(cls) -> Optional[str]: |
| 121 | """The current git hash, if known.""" |
| 122 | if cls.__git_hash_attr is None: |
| 123 | import subprocess |
| 124 | |
| 125 | try: |
| 126 | cls.__git_hash_attr = ( |
| 127 | subprocess.check_output( |
| 128 | ["git", "rev-parse", "HEAD"], cwd=cls._root_dir() |
| 129 | ) |
| 130 | .decode("ascii") |
| 131 | .strip() |
| 132 | ) |
| 133 | except subprocess.CalledProcessError: |
| 134 | cls.__git_hash_attr = "" # Non-None but empty. |
| 135 | # A non-None but empty value indicates that we don't know it. |
| 136 | return cls.__git_hash_attr if cls.__git_hash_attr else None |
| 137 | |
| 138 | @classmethod |
| 139 | def string(cls) -> str: |
| 140 | """The version string.""" |
| 141 | if cls.__string_attr is None: |
| 142 | # If set, BUILD_VERSION should override any local version |
| 143 | # information. CI will use this to manage, e.g., release vs. nightly |
| 144 | # versions. |
| 145 | version = os.getenv("BUILD_VERSION", "").strip() |
| 146 | if not version: |
| 147 | # Otherwise, read the version from a local file and add the git |
| 148 | # commit if available. |
| 149 | version = ( |
| 150 | open(os.path.join(cls._root_dir(), "version.txt")).read().strip() |
| 151 | ) |
| 152 | if cls.git_hash(): |
| 153 | version += "+" + cls.git_hash()[:7] # type: ignore[index] |
| 154 | cls.__string_attr = version |
| 155 | return cls.__string_attr |
| 156 | |
| 157 | @classmethod |
| 158 | def write_to_python_file(cls, path: str) -> None: |
| 159 | """Creates a file similar to PyTorch core's `torch/version.py`.""" |
| 160 |
no outgoing calls
no test coverage detected