core.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. from collections import OrderedDict
  17. from ..utils import logging
  18. from .utils import install_deps_using_pip
  19. from .meta import get_all_repo_names
  20. from .repo import build_repo_instance, build_repo_group_cloner, build_repo_group_installer
  21. __all__ = [
  22. 'set_parent_dirs', 'setup', 'wheel', 'is_initialized', 'initialize',
  23. 'get_versions'
  24. ]
  25. class _GlobalContext(object):
  26. REPO_PARENT_DIR = None
  27. PDX_COLLECTION_MOD = None
  28. REPOS = None
  29. @classmethod
  30. def set_parent_dirs(cls, repo_parent_dir, pdx_collection_mod):
  31. """ set_parent_dirs """
  32. cls.REPO_PARENT_DIR = repo_parent_dir
  33. cls.PDX_COLLECTION_MOD = pdx_collection_mod
  34. @classmethod
  35. def build_repo_instance(cls, repo_name):
  36. """ build_repo_instance """
  37. return build_repo_instance(repo_name, cls.REPO_PARENT_DIR,
  38. cls.PDX_COLLECTION_MOD)
  39. @classmethod
  40. def is_initialized(cls):
  41. """ is_initialized """
  42. return cls.REPOS is not None
  43. @classmethod
  44. def initialize(cls):
  45. """ initialize """
  46. cls.REPOS = []
  47. @classmethod
  48. def add_repo(cls, repo):
  49. """ add_repo """
  50. if not cls.is_initialized():
  51. cls.initialize()
  52. cls.REPOS.append(repo)
  53. @classmethod
  54. def add_repos(cls, repos):
  55. """ add_repos """
  56. if len(repos) == 0 and not cls.is_initialized():
  57. cls.initialize()
  58. for repo in repos:
  59. cls.add_repo(repo)
  60. set_parent_dirs = _GlobalContext.set_parent_dirs
  61. is_initialized = _GlobalContext.is_initialized
  62. def setup(repo_names,
  63. reinstall=None,
  64. no_deps=False,
  65. constraints=None,
  66. platform=None,
  67. update_repos=False):
  68. """ setup """
  69. repos = []
  70. for repo_name in repo_names:
  71. repo = _GlobalContext.build_repo_instance(repo_name)
  72. repos.append(repo)
  73. repos_to_clone = []
  74. for repo in repos:
  75. repo_name = repo.name
  76. if repo.check_repo_exiting():
  77. logging.warning(f"Existing of {repo.name} repo.")
  78. if reinstall is None:
  79. if sys.stdin.isatty():
  80. logging.warning("Should we remove it (y/n)?")
  81. try:
  82. remove_existing = input()
  83. except EOFError:
  84. logging.warning(
  85. "Unable to read from stdin. Please set `reinstall` to \
  86. True or False to apply a global setting for reclone repos."
  87. )
  88. raise
  89. remove_existing = remove_existing.lower() in ('y', 'yes')
  90. else:
  91. remove_existing = reinstall
  92. if remove_existing:
  93. repo.remove()
  94. repos_to_clone.append(repo)
  95. else:
  96. logging.warning(
  97. f"We will use the existing repo of {repo.name}.")
  98. else:
  99. repos_to_clone.append(repo)
  100. repos_to_install = []
  101. for repo in repos:
  102. repo_name = repo.name
  103. if repo.check_installation():
  104. logging.warning(f"Existing installation of {repo.name} detected.")
  105. if reinstall is None and not update_repos:
  106. if sys.stdin.isatty():
  107. logging.warning("Should we uninstall it (y/n)?")
  108. try:
  109. uninstall_existing = input()
  110. except EOFError:
  111. logging.warning(
  112. "Unable to read from stdin. Please set `reinstall` to \
  113. True or False to apply a global setting for reinstalling repos."
  114. )
  115. raise
  116. uninstall_existing = uninstall_existing.lower() in ('y', 'yes')
  117. else:
  118. if reinstall or update_repos:
  119. uninstall_existing = True
  120. if uninstall_existing:
  121. repo.uninstall()
  122. repos_to_install.append(repo)
  123. else:
  124. logging.warning(
  125. f"We will use the existing installation of {repo.name}.")
  126. else:
  127. repos_to_install.append(repo)
  128. cloner = build_repo_group_cloner(*repos_to_clone)
  129. installer = build_repo_group_installer(*repos_to_install)
  130. logging.info("Now cloning the repos...")
  131. cloner.clone(force_reclone=False, platform=platform)
  132. logging.info("All repos are existing.")
  133. if not no_deps:
  134. logging.info("Dependencies are listed below:")
  135. logging.info(installer.get_deps())
  136. logging.info("Now installing the packages...")
  137. install_deps_using_pip()
  138. if update_repos:
  139. installer.update()
  140. logging.info("All repos are updated.")
  141. installer.install(
  142. force_reinstall=False, no_deps=no_deps, constraints=constraints)
  143. logging.info("All packages are installed.")
  144. def wheel(repo_names, dst_dir='./', fail_fast=False):
  145. """ wheel """
  146. for repo_name in repo_names:
  147. repo = _GlobalContext.build_repo_instance(repo_name)
  148. logging.info(f"Now building Wheel for {repo_name}...")
  149. try:
  150. tgt_dir = os.path.join(dst_dir, repo.pkg_name)
  151. if os.path.exists(tgt_dir):
  152. raise FileExistsError(f"{tgt_dir} already exists.")
  153. repo.wheel(tgt_dir)
  154. except Exception as e:
  155. logging.warning(
  156. f"Failed to build wheel for {repo_name}. We encountered the following error:\n {str(e)}\n"
  157. )
  158. if fail_fast:
  159. raise
  160. else:
  161. logging.info(f"Wheel for {repo_name} is built.\n")
  162. def initialize(repo_names=None):
  163. """ initialize """
  164. if _GlobalContext.is_initialized():
  165. raise RuntimeError(
  166. "PDX has already been initialized. Reinitialization is not supported."
  167. )
  168. if repo_names is None:
  169. try_all = True
  170. repo_names = get_all_repo_names()
  171. else:
  172. try_all = False
  173. repos = []
  174. for repo_name in repo_names:
  175. logging.debug(f"Now initializing {repo_name}...")
  176. repo = _GlobalContext.build_repo_instance(repo_name)
  177. flag = repo.initialize()
  178. if flag:
  179. logging.debug(f"{repo_name} is initialized.")
  180. repos.append(repo)
  181. else:
  182. if try_all:
  183. logging.debug(
  184. f"Failed to initialize {repo_name}. Please make sure {repo_name} is properly installed."
  185. )
  186. else:
  187. pass
  188. _GlobalContext.add_repos(repos)
  189. def get_versions(repo_names=None):
  190. """ get_versions """
  191. if repo_names is None:
  192. repo_names = get_all_repo_names()
  193. name2versions = OrderedDict()
  194. for repo_name in repo_names:
  195. repo = _GlobalContext.build_repo_instance(repo_name)
  196. versions = repo.get_version()
  197. name2versions[repo_name] = versions
  198. return name2versions