2011-04-14 19:28:38 +02:00
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django.db.transaction import commit_on_success
|
2012-04-28 18:16:05 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2012-04-23 17:05:46 +02:00
|
|
|
from django.utils import timezone
|
2012-03-02 18:01:46 +01:00
|
|
|
|
2012-04-04 17:31:54 +02:00
|
|
|
from djeuscan.models import Package, Herd, Maintainer, Version
|
|
|
|
from djeuscan.models import HerdLog, MaintainerLog, CategoryLog, WorldLog
|
|
|
|
from djeuscan import charts
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
from distutils.version import StrictVersion, LooseVersion
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-03-14 14:07:06 +01:00
|
|
|
def compare_versions(version1, version2):
|
|
|
|
try:
|
|
|
|
return cmp(StrictVersion(version1), StrictVersion(version2))
|
|
|
|
# in case of abnormal version number, fall back to LooseVersion
|
|
|
|
except ValueError:
|
|
|
|
return cmp(LooseVersion(version1), LooseVersion(version2))
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2011-04-14 19:28:38 +02:00
|
|
|
class Command(BaseCommand):
|
|
|
|
_overlays = {}
|
|
|
|
help = 'Update counters'
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--quiet',
|
|
|
|
action='store_true',
|
|
|
|
dest='quiet',
|
|
|
|
default=False,
|
|
|
|
help='Be quiet'),
|
2012-03-02 18:01:46 +01:00
|
|
|
make_option('--fast',
|
|
|
|
action='store_true',
|
|
|
|
dest='fast',
|
|
|
|
default=False,
|
|
|
|
help='Skip sanity checks'),
|
|
|
|
make_option('--nolog',
|
|
|
|
action='store_true',
|
|
|
|
dest='nolog',
|
|
|
|
default=False,
|
|
|
|
help='Skip logs'),
|
|
|
|
|
2011-04-14 19:28:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@commit_on_success
|
|
|
|
def handle(self, *args, **options):
|
2012-04-23 17:05:46 +02:00
|
|
|
now = timezone.now()
|
2011-04-14 19:28:38 +02:00
|
|
|
|
|
|
|
categories = {}
|
|
|
|
herds = {}
|
|
|
|
maintainers = {}
|
|
|
|
|
2012-03-02 18:01:46 +01:00
|
|
|
wlog = None
|
|
|
|
|
|
|
|
if not options['nolog']:
|
|
|
|
wlog = WorldLog()
|
|
|
|
wlog.datetime = now
|
|
|
|
|
|
|
|
for cat in Package.objects.values('category').distinct():
|
|
|
|
clog = CategoryLog()
|
|
|
|
clog.datetime = now
|
|
|
|
clog.category = cat['category']
|
|
|
|
categories[clog.category] = clog
|
|
|
|
|
|
|
|
for herd in Herd.objects.all():
|
|
|
|
hlog = HerdLog()
|
|
|
|
hlog.datetime = now
|
|
|
|
hlog.herd = herd
|
|
|
|
herds[herd.id] = hlog
|
|
|
|
|
|
|
|
for maintainer in Maintainer.objects.all():
|
|
|
|
mlog = MaintainerLog()
|
|
|
|
mlog.datetime = now
|
|
|
|
mlog.maintainer = maintainer
|
|
|
|
maintainers[maintainer.id] = mlog
|
|
|
|
|
|
|
|
package_queryset = Package.objects.all()
|
|
|
|
|
|
|
|
n_versions = {}
|
|
|
|
n_packaged = {}
|
|
|
|
n_overlay = {}
|
|
|
|
|
|
|
|
last_versions_gentoo = {}
|
|
|
|
last_versions_overlay = {}
|
|
|
|
last_versions_upstream = {}
|
|
|
|
|
|
|
|
def add_safe(storage, key):
|
|
|
|
if key not in storage:
|
|
|
|
storage[key] = 1
|
|
|
|
else:
|
|
|
|
storage[key] += 1
|
|
|
|
|
|
|
|
def add_last_ver(storage, version):
|
|
|
|
key = version['package_id']
|
|
|
|
if key not in storage:
|
|
|
|
storage[key] = version
|
|
|
|
return
|
|
|
|
if version['version'].startswith('9999'):
|
|
|
|
return
|
2012-04-28 18:16:05 +02:00
|
|
|
if compare_versions(storage[key]['version'],
|
|
|
|
version['version']) < 0:
|
2012-03-02 18:01:46 +01:00
|
|
|
storage[key] = version
|
|
|
|
|
|
|
|
if not options['fast']:
|
|
|
|
attrs = ['id', 'version', 'overlay', 'packaged', 'package_id']
|
|
|
|
for version in Version.objects.all().values(*attrs):
|
|
|
|
overlay, packaged = version['overlay'], version['packaged']
|
|
|
|
package_id = version['package_id']
|
|
|
|
|
|
|
|
add_safe(n_versions, package_id)
|
|
|
|
|
|
|
|
if not packaged:
|
|
|
|
add_last_ver(last_versions_upstream, version)
|
|
|
|
continue
|
|
|
|
if overlay == 'gentoo':
|
|
|
|
add_safe(n_packaged, package_id)
|
|
|
|
add_last_ver(last_versions_gentoo, version)
|
|
|
|
else:
|
|
|
|
add_safe(n_overlay, package_id)
|
|
|
|
add_last_ver(last_versions_overlay, version)
|
|
|
|
|
|
|
|
for package in package_queryset.select_related('herds', 'maintainers'):
|
|
|
|
if not options['fast']:
|
|
|
|
package.n_versions = n_versions.get(package.id, 0)
|
|
|
|
package.n_packaged = n_packaged.get(package.id, 0)
|
|
|
|
package.n_overlay = n_overlay.get(package.id, 0)
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
default = {'id': None}
|
|
|
|
package.last_version_gentoo_id = last_versions_gentoo.get(
|
|
|
|
package.id, default
|
|
|
|
)['id']
|
|
|
|
package.last_version_overlay_id = last_versions_overlay.get(
|
|
|
|
package.id, default
|
|
|
|
)['id']
|
|
|
|
package.last_version_upstream_id = last_versions_upstream.get(
|
|
|
|
package.id, default
|
|
|
|
)['id']
|
2012-03-02 18:01:46 +01:00
|
|
|
|
|
|
|
package.save()
|
2011-04-14 19:28:38 +02:00
|
|
|
|
2011-05-03 08:19:01 +02:00
|
|
|
n_packages_gentoo = int(package.n_packaged == package.n_versions)
|
2012-04-28 18:16:05 +02:00
|
|
|
n_packages_overlay = int(package.n_overlay and package.n_packaged \
|
|
|
|
+ package.n_overlay == package.n_versions)
|
|
|
|
n_packages_outdated = int(package.n_packaged + package.n_overlay \
|
|
|
|
< package.n_versions)
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2012-03-02 18:01:46 +01:00
|
|
|
def update_row(storage, key):
|
2012-04-28 18:16:05 +02:00
|
|
|
storage[key].n_packages_gentoo += n_packages_gentoo
|
|
|
|
storage[key].n_packages_overlay += n_packages_overlay
|
2012-03-02 18:01:46 +01:00
|
|
|
storage[key].n_packages_outdated += n_packages_outdated
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
storage[key].n_versions_gentoo += package.n_packaged
|
|
|
|
storage[key].n_versions_overlay += package.n_overlay
|
|
|
|
storage[key].n_versions_upstream += package.n_versions - \
|
|
|
|
package.n_packaged - \
|
|
|
|
package.n_overlay
|
|
|
|
|
2012-03-02 18:01:46 +01:00
|
|
|
def update_log(storage, qs):
|
|
|
|
for row in qs:
|
|
|
|
update_row(storage, row['id'])
|
2011-04-14 19:28:38 +02:00
|
|
|
|
2012-03-02 18:01:46 +01:00
|
|
|
if not options['nolog']:
|
|
|
|
update_log(herds, package.herds.all().values('id'))
|
|
|
|
update_log(maintainers, package.maintainers.all().values('id'))
|
|
|
|
update_row(categories, package.category)
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
wlog.n_packages_gentoo += n_packages_gentoo
|
|
|
|
wlog.n_packages_overlay += n_packages_overlay
|
2012-03-02 18:01:46 +01:00
|
|
|
wlog.n_packages_outdated += n_packages_outdated
|
2011-04-14 19:28:38 +02:00
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
wlog.n_versions_gentoo += package.n_packaged
|
|
|
|
wlog.n_versions_overlay += package.n_overlay
|
|
|
|
wlog.n_versions_upstream += package.n_versions - \
|
|
|
|
package.n_packaged - \
|
|
|
|
package.n_overlay
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2012-03-02 18:01:46 +01:00
|
|
|
if options['nolog']:
|
|
|
|
return
|
2011-04-14 19:28:38 +02:00
|
|
|
|
|
|
|
for clog in categories.values():
|
|
|
|
if not options['quiet']:
|
2011-08-25 15:39:54 +02:00
|
|
|
self.stdout.write('+ [cl] %s\n' % clog)
|
2011-05-03 08:19:01 +02:00
|
|
|
charts.rrd_update('category-%s' % clog.category, now, clog)
|
2011-04-14 19:28:38 +02:00
|
|
|
clog.save()
|
|
|
|
|
|
|
|
for hlog in herds.values():
|
|
|
|
if not options['quiet']:
|
2011-08-25 15:39:54 +02:00
|
|
|
self.stdout.write('+ [hl] %s\n' % hlog)
|
2011-05-03 08:19:01 +02:00
|
|
|
charts.rrd_update('herd-%d' % hlog.herd.id, now, hlog)
|
2011-04-14 19:28:38 +02:00
|
|
|
hlog.save()
|
|
|
|
|
|
|
|
for mlog in maintainers.values():
|
|
|
|
if not options['quiet']:
|
2011-08-25 15:39:54 +02:00
|
|
|
self.stdout.write('+ [ml] %s\n' % mlog)
|
2011-05-03 08:19:01 +02:00
|
|
|
charts.rrd_update('maintainer-%d' % mlog.maintainer.id, now, mlog)
|
2011-04-14 19:28:38 +02:00
|
|
|
mlog.save()
|
2011-05-03 08:19:01 +02:00
|
|
|
|
2011-05-05 18:10:47 +02:00
|
|
|
charts.rrd_update('world', now, wlog)
|
2012-03-02 18:01:46 +01:00
|
|
|
wlog.save()
|