|
7 | 7 |
|
8 | 8 | try: |
9 | 9 | __version__ = version("bo4e") |
| 10 | + # Please keep this name in sync with the name of the project in pyproject.toml |
| 11 | + # This name is the name of the package on pypi.org |
10 | 12 | except PackageNotFoundError: |
11 | 13 | __version__ = "0.0.0" |
12 | | -# Please keep this name in sync with the name of the project in pyproject.toml |
13 | | -# This name is the name of the package on pypi.org |
14 | | -if re.match(r"^(\d+\.\d+\.\d+)(rc\d+)?$", __version__): |
15 | 14 |
|
16 | | - def _repl(match: re.Match[str]) -> str: |
17 | | - if match.group(2) is not None: |
18 | | - return f"v{match.group(1)}-{match.group(2)}" |
19 | | - return f"v{match.group(1)}" |
20 | 15 |
|
21 | | - __gh_version__ = re.sub(r"^(\d+\.\d+\.\d+)(rc\d+)?$", _repl, __version__) |
22 | | -else: |
23 | | - __gh_version__ = f"v{__version__}" |
| 16 | +def _parse_version_to_gh_version(version_str: str) -> str: |
| 17 | + """ |
| 18 | + Parse a version string into a GitHub version string. |
| 19 | + E.g. '202401.0.1-rc8+dev12asdf34' becomes 'v202401.0.1-rc8'. |
| 20 | + """ |
| 21 | + _REGEX_VERSION = re.compile( |
| 22 | + r"^(?P<major>\d{6})\." |
| 23 | + r"(?P<functional>\d+)\." |
| 24 | + r"(?P<technical>\d+)" |
| 25 | + r"(?:rc(?P<candidate>\d*))?" |
| 26 | + r"(?:\+g(?P<commit_part>\w+)" |
| 27 | + r"(?:\.d(?P<dirty_workdir_date_year>\d{4})" |
| 28 | + r"(?P<dirty_workdir_date_month>\d{2})" |
| 29 | + r"(?P<dirty_workdir_date_day>\d{2}))?)?$" |
| 30 | + ) |
| 31 | + match = _REGEX_VERSION.match(version_str) |
| 32 | + if match is None: |
| 33 | + raise ValueError(f"Invalid version string: {version_str}") |
| 34 | + |
| 35 | + return ( |
| 36 | + f"v{match.group('major')}.{match.group('functional')}.{match.group('technical')}" |
| 37 | + + (f"-rc{match.group('candidate')}" if match.group("candidate") else "") |
| 38 | + + (f"+g{match.group('commit_part')}" if match.group("commit_part") else "") |
| 39 | + + ( |
| 40 | + f".d{match.group('dirty_workdir_date_year')}" |
| 41 | + f"{match.group('dirty_workdir_date_month')}" |
| 42 | + f"{match.group('dirty_workdir_date_day')}" |
| 43 | + if match.group("dirty_workdir_date_year") |
| 44 | + else "" |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +__gh_version__ = _parse_version_to_gh_version(__version__) |
0 commit comments