Skip to content

Commit

Permalink
Use _requirestxt logic for writing 'requires.txt'
Browse files Browse the repository at this point in the history
This change extracts the logic for writing 'requires.txt' from
the ``egg_info`` module into ``_requirestxt``.

It also adds the missing pre-processing for ``install_requires`` and
``extras_require`` previously done in ``dist`` but now implemented in
``_requires.txt``.
  • Loading branch information
abravalheri committed May 3, 2023
1 parent 9cecaca commit f5ca18b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
30 changes: 30 additions & 0 deletions setuptools/command/_requirestxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
See https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#requires-txt
"""
import io
from collections import defaultdict
from itertools import filterfalse
from typing import Dict, List, Tuple, Mapping, TypeVar

from .. import _reqs
from ..extern.jaraco.text import yield_lines
from ..extern.packaging.requirements import Requirement


Expand Down Expand Up @@ -94,3 +96,31 @@ def _clean_req(req):

def _no_marker(req):
return not req.marker


def _write_requirements(stream, reqs):
lines = yield_lines(reqs or ())

def append_cr(line):
return line + '\n'
lines = map(append_cr, lines)
stream.writelines(lines)


def write_requirements(cmd, basename, filename):
dist = cmd.distribution
data = io.StringIO()
install_requires, extras_require = _prepare(
dist.install_requires or (), dist.extras_require or {}
)
_write_requirements(data, install_requires)
for extra in sorted(extras_require):
data.write('\n[{extra}]\n'.format(**vars()))
_write_requirements(data, extras_require[extra])
cmd.write_or_delete_file("requirements", filename, data.getvalue())


def write_setup_requirements(cmd, basename, filename):
data = io.StringIO()
_write_requirements(data, cmd.distribution.setup_requires)
cmd.write_or_delete_file("setup-requirements", filename, data.getvalue())
29 changes: 4 additions & 25 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from .._importlib import metadata
from .. import _entry_points, _normalization
from . import _requirestxt

from setuptools import Command
from setuptools.command.sdist import sdist
Expand All @@ -28,7 +29,6 @@
from setuptools.glob import glob

from setuptools.extern import packaging
from setuptools.extern.jaraco.text import yield_lines
from ..warnings import SetuptoolsDeprecationWarning


Expand Down Expand Up @@ -719,30 +719,9 @@ def warn_depends_obsolete(cmd, basename, filename):
)


def _write_requirements(stream, reqs):
lines = yield_lines(reqs or ())

def append_cr(line):
return line + '\n'
lines = map(append_cr, lines)
stream.writelines(lines)


def write_requirements(cmd, basename, filename):
dist = cmd.distribution
data = io.StringIO()
_write_requirements(data, dist.install_requires)
extras_require = dist.extras_require or {}
for extra in sorted(extras_require):
data.write('\n[{extra}]\n'.format(**vars()))
_write_requirements(data, extras_require[extra])
cmd.write_or_delete_file("requirements", filename, data.getvalue())


def write_setup_requirements(cmd, basename, filename):
data = io.StringIO()
_write_requirements(data, cmd.distribution.setup_requires)
cmd.write_or_delete_file("setup-requirements", filename, data.getvalue())
# Export API used in entry_points
write_requirements = _requirestxt.write_requirements
write_setup_requirements = _requirestxt.write_setup_requirements


def write_toplevel_names(cmd, basename, filename):
Expand Down

0 comments on commit f5ca18b

Please sign in to comment.