setup_version.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. import os
  3. import re
  4. import sys
  5. from dataclasses import asdict, dataclass
  6. from typing import Optional
  7. @dataclass
  8. class Version:
  9. major: str
  10. minor: str
  11. patch: str
  12. ext: str
  13. def bump_ext_version(self) -> None:
  14. if not self.ext:
  15. return
  16. reg = re.compile(r"[0-9]+$")
  17. num = reg.findall(self.ext)[0]
  18. self.ext = self.ext.replace(num, str(int(num) + 1))
  19. def validate_suffix(self, suffix="dev"):
  20. if suffix not in self.ext:
  21. raise Exception(f"Version does not contain suffix {suffix}")
  22. @property
  23. def name(self) -> str:
  24. """returns a string representation of a version"""
  25. return f"{self.major}.{self.minor}.{self.patch}"
  26. @property
  27. def dev_name(self) -> str:
  28. """returns a string representation of a version"""
  29. return f"{self.name}.{self.ext}"
  30. def __str__(self) -> str:
  31. """returns a string representation of a version"""
  32. version_str = f"{self.major}.{self.minor}.{self.patch}"
  33. if self.ext:
  34. version_str = f"{version_str}.{self.ext}"
  35. return version_str
  36. def __load_version_from_path(base_path: str) -> Version:
  37. """Load version.json file from base path."""
  38. with open(os.path.join(base_path, "version.json")) as version_file:
  39. data = json.load(version_file)
  40. return Version(**data)
  41. def __write_version_to_path(base_path: str, version: Version) -> None:
  42. with open(os.path.join(base_path, "version.json"), "w") as version_file:
  43. json.dump(asdict(version), version_file)
  44. def extract_version(base_path: str) -> Version:
  45. """
  46. Load version.json file from base path and return the version string.
  47. """
  48. return __load_version_from_path(base_path)
  49. def __setup_dev_version(
  50. version: Version, _base_path: str, name: Optional[str] = None, bump_dev_version: bool = False
  51. ) -> None:
  52. version.validate_suffix()
  53. name = f"{name}_VERSION" if name else "VERSION"
  54. print(f"{name}={version.dev_name}") # noqa: T201
  55. version.bump_ext_version()
  56. __write_version_to_path(_base_path, version)
  57. print(f"NEW_{name}={version.dev_name}") # noqa: T201
  58. def __setup_prod_version(version: Version, target_version: str, branch_name: str, name: str = None) -> None:
  59. if str(version) != target_version:
  60. raise ValueError(f"Current version={version} does not match target version={target_version}")
  61. if target_branch_name := f"release/{version.major}.{version.minor}" != branch_name:
  62. raise ValueError(
  63. f"Branch name mismatch branch={branch_name} does not match target branch name={target_branch_name}"
  64. )
  65. name = f"{name}_VERSION" if name else "VERSION"
  66. print(f"{name}={version.name}") # noqa: T201
  67. if __name__ == "__main__":
  68. paths = (
  69. [sys.argv[1]]
  70. if sys.argv[1] != "ALL"
  71. else [
  72. f"taipy{os.sep}config",
  73. f"taipy{os.sep}core",
  74. f"taipy{os.sep}rest",
  75. f"taipy{os.sep}gui",
  76. f"taipy{os.sep}templates",
  77. "taipy",
  78. ]
  79. )
  80. _environment = sys.argv[2]
  81. for _path in paths:
  82. _version = extract_version(_path)
  83. _name = None if _path == "taipy" else _path.split(os.sep)[-1]
  84. if _environment == "dev":
  85. __setup_dev_version(_version, _path, _name)
  86. if _environment == "production":
  87. __setup_prod_version(_version, sys.argv[3], sys.argv[4], _name)