2 # -*- coding: utf-8 -*-
3 #################################################################################
5 #################################################################################
8 # Handles layman actions.
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 ''' Provides the different actions that can be performed by layman.'''
19 __version__
= "$Id: action.py 312 2007-04-09 19:45:49Z wrobel $"
21 #===============================================================================
25 #-------------------------------------------------------------------------------
29 from layman
.db
import DB
, RemoteDB
31 from layman
.debug
import OUT
33 #===============================================================================
37 #-------------------------------------------------------------------------------
40 ''' Fetches the overlay listing.
43 >>> here = os.path.dirname(os.path.realpath(__file__))
44 >>> cache = os.tmpnam()
45 >>> config = {'overlays' :
46 ... 'file://' + here + '/tests/testfiles/global-overlays.xml',
54 >>> b = open(a.db.path(config['overlays']))
56 ' A collection of ebuilds from Gunnar Wrobel [wrobel@gentoo.org].\\n'
59 >>> os.unlink(a.db.path(config['overlays']))
61 >>> a.db.overlays.keys()
62 [u'wrobel', u'wrobel-stable']
65 def __init__(self
, config
):
66 self
.db
= RemoteDB(config
)
69 '''Fetch the overlay listing.'''
72 except Exception, error
:
73 OUT
.die('Failed to fetch overlay list!\nError was: '
78 #===============================================================================
82 #-------------------------------------------------------------------------------
85 ''' Syncs the selected overlays.'''
87 def __init__(self
, config
):
91 self
.rdb
= RemoteDB(config
)
93 self
.quiet
= int(config
['quietness']) < 3
95 self
.selection
= config
['sync']
97 if config
['sync_all'] or 'ALL' in self
.selection
:
98 self
.selection
= self
.db
.overlays
.keys()
100 enc
= sys
.getfilesystemencoding()
102 self
.selection
= [i
.decode(enc
) for i
in self
.selection
]
105 '''Synchronize the overlays.'''
107 OUT
.debug('Updating selected overlays', 6)
111 for i
in self
.selection
:
112 ordb
= self
.rdb
.select(i
)
113 odb
= self
.db
.select(i
)
114 if ordb
and odb
and ordb
.src
!= odb
.src
:
116 'The source of the overlay "' + i
+ '" seems to have c'
117 'hanged. You currently sync from "' + odb
.src
+ '" whi'
118 'le the global layman list reports "' + ordb
.src
+ '" '
119 'as correct location. Please consider removing and rea'
120 'dding the overlay!')
123 self
.db
.sync(i
, self
.quiet
)
124 success
.append('Successfully synchronized overlay "' + i
+ '".')
125 except Exception, error
:
127 'Failed to sync overlay "' + i
+ '".\nError was: '
131 OUT
.info('\nSuccess:\n------\n', 3)
136 OUT
.warn('\nErrors:\n------\n', 2)
138 OUT
.warn(i
+ '\n', 2)
143 #===============================================================================
147 #-------------------------------------------------------------------------------
150 ''' Adds the selected overlays.'''
152 def __init__(self
, config
):
158 self
.rdb
= RemoteDB(config
)
160 self
.quiet
= int(config
['quietness']) < 3
162 self
.selection
= config
['add']
164 enc
= sys
.getfilesystemencoding()
166 self
.selection
= [i
.decode(enc
) for i
in self
.selection
]
168 if 'ALL' in self
.selection
:
169 self
.selection
= self
.rdb
.overlays
.keys()
172 '''Add the overlay.'''
174 OUT
.debug('Adding selected overlays', 6)
178 for i
in self
.selection
:
179 overlay
= self
.rdb
.select(i
)
181 OUT
.debug('Selected overlay', 7)
185 self
.db
.add(overlay
, self
.quiet
)
186 OUT
.info('Successfully added overlay "' + i
+ '".', 2)
187 except Exception, error
:
188 OUT
.warn('Failed to add overlay "' + i
+ '".\nError was: '
192 OUT
.warn('Overlay "' + i
+ '" does not exist!', 2)
197 #===============================================================================
201 #-------------------------------------------------------------------------------
204 ''' Deletes the selected overlays.'''
206 def __init__(self
, config
):
210 self
.selection
= config
['delete']
212 enc
= sys
.getfilesystemencoding()
214 self
.selection
= [i
.decode(enc
) for i
in self
.selection
]
216 if 'ALL' in self
.selection
:
217 self
.selection
= self
.db
.overlays
.keys()
220 '''Delete the overlay.'''
222 OUT
.debug('Deleting selected overlays', 6)
226 for i
in self
.selection
:
227 overlay
= self
.db
.select(i
)
229 OUT
.debug('Selected overlay', 7)
233 self
.db
.delete(overlay
)
234 OUT
.info('Successfully deleted overlay "' + i
+ '".', 2)
235 except Exception, error
:
236 OUT
.warn('Failed to delete overlay "' + i
+ '".\nError was: '
240 OUT
.warn('Overlay "' + i
+ '" does not exist!', 2)
245 #===============================================================================
249 #-------------------------------------------------------------------------------
252 ''' Print information about the specified overlays.
255 >>> here = os.path.dirname(os.path.realpath(__file__))
256 >>> cache = os.tmpnam()
257 >>> config = {'overlays' :
258 ... 'file://' + here + '/tests/testfiles/global-overlays.xml',
261 ... 'info' : ['wrobel'],
262 ... 'nocheck' : False,
263 ... 'verbose': False,
271 * Source : https://overlays.gentoo.org/svn/dev/wrobel
272 * Contact : nobody@gentoo.org
273 * Type : Subversion; Priority: 10
281 def __init__(self
, config
):
283 OUT
.debug('Creating RemoteDB handler', 6)
285 self
.rdb
= RemoteDB(config
)
288 self
.selection
= config
['info']
290 enc
= sys
.getfilesystemencoding()
292 self
.selection
= [i
.decode(enc
) for i
in self
.selection
]
294 if 'ALL' in self
.selection
:
295 self
.selection
= self
.rdb
.overlays
.keys()
298 ''' Print information about the selected overlays.'''
302 for i
in self
.selection
:
303 overlay
= self
.rdb
.select(i
)
306 # Is the overlay supported?
307 OUT
.info(overlay
.__str
__(), 1)
308 if not overlay
.is_official():
309 OUT
.warn('*** This is no official gentoo overlay ***\n', 1)
310 if not overlay
.is_supported():
311 OUT
.error('*** You are lacking the necessary tools to install t'
314 OUT
.warn('Overlay "' + i
+ '" does not exist!', 2)
319 #===============================================================================
323 #-------------------------------------------------------------------------------
326 ''' Lists the available overlays.
329 >>> here = os.path.dirname(os.path.realpath(__file__))
330 >>> cache = os.tmpnam()
331 >>> config = {'overlays' :
332 ... 'file://' + here + '/tests/testfiles/global-overlays.xml',
335 ... 'nocheck' : False,
336 ... 'verbose': False,
343 * wrobel [Subversion] (https://o.g.o/svn/dev/wrobel )
345 >>> a.config['verbose'] = True
349 * Source : https://overlays.gentoo.org/svn/dev/wrobel
350 * Contact : nobody@gentoo.org
351 * Type : Subversion; Priority: 10
356 * *** This is no official gentoo overlay ***
360 * Source : rsync://gunnarwrobel.de/wrobel-stable
361 * Contact : nobody@gentoo.org
362 * Type : Rsync; Priority: 50
365 * A collection of ebuilds from Gunnar Wrobel [wrobel@gentoo.org].
370 def __init__(self
, config
):
372 OUT
.debug('Creating RemoteDB handler', 6)
374 self
.rdb
= RemoteDB(config
)
378 ''' List the available overlays.'''
380 for i
in self
.rdb
.list(self
.config
['verbose'], self
.config
['width']):
381 # Is the overlay supported?
383 # Is this an official overlay?
386 # Unofficial overlays will only be listed if we are not
387 # checking or listing verbose
388 elif self
.config
['nocheck'] or self
.config
['verbose']:
389 # Give a reason why this is marked yellow if it is a verbose
391 if self
.config
['verbose']:
392 OUT
.warn('*** This is no official gentoo overlay ***\n', 1)
394 # Unsupported overlays will only be listed if we are not checking
396 elif self
.config
['nocheck'] or self
.config
['verbose']:
397 # Give a reason why this is marked red if it is a verbose
399 if self
.config
['verbose']:
400 OUT
.error('*** You are lacking the necessary tools to insta'
401 'll this overlay ***\n')
406 #===============================================================================
410 #-------------------------------------------------------------------------------
413 ''' Lists the local overlays.'''
415 def __init__(self
, config
):
420 '''List the overlays.'''
422 for i
in self
.db
.list(self
.config
['verbose']):
424 OUT
.debug('Printing local overlay.', 8)
426 # Is the overlay supported?
428 # Is this an official overlay?
431 # Unofficial overlays will only be listed if we are not
432 # checking or listing verbose
434 # Give a reason why this is marked yellow if it is a verbose
436 if self
.config
['verbose']:
437 OUT
.warn('*** This is no official gentoo overlay ***\n', 1)
439 # Unsupported overlays will only be listed if we are not checking
442 # Give a reason why this is marked red if it is a verbose
444 if self
.config
['verbose']:
445 OUT
.error('*** You are lacking the necessary tools to insta'
446 'll this overlay ***\n')
451 #===============================================================================
455 #-------------------------------------------------------------------------------
458 '''Dispatches to the actions the user selected. '''
460 # Given in order of precedence
461 actions
= [('fetch', Fetch
),
468 ('list_local', ListLocal
),]
470 def __init__(self
, config
):
472 # Make fetching the overlay list a default action
473 if not 'nofetch' in config
.keys():
474 # Actions that implicitely call the fetch operation before
475 fetch_actions
= ['fetch', 'sync', 'sync_all', 'list']
476 for i
in fetch_actions
:
477 if i
in config
.keys():
478 # Implicitely call fetch, break loop
485 umask
= config
['umask']
487 new_umask
= int(umask
, 8)
488 old_umask
= os
.umask(new_umask
)
489 except Exception, error
:
490 OUT
.die('Failed setting to umask "' + umask
+ '"!\nError was: '
493 for i
in self
.actions
:
495 OUT
.debug('Checking for action', 7)
497 if i
[0] in config
.keys():
498 result
+= i
[1](config
).run()
508 #===============================================================================
512 #-------------------------------------------------------------------------------
514 if __name__
== '__main__':
517 # Ignore warnings here. We are just testing
518 from warnings
import filterwarnings
, resetwarnings
519 filterwarnings('ignore')
521 doctest
.testmod(sys
.modules
[__name__
])