From: Oleg Broytman Date: Fri, 5 Feb 2010 16:28:59 +0000 (+0000) Subject: Replaced readyexecd with a simple WSGI server. X-Git-Tag: v4.0.0~34 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=0b8b04b6bbf391cfdf676b88a0579edc59099e36;p=xsetbg.git Replaced readyexecd with a simple WSGI server. git-svn-id: file:///home/phd/archive/SVN/xsetbg/trunk@34 143022c7-580b-0410-bae3-87f2bf5d3141 --- diff --git a/TODO b/TODO index 6f1c6a7..9acd044 100644 --- 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 index 0000000..fc137b2 --- /dev/null +++ b/xsetbg-force @@ -0,0 +1,2 @@ +#! /bin/sh +exec wget -q -Y off -O /dev/null http://localhost:7999/force diff --git a/xsetbg-ping b/xsetbg-ping index 2fef273..d064b12 100755 --- a/xsetbg-ping +++ b/xsetbg-ping @@ -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 diff --git a/xsetbg-start b/xsetbg-start index 6254963..35cce33 100755 --- a/xsetbg-start +++ b/xsetbg-start @@ -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 & diff --git a/xsetbg-stop b/xsetbg-stop index 0623f20..59fa979 100755 --- a/xsetbg-stop +++ b/xsetbg-stop @@ -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 diff --git a/xsetbg.conf b/xsetbg.conf index 4c4e2f2..63546f2 100644 --- a/xsetbg.conf +++ b/xsetbg.conf @@ -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) diff --git a/xsetbg.py b/xsetbg.py index 8c70ee0..717d507 100755 --- 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()