add PackageMask{File} classes, change unit tests to use try/finally, makes sure the...
authorAlec Warner <antarus@gentoo.org>
Thu, 8 Mar 2007 07:10:40 +0000 (07:10 -0000)
committerAlec Warner <antarus@gentoo.org>
Thu, 8 Mar 2007 07:10:40 +0000 (07:10 -0000)
svn path=/main/trunk/; revision=6193

pym/portage/env/config.py
pym/portage/tests/env/config/test_PackageKeywordsFile.py
pym/portage/tests/env/config/test_PackageMaskFile.py [new file with mode: 0644]
pym/portage/tests/env/config/test_PackageUseFile.py

index eccd35e084298f10c498525181142cc70d4021d2..e3898c53967a0c185aa3ea9513d684c226adf64b 100644 (file)
@@ -112,3 +112,58 @@ class PackageUseFile(PackageUse):
                                                self.data[key].append(items)
                                        else:
                                                self.data[key] = items
+
+class PackageMask(UserDIct):
+       """
+       A base class for Package.mask functionality
+       """
+       data = {}
+       
+       def iteritems(self):
+               return self.data.iteritems()
+       
+       def __hash__(self):
+               return hash(self.data)
+       
+       def __contains__(self, other):
+               return other in self.data
+       
+       def keys(self):
+               return self.data.keys()
+       
+       def iterkeys(self):
+               return self.data.iterkeys()
+
+class PackageMaskFile(PackageMask):
+       """
+       A class that implements a file-based package.mask
+       
+       Entires in package.mask are of the form:
+       atom1
+       atom2
+       or optionally
+       -atom3
+       to revert a previous mask; this only works when masking files are stacked
+       """
+       
+       def __init__(self, filename):
+               self.fname = filename
+       
+       def load(self, recursive):
+               """
+               Package.keywords files have comments that begin with #.
+               The entries are of the form:
+               >>> atom useflag1 useflag2 useflag3..
+               useflags may optionally be negative with a minus sign (-)
+               >>> atom useflag1 -useflag2 useflag3
+               """
+               
+               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):
+                                       atom = split[0] # This is an extra assignment, but I think it makes the code more explicit in what goes into the dict
+                                       self.data[atom] = None # we only care about keys in the dict, basically abusing it as a list
index 6c5475881fbdb2a76ff34873c79371da559048db..1d72504b90e7886493fd15a5aae0f18f5999ee95 100644 (file)
@@ -18,12 +18,14 @@ class PackageKeywordsFileTestCase(TestCase):
                """
 
                self.BuildFile()
-               f = PackageKeywordsFile(self.fname)
-               f.load(recursive=False)
-               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()
+               try:
+                       f = PackageKeywordsFile(self.fname)
+                       f.load(recursive=False)
+                       for cpv, keyword in f.iteritems():
+                               self.assertEqual( cpv, self.cpv )
+                               [k for k in keyword if self.assertTrue(k in self.keywords)]
+               finally;
+                       self.NukeFile()
        
        def BuildFile(self):
                f = open(self.fname, 'wb')
diff --git a/pym/portage/tests/env/config/test_PackageMaskFile.py b/pym/portage/tests/env/config/test_PackageMaskFile.py
new file mode 100644 (file)
index 0000000..f17eaf0
--- /dev/null
@@ -0,0 +1,30 @@
+# test_PackageMaskFile.py -- Portage Unit Testing Functionality
+# Copyright 2007 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id: test_PackageMaskFile.py 6182 2007-03-06 07:35:22Z antarus $
+
+from portage.env.config import PackageMaskFile
+from portage.tests import TestCase
+
+class PackageMaskFileTestCase(TestCase):
+
+       fname = 'package.mask'
+       atoms = ['sys-apps/portage','dev-util/diffball','not@va1id@t0m']
+       
+       def testPackageMaskLoad(self):
+               self.BuildFile()
+               try:
+                       f = PackageMaskFile(self.fname)
+                       f.load(recursive=False)
+                       [atom for atom in f.iterkeys() if self.assertTrue(atom in self.atoms)]
+               finally:
+                       self.NukeFile()
+       
+       def BuildFile(self):
+               f = open(self.fname, 'wb')
+               f.write("\n".join(atoms))
+               f.close()
+       
+       def NukeFile(self):
+               import os
+               os.unlink(self.fname)
index 6dd76eeca6ea4f06988c345eecfbffa547fd7906..4638923df7baa6e8eee2ea2f5bd0715d6a939cff 100644 (file)
@@ -17,12 +17,14 @@ class PackageUseFileTestCase(TestCase):
                A simple test to ensure the load works properly
                """
                self.BuildFile()
-               f = PackageUseFile(self.fname)
-               f.load(recursive=False)
-               for cpv, use in f.iteritems():
-                       self.assertEqual( cpv, self.cpv )
-                       [flag for flag in use if self.assertTrue(flag in self.useflags)]
-               self.NukeFile()
+               try:
+                       f = PackageUseFile(self.fname)
+                       f.load(recursive=False)
+                       for cpv, use in f.iteritems():
+                               self.assertEqual( cpv, self.cpv )
+                               [flag for flag in use if self.assertTrue(flag in self.useflags)]
+               finally:
+                       self.NukeFile()
 
        def BuildFile(self):
                f = open(self.fname, 'wb')