Ticket #2933: r3909-bonjour.patch.diff
File r3909-bonjour.patch.diff, 3.9 KB (added by , 18 years ago) |
---|
-
django/core/management.py
6 6 import os, re, shutil, sys, textwrap 7 7 from optparse import OptionParser 8 8 from django.utils import termcolors 9 import atexit 9 10 10 11 # For Python 2.3 11 12 if not hasattr(__builtins__, 'set'): … … 1084 1085 sys.stderr.write(s.read()) 1085 1086 sys.exit(1) 1086 1087 1087 def runserver(addr, port, use_reloader=True, admin_media_dir=''): 1088 DNS_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 1091 def _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 1115 def _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 1123 def runserver(addr, port, use_reloader=True, admin_media_dir='', use_bonjour=False): 1088 1124 "Starts a lightweight Web server for development." 1089 1125 from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException 1090 1126 from django.core.handlers.wsgi import WSGIHandler … … 1118 1154 except (AttributeError, KeyError): 1119 1155 error_text = str(e) 1120 1156 sys.stderr.write(style.ERROR("Error: %s" % error_text) + '\n') 1157 _stop_bonjour() 1121 1158 sys.exit(1) 1122 1159 except KeyboardInterrupt: 1160 _stop_bonjour() 1123 1161 sys.exit(0) 1162 if use_bonjour: 1163 _start_bonjour(addr, port) 1124 1164 if use_reloader: 1125 1165 from django.utils import autoreload 1126 1166 autoreload.main(inner_run) 1127 1167 else: 1128 1168 inner_run() 1129 runserver.args = '[--noreload] [--adminmedia=ADMIN_MEDIA_PATH] [ optional port number, or ipaddr:port]'1169 runserver.args = '[--noreload] [--adminmedia=ADMIN_MEDIA_PATH] [--bonjour] [optional port number, or ipaddr:port]' 1130 1170 1131 1171 def createcachetable(tablename): 1132 1172 "Creates the table needed to use the SQL cache backend" … … 1310 1350 help='Tells Django to NOT prompt the user for input of any kind.') 1311 1351 parser.add_option('--noreload', action='store_false', dest='use_reloader', default=True, 1312 1352 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.') 1313 1355 parser.add_option('--verbosity', action='store', dest='verbosity', default='1', 1314 1356 type='choice', choices=['0', '1', '2'], 1315 1357 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), … … 1377 1419 addr, port = args[1].split(':') 1378 1420 except ValueError: 1379 1421 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) 1381 1423 elif action == 'runfcgi': 1382 1424 action_mapping[action](args[1:]) 1383 1425 else: