From ef78ba5de0acb557c3f8aecde9f8e2d8681b85b8 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 21:33:53 +0100 Subject: [PATCH 1/6] Add DISTUTILS_EXTRA_CONFIG option for passing setup.cfg overrides during build --- distutils/dist.py | 4 ++++ distutils/tests/test_dist.py | 21 +++++++++++++++++++++ docs/distutils/configfile.rst | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/distutils/dist.py b/distutils/dist.py index 0406ab19..d854cd99 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -359,6 +359,10 @@ def find_config_files(self): if os.path.isfile(local_file): files.append(local_file) + extra_file = os.getenv("DISTUTILS_EXTRA_CONFIG") + if extra_file and os.path.isfile(extra_file): + files.append(extra_file) + if DEBUG: self.announce("using config files: %s" % ', '.join(files)) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 25056af5..e14d7da1 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -492,6 +492,27 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) + def test_extra_pydistutils(self): + # make sure pydistutils.cfg is found + extra_filename = "overrides.cfg" + + temp_dir = self.mkdtemp() + extra_filename = os.path.join(temp_dir, extra_filename) + with open(extra_filename, 'w') as f: + f.write('.') + + # Testing will have been going terribly if this was set, but preserve + # it anyway (so it goes terribly but *consistently*) + old_extra_filename = os.environ.get("DISTUTILS_EXTRA_CONFIG") + os.environ["DISTUTILS_EXTRA_CONFIG"] = extra_filename + try: + dist = Distribution() + files = dist.find_config_files() + assert user_filename in files + finally: + os.remove(user_filename) + os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename + def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] fancy_options = fix_help_options(help_tuples) diff --git a/docs/distutils/configfile.rst b/docs/distutils/configfile.rst index 2a5c8329..e03d6d43 100644 --- a/docs/distutils/configfile.rst +++ b/docs/distutils/configfile.rst @@ -36,7 +36,8 @@ consequences: :file:`setup.py` * installers can override anything in :file:`setup.cfg` using the command-line - options to :file:`setup.py` + options to :file:`setup.py` or by pointing :envvar:`DISTUTILS_EXTRA_CONFIG` + to another configuration file The basic syntax of the configuration file is simple: From 825f02ab1073d430c2481bab8c44a41b6f061776 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 1 Sep 2022 22:12:07 +0100 Subject: [PATCH 2/6] Fix name in test --- distutils/tests/test_dist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index e14d7da1..beb9924d 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -508,9 +508,9 @@ def test_extra_pydistutils(self): try: dist = Distribution() files = dist.find_config_files() - assert user_filename in files + assert extra_filename in files finally: - os.remove(user_filename) + os.remove(extra_filename) os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename def test_fix_help_options(self): From 457df3899d61839dbe7966d6152a34d0fc6a1318 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:08:36 -0400 Subject: [PATCH 3/6] Prefer monkeypatch for setting environment variable --- distutils/tests/test_dist.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index beb9924d..33da08cb 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -492,7 +492,7 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) - def test_extra_pydistutils(self): + def test_extra_pydistutils(self, monkeypatch): # make sure pydistutils.cfg is found extra_filename = "overrides.cfg" @@ -501,17 +501,13 @@ def test_extra_pydistutils(self): with open(extra_filename, 'w') as f: f.write('.') - # Testing will have been going terribly if this was set, but preserve - # it anyway (so it goes terribly but *consistently*) - old_extra_filename = os.environ.get("DISTUTILS_EXTRA_CONFIG") - os.environ["DISTUTILS_EXTRA_CONFIG"] = extra_filename + monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', extra_filename) try: dist = Distribution() files = dist.find_config_files() assert extra_filename in files finally: os.remove(extra_filename) - os.environ["DISTUTILS_EXTRA_CONFIG"] = old_extra_filename def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] From a3614615895f69a0ff9b0c3139ecfd6180d30882 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 12:20:35 -0400 Subject: [PATCH 4/6] Use tmp_path and jaraco.path.build to build files. --- distutils/tests/test_dist.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 33da08cb..5c05ad40 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -8,6 +8,7 @@ import unittest.mock as mock import pytest +import jaraco.path from distutils.dist import Distribution, fix_help_options from distutils.cmd import Command @@ -492,22 +493,14 @@ def test_custom_pydistutils(self): finally: os.remove(user_filename) - def test_extra_pydistutils(self, monkeypatch): - # make sure pydistutils.cfg is found - extra_filename = "overrides.cfg" - - temp_dir = self.mkdtemp() - extra_filename = os.path.join(temp_dir, extra_filename) - with open(extra_filename, 'w') as f: - f.write('.') + def test_extra_pydistutils(self, monkeypatch, tmp_path): + jaraco.path.build({'overrides.cfg': '.'}, tmp_path) + filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', extra_filename) - try: - dist = Distribution() - files = dist.find_config_files() - assert extra_filename in files - finally: - os.remove(extra_filename) + monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', filename) + dist = Distribution() + files = dist.find_config_files() + assert str(filename) in files def test_fix_help_options(self): help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)] From 98a46dbaebc53fcb10b0643fadad98f76ba36e1b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:20:16 -0400 Subject: [PATCH 5/6] Update docs to reference environment variable. --- distutils/dist.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index 25227584..cf6a8bf6 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -324,14 +324,14 @@ def find_config_files(self): should be parsed. The filenames returned are guaranteed to exist (modulo nasty race conditions). - There are three possible config files: distutils.cfg in the - Distutils installation directory (ie. where the top-level - Distutils __inst__.py file lives), a file in the user's home - directory named .pydistutils.cfg on Unix and pydistutils.cfg - on Windows/Mac; and setup.cfg in the current directory. - - The file in the user's home directory can be disabled with the - --no-user-cfg option. + There are multiple possible config files: + - distutils.cfg in the Distutils installation directory (i.e. + where the top-level Distutils __inst__.py file lives) + - a file in the user's home directory named .pydistutils.cfg + on Unix and pydistutils.cfg on Windows/Mac; may be disabled + with the ``--no-user-cfg`` option + - setup.cfg in the current directory + - a file named by an environment variable """ check_environ() files = [str(path) for path in self._gen_paths() if path.is_file()] @@ -355,6 +355,7 @@ def _gen_paths(self): # All platforms support local setup.cfg yield pathlib.Path('setup.cfg') + # Additional config indicated in the environment with contextlib.suppress(TypeError): yield pathlib.Path(os.getenv("DISTUTILS_EXTRA_CONFIG")) From 8620c041db42ee84f71fa16a6680e6219b9d0dd7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 24 Sep 2022 13:22:50 -0400 Subject: [PATCH 6/6] Rename environment variable to DIST_EXTRA_CONFIG, decoupling it from the name of the implementation. --- distutils/dist.py | 2 +- distutils/tests/test_dist.py | 2 +- docs/distutils/configfile.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index cf6a8bf6..1dc25fe5 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -357,7 +357,7 @@ def _gen_paths(self): # Additional config indicated in the environment with contextlib.suppress(TypeError): - yield pathlib.Path(os.getenv("DISTUTILS_EXTRA_CONFIG")) + yield pathlib.Path(os.getenv("DIST_EXTRA_CONFIG")) def parse_config_files(self, filenames=None): # noqa: C901 from configparser import ConfigParser diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 3058d942..333ce014 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -491,7 +491,7 @@ def test_extra_pydistutils(self, monkeypatch, tmp_path): jaraco.path.build({'overrides.cfg': '.'}, tmp_path) filename = tmp_path / 'overrides.cfg' - monkeypatch.setenv('DISTUTILS_EXTRA_CONFIG', filename) + monkeypatch.setenv('DIST_EXTRA_CONFIG', filename) dist = Distribution() files = dist.find_config_files() assert str(filename) in files diff --git a/docs/distutils/configfile.rst b/docs/distutils/configfile.rst index e03d6d43..bdd7c455 100644 --- a/docs/distutils/configfile.rst +++ b/docs/distutils/configfile.rst @@ -36,7 +36,7 @@ consequences: :file:`setup.py` * installers can override anything in :file:`setup.cfg` using the command-line - options to :file:`setup.py` or by pointing :envvar:`DISTUTILS_EXTRA_CONFIG` + options to :file:`setup.py` or by pointing :envvar:`DIST_EXTRA_CONFIG` to another configuration file The basic syntax of the configuration file is simple: