|
14 | 14 | from distutils.errors import DistutilsOptionError
|
15 | 15 | from distutils.util import strtobool
|
16 | 16 | from distutils.debug import DEBUG
|
| 17 | +from distutils.fancy_getopt import translate_longopt |
17 | 18 | import itertools
|
18 | 19 |
|
19 | 20 | from collections import defaultdict
|
@@ -626,6 +627,53 @@ def _parse_config_files(self, filenames=None):
|
626 | 627 | except ValueError as msg:
|
627 | 628 | raise DistutilsOptionError(msg)
|
628 | 629 |
|
| 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 | + |
629 | 677 | def parse_config_files(self, filenames=None, ignore_option_errors=False):
|
630 | 678 | """Parses configuration files from various levels
|
631 | 679 | and loads configuration.
|
|
0 commit comments