summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-05-18 19:57:12 -0500
committerDan McGee <dan@archlinux.org>2012-05-18 19:58:27 -0500
commit158be107e4ad682de0c9360658dfa5a72c21ee58 (patch)
tree5aa90a97e38ea9214c4223a20bd86624b36f59ef
parentf6c2bc6c33bf1fe4fe4cfff4c0177fd296c3b587 (diff)
downloadarchweb-158be107e4ad6.tar.gz
archweb-158be107e4ad6.zip
Add a get_best_satisfier method to RelatedToBase
This is basically what we do in PackageDepend already. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--packages/models.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/packages/models.py b/packages/models.py
index f57c9f3c..a4d2a556 100644
--- a/packages/models.py
+++ b/packages/models.py
@@ -4,7 +4,7 @@ from django.db import models
from django.db.models.signals import pre_save
from django.contrib.auth.models import User
-from main.models import Arch, Repo
+from main.models import Arch, Repo, Package
from main.utils import set_created_field
class PackageRelation(models.Model):
@@ -224,6 +224,40 @@ class RelatedToBase(models.Model):
name = models.CharField(max_length=255, db_index=True)
version = models.CharField(max_length=255, default='')
+ def get_best_satisfier(self):
+ '''Find a satisfier for this related package that best matches the
+ given criteria. It will not search provisions, but will find packages
+ named and matching repo characteristics if possible.'''
+ # NOTE: this is cribbed directly from the PackageDepend method of the
+ # same name. Really, all of these things could use the same method if
+ # the PackageDepend class was moved here and field names were changed
+ # to match the layout we use here.
+ pkgs = Package.objects.normal().filter(pkgname=self.name)
+ if not self.pkg.arch.agnostic:
+ # make sure we match architectures if possible
+ arches = self.pkg.applicable_arches()
+ pkgs = pkgs.filter(arch__in=arches)
+ if len(pkgs) == 0:
+ # couldn't find a package in the DB
+ # it should be a virtual depend (or a removed package)
+ return None
+ if len(pkgs) == 1:
+ return pkgs[0]
+ # more than one package, see if we can't shrink it down
+ # grab the first though in case we fail
+ pkg = pkgs[0]
+ # prevents yet more DB queries, these lists should be short;
+ # after each grab the best available in case we remove all entries
+ pkgs = [p for p in pkgs if p.repo.staging == self.pkg.repo.staging]
+ if len(pkgs) > 0:
+ pkg = pkgs[0]
+
+ pkgs = [p for p in pkgs if p.repo.testing == self.pkg.repo.testing]
+ if len(pkgs) > 0:
+ pkg = pkgs[0]
+
+ return pkg
+
def __unicode__(self):
if self.version:
return u'%s%s%s' % (self.name, self.comparison, self.version)