2010-11-18 20:44:30 +03:00
|
|
|
import json
|
2010-09-15 00:22:33 +04:00
|
|
|
import logging
|
2010-11-18 20:44:30 +03:00
|
|
|
import os
|
2010-09-15 00:22:33 +04:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2010-12-21 02:10:42 +03:00
|
|
|
from django.conf import settings
|
2010-12-28 00:36:13 +03:00
|
|
|
from django.core.management import call_command
|
2010-09-15 00:22:33 +04:00
|
|
|
from celeryutils import task
|
|
|
|
|
|
|
|
from amo.decorators import write
|
2010-11-12 03:39:18 +03:00
|
|
|
from amo.utils import resize_image
|
2010-12-30 01:30:08 +03:00
|
|
|
from files.models import FileUpload, File, FileValidation
|
2010-12-28 00:36:13 +03:00
|
|
|
from applications.management.commands import dump_apps
|
2010-09-15 00:22:33 +04:00
|
|
|
|
|
|
|
log = logging.getLogger('z.devhub.task')
|
|
|
|
|
|
|
|
|
2010-12-30 01:30:08 +03:00
|
|
|
@task(queue='devhub')
|
2010-09-15 00:22:33 +04:00
|
|
|
@write
|
|
|
|
def validator(upload_id, **kw):
|
|
|
|
log.info('VALIDATING: %s' % upload_id)
|
|
|
|
upload = FileUpload.objects.get(pk=upload_id)
|
|
|
|
try:
|
2010-12-30 01:30:08 +03:00
|
|
|
result = _validator(upload.path)
|
2010-12-11 03:16:28 +03:00
|
|
|
upload.validation = result
|
|
|
|
upload.save() # We want to hit the custom save().
|
2010-09-15 00:22:33 +04:00
|
|
|
except:
|
|
|
|
# Store the error with the FileUpload job, then raise
|
|
|
|
# it for normal logging.
|
|
|
|
tb = traceback.format_exception(*sys.exc_info())
|
|
|
|
upload.update(task_error=''.join(tb))
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2010-12-30 01:30:08 +03:00
|
|
|
@task(queue='devhub')
|
|
|
|
@write
|
|
|
|
def file_validator(file_id, **kw):
|
|
|
|
log.info('VALIDATING file: %s' % file_id)
|
|
|
|
file = File.objects.get(pk=file_id)
|
|
|
|
# Unlike upload validation, let the validator
|
|
|
|
# raise an exception if there is one.
|
|
|
|
result = _validator(file.file_path)
|
|
|
|
return FileValidation.from_json(file, result)
|
|
|
|
|
|
|
|
|
|
|
|
def _validator(file_path):
|
2010-11-23 21:57:39 +03:00
|
|
|
|
2010-12-30 18:07:15 +03:00
|
|
|
from validator.validate import validate
|
2010-12-21 02:10:42 +03:00
|
|
|
|
|
|
|
# TODO(Kumar) remove this when validator is fixed, see bug 620503
|
|
|
|
from validator.testcases import scripting
|
|
|
|
scripting.SPIDERMONKEY_INSTALLATION = settings.SPIDERMONKEY
|
|
|
|
import validator.constants
|
|
|
|
validator.constants.SPIDERMONKEY_INSTALLATION = settings.SPIDERMONKEY
|
2010-12-10 21:46:13 +03:00
|
|
|
|
2010-12-28 00:36:13 +03:00
|
|
|
apps = dump_apps.Command.JSON_PATH
|
|
|
|
if not os.path.exists(apps):
|
|
|
|
call_command('dump_apps')
|
2010-12-10 00:27:44 +03:00
|
|
|
|
2010-12-30 01:30:08 +03:00
|
|
|
return validate(file_path, format='json',
|
2010-12-21 02:10:42 +03:00
|
|
|
# Continue validating each tier even if one has an error
|
|
|
|
determined=True,
|
|
|
|
approved_applications=apps,
|
|
|
|
spidermonkey=settings.SPIDERMONKEY)
|
2010-11-12 03:39:18 +03:00
|
|
|
|
|
|
|
|
2010-12-10 03:36:00 +03:00
|
|
|
@task(queue='images')
|
2010-11-12 03:39:18 +03:00
|
|
|
def resize_icon(src, dst, size, **kw):
|
|
|
|
"""Resizes addon icons."""
|
|
|
|
log.info('[1@None] Resizing icon: %s' % dst)
|
|
|
|
|
|
|
|
try:
|
2010-12-10 03:36:00 +03:00
|
|
|
if isinstance(size, list):
|
|
|
|
for s in size:
|
|
|
|
resize_image(src, '%s-%s.png' % (dst, s), (s, s),
|
|
|
|
remove_src=False)
|
|
|
|
os.remove(src)
|
|
|
|
else:
|
|
|
|
resize_image(src, dst, (size, size), remove_src=True)
|
|
|
|
|
2010-11-12 03:39:18 +03:00
|
|
|
except Exception, e:
|
|
|
|
log.error("Error saving addon icon: %s" % e)
|