2012-02-07 02:18:15 +04:00
|
|
|
import hashlib
|
2011-12-13 22:06:17 +04:00
|
|
|
import logging
|
2012-02-07 02:18:15 +04:00
|
|
|
import os
|
|
|
|
from tempfile import mkstemp
|
2011-12-13 22:06:17 +04:00
|
|
|
|
|
|
|
from celeryutils import task
|
|
|
|
|
2012-02-07 02:18:15 +04:00
|
|
|
from amo import set_user
|
|
|
|
from devhub.tasks import _fetch_content
|
|
|
|
from files.utils import get_sha256
|
|
|
|
from users.utils import get_task_user
|
2012-03-14 02:45:47 +04:00
|
|
|
from mkt.webapps.models import Webapp
|
2011-12-13 22:06:17 +04:00
|
|
|
|
|
|
|
task_log = logging.getLogger('z.task')
|
|
|
|
|
|
|
|
|
|
|
|
@task(rate_limit='15/m')
|
|
|
|
def webapp_update_weekly_downloads(data, **kw):
|
2012-02-07 02:18:15 +04:00
|
|
|
task_log.info('[%s@%s] Update weekly downloads.' %
|
2011-12-13 22:06:17 +04:00
|
|
|
(len(data), webapp_update_weekly_downloads.rate_limit))
|
|
|
|
|
|
|
|
for line in data:
|
|
|
|
webapp = Webapp.objects.get(pk=line['addon'])
|
|
|
|
webapp.update(weekly_downloads=line['count'])
|
2012-02-07 02:18:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
def _get_content_hash(temp, url):
|
|
|
|
# Fetch the webapp, write it to a file and return the hash of that.
|
|
|
|
data = _fetch_content(url).read()
|
|
|
|
with open(temp, 'w') as fp:
|
|
|
|
fp.write(data)
|
|
|
|
return 'sha256:%s' % get_sha256(temp)
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def update_manifests(ids, **kw):
|
|
|
|
task_log.info('[%s@%s] Update manifests.' %
|
|
|
|
(len(ids), update_manifests.rate_limit))
|
|
|
|
|
|
|
|
# Since we'll be logging the updated manifest change to the users log,
|
|
|
|
# we'll need to log in as user.
|
|
|
|
set_user(get_task_user())
|
|
|
|
|
|
|
|
for id in ids:
|
|
|
|
task_log.info('Fetching webapp manifest for: %s' % id)
|
|
|
|
|
|
|
|
webapp = Webapp.objects.get(pk=id)
|
|
|
|
file_ = webapp.get_latest_file()
|
|
|
|
if not file_:
|
|
|
|
task_log.info('Ignoring, no existing file for: %s' % id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Fetch the data.
|
|
|
|
temp = mkstemp(suffix='.webapp')[1]
|
|
|
|
try:
|
|
|
|
hash_ = _get_content_hash(temp, webapp.manifest_url)
|
|
|
|
except:
|
|
|
|
task_log.info('Failed to get manifest for: %s' % id,
|
|
|
|
exc_info=True)
|
|
|
|
os.unlink(temp)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Try to create a new version, if needed.
|
|
|
|
try:
|
|
|
|
if file_.hash != hash_:
|
|
|
|
task_log.info('Webapp manifest different for: %s' % id)
|
|
|
|
webapp.manifest_updated(temp)
|
|
|
|
else:
|
|
|
|
task_log.info('Webapp manifest the same for: %s' % id)
|
|
|
|
except:
|
|
|
|
task_log.info('Failed to create version for: %s' % id,
|
|
|
|
exc_info=True)
|
|
|
|
finally:
|
|
|
|
os.unlink(temp)
|