comparison env/lib/python3.9/site-packages/setuptools/command/setopt.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from distutils.util import convert_path
2 from distutils import log
3 from distutils.errors import DistutilsOptionError
4 import distutils
5 import os
6 import configparser
7
8 from setuptools import Command
9
10 __all__ = ['config_file', 'edit_config', 'option_base', 'setopt']
11
12
13 def config_file(kind="local"):
14 """Get the filename of the distutils, local, global, or per-user config
15
16 `kind` must be one of "local", "global", or "user"
17 """
18 if kind == 'local':
19 return 'setup.cfg'
20 if kind == 'global':
21 return os.path.join(
22 os.path.dirname(distutils.__file__), 'distutils.cfg'
23 )
24 if kind == 'user':
25 dot = os.name == 'posix' and '.' or ''
26 return os.path.expanduser(convert_path("~/%spydistutils.cfg" % dot))
27 raise ValueError(
28 "config_file() type must be 'local', 'global', or 'user'", kind
29 )
30
31
32 def edit_config(filename, settings, dry_run=False):
33 """Edit a configuration file to include `settings`
34
35 `settings` is a dictionary of dictionaries or ``None`` values, keyed by
36 command/section name. A ``None`` value means to delete the entire section,
37 while a dictionary lists settings to be changed or deleted in that section.
38 A setting of ``None`` means to delete that setting.
39 """
40 log.debug("Reading configuration from %s", filename)
41 opts = configparser.RawConfigParser()
42 opts.read([filename])
43 for section, options in settings.items():
44 if options is None:
45 log.info("Deleting section [%s] from %s", section, filename)
46 opts.remove_section(section)
47 else:
48 if not opts.has_section(section):
49 log.debug("Adding new section [%s] to %s", section, filename)
50 opts.add_section(section)
51 for option, value in options.items():
52 if value is None:
53 log.debug(
54 "Deleting %s.%s from %s",
55 section, option, filename
56 )
57 opts.remove_option(section, option)
58 if not opts.options(section):
59 log.info("Deleting empty [%s] section from %s",
60 section, filename)
61 opts.remove_section(section)
62 else:
63 log.debug(
64 "Setting %s.%s to %r in %s",
65 section, option, value, filename
66 )
67 opts.set(section, option, value)
68
69 log.info("Writing %s", filename)
70 if not dry_run:
71 with open(filename, 'w') as f:
72 opts.write(f)
73
74
75 class option_base(Command):
76 """Abstract base class for commands that mess with config files"""
77
78 user_options = [
79 ('global-config', 'g',
80 "save options to the site-wide distutils.cfg file"),
81 ('user-config', 'u',
82 "save options to the current user's pydistutils.cfg file"),
83 ('filename=', 'f',
84 "configuration file to use (default=setup.cfg)"),
85 ]
86
87 boolean_options = [
88 'global-config', 'user-config',
89 ]
90
91 def initialize_options(self):
92 self.global_config = None
93 self.user_config = None
94 self.filename = None
95
96 def finalize_options(self):
97 filenames = []
98 if self.global_config:
99 filenames.append(config_file('global'))
100 if self.user_config:
101 filenames.append(config_file('user'))
102 if self.filename is not None:
103 filenames.append(self.filename)
104 if not filenames:
105 filenames.append(config_file('local'))
106 if len(filenames) > 1:
107 raise DistutilsOptionError(
108 "Must specify only one configuration file option",
109 filenames
110 )
111 self.filename, = filenames
112
113
114 class setopt(option_base):
115 """Save command-line options to a file"""
116
117 description = "set an option in setup.cfg or another config file"
118
119 user_options = [
120 ('command=', 'c', 'command to set an option for'),
121 ('option=', 'o', 'option to set'),
122 ('set-value=', 's', 'value of the option'),
123 ('remove', 'r', 'remove (unset) the value'),
124 ] + option_base.user_options
125
126 boolean_options = option_base.boolean_options + ['remove']
127
128 def initialize_options(self):
129 option_base.initialize_options(self)
130 self.command = None
131 self.option = None
132 self.set_value = None
133 self.remove = None
134
135 def finalize_options(self):
136 option_base.finalize_options(self)
137 if self.command is None or self.option is None:
138 raise DistutilsOptionError("Must specify --command *and* --option")
139 if self.set_value is None and not self.remove:
140 raise DistutilsOptionError("Must specify --set-value or --remove")
141
142 def run(self):
143 edit_config(
144 self.filename, {
145 self.command: {self.option.replace('-', '_'): self.set_value}
146 },
147 self.dry_run
148 )