2011-04-13 19:00:31 +02:00
|
|
|
import subprocess
|
|
|
|
import portage
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
from portage import versions
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django.db.transaction import commit_on_success
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2012-04-04 17:31:54 +02:00
|
|
|
from djeuscan.models import Package, Herd, Maintainer
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
from gentoolkit.query import Query
|
2011-04-14 08:52:26 +02:00
|
|
|
from gentoolkit.errors import GentoolkitFatalError
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
_overlays = {}
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--all',
|
|
|
|
action='store_true',
|
|
|
|
dest='all',
|
|
|
|
default=False,
|
|
|
|
help='Scan all packages'),
|
|
|
|
make_option('--quiet',
|
|
|
|
action='store_true',
|
|
|
|
dest='quiet',
|
|
|
|
default=False,
|
|
|
|
help='Be quiet'),
|
|
|
|
)
|
|
|
|
args = '<package package ...>'
|
|
|
|
help = 'Scans metadata and fills database'
|
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
@commit_on_success
|
2011-04-13 19:00:31 +02:00
|
|
|
def handle(self, *args, **options):
|
2012-03-14 14:07:06 +01:00
|
|
|
self.options = options
|
|
|
|
|
2012-03-02 14:08:28 +01:00
|
|
|
if options['all']:
|
2011-04-13 19:00:31 +02:00
|
|
|
for pkg in Package.objects.all():
|
2012-03-14 14:07:06 +01:00
|
|
|
self.scan('%s/%s' % (pkg.category, pkg.name), pkg)
|
2012-03-02 14:08:28 +01:00
|
|
|
elif len(args):
|
2011-04-13 19:00:31 +02:00
|
|
|
for package in args:
|
2012-03-14 14:07:06 +01:00
|
|
|
self.scan(package)
|
2012-03-02 14:08:28 +01:00
|
|
|
else:
|
|
|
|
for package in sys.stdin.readlines():
|
2012-03-14 14:07:06 +01:00
|
|
|
self.scan(package[:-1])
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
def scan(self, query=None, obj=None):
|
2011-04-13 19:00:31 +02:00
|
|
|
matches = Query(query).find(
|
2012-04-04 14:33:40 +02:00
|
|
|
include_masked=True,
|
|
|
|
in_installed=False,
|
2011-04-13 19:00:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not matches:
|
2012-04-04 14:33:40 +02:00
|
|
|
sys.stderr.write(self.style.ERROR("Unknown package '%s'\n" % query))
|
|
|
|
return
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
matches = sorted(matches)
|
|
|
|
pkg = matches.pop()
|
2011-04-25 22:27:32 +02:00
|
|
|
if '9999' in pkg.version and len(matches):
|
2012-04-04 14:33:40 +02:00
|
|
|
pkg = matches.pop()
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
if not obj:
|
|
|
|
obj, created = Package.objects.get_or_create(category=pkg.category, name=pkg.name)
|
|
|
|
else:
|
|
|
|
created = False
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2011-04-14 08:52:26 +02:00
|
|
|
try:
|
|
|
|
obj.homepage = pkg.environment("HOMEPAGE")
|
|
|
|
obj.description = pkg.environment("DESCRIPTION")
|
|
|
|
except GentoolkitFatalError, err:
|
|
|
|
sys.stderr.write(self.style.ERROR("Gentoolkit fatal error: '%s'\n" % str(err)))
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
if created and not self.options['quiet']:
|
2011-08-25 15:39:54 +02:00
|
|
|
sys.stdout.write('+ [p] %s/%s\n' % (pkg.category, pkg.name))
|
2012-03-14 14:07:06 +01:00
|
|
|
|
2011-04-14 08:52:26 +02:00
|
|
|
if pkg.metadata:
|
2012-03-14 14:07:06 +01:00
|
|
|
herds = dict([(herd[0], herd) for herd in pkg.metadata.herds(True)])
|
|
|
|
maintainers = dict([(m.email, m) for m in pkg.metadata.maintainers()])
|
2011-04-15 19:28:37 +02:00
|
|
|
|
2012-03-14 14:07:06 +01: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())
|
|
|
|
|
|
|
|
existing_maintainers = [m.email for m in obj.maintainers.all()]
|
|
|
|
new_maintainers = set(maintainers.keys()).difference(existing_maintainers)
|
|
|
|
old_maintainers = set(existing_maintainers).difference(maintainers.keys())
|
|
|
|
|
|
|
|
for herd in obj.herds.all():
|
2012-04-23 17:05:46 +02:00
|
|
|
if herd.herd in old_herds:
|
2012-03-14 14:07:06 +01:00
|
|
|
obj.herds.remove(herd)
|
|
|
|
|
|
|
|
for herd in new_herds:
|
|
|
|
herd = self.store_herd(*herds[herd])
|
2011-04-14 08:52:26 +02:00
|
|
|
obj.herds.add(herd)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01: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]
|
|
|
|
maintainer = self.store_maintainer(maintainer.name, maintainer.email)
|
2011-04-14 08:52:26 +02:00
|
|
|
obj.maintainers.add(maintainer)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
obj.save()
|
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
def store_herd(self, name, email):
|
2011-04-15 19:28:37 +02:00
|
|
|
if not name:
|
|
|
|
name = '{nil}'
|
|
|
|
name = name.strip("\r").strip("\n").strip("\t").strip()
|
|
|
|
|
2011-04-13 19:00:31 +02:00
|
|
|
herd, created = Herd.objects.get_or_create(herd=name)
|
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
if created and not self.options['quiet']:
|
2011-08-31 15:38:32 +02:00
|
|
|
sys.stdout.write('+ [h] %s <%s>\n' % (name, email))
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2011-08-31 15:38:32 +02:00
|
|
|
herd.email = email
|
|
|
|
herd.save()
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
return herd
|
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
def store_maintainer(self, name, email):
|
2011-04-13 19:00:31 +02:00
|
|
|
if not name:
|
|
|
|
name = email
|
2011-04-15 19:28:37 +02:00
|
|
|
if not name:
|
|
|
|
name = '{nil}'
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2011-04-15 19:28:37 +02:00
|
|
|
maintainer, created = Maintainer.objects.get_or_create(email=email)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
if created:
|
2012-03-14 14:07:06 +01:00
|
|
|
if not self.options['quiet']:
|
2011-08-25 15:39:54 +02:00
|
|
|
sys.stdout.write('+ [m] %s <%s>\n' % (name.encode('utf-8'), email))
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
if not maintainer.name or name not in [maintainer.name, email, '{nil}']:
|
|
|
|
maintainer.name = name
|
2011-04-13 19:00:31 +02:00
|
|
|
maintainer.save()
|
|
|
|
|
|
|
|
return maintainer
|