/
mz_version.py
130 lines (102 loc) · 4.19 KB
/
mz_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
"""Version types"""
from __future__ import annotations
import json
import subprocess
from typing import TypeVar
try:
from semver.version import Version
except ImportError:
from semver import VersionInfo as Version # type: ignore
T = TypeVar("T", bound="TypedVersionBase")
class TypedVersionBase(Version):
"""Typed version, can be parsed from version string"""
@classmethod
def get_prefix(cls) -> str:
raise NotImplementedError(f"Not implemented in {cls}")
@classmethod
def create(
cls: type[T], major: int, minor: int, patch: int, prerelease: str | None = None
) -> T:
prerelease_suffix = f"-{prerelease}" if prerelease is not None else ""
return cls.parse(
f"{cls.get_prefix()}{major}.{minor}.{patch}{prerelease_suffix}"
)
@classmethod
def parse_without_prefix(
cls: type[T], version_without_prefix: str, drop_dev_suffix: bool = False
) -> T:
version = f"{cls.get_prefix()}{version_without_prefix}"
return cls.parse(version, drop_dev_suffix=drop_dev_suffix)
@classmethod
def parse(cls: type[T], version: str, drop_dev_suffix: bool = False) -> T:
"""Parses a version string with prefix, for example: v0.45.0-dev (f01773cb1)"""
expected_prefix = cls.get_prefix()
if not version.startswith(expected_prefix):
raise ValueError(
f"Invalid version string '{version}', expected prefix '{expected_prefix}'"
)
version = version.removeprefix(expected_prefix)
if " " in version:
version, git_hash = version.split(" ")
if not git_hash[0] == "(" or not git_hash[-1] == ")":
raise ValueError(f"Invalid mz version string: {version}")
# Hash ignored
if drop_dev_suffix:
version = version.replace("-dev", "")
return super().parse(version)
@classmethod
def try_parse(
cls: type[T], version: str, drop_dev_suffix: bool = False
) -> T | None:
"""Parses a version string but returns empty if that fails"""
try:
return cls.parse(version, drop_dev_suffix=drop_dev_suffix)
except ValueError:
return None
@classmethod
def is_valid_version_string(cls, version: str) -> bool:
return cls.try_parse(version) is not None
def str_without_prefix(self) -> str:
return super().__str__()
def __str__(self) -> str:
return f"{self.get_prefix()}{self.str_without_prefix()}"
class MzVersion(TypedVersionBase):
"""Version of Materialize, can be parsed from version string, SQL, cargo"""
@classmethod
def get_prefix(cls) -> str:
return "v"
@classmethod
def parse_mz(cls: type[T], version: str, drop_dev_suffix: bool = False) -> T:
"""Parses a version string with prefix, for example: v0.45.0-dev (f01773cb1)"""
return cls.parse(version=version, drop_dev_suffix=drop_dev_suffix)
@classmethod
def parse_cargo(cls) -> MzVersion:
"""Uses the cargo mz-environmentd package info to get the version of current source code state"""
metadata = json.loads(
subprocess.check_output(
["cargo", "metadata", "--no-deps", "--format-version=1"]
)
)
for package in metadata["packages"]:
if package["name"] == "mz-environmentd":
return cls.parse_without_prefix(package["version"])
else:
raise ValueError("No mz-environmentd version found in cargo metadata")
class MzCliVersion(TypedVersionBase):
"""Version of Materialize APT"""
@classmethod
def get_prefix(cls) -> str:
return "mz-v"
class MzLspServerVersion(TypedVersionBase):
"""Version of Materialize LSP Server"""
@classmethod
def get_prefix(cls) -> str:
return "mz-lsp-server-v"