Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | # |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # BitBake Toaster Implementation |
| 6 | # |
| 7 | # Copyright (C) 2013 Intel Corporation |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License version 2 as |
| 11 | # published by the Free Software Foundation. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License along |
| 19 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | |
| 22 | # Django settings for Toaster project. |
| 23 | |
| 24 | import os, re |
| 25 | |
| 26 | DEBUG = True |
| 27 | TEMPLATE_DEBUG = DEBUG |
| 28 | |
| 29 | # Set to True to see the SQL queries in console |
| 30 | SQL_DEBUG = False |
| 31 | if os.environ.get("TOASTER_SQLDEBUG", None) is not None: |
| 32 | SQL_DEBUG = True |
| 33 | |
| 34 | |
| 35 | ADMINS = ( |
| 36 | # ('Your Name', 'your_email@example.com'), |
| 37 | ) |
| 38 | |
| 39 | MANAGERS = ADMINS |
| 40 | |
| 41 | DATABASES = { |
| 42 | 'default': { |
| 43 | 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. |
| 44 | 'NAME': 'toaster.sqlite', # Or path to database file if using sqlite3. |
| 45 | 'USER': '', |
| 46 | 'PASSWORD': '', |
| 47 | 'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. |
| 48 | 'PORT': '3306', # Set to empty string for default. |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | # Needed when Using sqlite especially to add a longer timeout for waiting |
| 53 | # for the database lock to be released |
| 54 | # https://docs.djangoproject.com/en/1.6/ref/databases/#database-is-locked-errors |
| 55 | if 'sqlite' in DATABASES['default']['ENGINE']: |
| 56 | DATABASES['default']['OPTIONS'] = { 'timeout': 20 } |
| 57 | |
| 58 | # Reinterpret database settings if we have DATABASE_URL environment variable defined |
| 59 | |
| 60 | if 'DATABASE_URL' in os.environ: |
| 61 | dburl = os.environ['DATABASE_URL'] |
| 62 | if dburl.startswith('sqlite3://'): |
| 63 | result = re.match('sqlite3://(.*)', dburl) |
| 64 | if result is None: |
| 65 | raise Exception("ERROR: Could not read sqlite database url: %s" % dburl) |
| 66 | DATABASES['default'] = { |
| 67 | 'ENGINE': 'django.db.backends.sqlite3', |
| 68 | 'NAME': result.group(1), |
| 69 | 'USER': '', |
| 70 | 'PASSWORD': '', |
| 71 | 'HOST': '', |
| 72 | 'PORT': '', |
| 73 | } |
| 74 | elif dburl.startswith('mysql://'): |
| 75 | # URL must be in this form: mysql://user:pass@host:port/name |
| 76 | result = re.match(r"mysql://([^:]*):([^@]*)@([^:]*):(\d+)/([^/]*)", dburl) |
| 77 | if result is None: |
| 78 | raise Exception("ERROR: Could not read mysql database url: %s" % dburl) |
| 79 | DATABASES['default'] = { |
| 80 | 'ENGINE': 'django.db.backends.mysql', |
| 81 | 'NAME': result.group(5), |
| 82 | 'USER': result.group(1), |
| 83 | 'PASSWORD': result.group(2), |
| 84 | 'HOST': result.group(3), |
| 85 | 'PORT': result.group(4), |
| 86 | } |
| 87 | else: |
| 88 | raise Exception("FIXME: Please implement missing database url schema for url: %s" % dburl) |
| 89 | |
| 90 | |
| 91 | if 'TOASTER_MANAGED' in os.environ and os.environ['TOASTER_MANAGED'] == "1": |
| 92 | MANAGED = True |
| 93 | else: |
| 94 | MANAGED = False |
| 95 | |
| 96 | # Allows current database settings to be exported as a DATABASE_URL environment variable value |
| 97 | |
| 98 | def getDATABASE_URL(): |
| 99 | d = DATABASES['default'] |
| 100 | if d['ENGINE'] == 'django.db.backends.sqlite3': |
| 101 | if d['NAME'] == ':memory:': |
| 102 | return 'sqlite3://:memory:' |
| 103 | elif d['NAME'].startswith("/"): |
| 104 | return 'sqlite3://' + d['NAME'] |
| 105 | return "sqlite3://" + os.path.join(os.getcwd(), d['NAME']) |
| 106 | |
| 107 | elif d['ENGINE'] == 'django.db.backends.mysql': |
| 108 | return "mysql://" + d['USER'] + ":" + d['PASSWORD'] + "@" + d['HOST'] + ":" + d['PORT'] + "/" + d['NAME'] |
| 109 | |
| 110 | raise Exception("FIXME: Please implement missing database url schema for engine: %s" % d['ENGINE']) |
| 111 | |
| 112 | |
| 113 | |
| 114 | # Hosts/domain names that are valid for this site; required if DEBUG is False |
| 115 | # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts |
| 116 | ALLOWED_HOSTS = [] |
| 117 | |
| 118 | # Local time zone for this installation. Choices can be found here: |
| 119 | # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name |
| 120 | # although not all choices may be available on all operating systems. |
| 121 | # In a Windows environment this must be set to your system time zone. |
| 122 | |
| 123 | # Always use local computer's time zone, find |
| 124 | import hashlib |
| 125 | if 'TZ' in os.environ: |
| 126 | TIME_ZONE = os.environ['TZ'] |
| 127 | else: |
| 128 | # need to read the /etc/localtime file which is the libc standard |
| 129 | # and do a reverse-mapping to /usr/share/zoneinfo/; |
| 130 | # since the timezone may match any number of identical timezone definitions, |
| 131 | |
| 132 | zonefilelist = {} |
| 133 | ZONEINFOPATH = '/usr/share/zoneinfo/' |
| 134 | for dirpath, dirnames, filenames in os.walk(ZONEINFOPATH): |
| 135 | for fn in filenames: |
| 136 | filepath = os.path.join(dirpath, fn) |
| 137 | zonename = filepath.lstrip(ZONEINFOPATH).strip() |
| 138 | try: |
| 139 | import pytz |
| 140 | from pytz.exceptions import UnknownTimeZoneError |
| 141 | pass |
| 142 | try: |
| 143 | if pytz.timezone(zonename) is not None: |
| 144 | zonefilelist[hashlib.md5(open(filepath).read()).hexdigest()] = zonename |
| 145 | except UnknownTimeZoneError, ValueError: |
| 146 | # we expect timezone failures here, just move over |
| 147 | pass |
| 148 | except ImportError: |
| 149 | zonefilelist[hashlib.md5(open(filepath).read()).hexdigest()] = zonename |
| 150 | |
| 151 | TIME_ZONE = zonefilelist[hashlib.md5(open('/etc/localtime').read()).hexdigest()] |
| 152 | |
| 153 | # Language code for this installation. All choices can be found here: |
| 154 | # http://www.i18nguy.com/unicode/language-identifiers.html |
| 155 | LANGUAGE_CODE = 'en-us' |
| 156 | |
| 157 | SITE_ID = 1 |
| 158 | |
| 159 | # If you set this to False, Django will make some optimizations so as not |
| 160 | # to load the internationalization machinery. |
| 161 | USE_I18N = True |
| 162 | |
| 163 | # If you set this to False, Django will not format dates, numbers and |
| 164 | # calendars according to the current locale. |
| 165 | USE_L10N = True |
| 166 | |
| 167 | # If you set this to False, Django will not use timezone-aware datetimes. |
| 168 | USE_TZ = True |
| 169 | |
| 170 | # Absolute filesystem path to the directory that will hold user-uploaded files. |
| 171 | # Example: "/var/www/example.com/media/" |
| 172 | MEDIA_ROOT = '' |
| 173 | |
| 174 | # URL that handles the media served from MEDIA_ROOT. Make sure to use a |
| 175 | # trailing slash. |
| 176 | # Examples: "http://example.com/media/", "http://media.example.com/" |
| 177 | MEDIA_URL = '' |
| 178 | |
| 179 | # Absolute path to the directory static files should be collected to. |
| 180 | # Don't put anything in this directory yourself; store your static files |
| 181 | # in apps' "static/" subdirectories and in STATICFILES_DIRS. |
| 182 | # Example: "/var/www/example.com/static/" |
| 183 | STATIC_ROOT = '' |
| 184 | |
| 185 | # URL prefix for static files. |
| 186 | # Example: "http://example.com/static/", "http://static.example.com/" |
| 187 | STATIC_URL = '/static/' |
| 188 | |
| 189 | # Additional locations of static files |
| 190 | STATICFILES_DIRS = ( |
| 191 | # Put strings here, like "/home/html/static" or "C:/www/django/static". |
| 192 | # Always use forward slashes, even on Windows. |
| 193 | # Don't forget to use absolute paths, not relative paths. |
| 194 | ) |
| 195 | |
| 196 | # List of finder classes that know how to find static files in |
| 197 | # various locations. |
| 198 | STATICFILES_FINDERS = ( |
| 199 | 'django.contrib.staticfiles.finders.FileSystemFinder', |
| 200 | 'django.contrib.staticfiles.finders.AppDirectoriesFinder', |
| 201 | # 'django.contrib.staticfiles.finders.DefaultStorageFinder', |
| 202 | ) |
| 203 | |
| 204 | # Make this unique, and don't share it with anybody. |
| 205 | SECRET_KEY = 'NOT_SUITABLE_FOR_HOSTED_DEPLOYMENT' |
| 206 | |
| 207 | # List of callables that know how to import templates from various sources. |
| 208 | TEMPLATE_LOADERS = ( |
| 209 | 'django.template.loaders.filesystem.Loader', |
| 210 | 'django.template.loaders.app_directories.Loader', |
| 211 | # 'django.template.loaders.eggs.Loader', |
| 212 | ) |
| 213 | |
| 214 | MIDDLEWARE_CLASSES = ( |
| 215 | 'django.middleware.common.CommonMiddleware', |
| 216 | 'django.contrib.sessions.middleware.SessionMiddleware', |
| 217 | 'django.middleware.csrf.CsrfViewMiddleware', |
| 218 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 219 | 'django.contrib.messages.middleware.MessageMiddleware', |
| 220 | # Uncomment the next line for simple clickjacking protection: |
| 221 | # 'django.middleware.clickjacking.XFrameOptionsMiddleware', |
| 222 | ) |
| 223 | |
| 224 | CACHES = { |
| 225 | # 'default': { |
| 226 | # 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', |
| 227 | # 'LOCATION': '127.0.0.1:11211', |
| 228 | # }, |
| 229 | 'default': { |
| 230 | 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', |
| 231 | 'LOCATION': '/tmp/django-default-cache', |
| 232 | 'TIMEOUT': 1, |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | from os.path import dirname as DN |
| 238 | SITE_ROOT=DN(DN(os.path.abspath(__file__))) |
| 239 | |
| 240 | import subprocess |
| 241 | TOASTER_BRANCH = subprocess.Popen('git branch | grep "^* " | tr -d "* "', cwd = SITE_ROOT, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] |
| 242 | TOASTER_REVISION = subprocess.Popen('git rev-parse HEAD ', cwd = SITE_ROOT, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] |
| 243 | |
| 244 | ROOT_URLCONF = 'toastermain.urls' |
| 245 | |
| 246 | # Python dotted path to the WSGI application used by Django's runserver. |
| 247 | WSGI_APPLICATION = 'toastermain.wsgi.application' |
| 248 | |
| 249 | TEMPLATE_DIRS = ( |
| 250 | # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". |
| 251 | # Always use forward slashes, even on Windows. |
| 252 | # Don't forget to use absolute paths, not relative paths. |
| 253 | ) |
| 254 | |
| 255 | TEMPLATE_CONTEXT_PROCESSORS = ('django.contrib.auth.context_processors.auth', |
| 256 | 'django.core.context_processors.debug', |
| 257 | 'django.core.context_processors.i18n', |
| 258 | 'django.core.context_processors.media', |
| 259 | 'django.core.context_processors.static', |
| 260 | 'django.core.context_processors.tz', |
| 261 | 'django.contrib.messages.context_processors.messages', |
| 262 | "django.core.context_processors.request", |
| 263 | 'toastergui.views.managedcontextprocessor', |
| 264 | ) |
| 265 | |
| 266 | INSTALLED_APPS = ( |
| 267 | 'django.contrib.auth', |
| 268 | 'django.contrib.contenttypes', |
| 269 | 'django.contrib.messages', |
| 270 | 'django.contrib.sessions', |
| 271 | 'django.contrib.admin', |
| 272 | 'django.contrib.staticfiles', |
| 273 | |
| 274 | # Uncomment the next line to enable admin documentation: |
| 275 | # 'django.contrib.admindocs', |
| 276 | 'django.contrib.humanize', |
| 277 | 'bldcollector', |
| 278 | 'toastermain', |
| 279 | 'south', |
| 280 | ) |
| 281 | |
| 282 | |
| 283 | INTERNAL_IPS = ['127.0.0.1', '192.168.2.28'] |
| 284 | |
| 285 | # Load django-fresh is TOASTER_DEVEL is set, and the module is available |
| 286 | FRESH_ENABLED = False |
| 287 | if os.environ.get('TOASTER_DEVEL', None) is not None: |
| 288 | try: |
| 289 | import fresh |
| 290 | MIDDLEWARE_CLASSES = ("fresh.middleware.FreshMiddleware",) + MIDDLEWARE_CLASSES |
| 291 | INSTALLED_APPS = INSTALLED_APPS + ('fresh',) |
| 292 | FRESH_ENABLED = True |
| 293 | except: |
| 294 | pass |
| 295 | |
| 296 | DEBUG_PANEL_ENABLED = False |
| 297 | if os.environ.get('TOASTER_DEVEL', None) is not None: |
| 298 | try: |
| 299 | import debug_toolbar, debug_panel |
| 300 | MIDDLEWARE_CLASSES = ('debug_panel.middleware.DebugPanelMiddleware',) + MIDDLEWARE_CLASSES |
| 301 | #MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + ('debug_toolbar.middleware.DebugToolbarMiddleware',) |
| 302 | INSTALLED_APPS = INSTALLED_APPS + ('debug_toolbar','debug_panel',) |
| 303 | DEBUG_PANEL_ENABLED = True |
| 304 | |
| 305 | # this cache backend will be used by django-debug-panel |
| 306 | CACHES['debug-panel'] = { |
| 307 | 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', |
| 308 | 'LOCATION': '/var/tmp/debug-panel-cache', |
| 309 | 'TIMEOUT': 300, |
| 310 | 'OPTIONS': { |
| 311 | 'MAX_ENTRIES': 200 |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | except: |
| 316 | pass |
| 317 | |
| 318 | |
| 319 | SOUTH_TESTS_MIGRATE = False |
| 320 | |
| 321 | |
| 322 | # We automatically detect and install applications here if |
| 323 | # they have a 'models.py' or 'views.py' file |
| 324 | import os |
| 325 | currentdir = os.path.dirname(__file__) |
| 326 | for t in os.walk(os.path.dirname(currentdir)): |
| 327 | modulename = os.path.basename(t[0]) |
| 328 | #if we have a virtualenv skip it to avoid incorrect imports |
| 329 | if os.environ.has_key('VIRTUAL_ENV') and os.environ['VIRTUAL_ENV'] in t[0]: |
| 330 | continue |
| 331 | |
| 332 | if ("views.py" in t[2] or "models.py" in t[2]) and not modulename in INSTALLED_APPS: |
| 333 | INSTALLED_APPS = INSTALLED_APPS + (modulename,) |
| 334 | |
| 335 | # A sample logging configuration. The only tangible logging |
| 336 | # performed by this configuration is to send an email to |
| 337 | # the site admins on every HTTP 500 error when DEBUG=False. |
| 338 | # See http://docs.djangoproject.com/en/dev/topics/logging for |
| 339 | # more details on how to customize your logging configuration. |
| 340 | LOGGING = { |
| 341 | 'version': 1, |
| 342 | 'disable_existing_loggers': False, |
| 343 | 'filters': { |
| 344 | 'require_debug_false': { |
| 345 | '()': 'django.utils.log.RequireDebugFalse' |
| 346 | } |
| 347 | }, |
| 348 | 'formatters': { |
| 349 | 'datetime': { |
| 350 | 'format': '%(asctime)s %(levelname)s %(message)s' |
| 351 | } |
| 352 | }, |
| 353 | 'handlers': { |
| 354 | 'mail_admins': { |
| 355 | 'level': 'ERROR', |
| 356 | 'filters': ['require_debug_false'], |
| 357 | 'class': 'django.utils.log.AdminEmailHandler' |
| 358 | }, |
| 359 | 'console': { |
| 360 | 'level': 'DEBUG', |
| 361 | 'class': 'logging.StreamHandler', |
| 362 | 'formatter': 'datetime', |
| 363 | } |
| 364 | }, |
| 365 | 'loggers': { |
| 366 | 'toaster' : { |
| 367 | 'handlers': ['console'], |
| 368 | 'level': 'DEBUG', |
| 369 | }, |
| 370 | 'django.request': { |
| 371 | 'handlers': ['console'], |
| 372 | 'level': 'WARN', |
| 373 | 'propagate': True, |
| 374 | }, |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if DEBUG and SQL_DEBUG: |
| 379 | LOGGING['loggers']['django.db.backends'] = { |
| 380 | 'level': 'DEBUG', |
| 381 | 'handlers': ['console'], |
| 382 | } |
| 383 | |
| 384 | |
| 385 | # If we're using sqlite, we need to tweak the performance a bit |
| 386 | from django.db.backends.signals import connection_created |
| 387 | def activate_synchronous_off(sender, connection, **kwargs): |
| 388 | if connection.vendor == 'sqlite': |
| 389 | cursor = connection.cursor() |
| 390 | cursor.execute('PRAGMA synchronous = 0;') |
| 391 | connection_created.connect(activate_synchronous_off) |
| 392 | # |
| 393 | |
| 394 | |
| 395 | class InvalidString(str): |
| 396 | def __mod__(self, other): |
| 397 | from django.template.base import TemplateSyntaxError |
| 398 | raise TemplateSyntaxError( |
| 399 | "Undefined variable or unknown value for: \"%s\"" % other) |
| 400 | |
| 401 | TEMPLATE_STRING_IF_INVALID = InvalidString("%s") |
| 402 | |
| 403 | import sys |
| 404 | sys.path.append( |
| 405 | os.path.join( |
| 406 | os.path.join( |
| 407 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 408 | "contrib"), |
| 409 | "django-aggregate-if-master") |
| 410 | ) |