metadata.py 6.0 KB

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