Skip to content

Commit cdb5eea

Browse files
authored
Merge pull request pypa#1675 from pganssle/optional_setup_py
Add support for setup.cfg-only projects
2 parents c1243e9 + 6bda3d5 commit cdb5eea

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

changelog.d/1675.change.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for ``setup.cfg``-only projects when using the ``setuptools.build_meta`` backend. Projects that have enabled PEP 517 no longer need to have a ``setup.py`` and can use the purely declarative ``setup.cfg`` configuration file instead.

setuptools/build_meta.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Again, this is not a formal definition! Just a "taste" of the module.
2727
"""
2828

29+
import io
2930
import os
3031
import sys
3132
import tokenize
@@ -95,6 +96,14 @@ def _file_with_extension(directory, extension):
9596
return file
9697

9798

99+
def _open_setup_script(setup_script):
100+
if not os.path.exists(setup_script):
101+
# Supply a default setup.py
102+
return io.StringIO(u"from setuptools import setup; setup()")
103+
104+
return getattr(tokenize, 'open', open)(setup_script)
105+
106+
98107
class _BuildMetaBackend(object):
99108

100109
def _fix_config(self, config_settings):
@@ -120,9 +129,10 @@ def run_setup(self, setup_script='setup.py'):
120129
# Correctness comes first, then optimization later
121130
__file__ = setup_script
122131
__name__ = '__main__'
123-
f = getattr(tokenize, 'open', open)(__file__)
124-
code = f.read().replace('\\r\\n', '\\n')
125-
f.close()
132+
133+
with _open_setup_script(__file__) as f:
134+
code = f.read().replace(r'\r\n', r'\n')
135+
126136
exec(compile(code, __file__, 'exec'), locals())
127137

128138
def get_requires_for_build_wheel(self, config_settings=None):

setuptools/tests/test_build_meta.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ def run():
110110
print('hello')
111111
"""),
112112
},
113+
{
114+
'setup.cfg': DALS("""
115+
[metadata]
116+
name = foo
117+
version='0.0.0'
118+
119+
[options]
120+
py_modules=hello
121+
setup_requires=six
122+
"""),
123+
'hello.py': DALS("""
124+
def run():
125+
print('hello')
126+
""")
127+
},
113128
]
114129

115130

@@ -183,9 +198,13 @@ def test_build_sdist_version_change(self, build_backend):
183198
# if the setup.py changes subsequent call of the build meta
184199
# should still succeed, given the
185200
# sdist_directory the frontend specifies is empty
186-
with open(os.path.abspath("setup.py"), 'rt') as file_handler:
201+
setup_loc = os.path.abspath("setup.py")
202+
if not os.path.exists(setup_loc):
203+
setup_loc = os.path.abspath("setup.cfg")
204+
205+
with open(setup_loc, 'rt') as file_handler:
187206
content = file_handler.read()
188-
with open(os.path.abspath("setup.py"), 'wt') as file_handler:
207+
with open(setup_loc, 'wt') as file_handler:
189208
file_handler.write(
190209
content.replace("version='0.0.0'", "version='0.0.1'"))
191210

0 commit comments

Comments
 (0)