From e321b1c0abca64e2e3615a9e1a321b47a6adbbed Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Thu, 28 Jan 2010 01:10:45 +0100 Subject: Rename layman.overlay.Overlays to layman.dbbase.DbBase --- layman/db.py | 16 ++-- layman/dbbase.py | 233 +++++++++++++++++++++++++++++++++++++++++++++++ layman/overlay.py | 233 ----------------------------------------------- layman/tests/dtest.py | 4 +- layman/tests/external.py | 8 +- 5 files changed, 247 insertions(+), 247 deletions(-) create mode 100644 layman/dbbase.py delete mode 100644 layman/overlay.py diff --git a/layman/db.py b/layman/db.py index e0763b3..ceb89f1 100644 --- a/layman/db.py +++ b/layman/db.py @@ -27,7 +27,7 @@ __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" import os, codecs, os.path, urllib2, re, hashlib from layman.utils import path, delete_empty_directory -from layman.overlay import Overlays, UnknownOverlayException +from layman.dbbase import DbBase, UnknownOverlayException from layman.debug import OUT @@ -37,7 +37,7 @@ from layman.debug import OUT # #------------------------------------------------------------------------------- -class DB(Overlays): +class DB(DbBase): ''' Handle the list of local overlays.''' def __init__(self, config): @@ -53,7 +53,7 @@ class DB(Overlays): quiet = int(config['quietness']) < 3 - Overlays.__init__(self, + DbBase.__init__(self, [config['local_list'], ], config, ignore, @@ -90,7 +90,7 @@ class DB(Overlays): # >>> b.add(a.select('wrobel-stable')) #doctest: +ELLIPSIS # * Running command "/usr/bin/rsync -rlptDvz --progress --delete --delete-after --timeout=180 --exclude="distfiles/*" --exclude="local/*" --exclude="packages/*" "rsync://gunnarwrobel.de/wrobel-stable/*" "/tmp/file.../wrobel-stable""... - # >>> c = Overlays([write, ], dict()) + # >>> c = DbBase([write, ], dict()) # >>> c.overlays.keys() # [u'wrobel-stable'] @@ -162,12 +162,12 @@ class DB(Overlays): # * Running command "/usr/bin/rsync -rlptDvz --progress --delete --delete-after --timeout=180 --exclude="distfiles/*" --exclude="local/*" --exclude="packages/*" "rsync://gunnarwrobel.de/wrobel-stable/*" "/tmp/file.../wrobel-stable""... # >>> b.add(a.select('wrobel')) #doctest: +ELLIPSIS # * Running command "/usr/bin/svn co "https://overlays.gentoo.org/svn/dev/wrobel/" "/tmp/file.../wrobel""... - # >>> c = Overlays([write, ], dict()) + # >>> c = DbBase([write, ], dict()) # >>> c.overlays.keys() # [u'wrobel', u'wrobel-stable'] # >>> b.delete(b.select('wrobel')) - # >>> c = Overlays([write, ], dict()) + # >>> c = DbBase([write, ], dict()) # >>> c.overlays.keys() # [u'wrobel-stable'] @@ -206,7 +206,7 @@ class DB(Overlays): # #------------------------------------------------------------------------------- -class RemoteDB(Overlays): +class RemoteDB(DbBase): '''Handles fetching the remote overlay list.''' def __init__(self, config): @@ -236,7 +236,7 @@ class RemoteDB(Overlays): quiet = int(config['quietness']) < 3 - Overlays.__init__(self, paths, config, ignore, quiet) + DbBase.__init__(self, paths, config, ignore, quiet) def cache(self): ''' diff --git a/layman/dbbase.py b/layman/dbbase.py new file mode 100644 index 0000000..c5cd8a5 --- /dev/null +++ b/layman/dbbase.py @@ -0,0 +1,233 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +################################################################################# +# LAYMAN OVERLAY HANDLER +################################################################################# +# File: overlay.py +# +# Access to an xml list of overlays +# +# Copyright: +# (c) 2005 - 2009 Gunnar Wrobel +# (c) 2009 Sebastian Pipping +# (c) 2009 Christian Groschupp +# Distributed under the terms of the GNU General Public License v2 +# +# Author(s): +# Gunnar Wrobel +# Sebastian Pipping +# Christian Groschupp +# +'''Main handler for overlays.''' + +__version__ = "$Id: overlay.py 273 2006-12-30 15:54:50Z wrobel $" + +#=============================================================================== +# +# Dependencies +# +#------------------------------------------------------------------------------- + +import sys, codecs, os, os.path +import xml.etree.ElementTree as ET # Python 2.5 + +from layman.debug import OUT +from layman.utils import indent +from layman.overlays.overlay import Overlay + +#=============================================================================== +# +# Class UnknownOverlayException +# +#------------------------------------------------------------------------------- + +class UnknownOverlayException(Exception): + def __init__(self, repo_name): + message = 'Overlay "%s" does not exist.' % repo_name + super(UnknownOverlayException, self).__init__(message) + + +#=============================================================================== +# +# Class DbBase +# +#------------------------------------------------------------------------------- + +class DbBase: + ''' Handle a list of overlays.''' + + def __init__(self, paths, config, ignore = 0, quiet = False): + + self.config = config + self.quiet = quiet + self.paths = paths + self.ignore = ignore + + self.overlays = {} + + OUT.debug('Initializing overlay list handler', 8) + + for path in self.paths: + if os.path.exists(path): + self.read_file(path) + + def __eq__(self, other): + for key in set(self.overlays.keys() + other.overlays.keys()): + if self.overlays[key] != other.overlays[key]: + return False + return True + + def __ne__(self, other): + return not self.__eq__(other) + + def read_file(self, path): + '''Read the overlay definition file.''' + + try: + document = open(path, 'r').read() + + except Exception, error: + raise IOError('Failed to read the overlay list at ("' + + path + '")!\nError was:\n' + str(error)) + + self.read(document) + + def read(self, text): + ''' + Read an xml list of overlays. + + >>> here = os.path.dirname(os.path.realpath(__file__)) + >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} + >>> a = DbBase([here + '/tests/testfiles/global-overlays.xml', ], config) + >>> a.overlays.keys() + [u'wrobel', u'wrobel-stable'] + + >>> list(a.overlays['wrobel-stable'].source_uris()) + [u'rsync://gunnarwrobel.de/wrobel-stable'] + ''' + document = ET.fromstring(text) + overlays = document.findall('overlay') + \ + document.findall('repo') + + for overlay in overlays: + OUT.debug('Parsing overlay entry', 8) + try: + ovl = Overlay(overlay, self.config, self.ignore, self.quiet) + self.overlays[ovl.name] = ovl + except Exception, error: + OUT.warn(str(error), 3) + + + def write(self, path): + ''' + Write the list of overlays to a file. + + >>> write = os.tmpnam() + >>> here = os.path.dirname(os.path.realpath(__file__)) + >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} + >>> a = DbBase([here + '/tests/testfiles/global-overlays.xml', ], config) + >>> b = DbBase([write,], dict()) + >>> b.overlays['wrobel-stable'] = a.overlays['wrobel-stable'] + >>> b.write(write) + >>> c = DbBase([write,], dict()) + >>> c.overlays.keys() + [u'wrobel-stable'] + + >>> os.unlink(write) + ''' + + xml = ET.Element('repositories', version="1.0") + xml[:] = [e.to_xml() for e in self.overlays.values()] + indent(xml) + tree = ET.ElementTree(xml) + try: + f = open(path, 'w') + f.write("""\ + +""") + tree.write(f, encoding='utf-8') + f.close() + except Exception, error: + raise Exception('Failed to write to local overlays file: ' + + path + '\nError was:\n' + str(error)) + + def select(self, overlay): + ''' + Select an overlay from the list. + + >>> here = os.path.dirname(os.path.realpath(__file__)) + >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} + >>> a = DbBase([here + '/tests/testfiles/global-overlays.xml', ], config) + >>> list(a.select('wrobel-stable').source_uris()) + [u'rsync://gunnarwrobel.de/wrobel-stable'] + ''' + if not overlay in self.overlays.keys(): + raise UnknownOverlayException(overlay) + return self.overlays[overlay] + + def list(self, verbose = False, width = 0): + ''' + List all overlays. + + >>> here = os.path.dirname(os.path.realpath(__file__)) + >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} + >>> a = DbBase([here + '/tests/testfiles/global-overlays.xml', ], config) + >>> for i in a.list(True): + ... print i[0] + wrobel + ~~~~~~ + Source : https://overlays.gentoo.org/svn/dev/wrobel + Contact : nobody@gentoo.org + Type : Subversion; Priority: 10 + Quality : experimental + + Description: + Test + + wrobel-stable + ~~~~~~~~~~~~~ + Source : rsync://gunnarwrobel.de/wrobel-stable + Contact : nobody@gentoo.org + Type : Rsync; Priority: 50 + Quality : experimental + + Description: + A collection of ebuilds from Gunnar Wrobel [wrobel@gentoo.org]. + + + >>> for i in a.list(False, 80): + ... print i[0] + wrobel [Subversion] (https://o.g.o/svn/dev/wrobel ) + wrobel-stable [Rsync ] (rsync://gunnarwrobel.de/wrobel-stable) + ''' + result = [] + + for name, overlay in self.overlays.items(): + + if verbose: + result.append((str(overlay), overlay.is_supported(), + overlay.is_official())) + else: + result.append((overlay.short_list(width), overlay.is_supported(), + overlay.is_official())) + + result = sorted(result) + + return result + +#=============================================================================== +# +# Testing +# +#------------------------------------------------------------------------------- + +if __name__ == '__main__': + import doctest, sys + + # Ignore warnings here. We are just testing + from warnings import filterwarnings, resetwarnings + filterwarnings('ignore') + + doctest.testmod(sys.modules[__name__]) + + resetwarnings() diff --git a/layman/overlay.py b/layman/overlay.py deleted file mode 100644 index 21bbc70..0000000 --- a/layman/overlay.py +++ /dev/null @@ -1,233 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -################################################################################# -# LAYMAN OVERLAY HANDLER -################################################################################# -# File: overlay.py -# -# Access to an xml list of overlays -# -# Copyright: -# (c) 2005 - 2009 Gunnar Wrobel -# (c) 2009 Sebastian Pipping -# (c) 2009 Christian Groschupp -# Distributed under the terms of the GNU General Public License v2 -# -# Author(s): -# Gunnar Wrobel -# Sebastian Pipping -# Christian Groschupp -# -'''Main handler for overlays.''' - -__version__ = "$Id: overlay.py 273 2006-12-30 15:54:50Z wrobel $" - -#=============================================================================== -# -# Dependencies -# -#------------------------------------------------------------------------------- - -import sys, codecs, os, os.path -import xml.etree.ElementTree as ET # Python 2.5 - -from layman.debug import OUT -from layman.utils import indent -from layman.overlays.overlay import Overlay - -#=============================================================================== -# -# Class UnknownOverlayException -# -#------------------------------------------------------------------------------- - -class UnknownOverlayException(Exception): - def __init__(self, repo_name): - message = 'Overlay "%s" does not exist.' % repo_name - super(UnknownOverlayException, self).__init__(message) - - -#=============================================================================== -# -# Class Overlays -# -#------------------------------------------------------------------------------- - -class Overlays: - ''' Handle a list of overlays.''' - - def __init__(self, paths, config, ignore = 0, quiet = False): - - self.config = config - self.quiet = quiet - self.paths = paths - self.ignore = ignore - - self.overlays = {} - - OUT.debug('Initializing overlay list handler', 8) - - for path in self.paths: - if os.path.exists(path): - self.read_file(path) - - def __eq__(self, other): - for key in set(self.overlays.keys() + other.overlays.keys()): - if self.overlays[key] != other.overlays[key]: - return False - return True - - def __ne__(self, other): - return not self.__eq__(other) - - def read_file(self, path): - '''Read the overlay definition file.''' - - try: - document = open(path, 'r').read() - - except Exception, error: - raise IOError('Failed to read the overlay list at ("' - + path + '")!\nError was:\n' + str(error)) - - self.read(document) - - def read(self, text): - ''' - Read an xml list of overlays. - - >>> here = os.path.dirname(os.path.realpath(__file__)) - >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} - >>> a = Overlays([here + '/tests/testfiles/global-overlays.xml', ], config) - >>> a.overlays.keys() - [u'wrobel', u'wrobel-stable'] - - >>> list(a.overlays['wrobel-stable'].source_uris()) - [u'rsync://gunnarwrobel.de/wrobel-stable'] - ''' - document = ET.fromstring(text) - overlays = document.findall('overlay') + \ - document.findall('repo') - - for overlay in overlays: - OUT.debug('Parsing overlay entry', 8) - try: - ovl = Overlay(overlay, self.config, self.ignore, self.quiet) - self.overlays[ovl.name] = ovl - except Exception, error: - OUT.warn(str(error), 3) - - - def write(self, path): - ''' - Write the list of overlays to a file. - - >>> write = os.tmpnam() - >>> here = os.path.dirname(os.path.realpath(__file__)) - >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} - >>> a = Overlays([here + '/tests/testfiles/global-overlays.xml', ], config) - >>> b = Overlays([write,], dict()) - >>> b.overlays['wrobel-stable'] = a.overlays['wrobel-stable'] - >>> b.write(write) - >>> c = Overlays([write,], dict()) - >>> c.overlays.keys() - [u'wrobel-stable'] - - >>> os.unlink(write) - ''' - - xml = ET.Element('repositories', version="1.0") - xml[:] = [e.to_xml() for e in self.overlays.values()] - indent(xml) - tree = ET.ElementTree(xml) - try: - f = open(path, 'w') - f.write("""\ - -""") - tree.write(f, encoding='utf-8') - f.close() - except Exception, error: - raise Exception('Failed to write to local overlays file: ' - + path + '\nError was:\n' + str(error)) - - def select(self, overlay): - ''' - Select an overlay from the list. - - >>> here = os.path.dirname(os.path.realpath(__file__)) - >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} - >>> a = Overlays([here + '/tests/testfiles/global-overlays.xml', ], config) - >>> list(a.select('wrobel-stable').source_uris()) - [u'rsync://gunnarwrobel.de/wrobel-stable'] - ''' - if not overlay in self.overlays.keys(): - raise UnknownOverlayException(overlay) - return self.overlays[overlay] - - def list(self, verbose = False, width = 0): - ''' - List all overlays. - - >>> here = os.path.dirname(os.path.realpath(__file__)) - >>> config = {'svn_command': '/usr/bin/svn', 'rsync_command':'/usr/bin/rsync'} - >>> a = Overlays([here + '/tests/testfiles/global-overlays.xml', ], config) - >>> for i in a.list(True): - ... print i[0] - wrobel - ~~~~~~ - Source : https://overlays.gentoo.org/svn/dev/wrobel - Contact : nobody@gentoo.org - Type : Subversion; Priority: 10 - Quality : experimental - - Description: - Test - - wrobel-stable - ~~~~~~~~~~~~~ - Source : rsync://gunnarwrobel.de/wrobel-stable - Contact : nobody@gentoo.org - Type : Rsync; Priority: 50 - Quality : experimental - - Description: - A collection of ebuilds from Gunnar Wrobel [wrobel@gentoo.org]. - - - >>> for i in a.list(False, 80): - ... print i[0] - wrobel [Subversion] (https://o.g.o/svn/dev/wrobel ) - wrobel-stable [Rsync ] (rsync://gunnarwrobel.de/wrobel-stable) - ''' - result = [] - - for name, overlay in self.overlays.items(): - - if verbose: - result.append((str(overlay), overlay.is_supported(), - overlay.is_official())) - else: - result.append((overlay.short_list(width), overlay.is_supported(), - overlay.is_official())) - - result = sorted(result) - - return result - -#=============================================================================== -# -# Testing -# -#------------------------------------------------------------------------------- - -if __name__ == '__main__': - import doctest, sys - - # Ignore warnings here. We are just testing - from warnings import filterwarnings, resetwarnings - filterwarnings('ignore') - - doctest.testmod(sys.modules[__name__]) - - resetwarnings() diff --git a/layman/tests/dtest.py b/layman/tests/dtest.py index ed5cac1..2f5ce77 100644 --- a/layman/tests/dtest.py +++ b/layman/tests/dtest.py @@ -52,7 +52,7 @@ import unittest, doctest, sys import layman.action #CT import layman.config #CT import layman.db #CT -import layman.overlay #CT +import layman.dbbase #CT import layman.utils #CT import layman.overlays.overlay #CT import layman.overlays.tar #CT @@ -68,7 +68,7 @@ def test_suite(): doctest.DocTestSuite(layman.action), doctest.DocTestSuite(layman.config), doctest.DocTestSuite(layman.db), - doctest.DocTestSuite(layman.overlay), + doctest.DocTestSuite(layman.dbbase), doctest.DocTestSuite(layman.utils), doctest.DocTestSuite(layman.overlays.overlay), doctest.DocTestSuite(layman.overlays.tar), diff --git a/layman/tests/external.py b/layman/tests/external.py index 9b336ee..28654ff 100644 --- a/layman/tests/external.py +++ b/layman/tests/external.py @@ -17,7 +17,7 @@ import unittest import os -from layman.overlay import Overlays +from layman.dbbase import DbBase from warnings import filterwarnings, resetwarnings HERE = os.path.dirname(os.path.realpath(__file__)) @@ -27,7 +27,7 @@ class Unicode(unittest.TestCase): def _overlays_bug(self, number): config = {} filename = os.path.join(HERE, 'testfiles', 'overlays_bug_%d.xml' % number) - o = Overlays([filename], config) + o = DbBase([filename], config) for verbose in (True, False): for t in o.list(verbose=verbose): print t[0] @@ -47,10 +47,10 @@ class FormatSubpathCategory(unittest.TestCase): 'subpath-%d.xml' % number) # Read, write, re-read, compare - os1 = Overlays([filename1], config) + os1 = DbBase([filename1], config) filename2 = os.tmpnam() os1.write(filename2) - os2 = Overlays([filename2], config) + os2 = DbBase([filename2], config) os.unlink(filename2) self.assertTrue(os1 == os2) -- cgit v1.2.3-1-g7c22