metadata.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. Tools for converting old- to new-style metadata.
  3. """
  4. from __future__ import annotations
  5. import functools
  6. import itertools
  7. import os.path
  8. import re
  9. import textwrap
  10. from email.message import Message
  11. from email.parser import Parser
  12. from typing import Iterator
  13. from .vendored.packaging.requirements import Requirement
  14. def _nonblank(str):
  15. return str and not str.startswith("#")
  16. @functools.singledispatch
  17. def yield_lines(iterable):
  18. r"""
  19. Yield valid lines of a string or iterable.
  20. >>> list(yield_lines(''))
  21. []
  22. >>> list(yield_lines(['foo', 'bar']))
  23. ['foo', 'bar']
  24. >>> list(yield_lines('foo\nbar'))
  25. ['foo', 'bar']
  26. >>> list(yield_lines('\nfoo\n#bar\nbaz #comment'))
  27. ['foo', 'baz #comment']
  28. >>> list(yield_lines(['foo\nbar', 'baz', 'bing\n\n\n']))
  29. ['foo', 'bar', 'baz', 'bing']
  30. """
  31. return itertools.chain.from_iterable(map(yield_lines, iterable))
  32. @yield_lines.register(str)
  33. def _(text):
  34. return filter(_nonblank, map(str.strip, text.splitlines()))
  35. def split_sections(s):
  36. """Split a string or iterable thereof into (section, content) pairs
  37. Each ``section`` is a stripped version of the section header ("[section]")
  38. and each ``content`` is a list of stripped lines excluding blank lines and
  39. comment-only lines. If there are any such lines before the first section
  40. header, they're returned in a first ``section`` of ``None``.
  41. """
  42. section = None
  43. content = []
  44. for line in yield_lines(s):
  45. if line.startswith("["):
  46. if line.endswith("]"):
  47. if section or content:
  48. yield section, content
  49. section = line[1:-1].strip()
  50. content = []
  51. else:
  52. raise ValueError("Invalid section heading", line)
  53. else:
  54. content.append(line)
  55. # wrap up last segment
  56. yield section, content
  57. def safe_extra(extra):
  58. """Convert an arbitrary string to a standard 'extra' name
  59. Any runs of non-alphanumeric characters are replaced with a single '_',
  60. and the result is always lowercased.
  61. """
  62. return re.sub("[^A-Za-z0-9.-]+", "_", extra).lower()
  63. def safe_name(name):
  64. """Convert an arbitrary string to a standard distribution name
  65. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  66. """
  67. return re.sub("[^A-Za-z0-9.]+", "-", name)
  68. def requires_to_requires_dist(requirement: Requirement) -> str:
  69. """Return the version specifier for a requirement in PEP 345/566 fashion."""
  70. if getattr(requirement, "url", None):
  71. return " @ " + requirement.url
  72. requires_dist = []
  73. for spec in requirement.specifier:
  74. requires_dist.append(spec.operator + spec.version)
  75. if requires_dist:
  76. return " " + ",".join(sorted(requires_dist))
  77. else:
  78. return ""
  79. def convert_requirements(requirements: list[str]) -> Iterator[str]:
  80. """Yield Requires-Dist: strings for parsed requirements strings."""
  81. for req in requirements:
  82. parsed_requirement = Requirement(req)
  83. spec = requires_to_requires_dist(parsed_requirement)
  84. extras = ",".join(sorted(safe_extra(e) for e in parsed_requirement.extras))
  85. if extras:
  86. extras = f"[{extras}]"
  87. yield safe_name(parsed_requirement.name) + extras + spec
  88. def generate_requirements(
  89. extras_require: dict[str, list[str]]
  90. ) -> Iterator[tuple[str, str]]:
  91. """
  92. Convert requirements from a setup()-style dictionary to
  93. ('Requires-Dist', 'requirement') and ('Provides-Extra', 'extra') tuples.
  94. extras_require is a dictionary of {extra: [requirements]} as passed to setup(),
  95. using the empty extra {'': [requirements]} to hold install_requires.
  96. """
  97. for extra, depends in extras_require.items():
  98. condition = ""
  99. extra = extra or ""
  100. if ":" in extra: # setuptools extra:condition syntax
  101. extra, condition = extra.split(":", 1)
  102. extra = safe_extra(extra)
  103. if extra:
  104. yield "Provides-Extra", extra
  105. if condition:
  106. condition = "(" + condition + ") and "
  107. condition += "extra == '%s'" % extra
  108. if condition:
  109. condition = " ; " + condition
  110. for new_req in convert_requirements(depends):
  111. yield "Requires-Dist", new_req + condition
  112. def pkginfo_to_metadata(egg_info_path: str, pkginfo_path: str) -> Message:
  113. """
  114. Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
  115. """
  116. with open(pkginfo_path, encoding="utf-8") as headers:
  117. pkg_info = Parser().parse(headers)
  118. pkg_info.replace_header("Metadata-Version", "2.1")
  119. # Those will be regenerated from `requires.txt`.
  120. del pkg_info["Provides-Extra"]
  121. del pkg_info["Requires-Dist"]
  122. requires_path = os.path.join(egg_info_path, "requires.txt")
  123. if os.path.exists(requires_path):
  124. with open(requires_path, encoding="utf-8") as requires_file:
  125. requires = requires_file.read()
  126. parsed_requirements = sorted(split_sections(requires), key=lambda x: x[0] or "")
  127. for extra, reqs in parsed_requirements:
  128. for key, value in generate_requirements({extra: reqs}):
  129. if (key, value) not in pkg_info.items():
  130. pkg_info[key] = value
  131. description = pkg_info["Description"]
  132. if description:
  133. description_lines = pkg_info["Description"].splitlines()
  134. dedented_description = "\n".join(
  135. # if the first line of long_description is blank,
  136. # the first line here will be indented.
  137. (
  138. description_lines[0].lstrip(),
  139. textwrap.dedent("\n".join(description_lines[1:])),
  140. "\n",
  141. )
  142. )
  143. pkg_info.set_payload(dedented_description)
  144. del pkg_info["Description"]
  145. return pkg_info