setup_version.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2021-2024 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. # --------------------------------------------------------------------------------------------------
  12. # Checks that build version matches package version.
  13. # Updates version number for future 'dev' builds.
  14. #
  15. # Invoked from the workflow in build-and-release.yml.
  16. #
  17. # Outputs a line for each package (all packages if 'all'):
  18. # <package_short_name>_VERSION=<package_version>
  19. # If a dev release is requested, a similar line is issued indicating the next dev version number:
  20. # NEXT_<package_short_name>_VERSION=<package_version>
  21. # --------------------------------------------------------------------------------------------------
  22. import json
  23. import sys
  24. from pathlib import Path
  25. from common import PACKAGES, Package, Version, fetch_latest_github_taipy_releases
  26. def usage() -> None:
  27. print(f"Usage: {sys.argv[0]} <package> <release_type> [<version> <branch>]") # noqa: T201
  28. print(" if <package> is 'ALL' then all packages including taipy are processed.") # noqa: T201
  29. print(" <release_type> must be 'dev' or 'production'") # noqa: T201
  30. print(" If <release_type> is 'production', <version> must be specified as the target") # noqa: T201
  31. print(" version and <branch> must be set to the name of the current branch.") # noqa: T201
  32. def extract_version(package: Package) -> Version:
  33. """
  34. Returns a Version from a package's version.json content.
  35. """
  36. with open(Path(package.package_dir) / "version.json") as version_file:
  37. data = json.load(version_file)
  38. return Version(**data)
  39. def __setup_dev_version(package: Package, version: Version) -> None:
  40. if not version.validate_extension("dev"):
  41. raise ValueError(f"{version=} is not a 'dev' version in package {package}.")
  42. var_name = f"{package.short_name}_VERSION"
  43. print(f"{var_name}={version.full_name}") # noqa: T201
  44. # Increment dev version
  45. version = version.bump_ext_version()
  46. # Save in packages's version.json
  47. with open(Path(package.package_dir) / "version.json", "w") as version_file:
  48. json.dump(version.to_dict(), version_file)
  49. # Return it to the GitHub step
  50. print(f"NEXT_{var_name}={version.full_name}") # noqa: T201
  51. def __setup_prod_version(
  52. package: Package, version: Version, target_version: str, branch_name: str) -> None:
  53. if version.full_name != target_version:
  54. raise ValueError(f"Current {version=} does not match {target_version=} for package {package.name}")
  55. # Production releases can only be performed from a release branch
  56. if target_branch_name := f"release/{version.major}.{version.minor}" != branch_name:
  57. raise ValueError(
  58. f"Branch name mismatch branch={branch_name} does not match target branch name={target_branch_name}"
  59. )
  60. print(f"{package.short_name}_VERSION={version.name}") # noqa: T201
  61. if __name__ == "__main__":
  62. if len(sys.argv) < 3:
  63. usage()
  64. raise ValueError("Missing arguments.")
  65. packages = [sys.argv[1]] if sys.argv[1].lower() != "all" else PACKAGES + ["taipy"]
  66. release_type = sys.argv[2]
  67. for package_name in packages:
  68. package = Package(package_name)
  69. version = extract_version(package)
  70. if release_type == "dev":
  71. __setup_dev_version(package, version)
  72. elif release_type == "production":
  73. if len(sys.argv) < 5:
  74. usage()
  75. raise ValueError("Missing arguments.")
  76. __setup_prod_version(package, version, sys.argv[3], sys.argv[4])
  77. else:
  78. usage()
  79. raise ValueError(f"Invalid <release_type> argument ({release_type}).")
  80. # Print out the latest 'taipy' version that has no extension
  81. print(f"LATEST_TAIPY_VERSION={fetch_latest_github_taipy_releases()}") # noqa: T201