core.py 7.7 KB

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