esphome-docs/script/bump-version.py

83 lines
2.1 KiB
Python
Raw Normal View History

2020-07-17 14:32:34 +02:00
#!/usr/bin/env python3
import argparse
import re
from dataclasses import dataclass
import sys
@dataclass
class Version:
major: int
minor: int
patch: int
beta: int = 0
dev: bool = False
def __str__(self):
return f"{self.major}.{self.minor}.{self.full_patch}"
2020-07-17 14:32:34 +02:00
@property
def full_patch(self):
res = f"{self.patch}"
2020-07-17 14:32:34 +02:00
if self.beta > 0:
res += f"b{self.beta}"
2020-07-17 14:32:34 +02:00
if self.dev:
res += "-dev"
2020-07-17 14:32:34 +02:00
return res
@classmethod
def parse(cls, value):
match = re.match(r"(\d+).(\d+).(\d+)(b\d+)?(-dev)?", value)
2020-07-17 14:32:34 +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
dev = bool(match[5])
return Version(major=major, minor=minor, patch=patch, beta=beta, dev=dev)
2020-07-17 14:32:34 +02:00
def sub(path, pattern, repl, expected_count=1):
with open(path) as fh:
content = fh.read()
2020-07-25 23:13:13 +02:00
content, count = re.subn(pattern, repl, content, flags=re.MULTILINE)
2020-07-17 14:32:34 +02:00
if expected_count is not None:
2020-07-25 23:13:13 +02:00
assert count == expected_count, f"Pattern {pattern} replacement failed!"
2020-07-17 14:32:34 +02:00
with open(path, "wt") as fh:
fh.write(content)
def write_version(version: Version):
# ESPHOME_REF = 2021.8.0
2020-07-17 14:32:34 +02:00
sub(
"Makefile",
r"ESPHOME_REF = .*",
f"ESPHOME_REF = {version}" if not version.dev else "ESPHOME_REF = dev",
2020-07-17 14:32:34 +02:00
)
# PROJECT_NUMBER = 1.14.4
sub(
"Doxygen", r"PROJECT_NUMBER = .*", f"PROJECT_NUMBER = {version}"
2020-07-17 14:32:34 +02:00
)
# version = '1.14'
2021-03-22 05:29:09 +01:00
sub("conf.py", r'version = ".*"', f'version = "{version.major}.{version.minor}"')
2020-07-17 14:32:34 +02:00
# release = '1.14.4'
2021-03-22 05:29:09 +01:00
sub("conf.py", r'release = ".*"', f'release = "{version}"')
with open("_static/version", "wt") as fh:
2020-07-17 14:32:34 +02:00
fh.write(str(version))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("new_version", type=str)
2020-07-17 14:32:34 +02:00
args = parser.parse_args()
version = Version.parse(args.new_version)
print(f"Bumping to {version}")
write_version(version)
return 0
if __name__ == "__main__":
sys.exit(main() or 0)