]> code.delx.au - bg-scripts/blobdiff - randombg.py
Handle dead sockets and already-running servers
[bg-scripts] / randombg.py
index 4727b20b7d85604f255a7747b1e748d58c40e072..51f6f726a0e037e55aeb5e66e8bb93afddfa8e4b 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python2
 
 VERSION = "2.1"
 
@@ -239,7 +239,7 @@ class Cycler(object):
                self.cache_filename = options.cache_filename
 
                logging.debug("Initialising wallchanger")
-               wallchanger.init(options.background_colour, options.permanent, options.convert)
+               wallchanger.init(options.background_colour, options.convert)
 
                logging.debug("Initialising file list")
                if options.all_random:
@@ -354,10 +354,6 @@ class Listener(asyncore.dispatcher):
                asyncore.dispatcher.__init__(self)
                self.cycler = cycler
                self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
-               try:
-                       os.unlink(socket_filename)
-               except OSError:
-                       pass
                self.bind(socket_filename)
                self.listen(2) # Backlog = 2
        
@@ -371,21 +367,29 @@ class Listener(asyncore.dispatcher):
 
 def do_server(options, paths):
        try:
-               cycler = Cycler()
-               listener = Listener(options.socket_filename, cycler)
-               # Initialisation of Cycler delayed so we grab the socket quickly
-               cycler.init(options, paths)
-               try:
-                       asyncsched.loop()
-               except KeyboardInterrupt:
-                       print
-               cycler.finish()
-       finally:
-               # Make sure that the socket is cleaned up
-               try:
-                       os.unlink(options.socket_filename)
-               except:
-                       pass
+               sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+               sock.connect(options.socket_filename)
+               print >>sys.stderr, "Server is already running! Sending exit command."
+               sock = sock.makefile()
+               sock.write("cmd exit\n")
+               sock.close()
+       except Exception, e:
+               pass
+
+       try:
+               os.unlink(options.socket_filename)
+       except OSError:
+               pass
+
+       cycler = Cycler()
+       listener = Listener(options.socket_filename, cycler)
+       # Initialisation of Cycler delayed so we grab the socket quickly
+       cycler.init(options, paths)
+       try:
+               asyncsched.loop()
+       except KeyboardInterrupt:
+               print
+       cycler.finish()
 
 def do_client(options, args):
        if len(args) == 0:
@@ -411,9 +415,6 @@ def build_parser():
                        "\n(client) %prog [options] [next|prev|rescan|reload|pause] [...]"
                        "\nThe first instance to be run will be the server.\n"
                )
-       parser.add_option("-p", "--permanent",
-               action="store_true", dest="permanent", default=False,
-               help="Make the background permanent. Note: This will cause all machines logged in with this account to simultaneously change background [Default: %default]")
        parser.add_option("-v", '-d', "--verbose", "--debug",
                action="count", dest="verbose", default=0,
                help="Make the louder (good for debugging, or those who are curious)")