diff --git a/autospec/buildreq.py b/autospec/buildreq.py index bef9022..ef8c98b 100644 --- a/autospec/buildreq.py +++ b/autospec/buildreq.py @@ -391,6 +391,26 @@ def grab_pip_requirements(pkgname): add_requires(w2) +def get_python_build_version_from_classifier(filename): + """ + Detect if setup should use distutils23 or distutils3 only. + + Uses "Programming Language :: Python :: [2,3] :: Only" classifiers in the + setup.py file. Defaults to distutils23 if no such classifiers are found. + """ + + with open(filename) as setup_file: + data = setup_file.read() + + if "Programming Language :: Python :: 3 :: Only" in data: + return "distutils3" + + elif "Programming Language :: Python :: 2 :: Only" in data: + return "distutils2" + + return "distutils23" + + def add_setup_py_requires(filename): """ Detect build requirements listed in setup.py in the install_requires and @@ -563,7 +583,8 @@ def scan_for_configure(dirn): add_buildreq("pbr") add_buildreq("pip") add_setup_py_requires(dirpath + '/setup.py') - buildpattern.set_build_pattern("distutils23", default_score) + python_pattern = get_python_build_version_from_classifier(dirpath + '/setup.py') + buildpattern.set_build_pattern(python_pattern, default_score) if "Makefile.PL" in files or "Build.PL" in files: buildpattern.set_build_pattern("cpan", default_score) diff --git a/setup.py b/setup.py index 0b7052c..fe8057f 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ setup(name="autospec", 'Topic :: Software Development :: Build Tools', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', # Python versions supported. + 'Programming Language :: Python :: 3 :: Only', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', diff --git a/tests/test_buildreq.py b/tests/test_buildreq.py index 3d479de..9fe9e53 100644 --- a/tests/test_buildreq.py +++ b/tests/test_buildreq.py @@ -367,6 +367,54 @@ class TestBuildreq(unittest.TestCase): self.assertEqual(buildreq.buildreqs, set()) self.assertEqual(buildreq.requires, set()) + def test_setup_py3_version_classifier(self): + """ + test detection of python version from setup.py classifier + """ + + open_name = 'buildreq.open' + content = """classifiers = [ + 'Programming Language :: Python :: 3 :: Only', + ]""" + + m_open = mock_open(read_data=content) + with patch(open_name, m_open, create=True): + build_pattern = buildreq.get_python_build_version_from_classifier("filename") + + self.assertEqual(build_pattern, "distutils3") + + def test_setup_py2_version_classifier(self): + """ + test detection of python version from setup.py classifier + """ + + open_name = 'buildreq.open' + content = """classifiers = [ + 'Programming Language :: Python :: 2 :: Only', + ]""" + + m_open = mock_open(read_data=content) + with patch(open_name, m_open, create=True): + build_pattern = buildreq.get_python_build_version_from_classifier("filename") + + self.assertEqual(build_pattern, "distutils2") + + def test_setup_py23_version_classifier(self): + """ + test detection of python version from setup.py classifier + """ + + open_name = 'buildreq.open' + content = """classifiers = [ + 'Programming Language :: Python :: 3', + ]""" + + m_open = mock_open(read_data=content) + with patch(open_name, m_open, create=True): + build_pattern = buildreq.get_python_build_version_from_classifier("filename") + + self.assertEqual(build_pattern, "distutils23") + def test_scan_for_configure(self): """ Test scan_for_configure with a mocked package structure. There is so