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.

138 lines
4.4 KiB

4 years ago
  1. """
  2. Tools for converting old- to new-style metadata.
  3. """
  4. import os.path
  5. import re
  6. import textwrap
  7. import pkg_resources
  8. from .pkginfo import read_pkg_info
  9. # Support markers syntax with the extra at the end only
  10. EXTRA_RE = re.compile(
  11. r"""^(?P<package>.*?)(;\s*(?P<condition>.*?)(extra == '(?P<extra>.*?)')?)$""")
  12. def requires_to_requires_dist(requirement):
  13. """Return the version specifier for a requirement in PEP 345/566 fashion."""
  14. if getattr(requirement, 'url', None):
  15. return " @ " + requirement.url
  16. requires_dist = []
  17. for op, ver in requirement.specs:
  18. requires_dist.append(op + ver)
  19. if not requires_dist:
  20. return ''
  21. return " (%s)" % ','.join(sorted(requires_dist))
  22. def convert_requirements(requirements):
  23. """Yield Requires-Dist: strings for parsed requirements strings."""
  24. for req in requirements:
  25. parsed_requirement = pkg_resources.Requirement.parse(req)
  26. spec = requires_to_requires_dist(parsed_requirement)
  27. extras = ",".join(sorted(parsed_requirement.extras))
  28. if extras:
  29. extras = "[%s]" % extras
  30. yield (parsed_requirement.project_name + extras + spec)
  31. def generate_requirements(extras_require):
  32. """
  33. Convert requirements from a setup()-style dictionary to ('Requires-Dist', 'requirement')
  34. and ('Provides-Extra', 'extra') tuples.
  35. extras_require is a dictionary of {extra: [requirements]} as passed to setup(),
  36. using the empty extra {'': [requirements]} to hold install_requires.
  37. """
  38. for extra, depends in extras_require.items():
  39. condition = ''
  40. extra = extra or ''
  41. if ':' in extra: # setuptools extra:condition syntax
  42. extra, condition = extra.split(':', 1)
  43. extra = pkg_resources.safe_extra(extra)
  44. if extra:
  45. yield 'Provides-Extra', extra
  46. if condition:
  47. condition = "(" + condition + ") and "
  48. condition += "extra == '%s'" % extra
  49. if condition:
  50. condition = ' ; ' + condition
  51. for new_req in convert_requirements(depends):
  52. yield 'Requires-Dist', new_req + condition
  53. def pkginfo_to_metadata(egg_info_path, pkginfo_path):
  54. """
  55. Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
  56. """
  57. pkg_info = read_pkg_info(pkginfo_path)
  58. pkg_info.replace_header('Metadata-Version', '2.1')
  59. # Those will be regenerated from `requires.txt`.
  60. del pkg_info['Provides-Extra']
  61. del pkg_info['Requires-Dist']
  62. requires_path = os.path.join(egg_info_path, 'requires.txt')
  63. if os.path.exists(requires_path):
  64. with open(requires_path) as requires_file:
  65. requires = requires_file.read()
  66. parsed_requirements = sorted(pkg_resources.split_sections(requires),
  67. key=lambda x: x[0] or '')
  68. for extra, reqs in parsed_requirements:
  69. for key, value in generate_requirements({extra: reqs}):
  70. if (key, value) not in pkg_info.items():
  71. pkg_info[key] = value
  72. description = pkg_info['Description']
  73. if description:
  74. pkg_info.set_payload(dedent_description(pkg_info))
  75. del pkg_info['Description']
  76. return pkg_info
  77. def pkginfo_unicode(pkg_info, field):
  78. """Hack to coax Unicode out of an email Message() - Python 3.3+"""
  79. text = pkg_info[field]
  80. field = field.lower()
  81. if not isinstance(text, str):
  82. for item in pkg_info.raw_items():
  83. if item[0].lower() == field:
  84. text = item[1].encode('ascii', 'surrogateescape') \
  85. .decode('utf-8')
  86. break
  87. return text
  88. def dedent_description(pkg_info):
  89. """
  90. Dedent and convert pkg_info['Description'] to Unicode.
  91. """
  92. description = pkg_info['Description']
  93. # Python 3 Unicode handling, sorta.
  94. surrogates = False
  95. if not isinstance(description, str):
  96. surrogates = True
  97. description = pkginfo_unicode(pkg_info, 'Description')
  98. description_lines = description.splitlines()
  99. description_dedent = '\n'.join(
  100. # if the first line of long_description is blank,
  101. # the first line here will be indented.
  102. (description_lines[0].lstrip(),
  103. textwrap.dedent('\n'.join(description_lines[1:])),
  104. '\n'))
  105. if surrogates:
  106. description_dedent = description_dedent \
  107. .encode("utf8") \
  108. .decode("ascii", "surrogateescape")
  109. return description_dedent