core.py 8.7 KB

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