1
0

setup_version.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import json
  12. import os
  13. import re
  14. import sys
  15. from dataclasses import asdict, dataclass
  16. from typing import Optional
  17. @dataclass
  18. class Version:
  19. major: str
  20. minor: str
  21. patch: str
  22. ext: Optional[str] = None
  23. def bump_ext_version(self) -> None:
  24. if not self.ext:
  25. return
  26. reg = re.compile(r"[0-9]+$")
  27. num = reg.findall(self.ext)[0]
  28. self.ext = self.ext.replace(num, str(int(num) + 1))
  29. def validate_suffix(self, suffix="dev"):
  30. if suffix not in self.ext:
  31. raise Exception(f"Version does not contain suffix {suffix}")
  32. @property
  33. def name(self) -> str:
  34. """returns a string representation of a version"""
  35. return f"{self.major}.{self.minor}.{self.patch}"
  36. @property
  37. def dev_name(self) -> str:
  38. """returns a string representation of a version"""
  39. return f"{self.name}.{self.ext}"
  40. def __str__(self) -> str:
  41. """returns a string representation of a version"""
  42. version_str = f"{self.major}.{self.minor}.{self.patch}"
  43. if self.ext:
  44. version_str = f"{version_str}.{self.ext}"
  45. return version_str
  46. def __load_version_from_path(base_path: str) -> Version:
  47. """Load version.json file from base path."""
  48. with open(os.path.join(base_path, "version.json")) as version_file:
  49. data = json.load(version_file)
  50. return Version(**data)
  51. def __write_version_to_path(base_path: str, version: Version) -> None:
  52. with open(os.path.join(base_path, "version.json"), "w") as version_file:
  53. json.dump(asdict(version), version_file)
  54. def extract_version(base_path: str) -> Version:
  55. """
  56. Load version.json file from base path and return the version string.
  57. """
  58. return __load_version_from_path(base_path)
  59. def __setup_dev_version(version: Version, _base_path: str, name: Optional[str] = None) -> None:
  60. version.validate_suffix()
  61. name = f"{name}_VERSION" if name else "VERSION"
  62. print(f"{name}={version.dev_name}") # noqa: T201
  63. def bump_ext_version(version: Version, _base_path: str) -> None:
  64. version.bump_ext_version()
  65. __write_version_to_path(_base_path, version)
  66. def __setup_prod_version(version: Version, target_version: str, branch_name: str, name: str = None) -> None:
  67. if str(version) != target_version:
  68. raise ValueError(f"Current version={version} does not match target version={target_version}")
  69. if target_branch_name := f"release/{version.major}.{version.minor}" != branch_name:
  70. raise ValueError(
  71. f"Branch name mismatch branch={branch_name} does not match target branch name={target_branch_name}"
  72. )
  73. name = f"{name}_VERSION" if name else "VERSION"
  74. print(f"{name}={version.name}") # noqa: T201
  75. if __name__ == "__main__":
  76. paths = (
  77. [sys.argv[1]]
  78. if sys.argv[1] != "ALL"
  79. else [
  80. f"taipy{os.sep}common",
  81. f"taipy{os.sep}core",
  82. f"taipy{os.sep}rest",
  83. f"taipy{os.sep}gui",
  84. f"taipy{os.sep}templates",
  85. "taipy",
  86. ]
  87. )
  88. _environment = sys.argv[2]
  89. for _path in paths:
  90. _version = extract_version(_path)
  91. _name = None if _path == "taipy" else _path.split(os.sep)[-1]
  92. if _environment == "dev":
  93. __setup_dev_version(_version, _path, _name)
  94. if _environment == "production":
  95. __setup_prod_version(_version, sys.argv[3], sys.argv[4], _name)