core.py 7.1 KB

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