[yum-cvs] yum/yum __init__.py,1.182,1.183
Seth Vidal
skvidal at linux.duke.edu
Wed Feb 22 06:30:39 UTC 2006
Update of /home/groups/yum/cvs/yum/yum
In directory login1.linux.duke.edu:/tmp/cvs-serv18322/yum
Modified Files:
__init__.py
Log Message:
first stab at making the search function a generator.
In order to maintain API I made a searchGenerator() method which is used by
searchPackages to get the same result. searchGenerator should be much
lighter on memory for big searches and give you more immediate results.
Additionally, you don't need the search callback as results are yielded
immediately upon finding them.
need to port searchPackageProvides() to use a generator as well.
Index: __init__.py
===================================================================
RCS file: /home/groups/yum/cvs/yum/yum/__init__.py,v
retrieving revision 1.182
retrieving revision 1.183
diff -u -r1.182 -r1.183
--- __init__.py 1 Feb 2006 21:12:31 -0000 1.182
+++ __init__.py 22 Feb 2006 06:30:37 -0000 1.183
@@ -1090,19 +1090,15 @@
return results
- def searchPackages(self, fields, criteria, callback=None):
- """Search specified fields for matches to criteria
- optional callback specified to print out results
- as you go. Callback is a simple function of:
- callback(po, matched values list). It will
- just return a dict of dict[po]=matched values list"""
-
+ def searchGenerator(self, fields, criteria):
+ """Generator method to lighten memory load for some searches.
+ This is the preferred search function to use."""
self.doRepoSetup()
- self.doRpmDBSetup()
- matches = {}
+
+
for string in criteria:
restring = self._refineSearchPattern(string)
-
+ self.log(7, '%s into %s' % (string, restring))
try: crit_re = re.compile(restring, flags=re.I)
except sre_constants.error, e:
raise Errors.MiscError, \
@@ -1114,12 +1110,13 @@
value = po.returnSimple(field)
if value and crit_re.search(value):
tmpvalues.append(value)
+
if len(tmpvalues) > 0:
- if callback:
- callback(po, tmpvalues)
- matches[po] = tmpvalues
+ yield (po, tmpvalues)
# do the same for installed pkgs
+
+ self.doRpmDBSetup()
for hdr in self.rpmdb.getHdrList(): # this is more expensive so this is the top op
po = YumInstalledPackage(hdr)
tmpvalues = []
@@ -1137,10 +1134,27 @@
value = str(value)
if value and crit_re.search(value):
tmpvalues.append(value)
+
if len(tmpvalues) > 0:
- if callback:
- callback(po, tmpvalues)
- matches[po] = tmpvalues
+ yield (po, tmpvalues)
+
+ def searchPackages(self, fields, criteria, callback=None):
+ """Search specified fields for matches to criteria
+ optional callback specified to print out results
+ as you go. Callback is a simple function of:
+ callback(po, matched values list). It will
+ just return a dict of dict[po]=matched values list"""
+
+ matches = {}
+ match_gen = self.searchGenerator(fields, criteria)
+
+ for (po, matched_strings) in match_gen:
+ if callback:
+ callback(po, matched_strings)
+ if not matches.has_key(po):
+ matches[po] = []
+
+ matches[po].extend(matched_strings)
return matches
More information about the Yum-cvs-commits
mailing list