home-assistant-addon/script/bump-version.py

89 lines
2.3 KiB
Python
Raw Normal View History

2020-07-17 14:32:47 +02:00
#!/usr/bin/env python3
import argparse
import re
from dataclasses import dataclass
import sys
import os
sys.path.append(os.path.dirname(__file__))
import generate
2020-07-17 14:32:47 +02:00
@dataclass
class Version:
major: int
minor: int
patch: int
beta: int = 0
2024-04-17 05:32:13 +02:00
dev: str = ""
2020-07-17 14:32:47 +02:00
def __str__(self):
2024-04-17 05:32:13 +02:00
return f"{self.major}.{self.minor}.{self.full_patch}"
2020-07-17 14:32:47 +02:00
@property
def full_patch(self):
2024-04-17 05:32:13 +02:00
res = f"{self.patch}"
2020-07-17 14:32:47 +02:00
if self.beta > 0:
2024-04-17 05:32:13 +02:00
res += f"b{self.beta}"
2020-07-17 14:32:47 +02:00
if self.dev:
2024-04-17 05:32:13 +02:00
res += f"-dev{self.dev}"
2020-07-17 14:32:47 +02:00
return res
@classmethod
def parse(cls, value):
2024-04-17 05:32:13 +02:00
match = re.match(r"(\d+).(\d+).(\d+)(b\d+)?(-dev\d+)?", value)
2020-07-17 14:32:47 +02:00
assert match is not None
major = int(match[1])
minor = int(match[2])
patch = int(match[3])
beta = int(match[4][1:]) if match[4] else 0
2024-04-17 05:32:13 +02:00
dev = str(match[5][4:]) if match[5] else ""
return Version(major=major, minor=minor, patch=patch, beta=beta, dev=dev)
2020-07-17 14:32:47 +02:00
2024-04-17 05:32:13 +02:00
def _sub(path, pattern, repl, expected_count=1):
with open(path, encoding="utf-8") as fh:
2020-07-17 14:32:47 +02:00
content = fh.read()
2024-04-17 05:32:13 +02:00
content, count = re.subn(pattern, repl, content, flags=re.NOFLAG)
2020-07-17 14:32:47 +02:00
if expected_count is not None:
2020-07-25 23:14:27 +02:00
assert count == expected_count, f"Pattern {pattern} replacement failed!"
2024-04-17 05:32:13 +02:00
with open(path, "wt", encoding="utf-8") as fh:
2020-07-17 14:32:47 +02:00
fh.write(content)
2024-04-17 05:32:13 +02:00
def _write_version(target: str, version: Version):
# version: "2024.5.0-dev20240412" # DEV
# version: "1.14.5" # BETA
# version: "1.14.5" # STABLE
_sub(
"template/addon_config.yaml",
f' version: "[^"]+" # {target.upper()}',
f' version: "{version}" # {target.upper()}',
2020-07-17 14:32:47 +02:00
)
def main():
parser = argparse.ArgumentParser()
2024-04-17 05:32:13 +02:00
parser.add_argument("new_version", type=str)
2020-07-17 14:32:47 +02:00
args = parser.parse_args()
version = Version.parse(args.new_version)
2020-07-17 14:32:47 +02:00
print(f"Bumping to {version}")
2024-04-17 05:32:13 +02:00
if version.dev:
_write_version("dev", version)
generate.main(["dev"])
elif version.beta:
_write_version("beta", version)
generate.main(["beta"])
else:
2024-04-17 05:32:13 +02:00
_write_version("stable", version)
_write_version("beta", version)
generate.main(["stable", "beta"])
2020-07-17 14:32:47 +02:00
return 0
if __name__ == "__main__":
sys.exit(main() or 0)