]> git.phdru.name Git - xsetbg.git/commitdiff
Replaced readyexecd with a simple WSGI server.
authorOleg Broytman <phd@phdru.name>
Fri, 5 Feb 2010 16:28:59 +0000 (16:28 +0000)
committerOleg Broytman <phd@phdru.name>
Fri, 5 Feb 2010 16:28:59 +0000 (16:28 +0000)
git-svn-id: file:///home/phd/archive/SVN/xsetbg/trunk@34 143022c7-580b-0410-bae3-87f2bf5d3141

TODO
xsetbg-force [new file with mode: 0755]
xsetbg-ping
xsetbg-start
xsetbg-stop
xsetbg.conf
xsetbg.py

diff --git a/TODO b/TODO
index 6f1c6a7915c773cac7f4e9eaff2f4b14861e96da..9acd044e5dc76b9e8b65b7d2f692eac2a749bb3c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,3 @@
-HTTP server instead of readyexecd; WSGI?
-
-
 Serialized list of images for faster loading.
 
 
diff --git a/xsetbg-force b/xsetbg-force
new file mode 100755 (executable)
index 0000000..fc137b2
--- /dev/null
@@ -0,0 +1,2 @@
+#! /bin/sh
+exec wget -q -Y off -O /dev/null http://localhost:7999/force
index 2fef273c810c54974210c04f95562f77ca3ed18f..d064b129727850e7effa911f64a2d0805e464443 100755 (executable)
@@ -12,5 +12,4 @@ if ps a | grep -v grep | grep -q mplayer; then
    exit 0
 fi
 
-cd "$HOME/lib/xsetbg"
-exec readyexec xsetbg.sock
+exec wget -q -Y off -O /dev/null http://localhost:7999/ping
index 6254963f1e844265e973d5ccbbd980f6e73653ee..35cce3358a0631e5b30b1cd7e93c41545a7970f6 100755 (executable)
@@ -1,7 +1,4 @@
 #! /bin/sh
 
 umask 077
-cd "$HOME/lib/xsetbg"
-# Remove stale socket
-rm -f xsetbg.sock
-PYTHONPATH="`pwd`" python -O "`whence readyexecd.py`" xsetbg.sock xsetbg.run &
+"$HOME/lib/xsetbg"/xsetbg.py >/dev/null 2>&1 &
index 0623f205a61784146e36c913c52e82f491299599..59fa97908465a0824e2455a3e0780b5e11e65188 100755 (executable)
@@ -1,6 +1,2 @@
 #! /bin/sh
-
-cd "$HOME/lib/xsetbg"
-readyexec --stop xsetbg.sock
-# Remove socket
-exec rm -f xsetbg.sock
+exec wget -q -Y off -O /dev/null http://localhost:7999/stop
index 4c4e2f2a5acfbe4abaeda490d3ba604c7d6c735c..63546f2409ea22ba8672f0da06324007b29989e4 100644 (file)
@@ -1,6 +1,10 @@
 [images]
 directory0 = ~/lib/xsetbg/images
 
+[httpd]
+host = localhost
+port = 7999
+
 [xsetbg]
 ; minimum time in seconds between background image changes
 min_pause = 600 ; (10 minutes)
index 8c70ee0e2354afcbb9c574ef6acd46447e8269fd..717d50773255824df51b06c844e99ab948e4cc28 100755 (executable)
--- a/xsetbg.py
+++ b/xsetbg.py
@@ -17,13 +17,9 @@ __license__ = "GNU GPL"
 
 
 import sys, os
+import subprocess
 
 
-def usage():
-   sys.stderr.write("%s version %s\n" % (sys.argv[0], __version__))
-   sys.stderr.write("Usage: %s [force]\n" % sys.argv[0])
-   sys.exit(0)
-
 def error(error_str, error_code=1):
    sys.stderr.write("%s: Error: %s\n" % (sys.argv[0], error_str))
    sys.exit(error_code)
@@ -81,6 +77,17 @@ if config.has_option("xsetbg", "min_delay"):
 else:
    min_delay = 3600*24 # 24 hours
 
+# httpd settings
+if config.has_option("httpd", "host"):
+   host = config.get("httpd", "host")
+else:
+   host = 'localhost'
+
+if config.has_option("httpd", "port"):
+   port = config.getint("httpd", "port")
+else:
+   error("Config must specify a port to listen. Abort.")
+
 del config
 
 
@@ -139,17 +146,12 @@ if not images:
    error("No images found. Abort.")
 
 
-def run():
-   if len(sys.argv) not in (1, 2):
-      usage()
-
-   force = False
-   if len(sys.argv) == 2:
-      if sys.argv[1] == "force":
-         force = True
-      else:
-         usage()
+def published(func):
+    func._wsgi_published = True
+    return func
 
+@published
+def ping(force=False):
    # Use the program's file as the lock file:
    # lock it to prevent two processes run in parallel.
    lock_file = open("xsetbg.py", 'r')
@@ -162,11 +164,6 @@ def run():
 
    global_db = None
    try:
-      # Re-seed the RNG; this is neccessary because ReadyExecd forks
-      # and RNG in a child process got the same seed from the parent
-      # after every fork.
-      random.seed()
-
       # Reopen the global persistent dictionary
       global_db = shelve.open(global_db_name, 'w')
 
@@ -204,13 +201,60 @@ def run():
       # Flush and close the global persistent dictionary
       if global_db: global_db.close()
 
-   program_options = ["xli", "xli", "-onroot", "-quiet"] + \
+   program_options = ["xli", "-onroot", "-quiet"] + \
       ["-center", "-border", random.choice(borders), "-zoom", "auto",
          image_name]
 
-   os.execlp(*program_options)
-   error("cannot execute xli!")
-
-
-if __name__ == "__main__":
-   run()
+   rc = subprocess.call(program_options)
+   if rc:
+      error("cannot execute xli!")
+
+@published
+def force():
+    ping(force=True)
+
+@published
+def stop():
+    QuitWSGIServer._quit_flag = True
+
+
+from wsgiref.handlers import SimpleHandler
+from wsgiref import simple_server
+simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
+from wsgiref.simple_server import WSGIServer, make_server
+
+g = globals().copy()
+commands = dict([(name, g[name]) for name in g
+    if getattr(g[name], '_wsgi_published', False)])
+del g
+
+class QuitWSGIServer(WSGIServer):
+    _quit_flag = False
+
+    def serve_forever(self):
+        while not self._quit_flag:
+            self.handle_request()
+
+def app(env, start_response):
+    command = env['PATH_INFO'][1:] # Remove the leading slash
+    if command not in commands:
+        status = '404 Not found'
+        response_headers = [('Content-type', 'text/plain')]
+        start_response(status, response_headers)
+        return ['The command was not found.\n']
+
+    try:
+        commands[command]()
+    except:
+        status = '500 Error'
+        response_headers = [('Content-type', 'text/plain')]
+        start_response(status, response_headers)
+        return ['Error occured!\n']
+
+    status = '200 OK'
+    response_headers = [('Content-type', 'text/plain')]
+    start_response(status, response_headers)
+    return ['Ok\n']
+
+httpd = make_server(host, port, app, server_class=QuitWSGIServer)
+httpd.serve_forever()