]> code.delx.au - bg-scripts/commitdiff
Handle dead sockets and already-running servers
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 11 Nov 2014 08:44:17 +0000 (19:44 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 11 Nov 2014 08:44:17 +0000 (19:44 +1100)
randombg.py

index 9ff4642f665c8b4b86748f240bc5f897d748132a..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:
@@ -369,9 +369,11 @@ def do_server(options, paths):
        try:
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                sock.connect(options.socket_filename)
-               print >>sys.stderr, "Server is already running! Will do nothing"
-               return
-       except:
+               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:
@@ -379,22 +381,15 @@ def do_server(options, paths):
        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:
-               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 OSError:
-                       pass
+               asyncsched.loop()
+       except KeyboardInterrupt:
+               print
+       cycler.finish()
 
 def do_client(options, args):
        if len(args) == 0:
@@ -420,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)")