core.py 7.9 KB

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