2012-06-04 11:06:47 +02:00
|
|
|
import subprocess
|
|
|
|
from StringIO import StringIO
|
2012-06-08 14:21:11 +02:00
|
|
|
from itertools import islice
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
from celery.task import task, periodic_task
|
|
|
|
from celery.task.schedules import crontab
|
2012-05-30 22:54:55 +02:00
|
|
|
from celery.task.sets import TaskSet
|
|
|
|
|
2012-06-04 11:06:47 +02:00
|
|
|
from django.conf import settings
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-12 14:55:58 +02:00
|
|
|
from euscan import output as euscan_output
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
from djeuscan.models import Package, RefreshPackageQuery
|
2012-05-30 22:54:55 +02:00
|
|
|
from djeuscan.management.commands.regen_rrds import regen_rrds
|
|
|
|
from djeuscan.management.commands.update_counters import update_counters
|
|
|
|
from djeuscan.management.commands.scan_metadata import ScanMetadata
|
|
|
|
from djeuscan.management.commands.scan_portage import ScanPortage, \
|
|
|
|
purge_versions as scan_portage_purge
|
|
|
|
from djeuscan.management.commands.scan_upstream import ScanUpstream, \
|
|
|
|
purge_versions as scan_upstream_purge
|
|
|
|
|
|
|
|
|
2012-06-09 16:12:17 +02:00
|
|
|
class TaskFailedException(Exception):
|
|
|
|
"""
|
|
|
|
Exception for failed tasks
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
def _launch_command(cmd):
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
|
|
|
Helper for launching shell commands inside tasks
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
fp = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
output = StringIO(fp.communicate()[0])
|
|
|
|
return output.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
def _chunks(it, n):
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
|
|
|
Chunk generator, takes an iterator and the desired size of the chunk
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
for first in it:
|
|
|
|
yield [first] + list(islice(it, n - 1))
|
|
|
|
|
|
|
|
|
|
|
|
def _run_in_chunks(task, iterable, n=32):
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
|
|
|
Runs the given task with the given iterable of args in chunks of
|
|
|
|
n subtasks
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
output = []
|
|
|
|
for chunk in _chunks(iter(iterable), n):
|
|
|
|
job = TaskSet(tasks=[
|
|
|
|
task.subtask(args)
|
|
|
|
for args in chunk
|
|
|
|
])
|
|
|
|
result = job.apply_async()
|
2012-06-09 16:12:17 +02:00
|
|
|
# TODO: understand why this causes timeout
|
2012-06-12 13:05:58 +02:00
|
|
|
output.extend(list(result.join(timeout=3600)))
|
2012-06-08 14:21:11 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
@task
|
|
|
|
def regen_rrds_task():
|
2012-06-08 14:21:11 +02:00
|
|
|
return regen_rrds()
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def update_counters_task():
|
2012-06-08 14:21:11 +02:00
|
|
|
return update_counters()
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def scan_metadata_task(query, obj=None):
|
2012-05-31 09:11:40 +02:00
|
|
|
logger = scan_metadata_task.get_logger()
|
|
|
|
logger.info("Starting metadata scanning for package %s ...", query)
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
scan_metadata = ScanMetadata()
|
2012-06-12 13:05:58 +02:00
|
|
|
result = scan_metadata.scan(query, obj)
|
|
|
|
if not result:
|
|
|
|
raise TaskFailedException("Couldn't scan metadata")
|
|
|
|
return result
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
2012-06-05 13:30:31 +02:00
|
|
|
@task
|
|
|
|
def scan_metadata_list_task(query):
|
2012-06-08 14:21:11 +02:00
|
|
|
return _run_in_chunks(scan_metadata_task, [(p, ) for p in query.split()])
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
@task
|
|
|
|
def scan_metadata_all_task():
|
2012-06-08 14:21:11 +02:00
|
|
|
return _run_in_chunks(
|
|
|
|
scan_metadata_task,
|
|
|
|
[('%s/%s' % (pkg.category, pkg.name), pkg)
|
|
|
|
for pkg in Package.objects.all()]
|
|
|
|
)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-05 13:30:31 +02:00
|
|
|
def scan_portage_list_task(query, purge=False):
|
2012-05-30 22:54:55 +02:00
|
|
|
scan_portage = ScanPortage()
|
2012-06-08 14:21:11 +02:00
|
|
|
logger = scan_portage_list_task.get_logger()
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
for pkg in query.split():
|
|
|
|
logger.info("Starting Portage package scanning: %s ...", pkg)
|
|
|
|
|
|
|
|
scan_portage.scan(pkg)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
if purge:
|
2012-05-31 09:11:40 +02:00
|
|
|
logger.info("Purging")
|
2012-05-30 22:54:55 +02:00
|
|
|
scan_portage_purge()
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-05 13:30:31 +02:00
|
|
|
def scan_portage_all_task(purge=False):
|
|
|
|
logger = scan_portage_all_task.get_logger()
|
|
|
|
logger.info("Starting Portage scanning...")
|
2012-05-31 09:11:40 +02:00
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
scan_portage = ScanPortage()
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_portage.scan()
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
if purge:
|
2012-05-31 09:11:40 +02:00
|
|
|
logger.info("Purging")
|
2012-05-30 22:54:55 +02:00
|
|
|
scan_portage_purge()
|
|
|
|
|
|
|
|
|
2012-06-05 13:30:31 +02:00
|
|
|
@task
|
|
|
|
def scan_upstream_task(query):
|
|
|
|
logger = scan_upstream_task.get_logger()
|
|
|
|
logger.info("Starting upstream scanning for package %s ...", query)
|
|
|
|
|
2012-06-12 14:55:58 +02:00
|
|
|
euscan_output.clean()
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_upstream = ScanUpstream()
|
2012-06-09 16:12:17 +02:00
|
|
|
result = scan_upstream.scan(query)
|
2012-06-12 14:55:58 +02:00
|
|
|
euscan_output.clean()
|
2012-06-12 13:05:58 +02:00
|
|
|
if not result or result == {}:
|
|
|
|
raise TaskFailedException("Couldn't scan upstream")
|
2012-06-09 16:12:17 +02:00
|
|
|
return result
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def scan_upstream_list_task(query):
|
2012-06-08 14:21:11 +02:00
|
|
|
return _run_in_chunks(scan_upstream_task, [(p, ) for p in query.split()])
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
@task
|
|
|
|
def scan_upstream_all_task(purge=False):
|
2012-06-08 14:21:11 +02:00
|
|
|
output = _run_in_chunks(
|
|
|
|
scan_upstream_task,
|
|
|
|
[('%s/%s' % (pkg.category, pkg.name), )
|
2012-06-09 16:12:17 +02:00
|
|
|
for pkg in Package.objects.all()],
|
|
|
|
n=16
|
2012-06-08 14:21:11 +02:00
|
|
|
)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
if purge:
|
2012-06-12 13:05:58 +02:00
|
|
|
output += [scan_upstream_purge()]
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
return output
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
2012-06-04 11:06:47 +02:00
|
|
|
@task
|
|
|
|
def emerge_sync():
|
|
|
|
cmd = ["emerge", "--sync", "--root", settings.PORTAGE_ROOT,
|
|
|
|
"--config-root", settings.PORTAGE_CONFIGROOT]
|
|
|
|
return _launch_command(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def layman_sync():
|
2012-06-05 12:46:22 +02:00
|
|
|
from layman import Layman
|
2012-06-05 16:43:40 +02:00
|
|
|
l = Layman(config=settings.LAYMAN_CONFIG)
|
|
|
|
return l.sync(l.get_installed(), output_results=False)
|
2012-06-04 11:06:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def emerge_regen():
|
|
|
|
cmd = [
|
|
|
|
"emerge", "--regen", "--jobs", settings.EMERGE_REGEN_JOBS, "--root",
|
|
|
|
settings.PORTAGE_ROOT, "--config-root", settings.PORTAGE_CONFIGROOT
|
|
|
|
]
|
|
|
|
return _launch_command(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def eix_update():
|
|
|
|
cmd = ["eix-update"]
|
|
|
|
return _launch_command(cmd)
|
|
|
|
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
@periodic_task(run_every=crontab(minute="*/1"))
|
|
|
|
def refresh_package_consume():
|
|
|
|
try:
|
|
|
|
obj = RefreshPackageQuery.objects.latest()
|
|
|
|
except RefreshPackageQuery.DoesNotExist:
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
result = scan_upstream_task(obj.query)
|
|
|
|
obj.delete()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
admin_tasks = [
|
2012-06-03 09:43:49 +02:00
|
|
|
regen_rrds_task,
|
|
|
|
update_counters_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_metadata_list_task,
|
2012-06-03 09:43:49 +02:00
|
|
|
scan_metadata_all_task,
|
|
|
|
scan_portage_all_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_portage_list_task,
|
2012-06-03 09:43:49 +02:00
|
|
|
scan_upstream_all_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_upstream_list_task,
|
2012-06-04 11:06:47 +02:00
|
|
|
emerge_sync,
|
|
|
|
layman_sync,
|
|
|
|
emerge_regen,
|
|
|
|
eix_update,
|
2012-06-03 09:43:49 +02:00
|
|
|
]
|