core.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 sys
  15. from collections import OrderedDict
  16. from ..utils import logging
  17. from .meta import get_all_repo_names, get_repo_meta
  18. from .repo import (
  19. build_repo_group_getter,
  20. build_repo_group_installer,
  21. build_repo_instance,
  22. )
  23. __all__ = [
  24. "set_parent_dirs",
  25. "setup",
  26. "is_initialized",
  27. "initialize",
  28. "get_versions",
  29. ]
  30. def _parse_repo_deps(repos):
  31. ret = []
  32. for repo_name in repos:
  33. repo_meta = get_repo_meta(repo_name)
  34. ret.extend(_parse_repo_deps(repo_meta.get("requires", [])))
  35. ret.append(repo_name)
  36. return ret
  37. class _GlobalContext(object):
  38. REPO_PARENT_DIR = None
  39. PDX_COLLECTION_MOD = None
  40. REPOS = None
  41. @classmethod
  42. def set_parent_dirs(cls, repo_parent_dir, pdx_collection_mod):
  43. """set_parent_dirs"""
  44. cls.REPO_PARENT_DIR = repo_parent_dir
  45. cls.PDX_COLLECTION_MOD = pdx_collection_mod
  46. @classmethod
  47. def build_repo_instance(cls, repo_name):
  48. """build_repo_instance"""
  49. return build_repo_instance(
  50. repo_name, cls.REPO_PARENT_DIR, cls.PDX_COLLECTION_MOD
  51. )
  52. @classmethod
  53. def is_initialized(cls):
  54. """is_initialized"""
  55. return cls.REPOS is not None
  56. @classmethod
  57. def initialize(cls):
  58. """initialize"""
  59. cls.REPOS = []
  60. @classmethod
  61. def add_repo(cls, repo):
  62. """add_repo"""
  63. if not cls.is_initialized():
  64. cls.initialize()
  65. cls.REPOS.append(repo)
  66. @classmethod
  67. def add_repos(cls, repos):
  68. """add_repos"""
  69. if len(repos) == 0 and not cls.is_initialized():
  70. cls.initialize()
  71. for repo in repos:
  72. cls.add_repo(repo)
  73. set_parent_dirs = _GlobalContext.set_parent_dirs
  74. is_initialized = _GlobalContext.is_initialized
  75. def setup(
  76. repo_names,
  77. no_deps=False,
  78. constraints=None,
  79. platform=None,
  80. update_repos=False,
  81. use_local_repos=False,
  82. deps_to_replace=None,
  83. ):
  84. """setup"""
  85. if update_repos and use_local_repos:
  86. logging.error(
  87. f"The `--update_repos` and `--use_local_repos` should not be True at the same time. They are global setting for all repos. `--update_repos` means that update all repos to sync with remote, and `--use_local_repos` means that don't update when local repo is existing."
  88. )
  89. raise Exception()
  90. repo_names = list(set(_parse_repo_deps(repo_names)))
  91. repos = []
  92. for repo_name in repo_names:
  93. repo = _GlobalContext.build_repo_instance(repo_name)
  94. repos.append(repo)
  95. changed_repos = []
  96. repos_to_get = []
  97. for repo in repos:
  98. repo_name = repo.name
  99. if repo.check_repo_exiting():
  100. if use_local_repos:
  101. # when use_local_repos has been set, it can be only assume that the local repo has changed, otherwise there is no need to specify.
  102. changed_repos.append(repo_name)
  103. logging.warning(
  104. f"We will use the existing repo of {repo.name} and the repo will be reinstall."
  105. )
  106. continue
  107. logging.warning(f"Existing of {repo.name} repo.")
  108. if update_repos:
  109. remove_existing = True
  110. else:
  111. if sys.stdin.isatty():
  112. logging.warning("Should we remove it (y/n)?")
  113. try:
  114. remove_existing = input()
  115. except EOFError:
  116. logging.warning(
  117. "Unable to read from stdin. Please set `--use_local_repos` to \
  118. True or False to apply a global setting for using existing or re-getting repos."
  119. )
  120. raise
  121. remove_existing = remove_existing.lower() in ("y", "yes")
  122. if remove_existing:
  123. changed_repos.append(repo_name)
  124. repo.remove()
  125. logging.warning(f"Existing {repo.name} repo has been removed.")
  126. repos_to_get.append(repo)
  127. else:
  128. logging.warning(f"We will use the existing repo of {repo.name}.")
  129. else:
  130. changed_repos.append(repo)
  131. repos_to_get.append(repo)
  132. repos_to_install = []
  133. for repo in repos:
  134. repo_name = repo.name
  135. if repo.check_installation():
  136. logging.warning(f"Existing installation of {repo.name} detected.")
  137. reinstall = repo_name in changed_repos
  138. if reinstall:
  139. uninstall_existing = True
  140. else:
  141. if sys.stdin.isatty():
  142. logging.warning("Should we uninstall it (y/n)?")
  143. try:
  144. uninstall_existing = input()
  145. except EOFError:
  146. logging.warning(
  147. "Unable to read from stdin. Please set `reinstall` to \
  148. True or False to apply a global setting for reinstalling repos."
  149. )
  150. raise
  151. uninstall_existing = uninstall_existing.lower() in ("y", "yes")
  152. if uninstall_existing:
  153. build_repo_group_installer(repo).uninstall()
  154. repos_to_install.append(repo)
  155. else:
  156. logging.warning(
  157. f"We will use the existing installation of {repo.name}."
  158. )
  159. else:
  160. repos_to_install.append(repo)
  161. getter = build_repo_group_getter(*repos_to_get)
  162. installer = build_repo_group_installer(*repos_to_install)
  163. if len(repos_to_get) > 0:
  164. logging.info(
  165. f"Now download and update the repos: {list(repo.name for repo in repos_to_get)}."
  166. )
  167. getter.get(force=True, platform=platform)
  168. logging.info("All repos are existing.")
  169. else:
  170. logging.info("No repo need to download or update.")
  171. if not no_deps:
  172. logging.info("Dependencies are listed below:")
  173. logging.info(installer.get_deps())
  174. logging.info("Now installing the packages...")
  175. installer.install(
  176. force_reinstall=False,
  177. no_deps=no_deps,
  178. constraints=constraints,
  179. deps_to_replace=deps_to_replace,
  180. )
  181. logging.info("All packages are installed.")
  182. def initialize(repo_names=None):
  183. """initialize"""
  184. if _GlobalContext.is_initialized():
  185. raise RuntimeError(
  186. "PDX has already been initialized. Reinitialization is not supported."
  187. )
  188. if repo_names is None:
  189. try_all = True
  190. repo_names = get_all_repo_names()
  191. else:
  192. try_all = False
  193. repos = []
  194. for repo_name in repo_names:
  195. logging.debug(f"Now initializing {repo_name}...")
  196. repo = _GlobalContext.build_repo_instance(repo_name)
  197. flag = repo.initialize()
  198. if flag:
  199. logging.debug(f"{repo_name} is initialized.")
  200. repos.append(repo)
  201. else:
  202. if try_all:
  203. logging.debug(
  204. f"Failed to initialize {repo_name}. Please make sure {repo_name} is properly installed."
  205. )
  206. else:
  207. pass
  208. _GlobalContext.add_repos(repos)
  209. def get_versions(repo_names=None):
  210. """get_versions"""
  211. if repo_names is None:
  212. repo_names = get_all_repo_names()
  213. name2versions = OrderedDict()
  214. for repo_name in repo_names:
  215. repo = _GlobalContext.build_repo_instance(repo_name)
  216. versions = repo.get_version()
  217. name2versions[repo_name] = versions
  218. return name2versions