Coverage for jaypore_ci/config.py: 79%
43 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 09:04 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 09:04 +0000
1import os
2import tomllib
3from typing import NamedTuple
4import importlib.metadata
5from pathlib import Path
8class Version(NamedTuple):
9 major: int
10 minor: int
11 patch: int
12 trail: str = None
14 def __repr__(self):
15 if self.trail:
16 return f"{self.major}.{self.minor}.{self.patch}-{self.trail}"
17 return f"{self.major}.{self.minor}.{self.patch}"
19 def __str__(self):
20 return self.__repr__()
22 @classmethod
23 def parse(cls, inp: str) -> "Version":
24 if inp is None or inp == "":
25 return None
26 trail = None
27 major, minor, patch = inp.split(".")
28 major = major[1:] if major[0].lower() == "v" else major
29 assert major.isdigit()
30 assert minor.isdigit()
31 if "-" in patch: 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true
32 patch, trail = patch.split("-", 1)
33 assert patch.isdigit()
34 return cls(major=int(major), minor=int(minor), patch=int(patch), trail=trail)
37def get_version() -> Version:
38 try:
39 return Version.parse(importlib.metadata.version(__package__ or __name__))
40 except importlib.metadata.PackageNotFoundError:
41 try:
42 with open(
43 (Path(__file__) / "../../pyproject.toml").resolve(),
44 "rb",
45 ) as fl:
46 data = tomllib.load(fl)
47 return Version.parse(data["tool"]["poetry"]["version"])
48 except FileNotFoundError:
49 return None
52class Const(NamedTuple):
53 expected_version: Version = Version.parse(
54 os.environ.get("EXPECTED_JAYPORECI_VERSION")
55 )
56 version: Version = get_version()
59const = Const()