update_setup_requirements.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # Updates the setup.requirements.txt files for a given package.
  13. #
  14. # Invoked by workflows/build-and-release-single-package.yml and workflows/build-and-release.yml.
  15. # Working directory must be [root_dir].
  16. # --------------------------------------------------------------------------------------------------
  17. import os
  18. import re
  19. import sys
  20. import typing as t
  21. from common import PACKAGES, Version, retrieve_github_path
  22. BASE_PATH = "./tools/packages"
  23. def usage() -> None:
  24. packages = "> <".join(f"{p}_ver" for p in PACKAGES)
  25. print( # noqa: T201
  26. f"Usage: {sys.argv[0]} <package> <{packages}> <deps> [<gh_path>]"
  27. )
  28. packages = ", ".join(f"'{p}'" for p in PACKAGES[:-1])
  29. packages = f"{packages}, or '{PACKAGES[-1]}'"
  30. print(f" <package> must be one of {packages}.") # noqa: T201
  31. for p in PACKAGES:
  32. print(f" <{p}_ver>: minimal version of the taipy-{p} dependency.") # noqa: T201
  33. print(" <deps> must be 'Pypi' or 'GitHub', indicating where to find Taipy package dependencies.") # noqa: T201
  34. print(" <gh_path>: The path of GitHub repository (owner/repo), used if <deps> is 'GitHub'.") # noqa: T201
  35. def __build_taipy_package_line(line: str, version: Version, use_pypi: bool, gh_path: t.Optional[str]) -> str:
  36. line = line.strip()
  37. if use_pypi:
  38. # Target dependency version should the latest compatible with 'version'
  39. return f"{line} >={version.major}.{version.minor},<{version.major}.{version.minor + 1}\n"
  40. tag = f"{version}-{line.split('-')[1]}"
  41. tar_name = f"{line}-{version}"
  42. return f"{line} @ https://github.com/{gh_path}/releases/download/{tag}/{tar_name}.tar.gz\n"
  43. def update_setup_requirements(
  44. package: str, versions: dict[str, Version], publish_on_py_pi: bool, gh_path: t.Optional[str]
  45. ) -> None:
  46. path = os.path.join(BASE_PATH, "taipy" if package == "taipy" else f"taipy-{package}", "setup.requirements.txt")
  47. lines = []
  48. with open(path, mode="r") as req:
  49. for line in req:
  50. if match := re.match(r"^taipy(:?\-\w+)?\s*", line, re.MULTILINE):
  51. # Add subpackage version if not forced
  52. if not line[match.end() :] and (v := versions.get(line.strip())):
  53. if v == Version.UNKNOWN:
  54. raise ValueError(f"Missing version for dependency '{line.strip()}'.")
  55. line = __build_taipy_package_line(line, v, publish_on_py_pi, gh_path)
  56. lines.append(line)
  57. with open(path, "w") as file:
  58. file.writelines(lines)
  59. # Issue the generated files for logging information
  60. print(f"Generated setup.requirements.txt for package '{package}'") # noqa: T201
  61. for line in lines:
  62. print(line.strip()) # noqa: T201
  63. print("-" * 32) # noqa: T201
  64. if __name__ == "__main__":
  65. if len(sys.argv) < len(PACKAGES) + 3:
  66. usage()
  67. raise ValueError("Missing arguments.")
  68. package = sys.argv[1]
  69. # Store the provided version for each package listed in PACKAGES
  70. versions = {f"taipy-{p}": Version.from_string(sys.argv[i]) for i, p in enumerate(PACKAGES, 2)}
  71. # Keep compatibility with legacy actions ('true' is equivalent to 'Pypi')
  72. pypi_deps = sys.argv[len(PACKAGES) + 2].lower() in ["true", "pypi"]
  73. gh_path = None
  74. if not pypi_deps:
  75. if len(sys.argv) < len(PACKAGES) + 4:
  76. gh_path = retrieve_github_path()
  77. if gh_path is None:
  78. usage()
  79. raise ValueError("Couldn't figure out GitHub branch path.")
  80. else:
  81. gh_path = sys.argv[len(PACKAGES) + 3]
  82. update_setup_requirements(package, versions, pypi_deps, gh_path)