diff options
author | 2010-12-17 19:04:34 -0200 | |
---|---|---|
committer | 2010-12-17 19:04:34 -0200 | |
commit | 4834f0c4bab9f1b44a3540a4dd15a52799f764fd (patch) | |
tree | 2833c8d56709be9427eb24ccb98d70c473d361b4 | |
parent | added P/PN/PV to the svn description files, and added CAT (diff) | |
download | g-octave-4834f0c4bab9f1b44a3540a4dd15a52799f764fd.tar.gz g-octave-4834f0c4bab9f1b44a3540a4dd15a52799f764fd.tar.bz2 g-octave-4834f0c4bab9f1b44a3540a4dd15a52799f764fd.zip |
rewrote DescriptionTree, as a list of Description objects
-rw-r--r-- | g_octave/checksum.py | 10 | ||||
-rw-r--r-- | g_octave/description.py | 11 | ||||
-rw-r--r-- | g_octave/description_tree.py | 153 | ||||
-rw-r--r-- | g_octave/ebuild.py | 10 | ||||
-rw-r--r-- | tests/test_description.py | 17 | ||||
-rw-r--r-- | tests/test_description_tree.py | 54 |
6 files changed, 100 insertions, 155 deletions
diff --git a/g_octave/checksum.py b/g_octave/checksum.py index 1925da9..9954129 100644 --- a/g_octave/checksum.py +++ b/g_octave/checksum.py @@ -34,7 +34,7 @@ def sha1_compute(filename): def sha1_check(db, p): '''Checks if the SHA1 checksum of the package is OK.''' - description = db[p] + description = db.get(p) manifest = {} with open(os.path.join(config.db, 'manifest.json')) as fp: manifest = json.load(fp) @@ -44,9 +44,7 @@ def sha1_check(db, p): def sha1_check_db(db): '''Checks if the SHA1 checksums of the package database are OK.''' - for cat in db.pkg_list: - for pkg in db.pkg_list[cat]: - p = pkg['name']+'-'+pkg['version'] - if not sha1_check(db, p): - return False + for pkg in db: + if not sha1_check(db, pkg.P): + return False return True diff --git a/g_octave/description.py b/g_octave/description.py index bb9a867..a242592 100644 --- a/g_octave/description.py +++ b/g_octave/description.py @@ -20,7 +20,8 @@ __all__ = [ 'Description', 'SvnDescription', 're_depends', - 're_pkg_atom' + 're_pkg_atom', + 're_desc_file', ] import os @@ -50,7 +51,11 @@ conf = Config() re_depends = re.compile(r'^([a-zA-Z0-9-]+) *(\( *([><=]?=?) *([0-9.]+) *\))?') # we'll use atoms like 'control-1.0.11' for g-octave packages -re_pkg_atom = re.compile(r'^((.+)-([0-9.]+))\.DESCRIPTION$') +re_pkg_atom = re.compile(r'^(.+)-([0-9.]+)$') + +# pattern for DESCRIPTION filenames +re_desc_file = re.compile(r'^((.+)-([0-9.]+))\.DESCRIPTION$') + class Description(object): @@ -73,7 +78,7 @@ class Description(object): self._file = file self._info = Info(os.path.join(conf.db, 'info.json')) - my_atom = re_pkg_atom.match(os.path.basename(self._file)) + my_atom = re_desc_file.match(os.path.basename(self._file)) if my_atom is not None: self.P = my_atom.group(1) self.PN = my_atom.group(2) diff --git a/g_octave/description_tree.py b/g_octave/description_tree.py index 7b27b35..299ffe1 100644 --- a/g_octave/description_tree.py +++ b/g_octave/description_tree.py @@ -16,22 +16,23 @@ from __future__ import absolute_import __all__ = ['DescriptionTree'] +import glob import os import re +from .config import Config +from .description import Description +from .log import Log from portage.versions import vercmp -from .config import Config, ConfigException -from .description import * -from .exception import DescriptionTreeException -from .log import Log log = Log('g_octave.description_tree') - config = Config() + # from http://wiki.python.org/moin/HowTo/Sorting/ def cmp_to_key(mycmp): 'Convert a cmp= function into a key= function' + class K(object): def __init__(self, obj, *args): self.obj = obj @@ -47,132 +48,64 @@ def cmp_to_key(mycmp): return mycmp(self.obj, other.obj) >= 0 def __ne__(self, other): return mycmp(self.obj, other.obj) != 0 + return K -class DescriptionTree(object): + +class DescriptionTree(list): def __init__(self, parse_sysreq=True): - log.info('Parsing the package database.') - - self._parse_sysreq = parse_sysreq - self.pkg_list = {} - self._db_path = os.path.join(config.db, 'octave-forge') - - if not os.path.isdir(self._db_path): - log.error('Invalid db: %s' % self._db_path) - raise DescriptionTreeException('Invalid db: %s' % self._db_path) - - self.categories = {} - for cat in [i.strip() for i in config.categories.split(',')]: - catdir = os.path.join(self._db_path, cat) - if os.path.isdir(catdir): - self.pkg_list[cat] = [] - pkgs = os.listdir(catdir) - for pkg in pkgs: - pkgdir = os.path.join(catdir, pkg) - for desc_file in os.listdir(pkgdir): - pkg_p = desc_file[:-len('.DESCRIPTION')] - mypkg = re_pkg_atom.match(pkg_p) - if mypkg is None: - log.error('Invalid Atom: %s' % pkg_p) - raise DescriptionTreeException('Invalid Atom: %s' % pkg_p) - self.categories[mypkg.group(1)] = cat - self.pkg_list[cat].append({ - 'name': mypkg.group(1), - 'version': mypkg.group(2), - }) - - - def __getitem__(self, key): - - mykey = re_pkg_atom.match(key) - if mykey == None: - return None - - name = mykey.group(1) - version = mykey.group(2) - - for cat in self.pkg_list: - for pkg in self.pkg_list[cat]: - if pkg['name'] == name and pkg['version'] == version: - pkgfile = os.path.join( - self._db_path, - cat, - pkg['name'], - '%s-%s.DESCRIPTION' % (pkg['name'], pkg['version']), - ) - return Description(pkgfile, parse_sysreq=self._parse_sysreq) - - return None - + list.__init__(self) + self._categories = [i.strip() for i in config.categories.split(',')] + for my_file in glob.glob(os.path.join(config.db, 'octave-forge', \ + '**', '**', '*.DESCRIPTION')): + description = Description(my_file, parse_sysreq=parse_sysreq) + if description.CAT in self._categories: + self.append(description) - def package_versions(self, pkgname): - + def package_versions(self, pn): tmp = [] - - for cat in self.pkg_list: - for pkg in self.pkg_list[cat]: - if pkg['name'] == pkgname: - tmp.append(pkg['version']) - + for pkg in self: + if pkg.PN == pn: + tmp.append(pkg.PV) tmp.sort(key=cmp_to_key(vercmp)) return tmp - - def latest_version(self, pkgname): - - tmp = self.package_versions(pkgname) + def latest_version(self, pn): + tmp = self.package_versions(pn) return (len(tmp) > 0) and tmp[-1] or None - - def version_compare(self, versions): - - tmp = list(versions[:]) + def latest_version_from_list(self, pv_list): + tmp = pv_list[:] tmp.sort(key=cmp_to_key(vercmp)) return (len(tmp) > 0) and tmp[-1] or None - - - def packages(self): - - packages = [] - - for cat in self.pkg_list: - for pkg in self.pkg_list[cat]: - packages.append(pkg['name'] + '-' + pkg['version']) - - packages.sort() - return packages - def search(self, term): - # term can be a regular expression re_term = re.compile(r'%s' % term) packages = {} - - for cat in self.pkg_list: - for pkg in self.pkg_list[cat]: - if re_term.search(pkg['name']) is not None: - if pkg['name'] not in packages: - packages[pkg['name']] = [pkg['version'], '9999'] - else: - packages[pkg['name']].insert(-1, pkg['version']) - packages[pkg['name']].sort(key=cmp_to_key(vercmp)) - + for pkg in self: + if re_term.search(pkg.PN) is not None: + if pkg.PN not in packages: + packages[pkg.PN] = [] + packages[pkg.PN].append(pkg.PV) + packages[pkg.PN].sort(key=cmp_to_key(vercmp)) return packages def list(self): - packages = {} - - for cat in self.pkg_list: - packages[cat] = {} - for pkg in self.pkg_list[cat]: - if pkg['name'] not in packages[cat]: - packages[cat][pkg['name']] = [pkg['version'], '9999'] - else: - packages[cat][pkg['name']].insert(-1, pkg['version']) - packages[cat][pkg['name']].sort(key=cmp_to_key(vercmp)) - + for category in self._categories: + packages[category] = {} + for pkg in self: + if pkg.PN not in packages[pkg.CAT]: + packages[pkg.CAT][pkg.PN] = [] + packages[pkg.CAT][pkg.PN].append(pkg.PV) + packages[pkg.CAT][pkg.PN].sort(key=cmp_to_key(vercmp)) return packages + + def get(self, p): + for pkg in self: + if pkg.P == p: + return pkg + return None diff --git a/g_octave/ebuild.py b/g_octave/ebuild.py index 4906ce2..edacc2d 100644 --- a/g_octave/ebuild.py +++ b/g_octave/ebuild.py @@ -56,15 +56,13 @@ class Ebuild: self.pkgname = atom.group(1) self.version = atom.group(2) + self.__desc = self.__dbtree.get('%s-%s' % (self.pkgname, self.version)) if self.__scm: self.version = '9999' - category = self.__dbtree.categories.get(self.pkgname, None) - if category is not None: - self.__desc = SvnDescription(category, self.pkgname) + if self.__desc is not None: + self.__desc = SvnDescription(self.__desc.CAT, self.pkgname) else: raise EbuildException('Failed to find the octave-forge category of this package.') - else: - self.__desc = self.__dbtree['%s-%s' % (self.pkgname, self.version)] if self.__desc == None: raise EbuildException('Package not found: %s' % pkg_atom) @@ -160,7 +158,7 @@ RDEPEND="${DEPEND} if accept_keywords is None: accept_keywords = portage.settings['ACCEPT_KEYWORDS'] - category = self.__dbtree.categories.get(self.pkgname, '') + category = self.__desc.CAT vars = { 'eutils': '', diff --git a/tests/test_description.py b/tests/test_description.py index 980b807..75169c2 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -157,17 +157,27 @@ class TestDescription(testcase.TestCase): (match.group(1), match.group(3), match.group(4)), pkgtpl ) - + def test_re_pkg_atom(self): depends = [ + ('pkg-1', ('pkg', '1')), + ('pkg-1.0', ('pkg', '1.0')), + ('pkg-1.0.0', ('pkg', '1.0.0')), + ] + for pkgstr, pkgtpl in depends: + match = description.re_pkg_atom.match(pkgstr) + self.assertEqual((match.group(1), match.group(2)), pkgtpl) + + def test_re_desc_file(self): + depends = [ ('pkg-1.DESCRIPTION', ('pkg-1', 'pkg', '1')), ('pkg-1.0.DESCRIPTION', ('pkg-1.0', 'pkg', '1.0')), ('pkg-1.0.0.DESCRIPTION', ('pkg-1.0.0', 'pkg', '1.0.0')), ] for pkgstr, pkgtpl in depends: - match = description.re_pkg_atom.match(pkgstr) + match = description.re_desc_file.match(pkgstr) self.assertEqual((match.group(1), match.group(2), match.group(3)), pkgtpl) - + def test_attributes(self): # TODO: split this method to improve the error reporting # TODO: figure out how to test the comments @@ -203,6 +213,7 @@ def suite(): suite = unittest.TestSuite() suite.addTest(TestDescription('test_re_depends')) suite.addTest(TestDescription('test_re_pkg_atom')) + suite.addTest(TestDescription('test_re_desc_file')) suite.addTest(TestDescription('test_attributes')) return suite diff --git a/tests/test_description_tree.py b/tests/test_description_tree.py index 8e28553..ad00643 100644 --- a/tests/test_description_tree.py +++ b/tests/test_description_tree.py @@ -54,42 +54,42 @@ class TestDescriptionTree(testcase.TestCase): self._tree.latest_version(pkg) ) - def test_version_compare(self): + def test_latest_version_from_list(self): # TODO: cover a better range of versions versions = [ - # ((version1, version2), latest_version) - (('1', '2'), '2'), - (('0.1', '1'), '1'), - (('0.1', '0.2'), '0.2'), - (('0.0.1', '1'), '1'), - (('0.0.1', '0.1'), '0.1'), - (('0.0.1', '0.0.2'), '0.0.2'), - (('2', '1'), '2'), - (('1', '0.1'), '1'), - (('0.2', '0.1'), '0.2'), - (('1', '0.0.1'), '1'), - (('0.1', '0.0.1'), '0.1'), - (('0.0.2', '0.0.1'), '0.0.2'), + # ([version1, version2], latest_version) + (['1', '2'], '2'), + (['0.1', '1'], '1'), + (['0.1', '0.2'], '0.2'), + (['0.0.1', '1'], '1'), + (['0.0.1', '0.1'], '0.1'), + (['0.0.1', '0.0.2'], '0.0.2'), + (['2', '1'], '2'), + (['1', '0.1'], '1'), + (['0.2', '0.1'], '0.2'), + (['1', '0.0.1'], '1'), + (['0.1', '0.0.1'], '0.1'), + (['0.0.2', '0.0.1'], '0.0.2'), ] for ver, latest in versions: - self.assertEqual(self._tree.version_compare(ver), latest) + self.assertEqual(self._tree.latest_version_from_list(ver), latest) def test_description_files(self): packages = [ - ('main', 'main1', '0.0.1'), - ('main', 'main2', '0.0.1'), - ('main', 'main2', '0.0.2'), - ('extra', 'extra1', '0.0.1'), - ('extra', 'extra2', '0.0.1'), - ('extra', 'extra2', '0.0.2'), - ('language', 'language1', '0.0.1'), - ('language', 'language2', '0.0.1'), - ('language', 'language2', '0.0.2'), + 'main1-0.0.1', + 'main2-0.0.1', + 'main2-0.0.2', + 'extra1-0.0.1', + 'extra2-0.0.1', + 'extra2-0.0.2', + 'language1-0.0.1', + 'language2-0.0.1', + 'language2-0.0.2', ] - for cat, pkg, ver in packages: + for pkg in packages: self.assertTrue( isinstance( - self._tree[pkg+'-'+ver], + self._tree.get(pkg), description.Description ) ) @@ -99,6 +99,6 @@ def suite(): suite = unittest.TestSuite() suite.addTest(TestDescriptionTree('test_package_versions')) suite.addTest(TestDescriptionTree('test_latest_version')) - suite.addTest(TestDescriptionTree('test_version_compare')) + suite.addTest(TestDescriptionTree('test_latest_version_from_list')) suite.addTest(TestDescriptionTree('test_description_files')) return suite |