From 60cb59d71dd75b9ae476a9c6b6363ca1ff77375e Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Wed, 7 Mar 2007 06:38:05 +0000 Subject: Part of my attempt now involves cleaning up config; this means for me; removing the file-based stuff from it (config_path) and encapsulating that into classes. This is the first one, a simple PackageKeywords class that does file-based stuff, no recursion yet but soon. Trying to do TDD here as well,so tests first then code. svn path=/main/trunk/; revision=6190 --- pym/portage/env/__init__.py | 4 ++ pym/portage/env/config.py | 60 ++++++++++++++++++++++ pym/portage/tests/__init__.py | 14 +++-- pym/portage/tests/dep/test_isvalidatom.py | 5 +- pym/portage/tests/env/__init__.py | 5 ++ pym/portage/tests/env/config/__init__.py | 5 ++ .../tests/env/config/test_PackageKeywordsFile.py | 35 +++++++++++++ 7 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 pym/portage/env/__init__.py create mode 100644 pym/portage/env/config.py create mode 100644 pym/portage/tests/env/__init__.py create mode 100644 pym/portage/tests/env/config/__init__.py create mode 100644 pym/portage/tests/env/config/test_PackageKeywordsFile.py (limited to 'pym') diff --git a/pym/portage/env/__init__.py b/pym/portage/env/__init__.py new file mode 100644 index 000000000..63f6f9ef2 --- /dev/null +++ b/pym/portage/env/__init__.py @@ -0,0 +1,4 @@ +# Copyright: 2007 Gentoo Foundation +# License: GPL2 +# $Id$ + diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py new file mode 100644 index 000000000..05bb49274 --- /dev/null +++ b/pym/portage/env/config.py @@ -0,0 +1,60 @@ +# config.py -- Portage Config +# Copyright 2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + +import os +from UserDict import UserDict + +class PackageKeywords(UserDict): + """ + A base class stub for things to inherit from; some people may want a database based package.keywords or something + + Internally dict has pairs of the form + {'cpv':['key1','key2','key3'...] + """ + + data = {} + + def iteritems(self): + return self.data.iteritems() + + def keys(self): + return self.data.keys() + + def __contains__(self, other): + return other in self.data + + def __hash__( self ): + return self.data.__hash__() + +class PackageKeywordsFile(PackageKeywords): + """ + Inherits from PackageKeywords; implements a file-based backend. Doesn't handle recursion yet. + """ + def __init__( self, filename ): + self.fname = filename + + def load(self, recursive=False): + """ + Package.keywords files have comments that begin with #. + The entries are of the form: + >>> cpv [-~]keyword1 [-~]keyword2 keyword3 + >>> Exceptions include -*, ~*, and ** for keywords. + """ + + if os.path.exists( self.fname ): + f = open(self.fname, 'rb') + for line in f: + if line.startswith('#'): + continue + split = line.split() + if len(split): + # Surprisingly this works for iterables of length 1 + # fex ['sys-apps/portage','x86','amd64'] becomes {'sys-apps/portage':['x86','amd64']} + key, items = split[0],split[1:] + # if they specify the same cpv twice; stack the values (append) instead of overwriting. + if key in self.data: + self.data[key].append(items) + else: + self.data[key] = items diff --git a/pym/portage/tests/__init__.py b/pym/portage/tests/__init__.py index 159e67b63..7d13af1e6 100644 --- a/pym/portage/tests/__init__.py +++ b/pym/portage/tests/__init__.py @@ -7,7 +7,7 @@ import os, unittest, time import portage.tests def main(): - testDirs = ["util","versions", "dep", "xpak"] + testDirs = ["util","versions", "dep", "xpak", "env/config"] suite = unittest.TestSuite() basedir = os.path.dirname(__file__) for mydir in testDirs: @@ -37,13 +37,10 @@ def getTests( path, base_path ): parent_module = parent_module.replace('/','.') result = [] for mymodule in files: - try: - # Make the trailing / a . for module importing - modname = ".".join((parent_module, mymodule)) - mod = my_import(modname) - result.append( unittest.TestLoader().loadTestsFromModule(mod) ) - except ImportError: - raise + # Make the trailing / a . for module importing + modname = ".".join((parent_module, mymodule)) + mod = my_import(modname) + result.append( unittest.TestLoader().loadTestsFromModule(mod) ) return result class TextTestResult(unittest._TextTestResult): @@ -87,6 +84,7 @@ class TestCase(unittest.TestCase): # _testMethodName. self._testMethodName = methodName unittest.TestCase.__init__(self, methodName) + self.todo = False def defaultTestResult(self): return TextTestResult() diff --git a/pym/portage/tests/dep/test_isvalidatom.py b/pym/portage/tests/dep/test_isvalidatom.py index 35f7160b7..512d9b465 100644 --- a/pym/portage/tests/dep/test_isvalidatom.py +++ b/pym/portage/tests/dep/test_isvalidatom.py @@ -12,10 +12,9 @@ class IsValidAtom(TestCase): """ A simple testcase for isvalidatom """ - todo = True - def testIsValidAtom(self): - + + self.todo = True tests = [ ( "sys-apps/portage", True ), ( "=sys-apps/portage-2.1", True ), ( "=sys-apps/portage-2.1*", True ), diff --git a/pym/portage/tests/env/__init__.py b/pym/portage/tests/env/__init__.py new file mode 100644 index 000000000..50e38af99 --- /dev/null +++ b/pym/portage/tests/env/__init__.py @@ -0,0 +1,5 @@ +# tests/portage/env/__init__.py -- Portage Unit Test functionality +# Copyright 2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + diff --git a/pym/portage/tests/env/config/__init__.py b/pym/portage/tests/env/config/__init__.py new file mode 100644 index 000000000..7fc0d0e96 --- /dev/null +++ b/pym/portage/tests/env/config/__init__.py @@ -0,0 +1,5 @@ +# tests/portage/env/config/__init__.py -- Portage Unit Test functionality +# Copyright 2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id$ + diff --git a/pym/portage/tests/env/config/test_PackageKeywordsFile.py b/pym/portage/tests/env/config/test_PackageKeywordsFile.py new file mode 100644 index 000000000..f39ef96d7 --- /dev/null +++ b/pym/portage/tests/env/config/test_PackageKeywordsFile.py @@ -0,0 +1,35 @@ +# test_PackageKeywordsFile.py -- Portage Unit Testing Functionality +# Copyright 2006 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Id: test_PackageKeywordsFile.py 6182 2007-03-06 07:35:22Z antarus $ + +from portage.tests import TestCase +from portage.env.config import PackageKeywordsFile + +class PackageKeywordsFileTestCase(TestCase): + + fname = 'package.keywords' + cpv = 'sys-apps/portage' + keywords = ['~x86', 'amd64', '-mips'] + + def testPackageKeywordsLoad(self): + """ + A simple test to ensure the load works properly + """ + + self.BuildFile() + f = PackageKeywordsFile(self.fname) + f.load() + for cpv, keyword in f.iteritems(): + self.assertEqual( cpv, self.cpv ) + [k for k in keyword if self.assertTrue(k in self.keywords)] + self.NukeFile() + + def BuildFile(self): + f = open(self.fname, 'wb') + f.write('%s %s\n' % (self.cpv, ' '.join(self.keywords))) + f.close() + + def NukeFile(self): + import os + os.unlink(self.fname) -- cgit v1.2.3-1-g7c22