summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Hopkins <marduk@gentoo.org>2005-10-05 22:29:01 +0000
committerAlbert Hopkins <marduk@gentoo.org>2005-10-05 22:29:01 +0000
commit937ac99cee0555c6c417f3323ea22e77f511130e (patch)
tree661380b385136fd5dccb7ddd51b57889c975c3ed
parentRemoved mstring.py as Python's re.sub does the trick (diff)
downloadgentoo-937ac99cee0555c6c417f3323ea22e77f511130e.tar.gz
gentoo-937ac99cee0555c6c417f3323ea22e77f511130e.tar.bz2
gentoo-937ac99cee0555c6c417f3323ea22e77f511130e.zip
Caching of Ebuild objects. When creating a __new__ Ebuild, if one already
exists in the cache with the same cpv, return the cached one instead of creating a new one. This will ensure that Ebuilds objects created with the same category/package/version are indeed the same object (and also saves on memory). This is the first time I've actually had a use for Python's new-style classes or the weakref module.
-rw-r--r--src/packages/portage.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/packages/portage.py b/src/packages/portage.py
index 0082d13355..3290a6f134 100644
--- a/src/packages/portage.py
+++ b/src/packages/portage.py
@@ -11,11 +11,12 @@
#
"""Portage Objects"""
-__revision__ = "$Revision: 1.1.2.3 $"
+__revision__ = "$Revision: 1.1.2.4 $"
# $Source: /var/cvsroot/gentoo/src/packages/Attic/portage.py,v $
import sys
import os
+from weakref import WeakValueDictionary
# We need to "fake" as repoman so portage will ignore local system settings
os.environ['PORTAGE_CALLER'] = 'repoman'
@@ -168,7 +169,7 @@ class PackageFactory:
self.cache[key] = packages
return packages
-class Ebuild:
+class Ebuild(object):
"""Classic ebuild
To create a Ebuild object, we expect:
@@ -184,6 +185,17 @@ class Ebuild:
package_datetime (optional)
"""
+ __cache = WeakValueDictionary()
+ def __new__(cls, **kwargs):
+ # We want to share cpv's, so we cache them
+ cpv = '%(category)s/%(name)s-%(version)s' % kwargs
+ if cls.__cache.has_key(cpv):
+ return cls.__cache[cpv]
+ else:
+ new_Ebuild = object.__new__(cls, **kwargs)
+ cls.__cache[cpv] = new_Ebuild
+ return new_Ebuild
+
def __init__(self, **kwargs):
self.category = kwargs['category']
self.name = kwargs['name']