cache.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import os
  2. import textwrap
  3. from optparse import Values
  4. from typing import Any, List
  5. import pip._internal.utils.filesystem as filesystem
  6. from pip._internal.cli.base_command import Command
  7. from pip._internal.cli.status_codes import ERROR, SUCCESS
  8. from pip._internal.exceptions import CommandError, PipError
  9. from pip._internal.utils.logging import getLogger
  10. logger = getLogger(__name__)
  11. class CacheCommand(Command):
  12. """
  13. Inspect and manage pip's wheel cache.
  14. Subcommands:
  15. - dir: Show the cache directory.
  16. - info: Show information about the cache.
  17. - list: List filenames of packages stored in the cache.
  18. - remove: Remove one or more package from the cache.
  19. - purge: Remove all items from the cache.
  20. ``<pattern>`` can be a glob expression or a package name.
  21. """
  22. ignore_require_venv = True
  23. usage = """
  24. %prog dir
  25. %prog info
  26. %prog list [<pattern>] [--format=[human, abspath]]
  27. %prog remove <pattern>
  28. %prog purge
  29. """
  30. def add_options(self) -> None:
  31. self.cmd_opts.add_option(
  32. '--format',
  33. action='store',
  34. dest='list_format',
  35. default="human",
  36. choices=('human', 'abspath'),
  37. help="Select the output format among: human (default) or abspath"
  38. )
  39. self.parser.insert_option_group(0, self.cmd_opts)
  40. def run(self, options: Values, args: List[Any]) -> int:
  41. handlers = {
  42. "dir": self.get_cache_dir,
  43. "info": self.get_cache_info,
  44. "list": self.list_cache_items,
  45. "remove": self.remove_cache_items,
  46. "purge": self.purge_cache,
  47. }
  48. if not options.cache_dir:
  49. logger.error("pip cache commands can not "
  50. "function since cache is disabled.")
  51. return ERROR
  52. # Determine action
  53. if not args or args[0] not in handlers:
  54. logger.error(
  55. "Need an action (%s) to perform.",
  56. ", ".join(sorted(handlers)),
  57. )
  58. return ERROR
  59. action = args[0]
  60. # Error handling happens here, not in the action-handlers.
  61. try:
  62. handlers[action](options, args[1:])
  63. except PipError as e:
  64. logger.error(e.args[0])
  65. return ERROR
  66. return SUCCESS
  67. def get_cache_dir(self, options: Values, args: List[Any]) -> None:
  68. if args:
  69. raise CommandError('Too many arguments')
  70. logger.info(options.cache_dir)
  71. def get_cache_info(self, options: Values, args: List[Any]) -> None:
  72. if args:
  73. raise CommandError('Too many arguments')
  74. num_http_files = len(self._find_http_files(options))
  75. num_packages = len(self._find_wheels(options, '*'))
  76. http_cache_location = self._cache_dir(options, 'http')
  77. wheels_cache_location = self._cache_dir(options, 'wheels')
  78. http_cache_size = filesystem.format_directory_size(http_cache_location)
  79. wheels_cache_size = filesystem.format_directory_size(
  80. wheels_cache_location
  81. )
  82. message = textwrap.dedent("""
  83. Package index page cache location: {http_cache_location}
  84. Package index page cache size: {http_cache_size}
  85. Number of HTTP files: {num_http_files}
  86. Wheels location: {wheels_cache_location}
  87. Wheels size: {wheels_cache_size}
  88. Number of wheels: {package_count}
  89. """).format(
  90. http_cache_location=http_cache_location,
  91. http_cache_size=http_cache_size,
  92. num_http_files=num_http_files,
  93. wheels_cache_location=wheels_cache_location,
  94. package_count=num_packages,
  95. wheels_cache_size=wheels_cache_size,
  96. ).strip()
  97. logger.info(message)
  98. def list_cache_items(self, options: Values, args: List[Any]) -> None:
  99. if len(args) > 1:
  100. raise CommandError('Too many arguments')
  101. if args:
  102. pattern = args[0]
  103. else:
  104. pattern = '*'
  105. files = self._find_wheels(options, pattern)
  106. if options.list_format == 'human':
  107. self.format_for_human(files)
  108. else:
  109. self.format_for_abspath(files)
  110. def format_for_human(self, files: List[str]) -> None:
  111. if not files:
  112. logger.info('Nothing cached.')
  113. return
  114. results = []
  115. for filename in files:
  116. wheel = os.path.basename(filename)
  117. size = filesystem.format_file_size(filename)
  118. results.append(f' - {wheel} ({size})')
  119. logger.info('Cache contents:\n')
  120. logger.info('\n'.join(sorted(results)))
  121. def format_for_abspath(self, files: List[str]) -> None:
  122. if not files:
  123. return
  124. results = []
  125. for filename in files:
  126. results.append(filename)
  127. logger.info('\n'.join(sorted(results)))
  128. def remove_cache_items(self, options: Values, args: List[Any]) -> None:
  129. if len(args) > 1:
  130. raise CommandError('Too many arguments')
  131. if not args:
  132. raise CommandError('Please provide a pattern')
  133. files = self._find_wheels(options, args[0])
  134. # Only fetch http files if no specific pattern given
  135. if args[0] == '*':
  136. files += self._find_http_files(options)
  137. if not files:
  138. raise CommandError('No matching packages')
  139. for filename in files:
  140. os.unlink(filename)
  141. logger.verbose("Removed %s", filename)
  142. logger.info("Files removed: %s", len(files))
  143. def purge_cache(self, options: Values, args: List[Any]) -> None:
  144. if args:
  145. raise CommandError('Too many arguments')
  146. return self.remove_cache_items(options, ['*'])
  147. def _cache_dir(self, options: Values, subdir: str) -> str:
  148. return os.path.join(options.cache_dir, subdir)
  149. def _find_http_files(self, options: Values) -> List[str]:
  150. http_dir = self._cache_dir(options, 'http')
  151. return filesystem.find_files(http_dir, '*')
  152. def _find_wheels(self, options: Values, pattern: str) -> List[str]:
  153. wheel_dir = self._cache_dir(options, 'wheels')
  154. # The wheel filename format, as specified in PEP 427, is:
  155. # {distribution}-{version}(-{build})?-{python}-{abi}-{platform}.whl
  156. #
  157. # Additionally, non-alphanumeric values in the distribution are
  158. # normalized to underscores (_), meaning hyphens can never occur
  159. # before `-{version}`.
  160. #
  161. # Given that information:
  162. # - If the pattern we're given contains a hyphen (-), the user is
  163. # providing at least the version. Thus, we can just append `*.whl`
  164. # to match the rest of it.
  165. # - If the pattern we're given doesn't contain a hyphen (-), the
  166. # user is only providing the name. Thus, we append `-*.whl` to
  167. # match the hyphen before the version, followed by anything else.
  168. #
  169. # PEP 427: https://www.python.org/dev/peps/pep-0427/
  170. pattern = pattern + ("*.whl" if "-" in pattern else "-*.whl")
  171. return filesystem.find_files(wheel_dir, pattern)