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-11-18 20:44:30 +03:00
|
|
|
from django.core import management
|
|
|
|
|
2010-09-15 00:22:33 +04:00
|
|
|
from celeryutils import task
|
|
|
|
|
|
|
|
from amo.decorators import write
|
|
|
|
from files.models import FileUpload
|
|
|
|
|
|
|
|
log = logging.getLogger('z.devhub.task')
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
@write
|
|
|
|
def validator(upload_id, **kw):
|
|
|
|
log.info('VALIDATING: %s' % upload_id)
|
|
|
|
upload = FileUpload.objects.get(pk=upload_id)
|
|
|
|
try:
|
|
|
|
result = _validator(upload)
|
|
|
|
upload.update(validation=result)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def _validator(upload):
|
2010-11-18 20:44:30 +03:00
|
|
|
# Monkeypatch the validator's hard-coded applications & versions.
|
|
|
|
from validator.testcases import targetapplication as testcase
|
|
|
|
cmd = management.load_command_class('applications', 'dump_apps')
|
|
|
|
if not os.path.exists(cmd.JSON_PATH):
|
|
|
|
cmd.handle()
|
|
|
|
apps = json.load(open(cmd.JSON_PATH))
|
|
|
|
|
|
|
|
testcase.APPLICATIONS = dict((d['guid'], d['name']) for d in apps.values())
|
|
|
|
versions = dict((d['guid'], d['versions']) for d in apps.values())
|
|
|
|
testcase.APPROVED_APPLICATIONS = versions
|
|
|
|
|
2010-09-18 04:06:00 +04:00
|
|
|
# TODO(basta): this should be two lines.
|
|
|
|
# from addon_validator import validate
|
|
|
|
# return validate(path, format='json')
|
|
|
|
from cStringIO import StringIO
|
|
|
|
import validator.main as addon_validator
|
|
|
|
from validator.errorbundler import ErrorBundle
|
|
|
|
from validator.constants import PACKAGE_ANY
|
|
|
|
output = StringIO()
|
|
|
|
# I have no idea what these params mean.
|
|
|
|
eb = ErrorBundle(output, True)
|
|
|
|
eb.determined = True
|
|
|
|
eb.save_resource('listed', True)
|
|
|
|
addon_validator.prepare_package(eb, upload.path, PACKAGE_ANY)
|
|
|
|
eb.print_json()
|
|
|
|
return output.getvalue()
|