core.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_cloner, 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. reinstall=None,
  71. no_deps=False,
  72. constraints=None,
  73. platform=None,
  74. update_repos=False,
  75. use_local_repos=False,
  76. force_clone=False):
  77. """ setup """
  78. repo_names = list(set(_parse_repo_deps(repo_names)))
  79. repos = []
  80. for repo_name in repo_names:
  81. repo = _GlobalContext.build_repo_instance(repo_name)
  82. repos.append(repo)
  83. repos_to_clone = []
  84. for repo in repos:
  85. repo_name = repo.name
  86. if repo.check_repo_exiting():
  87. if use_local_repos:
  88. reinstall = True
  89. logging.warning(
  90. f"We will use the existing repo of {repo.name}.")
  91. continue
  92. logging.warning(f"Existing of {repo.name} repo.")
  93. if reinstall is None:
  94. if sys.stdin.isatty():
  95. logging.warning("Should we remove it (y/n)?")
  96. try:
  97. remove_existing = input()
  98. except EOFError:
  99. logging.warning(
  100. "Unable to read from stdin. Please set `reinstall` to \
  101. True or False to apply a global setting for reclone repos."
  102. )
  103. raise
  104. remove_existing = remove_existing.lower() in ('y', 'yes')
  105. else:
  106. remove_existing = reinstall
  107. if remove_existing:
  108. repo.remove()
  109. repos_to_clone.append(repo)
  110. else:
  111. logging.warning(
  112. f"We will use the existing repo of {repo.name}.")
  113. else:
  114. repos_to_clone.append(repo)
  115. repos_to_install = []
  116. for repo in repos:
  117. repo_name = repo.name
  118. if repo.check_installation():
  119. logging.warning(f"Existing installation of {repo.name} detected.")
  120. if reinstall is None and not update_repos:
  121. if sys.stdin.isatty():
  122. logging.warning("Should we uninstall it (y/n)?")
  123. try:
  124. uninstall_existing = input()
  125. except EOFError:
  126. logging.warning(
  127. "Unable to read from stdin. Please set `reinstall` to \
  128. True or False to apply a global setting for reinstalling repos."
  129. )
  130. raise
  131. uninstall_existing = uninstall_existing.lower() in ('y', 'yes')
  132. else:
  133. if reinstall or update_repos:
  134. uninstall_existing = True
  135. if uninstall_existing:
  136. repo.uninstall()
  137. repos_to_install.append(repo)
  138. else:
  139. logging.warning(
  140. f"We will use the existing installation of {repo.name}.")
  141. else:
  142. repos_to_install.append(repo)
  143. cloner = build_repo_group_cloner(*repos_to_clone)
  144. installer = build_repo_group_installer(*repos_to_install)
  145. logging.info("Now cloning the repos...")
  146. cloner.clone(force_clone=force_clone, platform=platform)
  147. logging.info("All repos are existing.")
  148. if not no_deps:
  149. logging.info("Dependencies are listed below:")
  150. logging.info(installer.get_deps())
  151. logging.info("Now installing the packages...")
  152. install_deps_using_pip()
  153. if update_repos:
  154. installer.update()
  155. logging.info("All repos are updated.")
  156. installer.install(
  157. force_reinstall=False, no_deps=no_deps, constraints=constraints)
  158. logging.info("All packages are installed.")
  159. def wheel(repo_names, dst_dir='./', fail_fast=False):
  160. """ wheel """
  161. for repo_name in repo_names:
  162. repo = _GlobalContext.build_repo_instance(repo_name)
  163. logging.info(f"Now building Wheel for {repo_name}...")
  164. try:
  165. tgt_dir = os.path.join(dst_dir, repo.pkg_name)
  166. if os.path.exists(tgt_dir):
  167. raise FileExistsError(f"{tgt_dir} already exists.")
  168. repo.wheel(tgt_dir)
  169. except Exception as e:
  170. logging.warning(
  171. f"Failed to build wheel for {repo_name}. We encountered the following error:\n {str(e)}\n"
  172. )
  173. if fail_fast:
  174. raise
  175. else:
  176. logging.info(f"Wheel for {repo_name} is built.\n")
  177. def initialize(repo_names=None):
  178. """ initialize """
  179. if _GlobalContext.is_initialized():
  180. raise RuntimeError(
  181. "PDX has already been initialized. Reinitialization is not supported."
  182. )
  183. if repo_names is None:
  184. try_all = True
  185. repo_names = get_all_repo_names()
  186. else:
  187. try_all = False
  188. repos = []
  189. for repo_name in repo_names:
  190. logging.debug(f"Now initializing {repo_name}...")
  191. repo = _GlobalContext.build_repo_instance(repo_name)
  192. flag = repo.initialize()
  193. if flag:
  194. logging.debug(f"{repo_name} is initialized.")
  195. repos.append(repo)
  196. else:
  197. if try_all:
  198. logging.debug(
  199. f"Failed to initialize {repo_name}. Please make sure {repo_name} is properly installed."
  200. )
  201. else:
  202. pass
  203. _GlobalContext.add_repos(repos)
  204. def get_versions(repo_names=None):
  205. """ get_versions """
  206. if repo_names is None:
  207. repo_names = get_all_repo_names()
  208. name2versions = OrderedDict()
  209. for repo_name in repo_names:
  210. repo = _GlobalContext.build_repo_instance(repo_name)
  211. versions = repo.get_version()
  212. name2versions[repo_name] = versions
  213. return name2versions