Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a1838b

Browse files
committed
Issue #19544 and Issue #1180: Restore global option to ignore ~/.pydistutils.cfg in Distutils, accidentally removed in backout of distutils2 changes.
1 parent ce4179d commit 2a1838b

5 files changed

Lines changed: 68 additions & 8 deletions

File tree

Doc/distutils/builtdist.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ tedious and error-prone, so it's usually best to put them in the setup
239239
configuration file, :file:`setup.cfg`\ ---see section :ref:`setup-config`. If
240240
you distribute or package many Python module distributions, you might want to
241241
put options that apply to all of them in your personal Distutils configuration
242-
file (:file:`~/.pydistutils.cfg`).
242+
file (:file:`~/.pydistutils.cfg`). If you want to temporarily disable
243+
this file, you can pass the :option:`--no-user-cfg` option to :file:`setup.py`.
243244

244245
There are three steps to building a binary RPM package, all of which are
245246
handled automatically by the Distutils:

Lib/distutils/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ class found in 'cmdclass' is used in place of the default, which is
128128
if _setup_stop_after == "config":
129129
return dist
130130

131-
# Parse the command line; any command-line errors are the end user's
132-
# fault, so turn them into SystemExit to suppress tracebacks.
131+
# Parse the command line and override config files; any
132+
# command-line errors are the end user's fault, so turn them into
133+
# SystemExit to suppress tracebacks.
133134
try:
134135
ok = dist.parse_command_line()
135136
except DistutilsArgError as msg:

Lib/distutils/dist.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Distribution:
5252
('quiet', 'q', "run quietly (turns verbosity off)"),
5353
('dry-run', 'n', "don't actually do anything"),
5454
('help', 'h', "show detailed help message"),
55-
]
55+
('no-user-cfg', None,
56+
'ignore pydistutils.cfg in your home directory'),
57+
]
5658

5759
# 'common_usage' is a short (2-3 line) string describing the common
5860
# usage of the setup script.
@@ -259,6 +261,22 @@ def __init__ (self, attrs=None):
259261
else:
260262
sys.stderr.write(msg + "\n")
261263

264+
# no-user-cfg is handled before other command line args
265+
# because other args override the config files, and this
266+
# one is needed before we can load the config files.
267+
# If attrs['script_args'] wasn't passed, assume false.
268+
#
269+
# This also make sure we just look at the global options
270+
self.want_user_cfg = True
271+
272+
if self.script_args is not None:
273+
for arg in self.script_args:
274+
if not arg.startswith('-'):
275+
break
276+
if arg == '--no-user-cfg':
277+
self.want_user_cfg = False
278+
break
279+
262280
self.finalize_options()
263281

264282
def get_option_dict(self, command):
@@ -310,7 +328,10 @@ def find_config_files(self):
310328
Distutils installation directory (ie. where the top-level
311329
Distutils __inst__.py file lives), a file in the user's home
312330
directory named .pydistutils.cfg on Unix and pydistutils.cfg
313-
on Windows/Mac, and setup.cfg in the current directory.
331+
on Windows/Mac; and setup.cfg in the current directory.
332+
333+
The file in the user's home directory can be disabled with the
334+
--no-user-cfg option.
314335
"""
315336
files = []
316337
check_environ()
@@ -330,15 +351,19 @@ def find_config_files(self):
330351
user_filename = "pydistutils.cfg"
331352

332353
# And look for the user config file
333-
user_file = os.path.join(os.path.expanduser('~'), user_filename)
334-
if os.path.isfile(user_file):
335-
files.append(user_file)
354+
if self.want_user_cfg:
355+
user_file = os.path.join(os.path.expanduser('~'), user_filename)
356+
if os.path.isfile(user_file):
357+
files.append(user_file)
336358

337359
# All platforms support local setup.cfg
338360
local_file = "setup.cfg"
339361
if os.path.isfile(local_file):
340362
files.append(local_file)
341363

364+
if DEBUG:
365+
self.announce("using config files: %s" % ', '.join(files))
366+
342367
return files
343368

344369
def parse_config_files(self, filenames=None):

Lib/distutils/tests/test_dist.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def find_config_files(self):
3939

4040

4141
class DistributionTestCase(support.LoggingSilencer,
42+
support.TempdirManager,
4243
support.EnvironGuard,
4344
unittest.TestCase):
4445

@@ -213,6 +214,34 @@ def test_announce(self):
213214
self.assertRaises(ValueError, dist.announce, args, kwargs)
214215

215216

217+
def test_find_config_files_disable(self):
218+
# Ticket #1180: Allow user to disable their home config file.
219+
temp_home = self.mkdtemp()
220+
if os.name == 'posix':
221+
user_filename = os.path.join(temp_home, ".pydistutils.cfg")
222+
else:
223+
user_filename = os.path.join(temp_home, "pydistutils.cfg")
224+
225+
with open(user_filename, 'w') as f:
226+
f.write('[distutils]\n')
227+
228+
def _expander(path):
229+
return temp_home
230+
231+
old_expander = os.path.expanduser
232+
os.path.expanduser = _expander
233+
try:
234+
d = Distribution()
235+
all_files = d.find_config_files()
236+
237+
d = Distribution(attrs={'script_args': ['--no-user-cfg']})
238+
files = d.find_config_files()
239+
finally:
240+
os.path.expanduser = old_expander
241+
242+
# make sure --no-user-cfg disables the user cfg file
243+
self.assertEquals(len(all_files)-1, len(files))
244+
216245
class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
217246
unittest.TestCase):
218247

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Core and Builtins
4747
Library
4848
-------
4949

50+
- Issue #19544 and Issue #1180: Restore global option to ignore
51+
~/.pydistutils.cfg in Distutils, accidentally removed in backout of
52+
distutils2 changes.
53+
5054
- Issue #19523: Closed FileHandler leak which occurred when delay was set.
5155

5256
- Issue #19544 and #6516: Restore support for --user and --group parameters to

0 commit comments

Comments
 (0)