|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2015-2021 Rapptz |
| 5 | +Copyright (c) 2021-present Pycord Development |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +copy of this software and associated documentation files (the "Software"), |
| 9 | +to deal in the Software without restriction, including without limitation |
| 10 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | +and/or sell copies of the Software, and to permit persons to whom the |
| 12 | +Software is furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | +DEALINGS IN THE SOFTWARE. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import datetime |
| 29 | +import re |
| 30 | +import warnings |
| 31 | + |
| 32 | +from typing_extensions import TypedDict |
| 33 | + |
| 34 | +__all__ = ("__version__", "VersionInfo", "version_info") |
| 35 | + |
| 36 | +from typing import Literal, NamedTuple |
| 37 | + |
| 38 | +from .utils import deprecated |
| 39 | +from ._version import __version__, __version_tuple__ |
| 40 | + |
| 41 | + |
| 42 | +class AdvancedVersionInfo(TypedDict): |
| 43 | + serial: int |
| 44 | + build: int | None |
| 45 | + commit: str | None |
| 46 | + date: datetime.date | None |
| 47 | + |
| 48 | + |
| 49 | +class VersionInfo(NamedTuple): |
| 50 | + major: int |
| 51 | + minor: int |
| 52 | + micro: int |
| 53 | + releaselevel: Literal["alpha", "beta", "candidate", "final", "dev"] |
| 54 | + |
| 55 | + # We can't set instance attributes on a NamedTuple, so we have to use a |
| 56 | + # global variable to store the advanced version info. |
| 57 | + @property |
| 58 | + def advanced(self) -> AdvancedVersionInfo: |
| 59 | + return _advanced |
| 60 | + |
| 61 | + @advanced.setter |
| 62 | + def advanced(self, value: object) -> None: |
| 63 | + global _advanced |
| 64 | + _advanced = value |
| 65 | + |
| 66 | + @property |
| 67 | + @deprecated("releaselevel", "2.4") |
| 68 | + def release_level(self) -> Literal["alpha", "beta", "candidate", "final", "dev"]: |
| 69 | + return self.releaselevel |
| 70 | + |
| 71 | + @property |
| 72 | + @deprecated('.advanced["serial"]', "2.4") |
| 73 | + def serial(self) -> int: |
| 74 | + return self.advanced["serial"] |
| 75 | + |
| 76 | + @property |
| 77 | + @deprecated('.advanced["build"]', "2.4") |
| 78 | + def build(self) -> int | None: |
| 79 | + return self.advanced["build"] |
| 80 | + |
| 81 | + @property |
| 82 | + @deprecated('.advanced["commit"]', "2.4") |
| 83 | + def commit(self) -> str | None: |
| 84 | + return self.advanced["commit"] |
| 85 | + |
| 86 | + @property |
| 87 | + @deprecated('.advanced["date"]', "2.4") |
| 88 | + def date(self) -> datetime.date | None: |
| 89 | + return self.advanced["date"] |
| 90 | + |
| 91 | + |
| 92 | +def parse_version_tuple(version_tuple): |
| 93 | + """Parse setuptools-scm version tuple into components.""" |
| 94 | + major = version_tuple[0] if len(version_tuple) > 0 else 0 |
| 95 | + minor = version_tuple[1] if len(version_tuple) > 1 else 0 |
| 96 | + micro = 0 |
| 97 | + releaselevel = "final" |
| 98 | + serial = 0 |
| 99 | + build = None |
| 100 | + commit = None |
| 101 | + date_info = None |
| 102 | + |
| 103 | + # Handle additional components |
| 104 | + for i, component in enumerate(version_tuple[2:], start=2): |
| 105 | + if isinstance(component, str): |
| 106 | + # Parse development/pre-release info |
| 107 | + if component.startswith("dev"): |
| 108 | + releaselevel = "dev" # Keep dev as its own category |
| 109 | + serial = int(component[3:]) if len(component) > 3 else 0 |
| 110 | + elif component.startswith("a"): |
| 111 | + releaselevel = "alpha" |
| 112 | + serial = int(component[1:]) if len(component) > 1 else 0 |
| 113 | + elif component.startswith("b"): |
| 114 | + releaselevel = "beta" |
| 115 | + serial = int(component[1:]) if len(component) > 1 else 0 |
| 116 | + elif component.startswith("rc"): |
| 117 | + releaselevel = "candidate" |
| 118 | + serial = int(component[2:]) if len(component) > 2 else 0 |
| 119 | + elif component.startswith("g") and "." in component: |
| 120 | + # Parse git info like 'g901fb98.d20250526' |
| 121 | + parts = component.split(".") |
| 122 | + if parts[0].startswith("g"): |
| 123 | + commit = parts[0][1:] # Remove 'g' prefix |
| 124 | + if len(parts) > 1 and parts[1].startswith("d"): |
| 125 | + date_str = parts[1][1:] # Remove 'd' prefix |
| 126 | + if len(date_str) == 8: |
| 127 | + date_info = datetime.date(int(date_str[:4]), int(date_str[4:6]), int(date_str[6:8])) |
| 128 | + elif isinstance(component, int) and i == 2: |
| 129 | + micro = component |
| 130 | + |
| 131 | + return { |
| 132 | + "major": major, |
| 133 | + "minor": minor, |
| 134 | + "micro": micro, |
| 135 | + "releaselevel": releaselevel, |
| 136 | + "serial": serial, |
| 137 | + "build": build, |
| 138 | + "commit": commit, |
| 139 | + "date": date_info, |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | +# Parse the version tuple |
| 144 | +parsed = parse_version_tuple(__version_tuple__) |
| 145 | + |
| 146 | +version_info: VersionInfo = VersionInfo( |
| 147 | + major=parsed["major"], |
| 148 | + minor=parsed["minor"], |
| 149 | + micro=parsed["micro"], |
| 150 | + releaselevel=parsed["releaselevel"], |
| 151 | +) |
| 152 | + |
| 153 | +_advanced = AdvancedVersionInfo( |
| 154 | + serial=parsed["serial"], |
| 155 | + build=parsed["build"], |
| 156 | + commit=parsed["commit"], |
| 157 | + date=parsed["date"], |
| 158 | +) |
0 commit comments