diff --git a/config/Desktop.py b/config/Desktop.py index bbba6f1..e1668b8 100644 --- a/config/Desktop.py +++ b/config/Desktop.py @@ -1,5 +1,6 @@ import os import os.path +import socket import cherrypy from tempfile import gettempdir @@ -8,8 +9,17 @@ username_postfix = os.environ.get( "USER" ) username_postfix = username_postfix and "_%s" % username_postfix or "" +# find an available TCP socket to listen on +sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) +sock.bind( ( "", 0 ) ) +socket_port = sock.getsockname()[ 1 ] +sock.close() + + + settings = { "global": { + "server.socket_port": socket_port, "server.thread_pool": 4, "session_filter.storage_type": "ram", "session_filter.timeout": 60 * 24 * 365, # one year @@ -18,6 +28,7 @@ settings = { "server.log_file": os.path.join( gettempdir(), "luminotes_error%s.log" % username_postfix ), "server.log_access_file": os.path.join( gettempdir(), "luminotes%s.log" % username_postfix ), "server.log_tracebacks": True, + "luminotes.port_file": os.path.join( gettempdir(), "luminotes_port%s" % username_postfix ), "luminotes.launch_browser": True, "luminotes.db_host": None, # use local SQLite database "luminotes.auto_login_username": "desktopuser", diff --git a/luminotes.py b/luminotes.py index 5f96256..e72d52c 100755 --- a/luminotes.py +++ b/luminotes.py @@ -51,7 +51,10 @@ def main( args ): launch_browser = cherrypy.config.configMap[ u"global" ].get( u"luminotes.launch_browser" ) socket.setdefaulttimeout( INITIAL_SOCKET_TIMEOUT_SECONDS ) - server_url = u"http://localhost:%d/" % cherrypy.config.configMap[ u"global" ].get( u"server.socket_port" ) + port_filename = cherrypy.config.configMap[ u"global" ].get( u"luminotes.port_file" ) + socket_port = cherrypy.config.configMap[ u"global" ].get( u"server.socket_port" ) + existing_socket_port = port_filename and os.path.exists( port_filename ) and file( port_filename ).read() or socket_port + server_url = u"http://localhost:%s/" % existing_socket_port server_present = True # if requested, attempt to shutdown an existing server and exit @@ -76,6 +79,8 @@ def main( args ): sys.exit( 0 ) + server_url = u"http://localhost:%s/" % socket_port + # remove the existing log files, if any try: log_access_file = cherrypy.config.configMap[ u"global" ].get( u"server.log_access_file" ) @@ -102,10 +107,16 @@ def main( args ): root = Root( database, cherrypy.config.configMap ) cherrypy.root = root - cherrypy.server.start_with_callback( callback, ( log_access_file, log_file, server_url, launch_browser ) ) + cherrypy.server.start_with_callback( callback, ( log_access_file, log_file, server_url, port_filename, socket_port, launch_browser ) ) -def callback( log_access_file, log_file, server_url, launch_browser = False ): +def callback( log_access_file, log_file, server_url, port_filename, socket_port, launch_browser = False ): + # record our listening socket port + if port_filename: + port_file = file( port_filename, "w" ) + port_file.write( "%s" % socket_port ) + port_file.close() + # this causes cherrypy to create the access log if log_access_file: try: