Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/setuptools/command/setopt.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
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 | |
7 from setuptools import Command | |
8 | |
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 from setuptools.compat import ConfigParser | |
41 | |
42 log.debug("Reading configuration from %s", filename) | |
43 opts = ConfigParser.RawConfigParser() | |
44 opts.read([filename]) | |
45 for section, options in settings.items(): | |
46 if options is None: | |
47 log.info("Deleting section [%s] from %s", section, filename) | |
48 opts.remove_section(section) | |
49 else: | |
50 if not opts.has_section(section): | |
51 log.debug("Adding new section [%s] to %s", section, filename) | |
52 opts.add_section(section) | |
53 for option, value in options.items(): | |
54 if value is None: | |
55 log.debug( | |
56 "Deleting %s.%s from %s", | |
57 section, option, filename | |
58 ) | |
59 opts.remove_option(section, option) | |
60 if not opts.options(section): | |
61 log.info("Deleting empty [%s] section from %s", | |
62 section, filename) | |
63 opts.remove_section(section) | |
64 else: | |
65 log.debug( | |
66 "Setting %s.%s to %r in %s", | |
67 section, option, value, filename | |
68 ) | |
69 opts.set(section, option, value) | |
70 | |
71 log.info("Writing %s", filename) | |
72 if not dry_run: | |
73 with open(filename, 'w') as f: | |
74 opts.write(f) | |
75 | |
76 | |
77 class option_base(Command): | |
78 """Abstract base class for commands that mess with config files""" | |
79 | |
80 user_options = [ | |
81 ('global-config', 'g', | |
82 "save options to the site-wide distutils.cfg file"), | |
83 ('user-config', 'u', | |
84 "save options to the current user's pydistutils.cfg file"), | |
85 ('filename=', 'f', | |
86 "configuration file to use (default=setup.cfg)"), | |
87 ] | |
88 | |
89 boolean_options = [ | |
90 'global-config', 'user-config', | |
91 ] | |
92 | |
93 def initialize_options(self): | |
94 self.global_config = None | |
95 self.user_config = None | |
96 self.filename = None | |
97 | |
98 def finalize_options(self): | |
99 filenames = [] | |
100 if self.global_config: | |
101 filenames.append(config_file('global')) | |
102 if self.user_config: | |
103 filenames.append(config_file('user')) | |
104 if self.filename is not None: | |
105 filenames.append(self.filename) | |
106 if not filenames: | |
107 filenames.append(config_file('local')) | |
108 if len(filenames) > 1: | |
109 raise DistutilsOptionError( | |
110 "Must specify only one configuration file option", | |
111 filenames | |
112 ) | |
113 self.filename, = filenames | |
114 | |
115 | |
116 class setopt(option_base): | |
117 """Save command-line options to a file""" | |
118 | |
119 description = "set an option in setup.cfg or another config file" | |
120 | |
121 user_options = [ | |
122 ('command=', 'c', 'command to set an option for'), | |
123 ('option=', 'o', 'option to set'), | |
124 ('set-value=', 's', 'value of the option'), | |
125 ('remove', 'r', 'remove (unset) the value'), | |
126 ] + option_base.user_options | |
127 | |
128 boolean_options = option_base.boolean_options + ['remove'] | |
129 | |
130 def initialize_options(self): | |
131 option_base.initialize_options(self) | |
132 self.command = None | |
133 self.option = None | |
134 self.set_value = None | |
135 self.remove = None | |
136 | |
137 def finalize_options(self): | |
138 option_base.finalize_options(self) | |
139 if self.command is None or self.option is None: | |
140 raise DistutilsOptionError("Must specify --command *and* --option") | |
141 if self.set_value is None and not self.remove: | |
142 raise DistutilsOptionError("Must specify --set-value or --remove") | |
143 | |
144 def run(self): | |
145 edit_config( | |
146 self.filename, { | |
147 self.command: {self.option.replace('-', '_'): self.set_value} | |
148 }, | |
149 self.dry_run | |
150 ) |