2011-02-08 08:25:33 +03:00
|
|
|
from datetime import datetime, timedelta
|
2012-02-10 04:46:00 +04:00
|
|
|
import dictconfig
|
|
|
|
import logging
|
2012-09-26 00:26:38 +04:00
|
|
|
import os
|
|
|
|
|
|
|
|
# get the right settings module
|
2012-09-26 01:18:20 +04:00
|
|
|
settingmodule = os.environ.get('DJANGO_SETTINGS_MODULE', 'settings_local')
|
2012-10-02 21:06:19 +04:00
|
|
|
if settingmodule.startswith(('zamboni', # typical git clone destination
|
|
|
|
'workspace', # Jenkins
|
|
|
|
'project', # vagrant VM
|
|
|
|
'freddo')):
|
|
|
|
settingmodule = settingmodule.split('.', 1)[1]
|
|
|
|
|
2012-09-26 00:26:38 +04:00
|
|
|
|
2011-02-08 08:25:33 +03:00
|
|
|
import posixpath
|
|
|
|
import re
|
2011-11-15 22:40:46 +04:00
|
|
|
import sys
|
|
|
|
|
2012-04-19 01:12:17 +04:00
|
|
|
from cef import log_cef as _log_cef
|
2011-11-15 22:40:46 +04:00
|
|
|
import MySQLdb as mysql
|
|
|
|
import sqlalchemy.pool as pool
|
|
|
|
|
|
|
|
from django.core.management import setup_environ
|
|
|
|
import commonware.log
|
|
|
|
|
2012-12-19 23:46:24 +04:00
|
|
|
from django.utils import importlib
|
|
|
|
settings = importlib.import_module(settingmodule)
|
|
|
|
|
2011-11-15 22:40:46 +04:00
|
|
|
# Pyflakes will complain about these, but they are required for setup.
|
|
|
|
setup_environ(settings)
|
2012-02-10 04:46:00 +04:00
|
|
|
from lib.log_settings_base import formatters, handlers, loggers
|
2011-02-08 08:25:33 +03:00
|
|
|
|
|
|
|
# Ugh. But this avoids any zamboni or django imports at all.
|
|
|
|
# Perhaps we can import these without any problems and we can
|
|
|
|
# remove all this.
|
2011-04-08 00:19:35 +04:00
|
|
|
from constants.applications import APPS_ALL
|
|
|
|
from constants.platforms import PLATFORMS
|
2012-04-23 22:19:14 +04:00
|
|
|
from constants.base import (ADDON_PREMIUM, STATUS_PUBLIC, STATUS_DISABLED,
|
|
|
|
STATUS_BETA, STATUS_LITE,
|
|
|
|
STATUS_LITE_AND_NOMINATED)
|
2012-02-02 02:58:00 +04:00
|
|
|
from constants.payments import (CONTRIB_CHARGEBACK, CONTRIB_PURCHASE,
|
2013-06-15 01:35:15 +04:00
|
|
|
CONTRIB_NO_CHARGE, CONTRIB_REFUND)
|
2011-02-08 08:25:33 +03:00
|
|
|
|
2011-04-08 00:19:35 +04:00
|
|
|
APP_GUIDS = dict([(app.guid, app.id) for app in APPS_ALL.values()])
|
|
|
|
PLATFORMS = dict([(plat.api_name, plat.id) for plat in PLATFORMS.values()])
|
2011-02-08 08:25:33 +03:00
|
|
|
|
|
|
|
ADDON_SLUGS_UPDATE = {
|
|
|
|
1: 'extension',
|
|
|
|
2: 'theme',
|
|
|
|
3: 'extension',
|
|
|
|
4: 'search',
|
|
|
|
5: 'item',
|
|
|
|
6: 'extension',
|
|
|
|
7: 'plugin'}
|
|
|
|
|
2011-02-09 11:10:55 +03:00
|
|
|
|
2011-04-08 00:19:35 +04:00
|
|
|
STATUSES_PUBLIC = {'STATUS_PUBLIC': STATUS_PUBLIC,
|
|
|
|
'STATUS_LITE': STATUS_LITE,
|
|
|
|
'STATUS_LITE_AND_NOMINATED': STATUS_LITE_AND_NOMINATED}
|
2011-02-09 11:10:55 +03:00
|
|
|
|
|
|
|
|
2011-02-08 08:25:33 +03:00
|
|
|
version_re = re.compile(r"""(?P<major>\d+) # major (x in x.y)
|
|
|
|
\.(?P<minor1>\d+) # minor1 (y in x.y)
|
|
|
|
\.?(?P<minor2>\d+|\*)? # minor2 (z in x.y.z)
|
|
|
|
\.?(?P<minor3>\d+|\*)? # minor3 (w in x.y.z.w)
|
|
|
|
(?P<alpha>[a|b]?) # alpha/beta
|
|
|
|
(?P<alpha_ver>\d*) # alpha/beta version
|
|
|
|
(?P<pre>pre)? # pre release
|
2013-03-31 06:09:25 +04:00
|
|
|
(?P<pre_ver>\d)? # pre release version
|
|
|
|
""",
|
|
|
|
re.VERBOSE)
|
2011-02-08 08:25:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_mirror(status, id, row):
|
|
|
|
if row['datestatuschanged']:
|
|
|
|
published = datetime.now() - row['datestatuschanged']
|
|
|
|
else:
|
|
|
|
published = timedelta(minutes=0)
|
|
|
|
|
|
|
|
if row['disabled_by_user'] or status == STATUS_DISABLED:
|
|
|
|
host = settings.PRIVATE_MIRROR_URL
|
|
|
|
elif (status == STATUS_PUBLIC
|
|
|
|
and not row['disabled_by_user']
|
2011-02-09 11:10:55 +03:00
|
|
|
and row['file_status'] in (STATUS_PUBLIC, STATUS_BETA)
|
2011-02-08 08:25:33 +03:00
|
|
|
and published > timedelta(minutes=settings.MIRROR_DELAY)
|
|
|
|
and not settings.DEBUG):
|
|
|
|
host = settings.MIRROR_URL
|
|
|
|
else:
|
|
|
|
host = settings.LOCAL_MIRROR_URL
|
|
|
|
|
|
|
|
return posixpath.join(host, str(id), row['filename'])
|
2011-11-15 22:40:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def getconn():
|
|
|
|
db = settings.SERVICES_DATABASE
|
|
|
|
return mysql.connect(host=db['HOST'], user=db['USER'],
|
|
|
|
passwd=db['PASSWORD'], db=db['NAME'])
|
|
|
|
|
|
|
|
|
|
|
|
mypool = pool.QueuePool(getconn, max_overflow=10, pool_size=5, recycle=300)
|
|
|
|
|
|
|
|
|
2012-02-10 04:46:00 +04:00
|
|
|
def log_configure():
|
|
|
|
"""You have to call this to explicity configure logging."""
|
|
|
|
cfg = {
|
|
|
|
'version': 1,
|
|
|
|
'filters': {},
|
2012-04-13 21:23:14 +04:00
|
|
|
'formatters': dict(prod=formatters['prod']),
|
|
|
|
'handlers': dict(syslog=handlers['syslog']),
|
2012-04-13 20:40:53 +04:00
|
|
|
'loggers': {
|
2012-04-21 03:42:45 +04:00
|
|
|
'z': {'handlers': ['syslog'], 'level': logging.INFO},
|
2012-04-13 20:40:53 +04:00
|
|
|
},
|
2012-02-10 04:46:00 +04:00
|
|
|
'root': {},
|
2012-08-09 03:46:33 +04:00
|
|
|
# Since this configuration is applied at import time
|
|
|
|
# in verify.py we don't want it to clobber other logs
|
|
|
|
# when imported into the marketplace Django app.
|
|
|
|
'disable_existing_loggers': False,
|
2012-02-10 04:46:00 +04:00
|
|
|
}
|
|
|
|
dictconfig.dictConfig(cfg)
|
2011-11-15 22:40:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def log_exception(data):
|
2012-09-28 22:03:07 +04:00
|
|
|
# Note: although this logs exceptions, it logs at the info level so that
|
|
|
|
# on prod, we log at the error level and result in no logs on prod.
|
|
|
|
typ, value, discard = sys.exc_info()
|
|
|
|
error_log = logging.getLogger('z.receipt')
|
2013-09-06 02:18:15 +04:00
|
|
|
error_log.exception(u'Type: %s, %s. Data: %s' % (typ, value, data))
|
2011-11-15 22:40:46 +04:00
|
|
|
|
|
|
|
|
2012-09-28 22:03:07 +04:00
|
|
|
def log_info(msg):
|
|
|
|
error_log = logging.getLogger('z.receipt')
|
|
|
|
error_log.info(msg)
|
2012-04-19 01:12:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
def log_cef(request, app, msg, longer):
|
|
|
|
"""Log receipt transactions to the CEF library."""
|
|
|
|
c = {'cef.product': getattr(settings, 'CEF_PRODUCT', 'AMO'),
|
|
|
|
'cef.vendor': getattr(settings, 'CEF_VENDOR', 'Mozilla'),
|
|
|
|
'cef.version': getattr(settings, 'CEF_VERSION', '0'),
|
|
|
|
'cef.device_version': getattr(settings, 'CEF_DEVICE_VERSION', '0'),
|
|
|
|
'cef.file': getattr(settings, 'CEF_FILE', 'syslog'), }
|
|
|
|
|
|
|
|
kwargs = {'username': getattr(request, 'amo_user', ''),
|
|
|
|
'signature': 'RECEIPT%s' % msg.upper(),
|
|
|
|
'msg': longer, 'config': c,
|
|
|
|
'cs2': app, 'cs2Label': 'ReceiptTransaction'}
|
|
|
|
return _log_cef('Receipt %s' % msg, 5, request, **kwargs)
|