| 1 |
import os |
| 2 |
import re |
| 3 |
from django.core.management.base import BaseCommand, CommandError |
| 4 |
from django.core.management.commands.runserver import naiveip_re |
| 5 |
from django.core.servers.basehttp import get_internal_wsgi_application |
| 6 |
from livereload import Server |
| 7 |
|
| 8 |
|
| 9 |
class Command(BaseCommand): |
| 10 |
help = 'Runs the development server with livereload enabled.' |
| 11 |
|
| 12 |
def add_arguments(self, parser): |
| 13 |
parser.add_argument('addrport', |
| 14 |
nargs='?', |
| 15 |
default='127.0.0.1:8000', |
| 16 |
help='host and optional port the django server should listen on (default: 127.0.0.1:8000)') |
| 17 |
parser.add_argument('-l', '--liveport', |
| 18 |
type=int, |
| 19 |
default=35729, |
| 20 |
help='port the livereload server should listen on (default: 35729)') |
| 21 |
|
| 22 |
def handle(self, *args, **options): |
| 23 |
m = re.match(naiveip_re, options['addrport']) |
| 24 |
if m is None: |
| 25 |
raise CommandError('"%s" is not a valid port number ' |
| 26 |
'or address:port pair.' % options['addrport']) |
| 27 |
addr, _ipv4, _ipv6, _fqdn, port = m.groups() |
| 28 |
if not port.isdigit(): |
| 29 |
raise CommandError("%r is not a valid port number." % port) |
| 30 |
|
| 31 |
if addr: |
| 32 |
if _ipv6: |
| 33 |
raise CommandError('IPv6 addresses are currently not supported.') |
| 34 |
|
| 35 |
|
| 36 |
application = get_internal_wsgi_application() |
| 37 |
server = Server(application) |
| 38 |
|
| 39 |
for file in os.listdir('.'): |
| 40 |
if file[0] != '.' and file[:2] != '__' and os.path.isdir(file): |
| 41 |
server.watch(file) |
| 42 |
|
| 43 |
server.serve(host=addr, port=port, liveport=options['liveport']) |