m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
12 KiB

6 months ago
  1. import json
  2. import logging
  3. from optparse import Values
  4. from typing import TYPE_CHECKING, Generator, List, Optional, Sequence, Tuple, cast
  5. from pip._vendor.packaging.utils import canonicalize_name
  6. from pip._internal.cli import cmdoptions
  7. from pip._internal.cli.req_command import IndexGroupCommand
  8. from pip._internal.cli.status_codes import SUCCESS
  9. from pip._internal.exceptions import CommandError
  10. from pip._internal.index.collector import LinkCollector
  11. from pip._internal.index.package_finder import PackageFinder
  12. from pip._internal.metadata import BaseDistribution, get_environment
  13. from pip._internal.models.selection_prefs import SelectionPreferences
  14. from pip._internal.network.session import PipSession
  15. from pip._internal.utils.compat import stdlib_pkgs
  16. from pip._internal.utils.misc import tabulate, write_output
  17. if TYPE_CHECKING:
  18. from pip._internal.metadata.base import DistributionVersion
  19. class _DistWithLatestInfo(BaseDistribution):
  20. """Give the distribution object a couple of extra fields.
  21. These will be populated during ``get_outdated()``. This is dirty but
  22. makes the rest of the code much cleaner.
  23. """
  24. latest_version: DistributionVersion
  25. latest_filetype: str
  26. _ProcessedDists = Sequence[_DistWithLatestInfo]
  27. logger = logging.getLogger(__name__)
  28. class ListCommand(IndexGroupCommand):
  29. """
  30. List installed packages, including editables.
  31. Packages are listed in a case-insensitive sorted order.
  32. """
  33. ignore_require_venv = True
  34. usage = """
  35. %prog [options]"""
  36. def add_options(self) -> None:
  37. self.cmd_opts.add_option(
  38. "-o",
  39. "--outdated",
  40. action="store_true",
  41. default=False,
  42. help="List outdated packages",
  43. )
  44. self.cmd_opts.add_option(
  45. "-u",
  46. "--uptodate",
  47. action="store_true",
  48. default=False,
  49. help="List uptodate packages",
  50. )
  51. self.cmd_opts.add_option(
  52. "-e",
  53. "--editable",
  54. action="store_true",
  55. default=False,
  56. help="List editable projects.",
  57. )
  58. self.cmd_opts.add_option(
  59. "-l",
  60. "--local",
  61. action="store_true",
  62. default=False,
  63. help=(
  64. "If in a virtualenv that has global access, do not list "
  65. "globally-installed packages."
  66. ),
  67. )
  68. self.cmd_opts.add_option(
  69. "--user",
  70. dest="user",
  71. action="store_true",
  72. default=False,
  73. help="Only output packages installed in user-site.",
  74. )
  75. self.cmd_opts.add_option(cmdoptions.list_path())
  76. self.cmd_opts.add_option(
  77. "--pre",
  78. action="store_true",
  79. default=False,
  80. help=(
  81. "Include pre-release and development versions. By default, "
  82. "pip only finds stable versions."
  83. ),
  84. )
  85. self.cmd_opts.add_option(
  86. "--format",
  87. action="store",
  88. dest="list_format",
  89. default="columns",
  90. choices=("columns", "freeze", "json"),
  91. help=(
  92. "Select the output format among: columns (default), freeze, or json. "
  93. "The 'freeze' format cannot be used with the --outdated option."
  94. ),
  95. )
  96. self.cmd_opts.add_option(
  97. "--not-required",
  98. action="store_true",
  99. dest="not_required",
  100. help="List packages that are not dependencies of installed packages.",
  101. )
  102. self.cmd_opts.add_option(
  103. "--exclude-editable",
  104. action="store_false",
  105. dest="include_editable",
  106. help="Exclude editable package from output.",
  107. )
  108. self.cmd_opts.add_option(
  109. "--include-editable",
  110. action="store_true",
  111. dest="include_editable",
  112. help="Include editable package from output.",
  113. default=True,
  114. )
  115. self.cmd_opts.add_option(cmdoptions.list_exclude())
  116. index_opts = cmdoptions.make_option_group(cmdoptions.index_group, self.parser)
  117. self.parser.insert_option_group(0, index_opts)
  118. self.parser.insert_option_group(0, self.cmd_opts)
  119. def _build_package_finder(
  120. self, options: Values, session: PipSession
  121. ) -> PackageFinder:
  122. """
  123. Create a package finder appropriate to this list command.
  124. """
  125. link_collector = LinkCollector.create(session, options=options)
  126. # Pass allow_yanked=False to ignore yanked versions.
  127. selection_prefs = SelectionPreferences(
  128. allow_yanked=False,
  129. allow_all_prereleases=options.pre,
  130. )
  131. return PackageFinder.create(
  132. link_collector=link_collector,
  133. selection_prefs=selection_prefs,
  134. )
  135. def run(self, options: Values, args: List[str]) -> int:
  136. if options.outdated and options.uptodate:
  137. raise CommandError("Options --outdated and --uptodate cannot be combined.")
  138. if options.outdated and options.list_format == "freeze":
  139. raise CommandError(
  140. "List format 'freeze' cannot be used with the --outdated option."
  141. )
  142. cmdoptions.check_list_path_option(options)
  143. skip = set(stdlib_pkgs)
  144. if options.excludes:
  145. skip.update(canonicalize_name(n) for n in options.excludes)
  146. packages: "_ProcessedDists" = [
  147. cast("_DistWithLatestInfo", d)
  148. for d in get_environment(options.path).iter_installed_distributions(
  149. local_only=options.local,
  150. user_only=options.user,
  151. editables_only=options.editable,
  152. include_editables=options.include_editable,
  153. skip=skip,
  154. )
  155. ]
  156. # get_not_required must be called firstly in order to find and
  157. # filter out all dependencies correctly. Otherwise a package
  158. # can't be identified as requirement because some parent packages
  159. # could be filtered out before.
  160. if options.not_required:
  161. packages = self.get_not_required(packages, options)
  162. if options.outdated:
  163. packages = self.get_outdated(packages, options)
  164. elif options.uptodate:
  165. packages = self.get_uptodate(packages, options)
  166. self.output_package_listing(packages, options)
  167. return SUCCESS
  168. def get_outdated(
  169. self, packages: "_ProcessedDists", options: Values
  170. ) -> "_ProcessedDists":
  171. return [
  172. dist
  173. for dist in self.iter_packages_latest_infos(packages, options)
  174. if dist.latest_version > dist.version
  175. ]
  176. def get_uptodate(
  177. self, packages: "_ProcessedDists", options: Values
  178. ) -> "_ProcessedDists":
  179. return [
  180. dist
  181. for dist in self.iter_packages_latest_infos(packages, options)
  182. if dist.latest_version == dist.version
  183. ]
  184. def get_not_required(
  185. self, packages: "_ProcessedDists", options: Values
  186. ) -> "_ProcessedDists":
  187. dep_keys = {
  188. canonicalize_name(dep.name)
  189. for dist in packages
  190. for dep in (dist.iter_dependencies() or ())
  191. }
  192. # Create a set to remove duplicate packages, and cast it to a list
  193. # to keep the return type consistent with get_outdated and
  194. # get_uptodate
  195. return list({pkg for pkg in packages if pkg.canonical_name not in dep_keys})
  196. def iter_packages_latest_infos(
  197. self, packages: "_ProcessedDists", options: Values
  198. ) -> Generator["_DistWithLatestInfo", None, None]:
  199. with self._build_session(options) as session:
  200. finder = self._build_package_finder(options, session)
  201. def latest_info(
  202. dist: "_DistWithLatestInfo",
  203. ) -> Optional["_DistWithLatestInfo"]:
  204. all_candidates = finder.find_all_candidates(dist.canonical_name)
  205. if not options.pre:
  206. # Remove prereleases
  207. all_candidates = [
  208. candidate
  209. for candidate in all_candidates
  210. if not candidate.version.is_prerelease
  211. ]
  212. evaluator = finder.make_candidate_evaluator(
  213. project_name=dist.canonical_name,
  214. )
  215. best_candidate = evaluator.sort_best_candidate(all_candidates)
  216. if best_candidate is None:
  217. return None
  218. remote_version = best_candidate.version
  219. if best_candidate.link.is_wheel:
  220. typ = "wheel"
  221. else:
  222. typ = "sdist"
  223. dist.latest_version = remote_version
  224. dist.latest_filetype = typ
  225. return dist
  226. for dist in map(latest_info, packages):
  227. if dist is not None:
  228. yield dist
  229. def output_package_listing(
  230. self, packages: "_ProcessedDists", options: Values
  231. ) -> None:
  232. packages = sorted(
  233. packages,
  234. key=lambda dist: dist.canonical_name,
  235. )
  236. if options.list_format == "columns" and packages:
  237. data, header = format_for_columns(packages, options)
  238. self.output_package_listing_columns(data, header)
  239. elif options.list_format == "freeze":
  240. for dist in packages:
  241. if options.verbose >= 1:
  242. write_output(
  243. "%s==%s (%s)", dist.raw_name, dist.version, dist.location
  244. )
  245. else:
  246. write_output("%s==%s", dist.raw_name, dist.version)
  247. elif options.list_format == "json":
  248. write_output(format_for_json(packages, options))
  249. def output_package_listing_columns(
  250. self, data: List[List[str]], header: List[str]
  251. ) -> None:
  252. # insert the header first: we need to know the size of column names
  253. if len(data) > 0:
  254. data.insert(0, header)
  255. pkg_strings, sizes = tabulate(data)
  256. # Create and add a separator.
  257. if len(data) > 0:
  258. pkg_strings.insert(1, " ".join("-" * x for x in sizes))
  259. for val in pkg_strings:
  260. write_output(val)
  261. def format_for_columns(
  262. pkgs: "_ProcessedDists", options: Values
  263. ) -> Tuple[List[List[str]], List[str]]:
  264. """
  265. Convert the package data into something usable
  266. by output_package_listing_columns.
  267. """
  268. header = ["Package", "Version"]
  269. running_outdated = options.outdated
  270. if running_outdated:
  271. header.extend(["Latest", "Type"])
  272. has_editables = any(x.editable for x in pkgs)
  273. if has_editables:
  274. header.append("Editable project location")
  275. if options.verbose >= 1:
  276. header.append("Location")
  277. if options.verbose >= 1:
  278. header.append("Installer")
  279. data = []
  280. for proj in pkgs:
  281. # if we're working on the 'outdated' list, separate out the
  282. # latest_version and type
  283. row = [proj.raw_name, str(proj.version)]
  284. if running_outdated:
  285. row.append(str(proj.latest_version))
  286. row.append(proj.latest_filetype)
  287. if has_editables:
  288. row.append(proj.editable_project_location or "")
  289. if options.verbose >= 1:
  290. row.append(proj.location or "")
  291. if options.verbose >= 1:
  292. row.append(proj.installer)
  293. data.append(row)
  294. return data, header
  295. def format_for_json(packages: "_ProcessedDists", options: Values) -> str:
  296. data = []
  297. for dist in packages:
  298. info = {
  299. "name": dist.raw_name,
  300. "version": str(dist.version),
  301. }
  302. if options.verbose >= 1:
  303. info["location"] = dist.location or ""
  304. info["installer"] = dist.installer
  305. if options.outdated:
  306. info["latest_version"] = str(dist.latest_version)
  307. info["latest_filetype"] = dist.latest_filetype
  308. editable_project_location = dist.editable_project_location
  309. if editable_project_location:
  310. info["editable_project_location"] = editable_project_location
  311. data.append(info)
  312. return json.dumps(data)