egg_info.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. """setuptools.command.egg_info
  2. Create a distribution's .egg-info directory and contents"""
  3. import functools
  4. import os
  5. import re
  6. import sys
  7. import time
  8. from collections.abc import Callable
  9. import packaging
  10. import packaging.requirements
  11. import packaging.version
  12. import setuptools.unicode_utils as unicode_utils
  13. from setuptools import Command
  14. from setuptools.command import bdist_egg
  15. from setuptools.command.sdist import sdist, walk_revctrl
  16. from setuptools.command.setopt import edit_config
  17. from setuptools.glob import glob
  18. from .. import _entry_points, _normalization
  19. from .._importlib import metadata
  20. from ..warnings import SetuptoolsDeprecationWarning
  21. from . import _requirestxt
  22. import distutils.errors
  23. import distutils.filelist
  24. from distutils import log
  25. from distutils.errors import DistutilsInternalError
  26. from distutils.filelist import FileList as _FileList
  27. from distutils.util import convert_path
  28. PY_MAJOR = f'{sys.version_info.major}.{sys.version_info.minor}'
  29. def translate_pattern(glob): # noqa: C901 # is too complex (14) # FIXME
  30. """
  31. Translate a file path glob like '*.txt' in to a regular expression.
  32. This differs from fnmatch.translate which allows wildcards to match
  33. directory separators. It also knows about '**/' which matches any number of
  34. directories.
  35. """
  36. pat = ''
  37. # This will split on '/' within [character classes]. This is deliberate.
  38. chunks = glob.split(os.path.sep)
  39. sep = re.escape(os.sep)
  40. valid_char = f'[^{sep}]'
  41. for c, chunk in enumerate(chunks):
  42. last_chunk = c == len(chunks) - 1
  43. # Chunks that are a literal ** are globstars. They match anything.
  44. if chunk == '**':
  45. if last_chunk:
  46. # Match anything if this is the last component
  47. pat += '.*'
  48. else:
  49. # Match '(name/)*'
  50. pat += f'(?:{valid_char}+{sep})*'
  51. continue # Break here as the whole path component has been handled
  52. # Find any special characters in the remainder
  53. i = 0
  54. chunk_len = len(chunk)
  55. while i < chunk_len:
  56. char = chunk[i]
  57. if char == '*':
  58. # Match any number of name characters
  59. pat += valid_char + '*'
  60. elif char == '?':
  61. # Match a name character
  62. pat += valid_char
  63. elif char == '[':
  64. # Character class
  65. inner_i = i + 1
  66. # Skip initial !/] chars
  67. if inner_i < chunk_len and chunk[inner_i] == '!':
  68. inner_i = inner_i + 1
  69. if inner_i < chunk_len and chunk[inner_i] == ']':
  70. inner_i = inner_i + 1
  71. # Loop till the closing ] is found
  72. while inner_i < chunk_len and chunk[inner_i] != ']':
  73. inner_i = inner_i + 1
  74. if inner_i >= chunk_len:
  75. # Got to the end of the string without finding a closing ]
  76. # Do not treat this as a matching group, but as a literal [
  77. pat += re.escape(char)
  78. else:
  79. # Grab the insides of the [brackets]
  80. inner = chunk[i + 1 : inner_i]
  81. char_class = ''
  82. # Class negation
  83. if inner[0] == '!':
  84. char_class = '^'
  85. inner = inner[1:]
  86. char_class += re.escape(inner)
  87. pat += f'[{char_class}]'
  88. # Skip to the end ]
  89. i = inner_i
  90. else:
  91. pat += re.escape(char)
  92. i += 1
  93. # Join each chunk with the dir separator
  94. if not last_chunk:
  95. pat += sep
  96. pat += r'\Z'
  97. return re.compile(pat, flags=re.MULTILINE | re.DOTALL)
  98. class InfoCommon:
  99. tag_build = None
  100. tag_date = None
  101. @property
  102. def name(self):
  103. return _normalization.safe_name(self.distribution.get_name())
  104. def tagged_version(self):
  105. tagged = self._maybe_tag(self.distribution.get_version())
  106. return _normalization.safe_version(tagged)
  107. def _maybe_tag(self, version):
  108. """
  109. egg_info may be called more than once for a distribution,
  110. in which case the version string already contains all tags.
  111. """
  112. return (
  113. version
  114. if self.vtags and self._already_tagged(version)
  115. else version + self.vtags
  116. )
  117. def _already_tagged(self, version: str) -> bool:
  118. # Depending on their format, tags may change with version normalization.
  119. # So in addition the regular tags, we have to search for the normalized ones.
  120. return version.endswith(self.vtags) or version.endswith(self._safe_tags())
  121. def _safe_tags(self) -> str:
  122. # To implement this we can rely on `safe_version` pretending to be version 0
  123. # followed by tags. Then we simply discard the starting 0 (fake version number)
  124. try:
  125. return _normalization.safe_version(f"0{self.vtags}")[1:]
  126. except packaging.version.InvalidVersion:
  127. return _normalization.safe_name(self.vtags.replace(' ', '.'))
  128. def tags(self) -> str:
  129. version = ''
  130. if self.tag_build:
  131. version += self.tag_build
  132. if self.tag_date:
  133. version += time.strftime("%Y%m%d")
  134. return version
  135. vtags = property(tags)
  136. class egg_info(InfoCommon, Command):
  137. description = "create a distribution's .egg-info directory"
  138. user_options = [
  139. (
  140. 'egg-base=',
  141. 'e',
  142. "directory containing .egg-info directories"
  143. " [default: top of the source tree]",
  144. ),
  145. ('tag-date', 'd', "Add date stamp (e.g. 20050528) to version number"),
  146. ('tag-build=', 'b', "Specify explicit tag to add to version number"),
  147. ('no-date', 'D', "Don't include date stamp [default]"),
  148. ]
  149. boolean_options = ['tag-date']
  150. negative_opt = {
  151. 'no-date': 'tag-date',
  152. }
  153. def initialize_options(self):
  154. self.egg_base = None
  155. self.egg_name = None
  156. self.egg_info = None
  157. self.egg_version = None
  158. self.ignore_egg_info_in_manifest = False
  159. ####################################
  160. # allow the 'tag_svn_revision' to be detected and
  161. # set, supporting sdists built on older Setuptools.
  162. @property
  163. def tag_svn_revision(self) -> None:
  164. pass
  165. @tag_svn_revision.setter
  166. def tag_svn_revision(self, value):
  167. pass
  168. ####################################
  169. def save_version_info(self, filename) -> None:
  170. """
  171. Materialize the value of date into the
  172. build tag. Install build keys in a deterministic order
  173. to avoid arbitrary reordering on subsequent builds.
  174. """
  175. # follow the order these keys would have been added
  176. # when PYTHONHASHSEED=0
  177. egg_info = dict(tag_build=self.tags(), tag_date=0)
  178. edit_config(filename, dict(egg_info=egg_info))
  179. def finalize_options(self) -> None:
  180. # Note: we need to capture the current value returned
  181. # by `self.tagged_version()`, so we can later update
  182. # `self.distribution.metadata.version` without
  183. # repercussions.
  184. self.egg_name = self.name
  185. self.egg_version = self.tagged_version()
  186. parsed_version = packaging.version.Version(self.egg_version)
  187. try:
  188. is_version = isinstance(parsed_version, packaging.version.Version)
  189. spec = "%s==%s" if is_version else "%s===%s"
  190. packaging.requirements.Requirement(spec % (self.egg_name, self.egg_version))
  191. except ValueError as e:
  192. raise distutils.errors.DistutilsOptionError(
  193. f"Invalid distribution name or version syntax: {self.egg_name}-{self.egg_version}"
  194. ) from e
  195. if self.egg_base is None:
  196. dirs = self.distribution.package_dir
  197. self.egg_base = (dirs or {}).get('', os.curdir)
  198. self.ensure_dirname('egg_base')
  199. self.egg_info = _normalization.filename_component(self.egg_name) + '.egg-info'
  200. if self.egg_base != os.curdir:
  201. self.egg_info = os.path.join(self.egg_base, self.egg_info)
  202. # Set package version for the benefit of dumber commands
  203. # (e.g. sdist, bdist_wininst, etc.)
  204. #
  205. self.distribution.metadata.version = self.egg_version
  206. def _get_egg_basename(self, py_version=PY_MAJOR, platform=None):
  207. """Compute filename of the output egg. Private API."""
  208. return _egg_basename(self.egg_name, self.egg_version, py_version, platform)
  209. def write_or_delete_file(self, what, filename, data, force: bool = False) -> None:
  210. """Write `data` to `filename` or delete if empty
  211. If `data` is non-empty, this routine is the same as ``write_file()``.
  212. If `data` is empty but not ``None``, this is the same as calling
  213. ``delete_file(filename)`. If `data` is ``None``, then this is a no-op
  214. unless `filename` exists, in which case a warning is issued about the
  215. orphaned file (if `force` is false), or deleted (if `force` is true).
  216. """
  217. if data:
  218. self.write_file(what, filename, data)
  219. elif os.path.exists(filename):
  220. if data is None and not force:
  221. log.warn("%s not set in setup(), but %s exists", what, filename)
  222. return
  223. else:
  224. self.delete_file(filename)
  225. def write_file(self, what, filename, data) -> None:
  226. """Write `data` to `filename` (if not a dry run) after announcing it
  227. `what` is used in a log message to identify what is being written
  228. to the file.
  229. """
  230. log.info("writing %s to %s", what, filename)
  231. data = data.encode("utf-8")
  232. if not self.dry_run:
  233. f = open(filename, 'wb')
  234. f.write(data)
  235. f.close()
  236. def delete_file(self, filename) -> None:
  237. """Delete `filename` (if not a dry run) after announcing it"""
  238. log.info("deleting %s", filename)
  239. if not self.dry_run:
  240. os.unlink(filename)
  241. def run(self) -> None:
  242. # Pre-load to avoid iterating over entry-points while an empty .egg-info
  243. # exists in sys.path. See pypa/pyproject-hooks#206
  244. writers = list(metadata.entry_points(group='egg_info.writers'))
  245. self.mkpath(self.egg_info)
  246. try:
  247. os.utime(self.egg_info, None)
  248. except OSError as e:
  249. msg = f"Cannot update time stamp of directory '{self.egg_info}'"
  250. raise distutils.errors.DistutilsFileError(msg) from e
  251. for ep in writers:
  252. writer = ep.load()
  253. writer(self, ep.name, os.path.join(self.egg_info, ep.name))
  254. # Get rid of native_libs.txt if it was put there by older bdist_egg
  255. nl = os.path.join(self.egg_info, "native_libs.txt")
  256. if os.path.exists(nl):
  257. self.delete_file(nl)
  258. self.find_sources()
  259. def find_sources(self) -> None:
  260. """Generate SOURCES.txt manifest file"""
  261. manifest_filename = os.path.join(self.egg_info, "SOURCES.txt")
  262. mm = manifest_maker(self.distribution)
  263. mm.ignore_egg_info_dir = self.ignore_egg_info_in_manifest
  264. mm.manifest = manifest_filename
  265. mm.run()
  266. self.filelist = mm.filelist
  267. class FileList(_FileList):
  268. # Implementations of the various MANIFEST.in commands
  269. def __init__(
  270. self, warn=None, debug_print=None, ignore_egg_info_dir: bool = False
  271. ) -> None:
  272. super().__init__(warn, debug_print)
  273. self.ignore_egg_info_dir = ignore_egg_info_dir
  274. def process_template_line(self, line) -> None:
  275. # Parse the line: split it up, make sure the right number of words
  276. # is there, and return the relevant words. 'action' is always
  277. # defined: it's the first word of the line. Which of the other
  278. # three are defined depends on the action; it'll be either
  279. # patterns, (dir and patterns), or (dir_pattern).
  280. (action, patterns, dir, dir_pattern) = self._parse_template_line(line)
  281. action_map: dict[str, Callable] = {
  282. 'include': self.include,
  283. 'exclude': self.exclude,
  284. 'global-include': self.global_include,
  285. 'global-exclude': self.global_exclude,
  286. 'recursive-include': functools.partial(
  287. self.recursive_include,
  288. dir,
  289. ),
  290. 'recursive-exclude': functools.partial(
  291. self.recursive_exclude,
  292. dir,
  293. ),
  294. 'graft': self.graft,
  295. 'prune': self.prune,
  296. }
  297. log_map = {
  298. 'include': "warning: no files found matching '%s'",
  299. 'exclude': ("warning: no previously-included files found matching '%s'"),
  300. 'global-include': (
  301. "warning: no files found matching '%s' anywhere in distribution"
  302. ),
  303. 'global-exclude': (
  304. "warning: no previously-included files matching "
  305. "'%s' found anywhere in distribution"
  306. ),
  307. 'recursive-include': (
  308. "warning: no files found matching '%s' under directory '%s'"
  309. ),
  310. 'recursive-exclude': (
  311. "warning: no previously-included files matching "
  312. "'%s' found under directory '%s'"
  313. ),
  314. 'graft': "warning: no directories found matching '%s'",
  315. 'prune': "no previously-included directories found matching '%s'",
  316. }
  317. try:
  318. process_action = action_map[action]
  319. except KeyError:
  320. msg = f"Invalid MANIFEST.in: unknown action {action!r} in {line!r}"
  321. raise DistutilsInternalError(msg) from None
  322. # OK, now we know that the action is valid and we have the
  323. # right number of words on the line for that action -- so we
  324. # can proceed with minimal error-checking.
  325. action_is_recursive = action.startswith('recursive-')
  326. if action in {'graft', 'prune'}:
  327. patterns = [dir_pattern]
  328. extra_log_args = (dir,) if action_is_recursive else ()
  329. log_tmpl = log_map[action]
  330. self.debug_print(
  331. ' '.join(
  332. [action] + ([dir] if action_is_recursive else []) + patterns,
  333. )
  334. )
  335. for pattern in patterns:
  336. if not process_action(pattern):
  337. log.warn(log_tmpl, pattern, *extra_log_args)
  338. def _remove_files(self, predicate):
  339. """
  340. Remove all files from the file list that match the predicate.
  341. Return True if any matching files were removed
  342. """
  343. found = False
  344. for i in range(len(self.files) - 1, -1, -1):
  345. if predicate(self.files[i]):
  346. self.debug_print(" removing " + self.files[i])
  347. del self.files[i]
  348. found = True
  349. return found
  350. def include(self, pattern):
  351. """Include files that match 'pattern'."""
  352. found = [f for f in glob(pattern) if not os.path.isdir(f)]
  353. self.extend(found)
  354. return bool(found)
  355. def exclude(self, pattern):
  356. """Exclude files that match 'pattern'."""
  357. match = translate_pattern(pattern)
  358. return self._remove_files(match.match)
  359. def recursive_include(self, dir, pattern):
  360. """
  361. Include all files anywhere in 'dir/' that match the pattern.
  362. """
  363. full_pattern = os.path.join(dir, '**', pattern)
  364. found = [f for f in glob(full_pattern, recursive=True) if not os.path.isdir(f)]
  365. self.extend(found)
  366. return bool(found)
  367. def recursive_exclude(self, dir, pattern):
  368. """
  369. Exclude any file anywhere in 'dir/' that match the pattern.
  370. """
  371. match = translate_pattern(os.path.join(dir, '**', pattern))
  372. return self._remove_files(match.match)
  373. def graft(self, dir):
  374. """Include all files from 'dir/'."""
  375. found = [
  376. item
  377. for match_dir in glob(dir)
  378. for item in distutils.filelist.findall(match_dir)
  379. ]
  380. self.extend(found)
  381. return bool(found)
  382. def prune(self, dir):
  383. """Filter out files from 'dir/'."""
  384. match = translate_pattern(os.path.join(dir, '**'))
  385. return self._remove_files(match.match)
  386. def global_include(self, pattern):
  387. """
  388. Include all files anywhere in the current directory that match the
  389. pattern. This is very inefficient on large file trees.
  390. """
  391. if self.allfiles is None:
  392. self.findall()
  393. match = translate_pattern(os.path.join('**', pattern))
  394. found = [f for f in self.allfiles if match.match(f)]
  395. self.extend(found)
  396. return bool(found)
  397. def global_exclude(self, pattern):
  398. """
  399. Exclude all files anywhere that match the pattern.
  400. """
  401. match = translate_pattern(os.path.join('**', pattern))
  402. return self._remove_files(match.match)
  403. def append(self, item) -> None:
  404. if item.endswith('\r'): # Fix older sdists built on Windows
  405. item = item[:-1]
  406. path = convert_path(item)
  407. if self._safe_path(path):
  408. self.files.append(path)
  409. def extend(self, paths) -> None:
  410. self.files.extend(filter(self._safe_path, paths))
  411. def _repair(self):
  412. """
  413. Replace self.files with only safe paths
  414. Because some owners of FileList manipulate the underlying
  415. ``files`` attribute directly, this method must be called to
  416. repair those paths.
  417. """
  418. self.files = list(filter(self._safe_path, self.files))
  419. def _safe_path(self, path):
  420. enc_warn = "'%s' not %s encodable -- skipping"
  421. # To avoid accidental trans-codings errors, first to unicode
  422. u_path = unicode_utils.filesys_decode(path)
  423. if u_path is None:
  424. log.warn(f"'{path}' in unexpected encoding -- skipping")
  425. return False
  426. # Must ensure utf-8 encodability
  427. utf8_path = unicode_utils.try_encode(u_path, "utf-8")
  428. if utf8_path is None:
  429. log.warn(enc_warn, path, 'utf-8')
  430. return False
  431. try:
  432. # ignore egg-info paths
  433. is_egg_info = ".egg-info" in u_path or b".egg-info" in utf8_path
  434. if self.ignore_egg_info_dir and is_egg_info:
  435. return False
  436. # accept is either way checks out
  437. if os.path.exists(u_path) or os.path.exists(utf8_path):
  438. return True
  439. # this will catch any encode errors decoding u_path
  440. except UnicodeEncodeError:
  441. log.warn(enc_warn, path, sys.getfilesystemencoding())
  442. class manifest_maker(sdist):
  443. template = "MANIFEST.in"
  444. def initialize_options(self) -> None:
  445. self.use_defaults = True
  446. self.prune = True
  447. self.manifest_only = True
  448. self.force_manifest = True
  449. self.ignore_egg_info_dir = False
  450. def finalize_options(self) -> None:
  451. pass
  452. def run(self) -> None:
  453. self.filelist = FileList(ignore_egg_info_dir=self.ignore_egg_info_dir)
  454. if not os.path.exists(self.manifest):
  455. self.write_manifest() # it must exist so it'll get in the list
  456. self.add_defaults()
  457. if os.path.exists(self.template):
  458. self.read_template()
  459. self.add_license_files()
  460. self._add_referenced_files()
  461. self.prune_file_list()
  462. self.filelist.sort()
  463. self.filelist.remove_duplicates()
  464. self.write_manifest()
  465. def _manifest_normalize(self, path):
  466. path = unicode_utils.filesys_decode(path)
  467. return path.replace(os.sep, '/')
  468. def write_manifest(self) -> None:
  469. """
  470. Write the file list in 'self.filelist' to the manifest file
  471. named by 'self.manifest'.
  472. """
  473. self.filelist._repair()
  474. # Now _repairs should encodability, but not unicode
  475. files = [self._manifest_normalize(f) for f in self.filelist.files]
  476. msg = f"writing manifest file '{self.manifest}'"
  477. self.execute(write_file, (self.manifest, files), msg)
  478. def warn(self, msg) -> None:
  479. if not self._should_suppress_warning(msg):
  480. sdist.warn(self, msg)
  481. @staticmethod
  482. def _should_suppress_warning(msg):
  483. """
  484. suppress missing-file warnings from sdist
  485. """
  486. return re.match(r"standard file .*not found", msg)
  487. def add_defaults(self) -> None:
  488. sdist.add_defaults(self)
  489. self.filelist.append(self.template)
  490. self.filelist.append(self.manifest)
  491. rcfiles = list(walk_revctrl())
  492. if rcfiles:
  493. self.filelist.extend(rcfiles)
  494. elif os.path.exists(self.manifest):
  495. self.read_manifest()
  496. if os.path.exists("setup.py"):
  497. # setup.py should be included by default, even if it's not
  498. # the script called to create the sdist
  499. self.filelist.append("setup.py")
  500. ei_cmd = self.get_finalized_command('egg_info')
  501. self.filelist.graft(ei_cmd.egg_info)
  502. def add_license_files(self) -> None:
  503. license_files = self.distribution.metadata.license_files or []
  504. for lf in license_files:
  505. log.info("adding license file '%s'", lf)
  506. self.filelist.extend(license_files)
  507. def _add_referenced_files(self):
  508. """Add files referenced by the config (e.g. `file:` directive) to filelist"""
  509. referenced = getattr(self.distribution, '_referenced_files', [])
  510. # ^-- fallback if dist comes from distutils or is a custom class
  511. for rf in referenced:
  512. log.debug("adding file referenced by config '%s'", rf)
  513. self.filelist.extend(referenced)
  514. def _safe_data_files(self, build_py):
  515. """
  516. The parent class implementation of this method
  517. (``sdist``) will try to include data files, which
  518. might cause recursion problems when
  519. ``include_package_data=True``.
  520. Therefore, avoid triggering any attempt of
  521. analyzing/building the manifest again.
  522. """
  523. if hasattr(build_py, 'get_data_files_without_manifest'):
  524. return build_py.get_data_files_without_manifest()
  525. SetuptoolsDeprecationWarning.emit(
  526. "`build_py` command does not inherit from setuptools' `build_py`.",
  527. """
  528. Custom 'build_py' does not implement 'get_data_files_without_manifest'.
  529. Please extend command classes from setuptools instead of distutils.
  530. """,
  531. see_url="https://peps.python.org/pep-0632/",
  532. # due_date not defined yet, old projects might still do it?
  533. )
  534. return build_py.get_data_files()
  535. def write_file(filename, contents) -> None:
  536. """Create a file with the specified name and write 'contents' (a
  537. sequence of strings without line terminators) to it.
  538. """
  539. contents = "\n".join(contents)
  540. # assuming the contents has been vetted for utf-8 encoding
  541. contents = contents.encode("utf-8")
  542. with open(filename, "wb") as f: # always write POSIX-style manifest
  543. f.write(contents)
  544. def write_pkg_info(cmd, basename, filename) -> None:
  545. log.info("writing %s", filename)
  546. if not cmd.dry_run:
  547. metadata = cmd.distribution.metadata
  548. metadata.version, oldver = cmd.egg_version, metadata.version
  549. metadata.name, oldname = cmd.egg_name, metadata.name
  550. try:
  551. metadata.write_pkg_info(cmd.egg_info)
  552. finally:
  553. metadata.name, metadata.version = oldname, oldver
  554. safe = getattr(cmd.distribution, 'zip_safe', None)
  555. bdist_egg.write_safety_flag(cmd.egg_info, safe)
  556. def warn_depends_obsolete(cmd, basename, filename) -> None:
  557. """
  558. Unused: left to avoid errors when updating (from source) from <= 67.8.
  559. Old installations have a .dist-info directory with the entry-point
  560. ``depends.txt = setuptools.command.egg_info:warn_depends_obsolete``.
  561. This may trigger errors when running the first egg_info in build_meta.
  562. TODO: Remove this function in a version sufficiently > 68.
  563. """
  564. # Export API used in entry_points
  565. write_requirements = _requirestxt.write_requirements
  566. write_setup_requirements = _requirestxt.write_setup_requirements
  567. def write_toplevel_names(cmd, basename, filename) -> None:
  568. pkgs = dict.fromkeys([
  569. k.split('.', 1)[0] for k in cmd.distribution.iter_distribution_names()
  570. ])
  571. cmd.write_file("top-level names", filename, '\n'.join(sorted(pkgs)) + '\n')
  572. def overwrite_arg(cmd, basename, filename) -> None:
  573. write_arg(cmd, basename, filename, True)
  574. def write_arg(cmd, basename, filename, force: bool = False) -> None:
  575. argname = os.path.splitext(basename)[0]
  576. value = getattr(cmd.distribution, argname, None)
  577. if value is not None:
  578. value = '\n'.join(value) + '\n'
  579. cmd.write_or_delete_file(argname, filename, value, force)
  580. def write_entries(cmd, basename, filename) -> None:
  581. eps = _entry_points.load(cmd.distribution.entry_points)
  582. defn = _entry_points.render(eps)
  583. cmd.write_or_delete_file('entry points', filename, defn, True)
  584. def _egg_basename(egg_name, egg_version, py_version=None, platform=None):
  585. """Compute filename of the output egg. Private API."""
  586. name = _normalization.filename_component(egg_name)
  587. version = _normalization.filename_component(egg_version)
  588. egg = f"{name}-{version}-py{py_version or PY_MAJOR}"
  589. if platform:
  590. egg += f"-{platform}"
  591. return egg
  592. class EggInfoDeprecationWarning(SetuptoolsDeprecationWarning):
  593. """Deprecated behavior warning for EggInfo, bypassing suppression."""