|
0
|
1 |
from django.core.management.base import BaseCommand, CommandError |
|
|
2 |
from optparse import make_option |
|
|
3 |
import os |
|
|
4 |
import sys |
|
|
5 |
|
|
|
6 |
class Command(BaseCommand): |
|
|
7 |
option_list = BaseCommand.option_list + ( |
|
|
8 |
make_option('--noreload', action='store_false', dest='use_reloader', default=True, |
|
|
9 |
help='Tells Django to NOT use the auto-reloader.'), |
|
|
10 |
make_option('--adminmedia', dest='admin_media_path', default='', |
|
|
11 |
help='Specifies the directory from which to serve admin media.'), |
|
|
12 |
) |
|
|
13 |
help = "Starts a lightweight Web server for development." |
|
|
14 |
args = '[optional port number, or ipaddr:port]' |
|
|
15 |
|
|
|
16 |
# Validation is called explicitly each time the server is reloaded. |
|
|
17 |
requires_model_validation = False |
|
|
18 |
|
|
|
19 |
def handle(self, addrport='', *args, **options): |
|
|
20 |
import django |
|
|
21 |
from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException |
|
|
22 |
from django.core.handlers.wsgi import WSGIHandler |
|
|
23 |
if args: |
|
|
24 |
raise CommandError('Usage is runserver %s' % self.args) |
|
|
25 |
if not addrport: |
|
|
26 |
addr = '' |
|
|
27 |
port = '8000' |
|
|
28 |
else: |
|
|
29 |
try: |
|
|
30 |
addr, port = addrport.split(':') |
|
|
31 |
except ValueError: |
|
|
32 |
addr, port = '', addrport |
|
|
33 |
if not addr: |
|
|
34 |
addr = '127.0.0.1' |
|
|
35 |
|
|
|
36 |
if not port.isdigit(): |
|
|
37 |
raise CommandError("%r is not a valid port number." % port) |
|
|
38 |
|
|
|
39 |
use_reloader = options.get('use_reloader', True) |
|
|
40 |
admin_media_path = options.get('admin_media_path', '') |
|
|
41 |
shutdown_message = options.get('shutdown_message', '') |
|
|
42 |
quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C' |
|
|
43 |
|
|
|
44 |
def inner_run(): |
|
|
45 |
from django.conf import settings |
|
|
46 |
from django.utils import translation |
|
|
47 |
print "Validating models..." |
|
|
48 |
self.validate(display_num_errors=True) |
|
|
49 |
print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE) |
|
|
50 |
print "Development server is running at http://%s:%s/" % (addr, port) |
|
|
51 |
print "Quit the server with %s." % quit_command |
|
|
52 |
|
|
|
53 |
# django.core.management.base forces the locale to en-us. We should |
|
|
54 |
# set it up correctly for the first request (particularly important |
|
|
55 |
# in the "--noreload" case). |
|
|
56 |
translation.activate(settings.LANGUAGE_CODE) |
|
|
57 |
|
|
|
58 |
try: |
|
|
59 |
handler = AdminMediaHandler(WSGIHandler(), admin_media_path) |
|
|
60 |
run(addr, int(port), handler) |
|
|
61 |
except WSGIServerException, e: |
|
|
62 |
# Use helpful error messages instead of ugly tracebacks. |
|
|
63 |
ERRORS = { |
|
|
64 |
13: "You don't have permission to access that port.", |
|
|
65 |
98: "That port is already in use.", |
|
|
66 |
99: "That IP address can't be assigned-to.", |
|
|
67 |
} |
|
|
68 |
try: |
|
|
69 |
error_text = ERRORS[e.args[0].args[0]] |
|
|
70 |
except (AttributeError, KeyError): |
|
|
71 |
error_text = str(e) |
|
|
72 |
sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n') |
|
|
73 |
# Need to use an OS exit because sys.exit doesn't work in a thread |
|
|
74 |
os._exit(1) |
|
|
75 |
except KeyboardInterrupt: |
|
|
76 |
if shutdown_message: |
|
|
77 |
print shutdown_message |
|
|
78 |
sys.exit(0) |
|
|
79 |
|
|
|
80 |
if use_reloader: |
|
|
81 |
from django.utils import autoreload |
|
|
82 |
autoreload.main(inner_run) |
|
|
83 |
else: |
|
|
84 |
inner_run() |