Ticket #2933: r3909-bonjour.patch.diff

File r3909-bonjour.patch.diff, 3.9 KB (added by gandalf@…, 9 years ago)

Bonjour patch for #2933

  • django/core/management.py

     
    66import os, re, shutil, sys, textwrap
    77from optparse import OptionParser
    88from django.utils import termcolors
     9import atexit
    910
    1011# For Python 2.3
    1112if not hasattr(__builtins__, 'set'):
     
    10841085        sys.stderr.write(s.read())
    10851086        sys.exit(1)
    10861087
    1087 def runserver(addr, port, use_reloader=True, admin_media_dir=''):
     1088DNS_SD_PID = None
     1089#code adopted from turbogears startup.py which is MIT licensed
     1090#http://www.turbogears.org/svn/turbogears/trunk/turbogears/startup.py
     1091def _start_bonjour(host, port):
     1092    global DNS_SD_PID
     1093    if DNS_SD_PID:
     1094        return
     1095       
     1096    #only local. works right now
     1097    host = '.'
     1098    type = "_http._tcp"
     1099    from django.conf import settings
     1100    name  = settings.SETTINGS_MODULE
     1101
     1102    cmds = [['/usr/bin/avahi-publish-service', ["-H", host, name, type, port]],
     1103            ['/usr/bin/dns-sd', ['-R', name, type, host, port, "path=/"]]]
     1104
     1105    for cmd, args in cmds:
     1106        # TODO:. This check is flawed.  If one has both services installed and
     1107        # avahi isn't the one running, then this won't work.  We should either
     1108        # try registering with both or checking what service is running and use
     1109        # that.  Program availability on the filesystem was never enough...
     1110        if os.path.exists(cmd):
     1111            DNS_SD_PID = os.spawnv(os.P_NOWAIT, cmd, [cmd]+args)
     1112            atexit.register(_stop_bonjour)
     1113            break
     1114
     1115def _stop_bonjour():
     1116    if not DNS_SD_PID:
     1117        return
     1118    try:
     1119        os.kill(DNS_SD_PID, 15)
     1120    except OSError:
     1121        pass
     1122
     1123def runserver(addr, port, use_reloader=True, admin_media_dir='', use_bonjour=False):
    10881124    "Starts a lightweight Web server for development."
    10891125    from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
    10901126    from django.core.handlers.wsgi import WSGIHandler
     
    11181154            except (AttributeError, KeyError):
    11191155                error_text = str(e)
    11201156            sys.stderr.write(style.ERROR("Error: %s" % error_text) + '\n')
     1157            _stop_bonjour()
    11211158            sys.exit(1)
    11221159        except KeyboardInterrupt:
     1160            _stop_bonjour()
    11231161            sys.exit(0)
     1162    if use_bonjour:
     1163        _start_bonjour(addr, port)
    11241164    if use_reloader:
    11251165        from django.utils import autoreload
    11261166        autoreload.main(inner_run)
    11271167    else:
    11281168        inner_run()
    1129 runserver.args = '[--noreload] [--adminmedia=ADMIN_MEDIA_PATH] [optional port number, or ipaddr:port]'
     1169runserver.args = '[--noreload] [--adminmedia=ADMIN_MEDIA_PATH] [--bonjour] [optional port number, or ipaddr:port]'
    11301170
    11311171def createcachetable(tablename):
    11321172    "Creates the table needed to use the SQL cache backend"
     
    13101350        help='Tells Django to NOT prompt the user for input of any kind.')
    13111351    parser.add_option('--noreload', action='store_false', dest='use_reloader', default=True,
    13121352        help='Tells Django to NOT use the auto-reloader when running the development server.')
     1353    parser.add_option('--bonjour', action='store_true', dest='use_bonjour',
     1354        help='Tells Django to try to use Bonjour make the development server known.')
    13131355    parser.add_option('--verbosity', action='store', dest='verbosity', default='1',
    13141356        type='choice', choices=['0', '1', '2'],
    13151357        help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
     
    13771419                addr, port = args[1].split(':')
    13781420            except ValueError:
    13791421                addr, port = '', args[1]
    1380         action_mapping[action](addr, port, options.use_reloader, options.admin_media_path)
     1422        action_mapping[action](addr, port, options.use_reloader, options.admin_media_path, options.use_bonjour)
    13811423    elif action == 'runfcgi':
    13821424        action_mapping[action](args[1:])
    13831425    else:
Back to Top