2 from setuptools import Command
3 from distutils.util import convert_path
4 from distutils import log
5 from distutils.errors import *
7 __all__ = ['config_file', 'edit_config', 'option_base', 'setopt']
10 def config_file(kind="local"):
11 """Get the filename of the distutils, local, global, or per-user config
13 `kind` must be one of "local", "global", or "user"
19 os.path.dirname(distutils.__file__),'distutils.cfg'
22 dot = os.name=='posix' and '.' or ''
23 return os.path.expanduser(convert_path("~/%spydistutils.cfg" % dot))
25 "config_file() type must be 'local', 'global', or 'user'", kind
42 def edit_config(filename, settings, dry_run=False):
43 """Edit a configuration file to include `settings`
45 `settings` is a dictionary of dictionaries or ``None`` values, keyed by
46 command/section name. A ``None`` value means to delete the entire section,
47 while a dictionary lists settings to be changed or deleted in that section.
48 A setting of ``None`` means to delete that setting.
50 from ConfigParser import RawConfigParser
51 log.debug("Reading configuration from %s", filename)
52 opts = RawConfigParser()
54 for section, options in settings.items():
56 log.info("Deleting section [%s] from %s", section, filename)
57 opts.remove_section(section)
59 if not opts.has_section(section):
60 log.debug("Adding new section [%s] to %s", section, filename)
61 opts.add_section(section)
62 for option,value in options.items():
64 log.debug("Deleting %s.%s from %s",
65 section, option, filename
67 opts.remove_option(section,option)
68 if not opts.options(section):
69 log.info("Deleting empty [%s] section from %s",
71 opts.remove_section(section)
74 "Setting %s.%s to %r in %s",
75 section, option, value, filename
77 opts.set(section,option,value)
79 log.info("Writing %s", filename)
81 f = open(filename,'w'); opts.write(f); f.close()
83 class option_base(Command):
84 """Abstract base class for commands that mess with config files"""
87 ('global-config', 'g',
88 "save options to the site-wide distutils.cfg file"),
90 "save options to the current user's pydistutils.cfg file"),
92 "configuration file to use (default=setup.cfg)"),
96 'global-config', 'user-config',
99 def initialize_options(self):
100 self.global_config = None
101 self.user_config = None
104 def finalize_options(self):
106 if self.global_config:
107 filenames.append(config_file('global'))
109 filenames.append(config_file('user'))
110 if self.filename is not None:
111 filenames.append(self.filename)
113 filenames.append(config_file('local'))
115 raise DistutilsOptionError(
116 "Must specify only one configuration file option",
119 self.filename, = filenames
124 class setopt(option_base):
125 """Save command-line options to a file"""
127 description = "set an option in setup.cfg or another config file"
130 ('command=', 'c', 'command to set an option for'),
131 ('option=', 'o', 'option to set'),
132 ('set-value=', 's', 'value of the option'),
133 ('remove', 'r', 'remove (unset) the value'),
134 ] + option_base.user_options
136 boolean_options = option_base.boolean_options + ['remove']
138 def initialize_options(self):
139 option_base.initialize_options(self)
142 self.set_value = None
145 def finalize_options(self):
146 option_base.finalize_options(self)
147 if self.command is None or self.option is None:
148 raise DistutilsOptionError("Must specify --command *and* --option")
149 if self.set_value is None and not self.remove:
150 raise DistutilsOptionError("Must specify --set-value or --remove")
155 self.command: {self.option.replace('-','_'):self.set_value}