mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
Fixed some bugs and mis-features in handling config files:
* options can now be spelled "foo-bar" or "foo_bar" (handled in 'parse_config_files()', just after we parse a file) * added a "[global]" section so there's a place to set global options like verbose/quiet and dry-run * respect the "negative alias" dictionary so (eg.) "quiet=1" is the same as "verbose=0" (this had to be done twice: once in 'parse_config_file()' for global options, and once in '_set_command_options()' for per-command options) * the other half of handling boolean options correctly: allow commands to list their boolean options in a 'boolean_options' class attribute, and use it to translate strings (like "yes", "1", "no", "0", etc) to true or false
This commit is contained in:
parent
84da8aa7e2
commit
ceb9e226a6
1 changed files with 44 additions and 9 deletions
|
@ -15,7 +15,7 @@ from copy import copy
|
||||||
from distutils.errors import *
|
from distutils.errors import *
|
||||||
from distutils import sysconfig
|
from distutils import sysconfig
|
||||||
from distutils.fancy_getopt import FancyGetopt, longopt_xlate
|
from distutils.fancy_getopt import FancyGetopt, longopt_xlate
|
||||||
from distutils.util import check_environ
|
from distutils.util import check_environ, strtobool
|
||||||
|
|
||||||
|
|
||||||
# Regex to define acceptable Distutils command names. This is not *quite*
|
# Regex to define acceptable Distutils command names. This is not *quite*
|
||||||
|
@ -321,7 +321,9 @@ class Distribution:
|
||||||
|
|
||||||
for opt in options:
|
for opt in options:
|
||||||
if opt != '__name__':
|
if opt != '__name__':
|
||||||
opt_dict[opt] = (filename, parser.get(section,opt))
|
val = parser.get(section,opt)
|
||||||
|
opt = string.replace(opt, '-', '_')
|
||||||
|
opt_dict[opt] = (filename, val)
|
||||||
|
|
||||||
# Make the ConfigParser forget everything (so we retain
|
# Make the ConfigParser forget everything (so we retain
|
||||||
# the original filenames that options come from) -- gag,
|
# the original filenames that options come from) -- gag,
|
||||||
|
@ -329,6 +331,22 @@ class Distribution:
|
||||||
# specific config parser (sigh...)
|
# specific config parser (sigh...)
|
||||||
parser.__init__()
|
parser.__init__()
|
||||||
|
|
||||||
|
# If there was a "global" section in the config file, use it
|
||||||
|
# to set Distribution options.
|
||||||
|
|
||||||
|
if self.command_options.has_key('global'):
|
||||||
|
for (opt, (src, val)) in self.command_options['global'].items():
|
||||||
|
alias = self.negative_opt.get(opt)
|
||||||
|
try:
|
||||||
|
if alias:
|
||||||
|
setattr(self, alias, not strtobool(val))
|
||||||
|
elif opt in ('verbose', 'dry_run'): # ugh!
|
||||||
|
setattr(self, opt, strtobool(val))
|
||||||
|
except ValueError, msg:
|
||||||
|
raise DistutilsOptionError, msg
|
||||||
|
|
||||||
|
# parse_config_files ()
|
||||||
|
|
||||||
|
|
||||||
# -- Command-line parsing methods ----------------------------------
|
# -- Command-line parsing methods ----------------------------------
|
||||||
|
|
||||||
|
@ -346,7 +364,7 @@ class Distribution:
|
||||||
attribute raises DistutilsGetoptError; any error on the
|
attribute raises DistutilsGetoptError; any error on the
|
||||||
command-line raises DistutilsArgError. If no Distutils commands
|
command-line raises DistutilsArgError. If no Distutils commands
|
||||||
were found on the command line, raises DistutilsArgError. Return
|
were found on the command line, raises DistutilsArgError. Return
|
||||||
true if command-line were successfully parsed and we should carry
|
true if command-line was successfully parsed and we should carry
|
||||||
on with executing commands; false if no errors but we shouldn't
|
on with executing commands; false if no errors but we shouldn't
|
||||||
execute commands (currently, this only happens if user asks for
|
execute commands (currently, this only happens if user asks for
|
||||||
help).
|
help).
|
||||||
|
@ -714,7 +732,7 @@ class Distribution:
|
||||||
this means copying elements of a dictionary ('option_dict') to
|
this means copying elements of a dictionary ('option_dict') to
|
||||||
attributes of an instance ('command').
|
attributes of an instance ('command').
|
||||||
|
|
||||||
'command_obj' must be a Commnd instance. If 'option_dict' is not
|
'command_obj' must be a Command instance. If 'option_dict' is not
|
||||||
supplied, uses the standard option dictionary for this command
|
supplied, uses the standard option dictionary for this command
|
||||||
(from 'self.command_options').
|
(from 'self.command_options').
|
||||||
"""
|
"""
|
||||||
|
@ -727,11 +745,28 @@ class Distribution:
|
||||||
if DEBUG: print " setting options for '%s' command:" % command_name
|
if DEBUG: print " setting options for '%s' command:" % command_name
|
||||||
for (option, (source, value)) in option_dict.items():
|
for (option, (source, value)) in option_dict.items():
|
||||||
if DEBUG: print " %s = %s (from %s)" % (option, value, source)
|
if DEBUG: print " %s = %s (from %s)" % (option, value, source)
|
||||||
if not hasattr(command_obj, option):
|
try:
|
||||||
raise DistutilsOptionError, \
|
bool_opts = command_obj.boolean_options
|
||||||
("error in %s: command '%s' has no such option '%s'") % \
|
except AttributeError:
|
||||||
(source, command_name, option)
|
bool_opts = []
|
||||||
|
try:
|
||||||
|
neg_opt = command_obj.negative_opt
|
||||||
|
except AttributeError:
|
||||||
|
neg_opt = {}
|
||||||
|
|
||||||
|
try:
|
||||||
|
if neg_opt.has_key(option):
|
||||||
|
setattr(command_obj, neg_opt[option], not strtobool(value))
|
||||||
|
elif option in bool_opts:
|
||||||
|
setattr(command_obj, option, strtobool(value))
|
||||||
|
elif hasattr(command_obj, option):
|
||||||
setattr(command_obj, option, value)
|
setattr(command_obj, option, value)
|
||||||
|
else:
|
||||||
|
raise DistutilsOptionError, \
|
||||||
|
("error in %s: command '%s' has no such option '%s'"
|
||||||
|
% (source, command_name, option))
|
||||||
|
except ValueError, msg:
|
||||||
|
raise DistutilsOptionError, msg
|
||||||
|
|
||||||
def reinitialize_command (self, command, reinit_subcommands=0):
|
def reinitialize_command (self, command, reinit_subcommands=0):
|
||||||
"""Reinitializes a command to the state it was in when first
|
"""Reinitializes a command to the state it was in when first
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue