1import sys
2from distutils.errors import DistutilsOptionError
3from distutils.util import strtobool
4from distutils.debug import DEBUG
5
6
7class Distribution_parse_config_files:
8    """
9    Mix-in providing forward-compatibility for functionality to be
10    included by default on Python 3.7.
11
12    Do not edit the code in this class except to update functionality
13    as implemented in distutils.
14    """
15    def parse_config_files(self, filenames=None):
16        from configparser import ConfigParser
17
18        # Ignore install directory options if we have a venv
19        if sys.prefix != sys.base_prefix:
20            ignore_options = [
21                'install-base', 'install-platbase', 'install-lib',
22                'install-platlib', 'install-purelib', 'install-headers',
23                'install-scripts', 'install-data', 'prefix', 'exec-prefix',
24                'home', 'user', 'root']
25        else:
26            ignore_options = []
27
28        ignore_options = frozenset(ignore_options)
29
30        if filenames is None:
31            filenames = self.find_config_files()
32
33        if DEBUG:
34            self.announce("Distribution.parse_config_files():")
35
36        parser = ConfigParser(interpolation=None)
37        for filename in filenames:
38            if DEBUG:
39                self.announce("  reading %s" % filename)
40            parser.read(filename)
41            for section in parser.sections():
42                options = parser.options(section)
43                opt_dict = self.get_option_dict(section)
44
45                for opt in options:
46                    if opt != '__name__' and opt not in ignore_options:
47                        val = parser.get(section,opt)
48                        opt = opt.replace('-', '_')
49                        opt_dict[opt] = (filename, val)
50
51            # Make the ConfigParser forget everything (so we retain
52            # the original filenames that options come from)
53            parser.__init__()
54
55        # If there was a "global" section in the config file, use it
56        # to set Distribution options.
57
58        if 'global' in self.command_options:
59            for (opt, (src, val)) in self.command_options['global'].items():
60                alias = self.negative_opt.get(opt)
61                try:
62                    if alias:
63                        setattr(self, alias, not strtobool(val))
64                    elif opt in ('verbose', 'dry_run'): # ugh!
65                        setattr(self, opt, strtobool(val))
66                    else:
67                        setattr(self, opt, val)
68                except ValueError as msg:
69                    raise DistutilsOptionError(msg)
70
71
72if sys.version_info < (3,):
73    # Python 2 behavior is sufficient
74    class Distribution_parse_config_files:
75        pass
76
77
78if False:
79    # When updated behavior is available upstream,
80    # disable override here.
81    class Distribution_parse_config_files:
82        pass
83