2012-07-20 11:48:34 +02:00
|
|
|
import os
|
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
from gentoolkit.query import Query
|
2012-07-20 11:48:34 +02:00
|
|
|
from gentoolkit.dbapi import PORTDB
|
|
|
|
|
|
|
|
import xml.etree.cElementTree as etree
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
from django.db.transaction import commit_on_success
|
|
|
|
from django.core.management.color import color_style
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
|
|
from djeuscan.models import Package, Herd, Maintainer
|
2012-06-22 11:31:15 +02:00
|
|
|
from djeuscan.processing import FakeLogger
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-07 15:13:59 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
class ScanMetadata(object):
|
2012-06-22 11:31:15 +02:00
|
|
|
def __init__(self, logger=None):
|
2012-06-22 09:32:39 +02:00
|
|
|
self.style = color_style()
|
2012-06-22 11:31:15 +02:00
|
|
|
self.logger = logger or FakeLogger()
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
def scan(self, query=None, obj=None):
|
2012-07-02 11:19:49 +02:00
|
|
|
matches = Query(query).smart_find(
|
|
|
|
in_installed=True,
|
|
|
|
in_porttree=True,
|
|
|
|
in_overlay=True,
|
|
|
|
include_masked=True,
|
|
|
|
show_progress=False,
|
|
|
|
no_matches_fatal=False,
|
2012-06-22 09:32:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not matches:
|
2012-06-22 11:31:15 +02:00
|
|
|
self.logger.error(
|
|
|
|
self.style.ERROR("Unknown package '%s'" % query)
|
2012-06-22 09:32:39 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
matches = sorted(matches)
|
|
|
|
pkg = matches.pop()
|
|
|
|
if '9999' in pkg.version and len(matches):
|
|
|
|
pkg = matches.pop()
|
|
|
|
|
|
|
|
if not obj:
|
|
|
|
obj, created = Package.objects.get_or_create(
|
|
|
|
category=pkg.category, name=pkg.name
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
created = False
|
|
|
|
|
2012-06-22 11:31:15 +02:00
|
|
|
if created:
|
|
|
|
self.logger.info('+ [p] %s/%s' % (pkg.category, pkg.name))
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-26 21:18:31 +02:00
|
|
|
try:
|
|
|
|
if not pkg.metadata:
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(
|
2012-07-27 11:33:27 +02:00
|
|
|
self.style.ERROR('%s/%s: %s' %
|
|
|
|
(pkg.category, pkg.name, str(e)))
|
2012-07-26 21:18:31 +02:00
|
|
|
)
|
2012-07-02 11:19:49 +02:00
|
|
|
return
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
herds = dict(
|
|
|
|
[(herd[0], herd) for herd in pkg.metadata.herds(True)]
|
|
|
|
)
|
|
|
|
maintainers = dict(
|
|
|
|
[(m.email, m) for m in pkg.metadata.maintainers()]
|
|
|
|
)
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
existing_herds = [h.herd for h in obj.herds.all()]
|
|
|
|
new_herds = set(herds.keys()).difference(existing_herds)
|
|
|
|
old_herds = set(existing_herds).difference(herds.keys())
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
existing_maintainers = [m.email for m in obj.maintainers.all()]
|
2012-07-07 15:13:59 +02:00
|
|
|
new_maintainers = set(maintainers.keys()).\
|
|
|
|
difference(existing_maintainers)
|
|
|
|
old_maintainers = set(existing_maintainers).\
|
|
|
|
difference(maintainers.keys())
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
for herd in obj.herds.all():
|
|
|
|
if herd.herd in old_herds:
|
|
|
|
obj.herds.remove(herd)
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
for herd in new_herds:
|
|
|
|
herd = self.store_herd(*herds[herd])
|
|
|
|
obj.herds.add(herd)
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
for maintainer in obj.maintainers.all():
|
|
|
|
if maintainer.email in old_maintainers:
|
|
|
|
obj.maintainers.remove(maintainer)
|
|
|
|
|
|
|
|
for maintainer in new_maintainers:
|
|
|
|
maintainer = maintainers[maintainer]
|
|
|
|
try:
|
|
|
|
maintainer = self.store_maintainer(
|
|
|
|
maintainer.name, maintainer.email
|
2012-06-22 09:32:39 +02:00
|
|
|
)
|
2012-07-02 11:19:49 +02:00
|
|
|
obj.maintainers.add(maintainer)
|
|
|
|
except ValidationError:
|
|
|
|
self.logger.error(
|
|
|
|
self.style.ERROR("Bad maintainer: '%s' '%s'" % \
|
2012-06-22 09:32:39 +02:00
|
|
|
(maintainer.name, maintainer.email))
|
2012-07-20 11:48:34 +02:00
|
|
|
)
|
2012-06-22 09:32:39 +02:00
|
|
|
obj.save()
|
|
|
|
|
|
|
|
def store_herd(self, name, email):
|
|
|
|
if not name:
|
|
|
|
name = '{nil}'
|
|
|
|
name = name.strip("\r").strip("\n").strip("\t").strip()
|
|
|
|
|
|
|
|
herd, created = Herd.objects.get_or_create(
|
|
|
|
herd=name,
|
|
|
|
defaults={"email": email}
|
|
|
|
)
|
|
|
|
|
2012-06-22 11:31:15 +02:00
|
|
|
if created:
|
|
|
|
self.logger.info('+ [h] %s <%s>' % (name, email))
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
herd.email = email
|
|
|
|
herd.save()
|
|
|
|
|
|
|
|
return herd
|
|
|
|
|
|
|
|
def store_maintainer(self, name, email):
|
|
|
|
if not name:
|
|
|
|
name = email
|
|
|
|
if not name:
|
|
|
|
name = '{nil}'
|
|
|
|
|
|
|
|
maintainer, created = Maintainer.objects.get_or_create(
|
|
|
|
email=email,
|
|
|
|
defaults={"name": name}
|
|
|
|
)
|
|
|
|
|
|
|
|
if created:
|
2012-06-22 11:31:15 +02:00
|
|
|
self.logger.info(
|
|
|
|
'+ [m] %s <%s>' % (name.encode('utf-8'), email)
|
|
|
|
)
|
2012-06-22 09:32:39 +02:00
|
|
|
return maintainer
|
|
|
|
|
2012-07-20 14:15:28 +02:00
|
|
|
def populate_herds_and_maintainers(self, herds_xml_path=None):
|
2012-07-20 11:48:34 +02:00
|
|
|
self.logger.info("Populating herds and maintainers from herds.xml...")
|
|
|
|
|
2012-07-20 14:15:28 +02:00
|
|
|
herds_xml_path = herds_xml_path or os.path.join(
|
2012-07-20 11:48:34 +02:00
|
|
|
PORTDB.settings["PORTDIR"], "metadata", "herds.xml"
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
self._herdstree = etree.parse(herds_xml_path)
|
|
|
|
except IOError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for herd_node in self._herdstree.getiterator('herd'):
|
|
|
|
herd_name = herd_node.findtext('name')
|
|
|
|
herd_email = herd_node.findtext('email')
|
|
|
|
|
|
|
|
try:
|
|
|
|
herd = self.store_herd(herd_name, herd_email)
|
|
|
|
except ValidationError: # just skip invalid data
|
|
|
|
continue
|
|
|
|
|
|
|
|
herd.maintainers.clear() # clear previous data
|
|
|
|
|
|
|
|
for maintainer_node in herd_node:
|
|
|
|
if maintainer_node.tag == "maintainer":
|
|
|
|
maintainer_name = maintainer_node.findtext('name')
|
|
|
|
maintainer_email = maintainer_node.findtext('email')
|
|
|
|
|
|
|
|
maintainer = self.store_maintainer(
|
|
|
|
maintainer_name, maintainer_email
|
|
|
|
)
|
2012-07-24 07:59:40 +02:00
|
|
|
|
2012-07-20 11:48:34 +02:00
|
|
|
herd.maintainers.add(maintainer)
|
|
|
|
|
2012-07-07 15:13:59 +02:00
|
|
|
|
2012-07-02 11:19:49 +02:00
|
|
|
@commit_on_success
|
2012-07-20 13:25:03 +02:00
|
|
|
def scan_metadata(packages=None, category=None, logger=None, populate=False):
|
2012-06-22 11:31:15 +02:00
|
|
|
scan_handler = ScanMetadata(logger=logger)
|
2012-07-02 11:19:49 +02:00
|
|
|
|
|
|
|
if category:
|
|
|
|
packages = Package.objects.filter(category=category)
|
2012-07-24 09:03:14 +02:00
|
|
|
elif packages is None:
|
2012-06-22 09:32:39 +02:00
|
|
|
packages = Package.objects.all()
|
|
|
|
|
2012-07-20 11:48:34 +02:00
|
|
|
if populate:
|
|
|
|
scan_handler.populate_herds_and_maintainers()
|
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
for pkg in packages:
|
|
|
|
if isinstance(pkg, Package):
|
2012-07-02 11:19:49 +02:00
|
|
|
scan_handler.scan('%s/%s' % (pkg.category, pkg.name), pkg)
|
2012-06-22 09:32:39 +02:00
|
|
|
else:
|
2012-07-02 11:19:49 +02:00
|
|
|
scan_handler.scan(pkg)
|