setup_version.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import sys
  2. import json
  3. import os
  4. from dataclasses import dataclass, asdict
  5. import re
  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. def __str__(self) -> str:
  23. """returns a string representation of a version"""
  24. version_str = f"{self.major}.{self.minor}.{self.patch}"
  25. if self.ext:
  26. version_str = f"{version_str}.{self.ext}"
  27. return version_str
  28. def __load_version_from_path(base_path: str) -> Version:
  29. """Load version.json file from base path."""
  30. with open(os.path.join(base_path, "version.json")) as version_file:
  31. data = json.load(version_file)
  32. return Version(**data)
  33. def __write_version_to_path(base_path: str, version: Version) -> None:
  34. with open(os.path.join(base_path, "version.json"), "w") as version_file:
  35. json.dump(asdict(version), version_file)
  36. def extract_version(base_path: str) -> Version:
  37. """
  38. Load version.json file from base path and return the version string.
  39. """
  40. return __load_version_from_path(base_path)
  41. def __setup_dev_version(
  42. version: Version, _base_path: str, name: Optional[str] = None, bump_dev_version: bool = False
  43. ) -> None:
  44. name = f"{name}_VERSION" if name else "VERSION"
  45. version.validate_suffix()
  46. print(f"{name}={version}")
  47. if bump_dev_version:
  48. version.bump_ext_version()
  49. __write_version_to_path(_base_path, version)
  50. print(f"NEW_{name}={version}")
  51. def __setup_prod_version(version: Version, target_version: str, branch_name: str) -> None:
  52. if str(version) != target_version:
  53. raise ValueError(f"Current version={version} does not match target version={target_version}")
  54. if target_branch_name := f"release/{version.major}.{version.minor}" != branch_name:
  55. raise ValueError(
  56. f"Branch name mismatch branch={branch_name} does not match target branch name={target_branch_name}"
  57. )
  58. if __name__ == "__main__":
  59. paths = (
  60. [sys.argv[1]]
  61. if sys.argv[1] != "ALL"
  62. else [
  63. f"taipy{os.sep}config",
  64. f"taipy{os.sep}core",
  65. f"taipy{os.sep}rest",
  66. f"taipy{os.sep}gui",
  67. f"taipy{os.sep}templates",
  68. "taipy",
  69. ]
  70. )
  71. _environment = sys.argv[2]
  72. should_bump = False
  73. try:
  74. should_bump = True if sys.argv[3] == "bump" else False
  75. except IndexError:
  76. pass
  77. for _path in paths:
  78. _version = extract_version(_path)
  79. if _environment == "dev":
  80. _name = None if _path == "taipy" else _path.split(os.sep)[-1]
  81. __setup_dev_version(_version, _path, _name, should_bump)
  82. if _environment == "prod":
  83. __setup_prod_version(_version, sys.argv[3], sys.argv[4])