2012-07-20 11:48:34 +02:00
|
|
|
import os
|
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
import portage
|
|
|
|
from gentoolkit.metadata import MetaData
|
2012-07-20 11:48:34 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
from djeuscan.models import Package, Version, 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
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
def get_package(self, query):
|
|
|
|
try:
|
|
|
|
return Package.objects.get(name=query)
|
|
|
|
except Package.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
category, package = portage.catsplit(query)
|
2012-08-14 09:11:23 +02:00
|
|
|
return Package.objects.get(category=category, name=package)
|
2012-08-03 21:59:09 +02:00
|
|
|
except Package.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
category, package, ver, rev = portage.catpkgsplit(query)
|
|
|
|
return Package.objects.get(category=category, name=package)
|
|
|
|
except Package.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def metadata_from_db(self, query, pkg=None):
|
|
|
|
if not pkg:
|
|
|
|
pkg = self.get_package(query)
|
|
|
|
|
|
|
|
try:
|
2012-08-07 09:39:49 +02:00
|
|
|
version = Version.objects.filter(package=pkg).\
|
|
|
|
values('metadata_path').order_by('version', 'revision')[0]
|
2012-08-03 21:59:09 +02:00
|
|
|
except IndexError:
|
|
|
|
return pkg, None
|
|
|
|
|
|
|
|
if not version['metadata_path']:
|
|
|
|
return pkg, None
|
|
|
|
return pkg, MetaData(version['metadata_path'])
|
|
|
|
|
|
|
|
def metadata_from_portage(self, query, pkg=None):
|
|
|
|
from gentoolkit.query import Query
|
|
|
|
|
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
|
|
|
)
|
2012-08-03 21:59:09 +02:00
|
|
|
return pkg, None
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
matches = sorted(matches)
|
2012-08-03 21:59:09 +02:00
|
|
|
package = matches.pop()
|
|
|
|
if '9999' in package.version and len(matches):
|
|
|
|
package = matches.pop()
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
if not pkg:
|
|
|
|
pkg, created = Package.objects.get_or_create(
|
|
|
|
category=package.category, name=package.name
|
2012-06-22 09:32:39 +02:00
|
|
|
)
|
|
|
|
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-08-03 21:59:09 +02:00
|
|
|
return pkg, package.metadata
|
|
|
|
|
|
|
|
def scan(self, query=None, pkg=None):
|
2012-07-26 21:18:31 +02:00
|
|
|
try:
|
2012-08-03 21:59:09 +02:00
|
|
|
metadata = None
|
|
|
|
pkg, metadata = self.metadata_from_db(query, pkg)
|
2012-08-15 11:04:29 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
if not metadata:
|
|
|
|
pkg, metadata = self.metadata_from_portage(query, pkg)
|
2012-08-15 11:04:29 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
if not metadata:
|
2012-07-26 21:18:31 +02:00
|
|
|
return
|
|
|
|
except Exception as e:
|
2012-08-03 21:59:09 +02:00
|
|
|
if pkg:
|
|
|
|
self.logger.error(
|
|
|
|
self.style.ERROR('%s/%s: %s' %
|
|
|
|
(pkg.category, pkg.name, str(e)))
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.logger.error(
|
|
|
|
self.style.ERROR('%s: %s' % (query, str(e)))
|
|
|
|
)
|
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(
|
2012-08-03 21:59:09 +02:00
|
|
|
[(herd[0], herd) for herd in metadata.herds(True)]
|
2012-07-02 11:19:49 +02:00
|
|
|
)
|
|
|
|
maintainers = dict(
|
2012-08-03 21:59:09 +02:00
|
|
|
[(m.email, m) for m in metadata.maintainers()]
|
2012-07-02 11:19:49 +02:00
|
|
|
)
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
existing_herds = [h.herd for h in pkg.herds.all()]
|
2012-07-02 11:19:49 +02:00
|
|
|
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-08-03 21:59:09 +02:00
|
|
|
existing_maintainers = [m.email for m in pkg.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-08-03 21:59:09 +02:00
|
|
|
for herd in pkg.herds.all():
|
2012-07-02 11:19:49 +02:00
|
|
|
if herd.herd in old_herds:
|
2012-08-03 21:59:09 +02:00
|
|
|
pkg.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])
|
2012-08-03 21:59:09 +02:00
|
|
|
pkg.herds.add(herd)
|
2012-06-22 09:32:39 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
for maintainer in pkg.maintainers.all():
|
2012-07-02 11:19:49 +02:00
|
|
|
if maintainer.email in old_maintainers:
|
2012-08-03 21:59:09 +02:00
|
|
|
pkg.maintainers.remove(maintainer)
|
2012-07-02 11:19:49 +02:00
|
|
|
|
|
|
|
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-08-03 21:59:09 +02:00
|
|
|
pkg.maintainers.add(maintainer)
|
2012-07-02 11:19:49 +02:00
|
|
|
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-08-15 11:04:29 +02:00
|
|
|
|
2012-08-03 21:59:09 +02:00
|
|
|
pkg.save()
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
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-08-03 21:59:09 +02:00
|
|
|
portage.settings["PORTDIR"], "metadata", "herds.xml"
|
2012-07-20 11:48:34 +02:00
|
|
|
)
|
|
|
|
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)
|