From 52fc4a5df42a86231d43b95dcc9b4bfc835f87a9 Mon Sep 17 00:00:00 2001 From: volpino Date: Sat, 5 May 2012 11:48:12 +0200 Subject: [PATCH 1/2] VersionLog manager --- euscanwww/djeuscan/api/handlers.py | 2 +- euscanwww/djeuscan/feeds.py | 11 ++++------- euscanwww/djeuscan/managers.py | 29 +++++++++++++++++++++++++++++ euscanwww/djeuscan/models.py | 4 +++- euscanwww/djeuscan/views.py | 2 +- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/euscanwww/djeuscan/api/handlers.py b/euscanwww/djeuscan/api/handlers.py index 7c7cb82..18e1aca 100644 --- a/euscanwww/djeuscan/api/handlers.py +++ b/euscanwww/djeuscan/api/handlers.py @@ -127,7 +127,7 @@ class PackageHandler(AnonymousBaseHandler): order_by('-datetime')[:1] log = log[0] if log else None - vlog = VersionLog.objects.filter(package=package).order_by('-id') + vlog = VersionLog.objects.for_package(package, order=True) herds = [] for herd in package.herds.all(): diff --git a/euscanwww/djeuscan/feeds.py b/euscanwww/djeuscan/feeds.py index f3c58d1..e8045b1 100644 --- a/euscanwww/djeuscan/feeds.py +++ b/euscanwww/djeuscan/feeds.py @@ -77,7 +77,7 @@ class PackageFeed(BaseFeed): return package.description def items(self, package): - return VersionLog.objects.filter(package=package).order_by('-id')[:30] + return VersionLog.objects.for_package(package, order=True)[:30] def item_description(self, vlog): return '' @@ -100,8 +100,7 @@ class MaintainerFeed(BaseFeed): kwargs={'maintainer_id': maintainer.id}) def items(self, maintainer): - q = VersionLog.objects.filter(package__maintainers__id=maintainer.id) - return q.order_by('-id')[:50] + return VersionLog.objects.for_maintainer(maintainer, order=True)[:50] class HerdFeed(BaseFeed): @@ -120,8 +119,7 @@ class HerdFeed(BaseFeed): return reverse('djeuscan.views.herd', kwargs={'herd': herd.herd}) def items(self, herd): - q = VersionLog.objects.filter(package__herds__id=herd.id) - return q.order_by('-id')[:100] + return VersionLog.objects.for_herd(herd, order=True)[:100] class CategoryFeed(BaseFeed): @@ -142,5 +140,4 @@ class CategoryFeed(BaseFeed): return reverse('djeuscan.views.category', args=[category]) def items(self, category): - q = VersionLog.objects.filter(package__category=category) - return q.order_by('-id')[:100] + return VersionLog.objects.for_category(category, order=True)[:100] diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index 91d5f44..087a01f 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -131,3 +131,32 @@ class PackageQuerySet(models.query.QuerySet, PackageMixin): class PackageManager(models.Manager, PackageMixin): def get_query_set(self): return PackageQuerySet(self.model, using=self._db) + + +class VersionLogMixin(object): + def for_package(self, package, order=False): + res = filter(package=package) + if order: + res = res.order_by('-id') + return res + + def for_maintainer(self, maintainer, order=False): + res = self.filter(package__maintainers__id=maintainer.id) + if order: + res = res.order_by('-id') + return res + + def for_category(self, category, order=False): + res = self.filter(package__category=category) + if order: + res = res.order_by('-id') + return res + + +class VersionLogQuerySet(models.query.QuerySet, VersionLogMixin): + pass + + +class VersionLogManager(models.Manager, VersionLogMixin): + def get_query_set(self): + return VersionLogQuerySet(self.model, using=self._db) diff --git a/euscanwww/djeuscan/models.py b/euscanwww/djeuscan/models.py index b658692..05b1663 100644 --- a/euscanwww/djeuscan/models.py +++ b/euscanwww/djeuscan/models.py @@ -1,6 +1,6 @@ from django.db import models -from djeuscan.managers import PackageManager +from djeuscan.managers import PackageManager, VersionLogManager class Herd(models.Model): @@ -114,6 +114,8 @@ class VersionLog(models.Model): overlay = models.CharField(max_length=128, default='gentoo') action = models.IntegerField(choices=VERSION_ACTIONS) + objects = VersionLogManager() + def tag(self): return '%s-%s:%s-[%s]' % (self.version, self.revision, self.slot, self.overlay) diff --git a/euscanwww/djeuscan/views.py b/euscanwww/djeuscan/views.py index 346adb2..9af6062 100644 --- a/euscanwww/djeuscan/views.py +++ b/euscanwww/djeuscan/views.py @@ -102,7 +102,7 @@ def package(request, category, package): log = EuscanResult.objects.filter(package=package).\ order_by('-datetime')[:1] log = log[0] if log else None - vlog = VersionLog.objects.filter(package=package).order_by('-id') + vlog = VersionLog.objects.for_package(package, order=True) return {'package': package, 'packaged': packaged, 'upstream': upstream, 'log': log, 'vlog': vlog} From 058b50556baefeccdd5145d143b13bb622ccd69c Mon Sep 17 00:00:00 2001 From: volpino Date: Sat, 5 May 2012 14:28:48 +0200 Subject: [PATCH 2/2] issue #4 --- bin/euscan | 20 +++++----- pym/euscan/helpers.py | 86 +++++++++++++++++++++++++++++++++++++++++++ pym/euscan/scan.py | 7 ++++ 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/bin/euscan b/bin/euscan index 52606d6..e7708e3 100755 --- a/bin/euscan +++ b/bin/euscan @@ -42,7 +42,7 @@ def setupSignals(): def exithandler(signum, frame): signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) - print () + print() sys.exit(errno.EINTR) signal.signal(signal.SIGINT, exithandler) @@ -236,21 +236,21 @@ def main(): pp.die(1, '%s: %s' % (package, str(err))) if not CONFIG['quiet']: - print () + print() for cp, url, version in ret: if not CONFIG['quiet']: - print ("Upstream Version: " - + pp.number("%s" % version) - + pp.path(" %s" % url)) + print("Upstream Version: " + + pp.number("%s" % version) + + pp.path(" %s" % url)) else: - print (pp.cpv("%s-%s" % (cp, version)) - + ": " + pp.path(url)) + print(pp.cpv("%s-%s" % (cp, version)) + + ": " + pp.path(url)) if not len(ret) and not CONFIG['quiet']: - print (pp.warn("Didn't find any new version, " - + "check package's homepage for " - + "more informations")) + print(pp.warn("Didn't find any new version, " + + "check package's homepage for " + + "more informations")) if __name__ == "__main__": diff --git a/pym/euscan/helpers.py b/pym/euscan/helpers.py index 4c04a86..7af055b 100644 --- a/pym/euscan/helpers.py +++ b/pym/euscan/helpers.py @@ -1,7 +1,9 @@ import os +import sys import re import pkg_resources import errno +import imp import urllib2 @@ -14,6 +16,11 @@ except ImportError: import portage from portage import dep +from portage.const import VDB_PATH +from portage import _encodings +from portage import _shell_quote +from portage import _unicode_decode +from portage import _unicode_encode from euscan import CONFIG, BLACKLIST_VERSIONS, ROBOTS_TXT_BLACKLIST_DOMAINS import euscan @@ -566,3 +573,82 @@ def parse_mirror(uri): return None return uri + + +# Stolen from ebuild +def query_from_ebuild(ebuild): + pf = None + if ebuild.endswith(".ebuild"): + pf = os.path.basename(ebuild)[:-7] + else: + return False + + if not os.path.isabs(ebuild): + mycwd = os.getcwd() + # Try to get the non-canonical path from the PWD evironment variable, + # since the canonical path returned from os.getcwd() may may be + # unusable in cases where the directory stucture is built from + # symlinks. + pwd = os.environ.get('PWD', '') + if sys.hexversion < 0x3000000: + pwd = _unicode_decode(pwd, encoding=_encodings['content'], + errors='strict') + if pwd and pwd != mycwd and \ + os.path.realpath(pwd) == mycwd: + mycwd = portage.normalize_path(pwd) + ebuild = os.path.join(mycwd, ebuild) + + ebuild = portage.normalize_path(ebuild) + # portdbapi uses the canonical path for the base of the portage tree, but + # subdirectories of the base can be built from symlinks (like crossdev + # does). + ebuild_portdir = os.path.realpath( + os.path.dirname(os.path.dirname(os.path.dirname(ebuild)))) + ebuild = os.path.join(ebuild_portdir, *ebuild.split(os.path.sep)[-3:]) + vdb_path = os.path.join(portage.settings['ROOT'], VDB_PATH) + + # Make sure that portdb.findname() returns the correct ebuild. + if ebuild_portdir != vdb_path and \ + ebuild_portdir not in portage.portdb.porttrees: + if sys.hexversion >= 0x3000000: + os.environ["PORTDIR_OVERLAY"] = \ + os.environ.get("PORTDIR_OVERLAY", "") + \ + " " + _shell_quote(ebuild_portdir) + else: + os.environ["PORTDIR_OVERLAY"] = \ + os.environ.get("PORTDIR_OVERLAY", "") + \ + " " + _unicode_encode(_shell_quote(ebuild_portdir), + encoding=_encodings['content'], errors='strict') + + portage.close_portdbapi_caches() + imp.reload(portage) + del portage.portdb.porttrees[1:] + if ebuild_portdir != portage.portdb.porttree_root: + portage.portdb.porttrees.append(ebuild_portdir) + + if not os.path.exists(ebuild): + return False + + ebuild_split = ebuild.split("/") + cpv = "%s/%s" % (ebuild_split[-3], pf) + + if not portage.catpkgsplit(cpv): + return False + + if ebuild.startswith(os.path.join(portage.root, portage.const.VDB_PATH)): + mytree = "vartree" + + portage_ebuild = portage.db[portage.root][mytree].dbapi.findname(cpv) + + if os.path.realpath(portage_ebuild) != ebuild: + return False + + else: + mytree = "porttree" + + portage_ebuild = portage.portdb.findname(cpv) + + if not portage_ebuild or portage_ebuild != ebuild: + return False + + return cpv diff --git a/pym/euscan/scan.py b/pym/euscan/scan.py index d6be6cb..17bd938 100644 --- a/pym/euscan/scan.py +++ b/pym/euscan/scan.py @@ -66,6 +66,13 @@ def scan_upstream_urls(cpv, urls): def scan_upstream(query): + + # check if the query is an ebuild file. + # if it's a valid ebuild convert the filename in an euscan query + ebuild_query = helpers.query_from_ebuild(query) + if ebuild_query: + query = ebuild_query + matches = Query(query).find( include_masked=True, in_installed=False