2015-06-11 23:05:30 +03:00
# Copyright (c) 2015 Ansible, Inc.
2013-06-23 21:21:02 +04:00
# All Rights Reserved.
import os
import sys
2013-07-15 21:29:06 +04:00
import warnings
2013-06-23 21:21:02 +04:00
2016-05-20 23:46:41 +03:00
__version__ = ' 3.1.0 '
2015-02-12 00:42:55 +03:00
2013-06-23 21:21:02 +04:00
__all__ = [ ' __version__ ' ]
# Check for the presence/absence of "devonly" module to determine if running
# from a source code checkout or release packaage.
try :
2015-02-05 18:40:22 +03:00
import awx . devonly # noqa
2013-06-23 21:21:02 +04:00
MODE = ' development '
2014-02-07 01:52:09 +04:00
except ImportError : # pragma: no cover
2013-06-23 21:21:02 +04:00
MODE = ' production '
2013-06-25 19:35:40 +04:00
def find_commands ( management_dir ) :
# Modified version of function from django/core/management/__init__.py.
command_dir = os . path . join ( management_dir , ' commands ' )
commands = [ ]
try :
for f in os . listdir ( command_dir ) :
if f . startswith ( ' _ ' ) :
continue
elif f . endswith ( ' .py ' ) and f [ : - 3 ] not in commands :
commands . append ( f [ : - 3 ] )
2014-02-07 01:52:09 +04:00
elif f . endswith ( ' .pyc ' ) and f [ : - 4 ] not in commands : # pragma: no cover
2013-06-25 19:35:40 +04:00
commands . append ( f [ : - 4 ] )
except OSError :
pass
return commands
2013-07-29 00:30:19 +04:00
def prepare_env ( ) :
2013-06-23 21:21:02 +04:00
# Update the default settings environment variable based on current mode.
os . environ . setdefault ( ' DJANGO_SETTINGS_MODULE ' , ' awx.settings. %s ' % MODE )
2013-07-15 21:29:06 +04:00
# Hide DeprecationWarnings when running in production. Need to first load
# settings to apply our filter after Django's own warnings filter.
from django . conf import settings
2014-02-07 01:52:09 +04:00
if not settings . DEBUG : # pragma: no cover
2013-07-15 21:29:06 +04:00
warnings . simplefilter ( ' ignore ' , DeprecationWarning )
2013-06-25 19:35:40 +04:00
# Monkeypatch Django find_commands to also work with .pyc files.
import django . core . management
django . core . management . find_commands = find_commands
2013-07-29 00:30:19 +04:00
# Fixup sys.modules reference to django.utils.six to allow jsonfield to
# work when using Django 1.4.
import django . utils
try :
import django . utils . six
2014-02-07 01:52:09 +04:00
except ImportError : # pragma: no cover
2013-07-29 00:30:19 +04:00
import six
sys . modules [ ' django.utils.six ' ] = sys . modules [ ' six ' ]
django . utils . six = sys . modules [ ' django.utils.six ' ]
2015-02-05 18:40:22 +03:00
from django . utils import six # noqa
2013-10-16 19:43:18 +04:00
# Use the AWX_TEST_DATABASE_* environment variables to specify the test
# database settings to use when management command is run as an external
2013-10-01 06:08:05 +04:00
# program via unit tests.
2014-02-07 01:52:09 +04:00
for opt in ( ' ENGINE ' , ' NAME ' , ' USER ' , ' PASSWORD ' , ' HOST ' , ' PORT ' ) : # pragma: no cover
2013-10-16 19:43:18 +04:00
if os . environ . get ( ' AWX_TEST_DATABASE_ %s ' % opt , None ) :
settings . DATABASES [ ' default ' ] [ opt ] = os . environ [ ' AWX_TEST_DATABASE_ %s ' % opt ]
2014-02-05 08:04:58 +04:00
# Disable capturing all SQL queries in memory when in DEBUG mode.
if settings . DEBUG and not getattr ( settings , ' SQL_DEBUG ' , True ) :
2016-02-02 22:50:42 +03:00
from django . db . backends . base . base import BaseDatabaseWrapper
from django . db . backends . utils import CursorWrapper
2014-02-05 08:04:58 +04:00
BaseDatabaseWrapper . make_debug_cursor = lambda self , cursor : CursorWrapper ( cursor , self )
2013-07-29 00:30:19 +04:00
2016-02-02 22:50:42 +03:00
# Use the default devserver addr/port defined in settings for runserver.
default_addr = getattr ( settings , ' DEVSERVER_DEFAULT_ADDR ' , ' 127.0.0.1 ' )
default_port = getattr ( settings , ' DEVSERVER_DEFAULT_PORT ' , 8000 )
from django . core . management . commands import runserver as core_runserver
original_handle = core_runserver . Command . handle
def handle ( self , * args , * * options ) :
if not options . get ( ' addrport ' ) :
options [ ' addrport ' ] = ' %s : %d ' % ( default_addr , int ( default_port ) )
elif options . get ( ' addrport ' ) . isdigit ( ) :
options [ ' addrport ' ] = ' %s : %d ' % ( default_addr , int ( options [ ' addrport ' ] ) )
return original_handle ( self , * args , * * options )
core_runserver . Command . handle = handle
2013-07-29 00:30:19 +04:00
def manage ( ) :
# Prepare the AWX environment.
prepare_env ( )
2013-06-25 19:35:40 +04:00
# Now run the command (or display the version).
2014-08-13 09:03:52 +04:00
from django . conf import settings
2013-06-23 21:21:02 +04:00
from django . core . management import execute_from_command_line
2014-02-07 01:52:09 +04:00
if len ( sys . argv ) > = 2 and sys . argv [ 1 ] in ( ' version ' , ' --version ' ) : # pragma: no cover
2014-01-29 23:04:31 +04:00
sys . stdout . write ( ' %s \n ' % __version__ )
2014-08-13 09:03:52 +04:00
# If running as a user without permission to read settings, display an
# error message. Allow --help to still work.
2014-08-13 17:53:34 +04:00
elif settings . SECRET_KEY == ' permission-denied ' :
if len ( sys . argv ) == 1 or len ( sys . argv ) > = 2 and sys . argv [ 1 ] in ( ' -h ' , ' --help ' , ' help ' ) :
2014-08-13 09:03:52 +04:00
execute_from_command_line ( sys . argv )
sys . stdout . write ( ' \n ' )
prog = os . path . basename ( sys . argv [ 0 ] )
2014-08-13 17:53:34 +04:00
sys . stdout . write ( ' Permission denied: %s must be run as root or awx. \n ' % prog )
2014-08-13 09:03:52 +04:00
sys . exit ( 1 )
2013-06-23 21:21:02 +04:00
else :
execute_from_command_line ( sys . argv )