2011-04-13 19:00:31 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
from django.db.transaction import commit_on_success
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.management.color import color_style
|
2012-05-29 15:29:03 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
from djeuscan.models import Package, Herd, Maintainer
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
class ScanMetadata(object):
|
|
|
|
def __init__(self, quiet):
|
|
|
|
self.quiet = quiet
|
2012-05-28 23:24:09 +02:00
|
|
|
self.style = color_style()
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
@commit_on_success
|
|
|
|
def run(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-28 18:16:05 +02:00
|
|
|
sys.stderr.write(
|
2012-05-28 23:24:09 +02:00
|
|
|
self.style.ERROR("Unknown package '%s'\n" % query)
|
2012-04-28 18:16:05 +02:00
|
|
|
)
|
2012-04-04 14:33:40 +02:00
|
|
|
return
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
matches = sorted(matches)
|
2011-04-13 19:00:31 +02:00
|
|
|
pkg = matches.pop()
|
2012-04-28 18:16:05 +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:
|
2012-04-28 18:16:05 +02:00
|
|
|
obj, created = Package.objects.get_or_create(
|
|
|
|
category=pkg.category, name=pkg.name
|
|
|
|
)
|
2012-03-14 14:07:06 +01:00
|
|
|
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:
|
2012-04-28 18:16:05 +02:00
|
|
|
sys.stderr.write(
|
2012-05-28 23:24:09 +02:00
|
|
|
self.style.ERROR(
|
2012-04-28 18:16:05 +02:00
|
|
|
"Gentoolkit fatal error: '%s'\n" % str(err)
|
|
|
|
)
|
|
|
|
)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
if created and not self.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-04-28 18:16:05 +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()]
|
|
|
|
)
|
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()]
|
2012-04-28 18:16:05 +02:00
|
|
|
new_maintainers = set(
|
|
|
|
maintainers.keys()).difference(existing_maintainers
|
|
|
|
)
|
|
|
|
old_maintainers = set(
|
|
|
|
existing_maintainers).difference(maintainers.keys()
|
|
|
|
)
|
2012-03-14 14:07:06 +01:00
|
|
|
|
|
|
|
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]
|
2012-05-29 15:29:03 +02:00
|
|
|
try:
|
|
|
|
maintainer = self.store_maintainer(
|
|
|
|
maintainer.name, maintainer.email
|
|
|
|
)
|
|
|
|
obj.maintainers.add(maintainer)
|
|
|
|
except ValidationError:
|
|
|
|
sys.stderr.write(
|
|
|
|
self.style.ERROR("Bad maintainer: '%s' '%s'\n" % (maintainer.name, maintainer.email))
|
|
|
|
)
|
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()
|
|
|
|
|
2012-05-28 22:57:33 +02:00
|
|
|
herd, created = Herd.objects.get_or_create(
|
|
|
|
herd=name,
|
|
|
|
defaults={"email": email}
|
|
|
|
)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
2012-05-28 21:16:38 +02:00
|
|
|
if created and not self.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
|
|
|
|
2012-05-28 22:57:33 +02:00
|
|
|
maintainer, created = Maintainer.objects.get_or_create(
|
|
|
|
email=email,
|
|
|
|
defaults={"name": name}
|
|
|
|
)
|
2011-04-13 19:00:31 +02:00
|
|
|
|
|
|
|
if created:
|
2012-05-28 21:16:38 +02:00
|
|
|
if not self.quiet:
|
2012-04-28 18:16:05 +02:00
|
|
|
sys.stdout.write(
|
|
|
|
'+ [m] %s <%s>\n' % (name.encode('utf-8'), email)
|
|
|
|
)
|
2011-04-13 19:00:31 +02:00
|
|
|
return maintainer
|
2012-05-28 21:16:38 +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'
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
self.options = options
|
|
|
|
|
|
|
|
scan_metadata = ScanMetadata(quiet=options["quiet"])
|
|
|
|
|
|
|
|
if options['all']:
|
|
|
|
for pkg in Package.objects.all():
|
|
|
|
scan_metadata.run('%s/%s' % (pkg.category, pkg.name), pkg)
|
|
|
|
elif len(args) > 0:
|
|
|
|
for package in args:
|
|
|
|
scan_metadata.run(package)
|
|
|
|
else:
|
|
|
|
for package in sys.stdin.readlines():
|
|
|
|
scan_metadata.run(package[:-1])
|