Add global --configuration option in Django >= 1.8.

This commit is contained in:
Jannis Leidel 2015-02-13 21:47:08 +01:00
parent 9be0c4f700
commit f35e7e57e0
3 changed files with 37 additions and 28 deletions

View file

@ -2,9 +2,9 @@ import imp
import logging
import os
import sys
from optparse import make_option
from optparse import OptionParser, make_option
from django import VERSION as DJ_VERSION
from django import VERSION as DJANGO_VERSION
from django.conf import ENVIRONMENT_VARIABLE as SETTINGS_ENVIRONMENT_VARIABLE
from django.core.exceptions import ImproperlyConfigured
from django.core.management import base
@ -16,10 +16,10 @@ installed = False
CONFIGURATION_ENVIRONMENT_VARIABLE = 'DJANGO_CONFIGURATION'
CONFIGURATION_ARGUMENT = '--configuration'
CONFIGURATION_ARGUMENT_HELP = ('The name of the configuration class to load, e.g. '
'"Development". If this isn\'t provided, the '
'DJANGO_CONFIGURATION environment variable will '
'be used.')
CONFIGURATION_ARGUMENT_HELP = ('The name of the configuration class to load, '
'e.g. "Development". If this isn\'t provided, '
'the DJANGO_CONFIGURATION environment '
'variable will be used.')
configuration_options = (make_option(CONFIGURATION_ARGUMENT,
@ -29,8 +29,17 @@ configuration_options = (make_option(CONFIGURATION_ARGUMENT,
def install(check_options=False):
global installed
if not installed:
if DJ_VERSION >= (1, 8):
pass
if DJANGO_VERSION >= (1, 8):
orig_create_parser = base.BaseCommand.create_parser
def create_parser(self, prog_name, subcommand):
parser = orig_create_parser(self, prog_name, subcommand)
if not isinstance(parser, OptionParser):
# probably argparse, let's not import argparse though
parser.add_argument(CONFIGURATION_ARGUMENT,
help=CONFIGURATION_ARGUMENT_HELP)
return parser
base.BaseCommand.create_parser = create_parser
else:
# add the configuration option to all management commands
base.BaseCommand.option_list += configuration_options
@ -71,10 +80,10 @@ class ConfigurationImporter(object):
def check_options(self):
# django switched to argparse in version 1.8
if DJ_VERSION >= (1, 8):
if DJANGO_VERSION >= (1, 8):
parser = base.CommandParser(None,
usage="%(prog)s subcommand [options] [args]",
add_help=False)
usage="%(prog)s subcommand [options] [args]",
add_help=False)
parser.add_argument('--settings')
parser.add_argument('--pythonpath')
parser.add_argument(CONFIGURATION_ARGUMENT,

View file

@ -1,17 +0,0 @@
import os
import subprocess
PROJECT_DIR = os.getcwd()
TEST_PROJECT_DIR = os.path.join(PROJECT_DIR, 'test_project')
def test_configuration_argument_in_cli():
"""Verify that's configuration option has been added to managements commands"""
os.chdir(TEST_PROJECT_DIR)
p = subprocess.Popen(['python', 'manage.py', 'test',
'--help'], stdout=subprocess.PIPE)
assert '--configuration' in p.communicate()[0].decode('UTF-8')
p = subprocess.Popen(['python', 'manage.py', 'runserver',
'--help'], stdout=subprocess.PIPE)
assert '--configuration' in p.communicate()[0].decode('UTF-8')
os.chdir(PROJECT_DIR)

View file

@ -1,4 +1,5 @@
import os
import subprocess
import sys
from django.conf import global_settings
@ -9,6 +10,9 @@ from mock import patch
from configurations.importer import ConfigurationImporter
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
TEST_PROJECT_DIR = os.path.join(ROOT_DIR, 'test_project')
class MainTests(TestCase):
@ -92,3 +96,16 @@ class MainTests(TestCase):
importer = ConfigurationImporter(check_options=True)
self.assertEqual(importer.module, 'tests.settings.main')
self.assertEqual(importer.name, 'Test')
def test_configuration_argument_in_cli(self):
"""
Verify that's configuration option has been added to managements
commands
"""
manage_args = ['python', os.path.join(ROOT_DIR, 'manage.py')]
proc = subprocess.Popen(manage_args + ['test', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0])
proc = subprocess.Popen(manage_args + ['runserver', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0])