setup_project.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import json
  12. import os
  13. import platform
  14. import re
  15. import subprocess
  16. import sys
  17. from pathlib import Path
  18. import toml # type: ignore
  19. def get_requirements(pkg: str, env: str = "dev") -> list:
  20. # get requirements from the different setups in tools/packages (removing taipy packages)
  21. reqs = set()
  22. pkg_name = pkg if pkg == "taipy" else f"taipy-{pkg}"
  23. root_folder = Path(__file__).parent
  24. package_path = os.path.join(root_folder.parent, "packages", pkg_name)
  25. requirements_file = os.path.join(package_path, "setup.requirements.txt")
  26. if os.path.exists(requirements_file):
  27. reqs.update(Path(requirements_file).read_text("UTF-8").splitlines())
  28. if env == "dev":
  29. return [r for r in reqs if r and not r.startswith("taipy")]
  30. return list(reqs)
  31. def update_pyproject(version_path: str, pyproject_path: str, env: str = "dev"):
  32. with open(version_path) as version_file:
  33. version = json.load(version_file)
  34. version_string = f'{version.get("major", 0)}.{version.get("minor", 0)}.{version.get("patch", 0)}'
  35. if vext := version.get("ext"):
  36. version_string = f"{version_string}.{vext}"
  37. pyproject_data = toml.load(pyproject_path)
  38. pyproject_data["project"]["version"] = version_string
  39. pyproject_data["project"]["urls"]["Release notes"] = f"https://docs.taipy.io/en/release-{version_string}/relnotes/"
  40. pyproject_data["project"]["dependencies"] = get_requirements(get_pkg_name(pyproject_path), env)
  41. with open(pyproject_path, "w", encoding="utf-8") as pyproject_file:
  42. toml.dump(pyproject_data, pyproject_file)
  43. def _build_webapp(webapp_path: str):
  44. already_exists = Path(webapp_path).exists()
  45. if not already_exists:
  46. os.system("cd ../../frontend/taipy-gui/dom && npm ci")
  47. os.system("cd ../../frontend/taipy-gui && npm ci && npm run build")
  48. def get_pkg_name(path: str) -> str:
  49. # The regex pattern
  50. pattern = r"([^/\\]+)[/\\]pyproject\.toml$"
  51. # Search for the pattern
  52. match = re.search(pattern, os.path.abspath(path))
  53. if not match:
  54. raise ValueError(f"Could not find package name in path: {path}")
  55. return match.group(1)
  56. if __name__ == "__main__":
  57. _pyproject_path = os.path.join(sys.argv[1], "pyproject.toml")
  58. try:
  59. env = sys.argv[2]
  60. except IndexError:
  61. env = "dev"
  62. pkg = get_pkg_name(_pyproject_path)
  63. if pkg == "taipy":
  64. _version_path = os.path.join(sys.argv[1], "taipy", "version.json")
  65. _webapp_path = os.path.join(sys.argv[1], "taipy", "gui", "webapp", "index.html")
  66. else:
  67. _version_path = os.path.join(sys.argv[1], "version.json")
  68. _webapp_path = os.path.join(sys.argv[1], "webapp", "index.html")
  69. update_pyproject(_version_path, _pyproject_path, env)
  70. if pkg == "gui":
  71. _build_webapp(_webapp_path)
  72. if pkg == "taipy":
  73. subprocess.run(
  74. ["python", "bundle_build.py"],
  75. cwd=os.path.join("tools", "frontend"),
  76. check=True,
  77. shell=platform.system() == "Windows",
  78. )