Skip to content

Commit 85af003

Browse files
tripzeromatthewrsj
authored andcommitted
Try to autodetect python version by classifiers
If a python package is python3-only and has a setup.py, "distutils23" will be used as build pattern even if the project is explicitely specified as python 2 or python 3 only. This change tries to find if either of these are the case by looking at the classifiers in the setup.py file for the following string: "Programming Language :: Python :: 3 :: Only" "Programming Language :: Python :: 2 :: Only" The default is still "distutils23" unless either of these strings are found. If they are "distutils2" or "distutils3" will be used corresponding to the version in the classifier. Signed-off-by: Kevron Rees <[email protected]>
1 parent 1fd3342 commit 85af003

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

autospec/buildreq.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ def grab_pip_requirements(pkgname):
391391
add_requires(w2)
392392

393393

394+
def get_python_build_version_from_classifier(filename):
395+
"""
396+
Detect if setup should use distutils23 or distutils3 only.
397+
398+
Uses "Programming Language :: Python :: [2,3] :: Only" classifiers in the
399+
setup.py file. Defaults to distutils23 if no such classifiers are found.
400+
"""
401+
402+
with open(filename) as setup_file:
403+
data = setup_file.read()
404+
405+
if "Programming Language :: Python :: 3 :: Only" in data:
406+
return "distutils3"
407+
408+
elif "Programming Language :: Python :: 2 :: Only" in data:
409+
return "distutils2"
410+
411+
return "distutils23"
412+
413+
394414
def add_setup_py_requires(filename):
395415
"""
396416
Detect build requirements listed in setup.py in the install_requires and
@@ -563,7 +583,8 @@ def scan_for_configure(dirn):
563583
add_buildreq("pbr")
564584
add_buildreq("pip")
565585
add_setup_py_requires(dirpath + '/setup.py')
566-
buildpattern.set_build_pattern("distutils23", default_score)
586+
python_pattern = get_python_build_version_from_classifier(dirpath + '/setup.py')
587+
buildpattern.set_build_pattern(python_pattern, default_score)
567588

568589
if "Makefile.PL" in files or "Build.PL" in files:
569590
buildpattern.set_build_pattern("cpan", default_score)

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def readme():
2121
'Topic :: Software Development :: Build Tools',
2222
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
2323
# Python versions supported.
24+
'Programming Language :: Python :: 3 :: Only',
2425
'Programming Language :: Python :: 3',
2526
'Programming Language :: Python :: 3.2',
2627
'Programming Language :: Python :: 3.3',

tests/test_buildreq.py

+48
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,54 @@ def test_add_setup_py_requires_single_variable(self):
367367
self.assertEqual(buildreq.buildreqs, set())
368368
self.assertEqual(buildreq.requires, set())
369369

370+
def test_setup_py3_version_classifier(self):
371+
"""
372+
test detection of python version from setup.py classifier
373+
"""
374+
375+
open_name = 'buildreq.open'
376+
content = """classifiers = [
377+
'Programming Language :: Python :: 3 :: Only',
378+
]"""
379+
380+
m_open = mock_open(read_data=content)
381+
with patch(open_name, m_open, create=True):
382+
build_pattern = buildreq.get_python_build_version_from_classifier("filename")
383+
384+
self.assertEqual(build_pattern, "distutils3")
385+
386+
def test_setup_py2_version_classifier(self):
387+
"""
388+
test detection of python version from setup.py classifier
389+
"""
390+
391+
open_name = 'buildreq.open'
392+
content = """classifiers = [
393+
'Programming Language :: Python :: 2 :: Only',
394+
]"""
395+
396+
m_open = mock_open(read_data=content)
397+
with patch(open_name, m_open, create=True):
398+
build_pattern = buildreq.get_python_build_version_from_classifier("filename")
399+
400+
self.assertEqual(build_pattern, "distutils2")
401+
402+
def test_setup_py23_version_classifier(self):
403+
"""
404+
test detection of python version from setup.py classifier
405+
"""
406+
407+
open_name = 'buildreq.open'
408+
content = """classifiers = [
409+
'Programming Language :: Python :: 3',
410+
]"""
411+
412+
m_open = mock_open(read_data=content)
413+
with patch(open_name, m_open, create=True):
414+
build_pattern = buildreq.get_python_build_version_from_classifier("filename")
415+
416+
self.assertEqual(build_pattern, "distutils23")
417+
370418
def test_scan_for_configure(self):
371419
"""
372420
Test scan_for_configure with a mocked package structure. There is so

0 commit comments

Comments
 (0)