Skip to content

Commit b1615d1

Browse files
committed
Adopt distutils.dist.Distribution._set_command_options to support better string detection.
1 parent 80082fa commit b1615d1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

setuptools/dist.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from distutils.errors import DistutilsOptionError
1515
from distutils.util import strtobool
1616
from distutils.debug import DEBUG
17+
from distutils.fancy_getopt import translate_longopt
1718
import itertools
1819

1920
from collections import defaultdict
@@ -626,6 +627,53 @@ def _parse_config_files(self, filenames=None):
626627
except ValueError as msg:
627628
raise DistutilsOptionError(msg)
628629

630+
def _set_command_options(self, command_obj, option_dict=None):
631+
"""
632+
Set the options for 'command_obj' from 'option_dict'. Basically
633+
this means copying elements of a dictionary ('option_dict') to
634+
attributes of an instance ('command').
635+
636+
'command_obj' must be a Command instance. If 'option_dict' is not
637+
supplied, uses the standard option dictionary for this command
638+
(from 'self.command_options').
639+
640+
(Adopted from distutils.dist.Distribution._set_command_options)
641+
"""
642+
command_name = command_obj.get_command_name()
643+
if option_dict is None:
644+
option_dict = self.get_option_dict(command_name)
645+
646+
if DEBUG:
647+
self.announce(" setting options for '%s' command:" % command_name)
648+
for (option, (source, value)) in option_dict.items():
649+
if DEBUG:
650+
self.announce(" %s = %s (from %s)" % (option, value,
651+
source))
652+
try:
653+
bool_opts = [translate_longopt(o)
654+
for o in command_obj.boolean_options]
655+
except AttributeError:
656+
bool_opts = []
657+
try:
658+
neg_opt = command_obj.negative_opt
659+
except AttributeError:
660+
neg_opt = {}
661+
662+
try:
663+
is_string = isinstance(value, str)
664+
if option in neg_opt and is_string:
665+
setattr(command_obj, neg_opt[option], not strtobool(value))
666+
elif option in bool_opts and is_string:
667+
setattr(command_obj, option, strtobool(value))
668+
elif hasattr(command_obj, option):
669+
setattr(command_obj, option, value)
670+
else:
671+
raise DistutilsOptionError(
672+
"error in %s: command '%s' has no such option '%s'"
673+
% (source, command_name, option))
674+
except ValueError as msg:
675+
raise DistutilsOptionError(msg)
676+
629677
def parse_config_files(self, filenames=None, ignore_option_errors=False):
630678
"""Parses configuration files from various levels
631679
and loads configuration.

0 commit comments

Comments
 (0)