2 # -*- coding: utf-8 -*-
3 #################################################################################
5 #################################################################################
8 # Handles layman configuration
11 # (c) 2005 - 2008 Gunnar Wrobel
12 # Distributed under the terms of the GNU General Public License v2
15 # Gunnar Wrobel <wrobel@gentoo.org>
17 '''Defines the configuration options and provides parsing functionality.'''
19 __version__
= "$Id: config.py 286 2007-01-09 17:48:23Z wrobel $"
21 #===============================================================================
25 #-------------------------------------------------------------------------------
27 import sys
, ConfigParser
29 from optparse
import OptionParser
, OptionGroup
30 from layman
.debug
import OUT
31 from layman
.version
import VERSION
33 #===============================================================================
37 #-------------------------------------------------------------------------------
40 '''Handles the configuration.'''
44 Creates and describes all possible polymeraZe options and creates
48 >>> here = os.path.dirname(os.path.realpath(__file__))
49 >>> sys.argv.append('--config')
50 >>> sys.argv.append(here + '/../etc/layman.cfg')
53 '\\nhttp://www.gentoo.org/proj/en/overlays/layman-global.txt'
55 ['cache', 'config', 'local_list', 'make_conf', 'nocheck', 'overlays', 'proxy', 'quietness', 'storage', 'umask', 'width']
58 self
.defaults
= {'config' : '/etc/layman/layman.cfg',
59 'storage' : '/usr/portage/local/layman',
60 'cache' : '%(storage)s/cache',
61 'local_list': '%(storage)s/overlays.xml',
62 'make_conf' : '%(storage)s/make.conf',
67 'http://www.gentoo.org/proj/en/overlays/layman-global.'
71 self
.parser
= OptionParser(
72 usage
= '\n\nlayman -a/-d/-S|overlay\nlayman -f [-o url]\nlayman' \
76 #-----------------------------------------------------------------
79 group
= OptionGroup(self
.parser
,
82 group
.add_option('-a',
85 help = 'Add the given overlay from the cached remote li'
86 'st to your locally installed overlays.. Specify "ALL" '
87 'to add all overlays from the remote list.')
89 group
.add_option('-d',
92 help = 'Remove the given overlay from your locally inst'
93 'alled overlays. Specify "ALL" to remove all overlays')
95 group
.add_option('-s',
98 help = 'Update the specified overlay. Use "ALL" as para'
99 'meter to synchronize all overlays')
101 group
.add_option('-i',
104 help = 'Display information about the specified overlay'
107 group
.add_option('-S',
109 action
= 'store_true',
110 help = 'Update all overlays.')
112 group
.add_option('-L',
114 action
= 'store_true',
115 help = 'List the contents of the remote list.')
117 group
.add_option('-l',
119 action
= 'store_true',
120 help = 'List the locally installed overlays.')
122 group
.add_option('-f',
124 action
= 'store_true',
125 help = 'Fetch a remote list of overlays. This option is'
126 ' deprecated. The fetch operation will be performed by '
127 'default when you run sync, sync-all, or list.')
129 group
.add_option('-n',
131 action
= 'store_true',
132 help = 'Do not fetch a remote list of overlays.')
134 group
.add_option('-p',
137 help = 'Use this with the --add switch to set the prior'
138 'ity of the added overlay. This will influence the sort'
139 'ing order of the overlays in the PORTDIR_OVERLAY varia'
142 self
.parser
.add_option_group(group
)
144 #-----------------------------------------------------------------
147 group
= OptionGroup(self
.parser
,
150 group
.add_option('-c',
153 help = 'Path to the config file [default: ' \
154 + self
.defaults
['config'] + '].')
156 group
.add_option('-o',
159 help = 'The list of overlays [default: ' \
160 + self
.defaults
['overlays'] + '].')
162 self
.parser
.add_option_group(group
)
164 #-----------------------------------------------------------------
167 group
= OptionGroup(self
.parser
,
170 group
.add_option('-v',
172 action
= 'store_true',
173 help = 'Increase the amount of output and describe the '
176 group
.add_option('-q',
178 action
= 'store_true',
179 help = 'Yield no output. Please be careful with this op'
180 'tion: If the processes spawned by layman when adding o'
181 'r synchronizing overlays require any input layman will'
182 ' hang without telling you why. This might happen for e'
183 'xample if your overlay resides in subversion and the S'
184 'SL certificate of the server needs acceptance.')
186 group
.add_option('-N',
188 action
= 'store_true',
189 help = 'Remove color codes from the layman output.')
191 group
.add_option('-Q',
196 help = 'Set the level of output (0-4). Default: 4. Once'
197 ' you set this below 2 the same warning as given for --'
200 group
.add_option('-W',
205 help = 'Sets the screen width. This setting is usually '
206 'not required as layman is capable of detecting the ava'
207 'available number of columns automatically.')
209 group
.add_option('-k',
211 action
= 'store_true',
212 help = 'Do not check overlay definitions and do not i'
213 'ssue a warning if description or contact information'
216 self
.parser
.add_option_group(group
)
218 #-----------------------------------------------------------------
221 OUT
.cli_opts(self
.parser
)
223 # Parse the command line first since we need to get the config
225 (self
.options
, args
) = self
.parser
.parse_args()
228 OUT
.cli_handle(self
.options
)
230 if self
.options
.__dict
__['nocolor']:
233 # Fetch only an alternate config setting from the options
234 if not self
.options
.__dict
__['config'] is None:
235 self
.defaults
['config'] = self
.options
.__dict
__['config']
237 OUT
.debug('Got config file at ' + self
.defaults
['config'], 8)
239 # Now parse the config file
240 self
.config
= ConfigParser
.ConfigParser(self
.defaults
)
241 self
.config
.add_section('MAIN')
245 OUT
.set_info_level(1)
246 OUT
.set_warn_level(1)
247 self
.defaults
['quietness'] = 0
249 elif 'quietness' in self
.keys():
250 OUT
.set_info_level(int(self
['quietness']))
251 OUT
.set_warn_level(int(self
['quietness']))
253 OUT
.debug('Reading config file at ' + self
.defaults
['config'], 8)
255 self
.config
.read(self
.defaults
['config'])
257 def __getitem__(self
, key
):
259 if key
== 'overlays':
261 if (key
in self
.options
.__dict
__.keys()
262 and not self
.options
.__dict
__[key
] is None):
263 overlays
= '\n'.join(self
.options
.__dict
__[key
])
264 if self
.config
.has_option('MAIN', 'overlays'):
265 overlays
+= '\n' + self
.config
.get('MAIN', 'overlays')
269 OUT
.debug('Retrieving option', 8)
271 if (key
in self
.options
.__dict
__.keys()
272 and not self
.options
.__dict
__[key
] is None):
273 return self
.options
.__dict
__[key
]
275 OUT
.debug('Retrieving option', 8)
277 if self
.config
.has_option('MAIN', key
):
279 if self
.config
.get('MAIN', key
).lower() == 'yes':
283 return self
.config
.get('MAIN', key
)
285 OUT
.debug('Retrieving option', 8)
287 if key
in self
.defaults
.keys():
288 return self
.defaults
[key
]
290 OUT
.debug('Retrieving option', 8)
295 '''Special handler for the configuration keys.'''
297 OUT
.debug('Retrieving keys', 8)
299 keys
= [i
for i
in self
.options
.__dict
__.keys()
300 if not self
.options
.__dict
__[i
] is None]
302 OUT
.debug('Retrieving keys', 8)
304 keys
+= [name
for name
, value
in self
.config
.items('MAIN')
307 OUT
.debug('Retrieving keys', 8)
309 keys
+= [i
for i
in self
.defaults
.keys()
312 OUT
.debug('Retrieving keys', 8)
317 #===============================================================================
321 #-------------------------------------------------------------------------------
323 if __name__
== '__main__':
325 doctest
.testmod(sys
.modules
[__name__
])