Author: dmeyer
Date: Sat Mar 25 17:21:46 2006
New Revision: 1330
Added:
trunk/WIP/vfs/bin/kaa-vfs
Modified:
trunk/WIP/vfs/setup.py
trunk/WIP/vfs/src/__init__.py
trunk/WIP/vfs/src/server.py
trunk/WIP/vfs/src/thumbnail/__init__.py
trunk/WIP/vfs/src/thumbnail/server.py
trunk/WIP/vfs/src/thumbnail/thumbnail.py
trunk/WIP/vfs/test/cache.py
Log:
Remove autostart of the server and create a new script
kaa-vfs that needs to be started by the application
using kaa.vfs.
Added: trunk/WIP/vfs/bin/kaa-vfs
==============================================================================
--- (empty file)
+++ trunk/WIP/vfs/bin/kaa-vfs Sat Mar 25 17:21:46 2006
@@ -0,0 +1,183 @@
+#!/usr/bin/python
+
+# python imports
+import os
+import sys
+import gc
+import getopt
+import time
+import logging
+
+# insert freevo path information
+__site__ = '../lib/python%s.%s/site-packages' % sys.version_info[:2]
+__site__ = os.path.normpath(os.path.join(os.path.dirname(__file__), __site__))
+if not __site__ in sys.path:
+ sys.path.insert(0, __site__)
+
+# get logging object
+log = logging.getLogger()
+
+def usage(error_code):
+ print 'kaa-vfs [options]'
+ print '--start start vfs daemon'
+ print '--stop stop running vfs daemon'
+ print '--daemon detach for parent process'
+ print '--autoshutdown stop server when no clients are connected'
+ print '--logfile file use file for logging'
+ print '--loglevel level log level (warning, info, debug)'
+ print '--help | -h this message'
+ print
+ sys.exit(error_code)
+
+
+try:
+ # read arguments
+ opts = [ 'start', 'stop', 'daemon', 'autoshutdown', 'logfile=',
'loglevel=', 'help' ]
+ opts, args = getopt.getopt(sys.argv[1:], 'h', opts)
+except getopt.GetoptError:
+ usage(1)
+
+logfile = ''
+mode = None
+detach = False
+shutdown = False
+
+for o, a in opts:
+ if o in ('--start', '--stop'):
+ if mode:
+ usage(1)
+ mode = o[2:]
+ if o == '--daemon':
+ detach = True
+ if o == '--autoshutdown':
+ shutdown = True
+ if o == '--logfile':
+ logfile = a
+ if o == '--loglevel':
+ try:
+ log.setLevel(getattr(logging, a.upper()))
+ except:
+ print 'invalid loglevel: %s' % a
+ usage(1)
+ if o in ('--help', '-h'):
+ usage(0)
+
+if not mode:
+ # no idea what to do
+ usage(0)
+
+if mode == 'stop':
+ # stop a running vfs server
+
+ import kaa.ipc
+
+ try:
+ kaa.ipc.IPCClient('vfs').get_object('shutdown')()
+ except kaa.ipc.IPCDisconnectedError:
+ pass
+ except:
+ print 'server not running'
+ sys.exit(0)
+
+
+# When we reach this point we need to start a new vfs server. If run
+# in daemon mode we fork and detach from the parent. After that a logger
+# is created and the thumbnail server will be forked out.
+
+if detach:
+ if os.fork():
+ sys.exit(0)
+ os.setsid()
+
+# create logger
+if logfile:
+ if not os.path.isdir(os.path.dirname(logfile)):
+ os.makedirs(os.path.dirname(logfile))
+ handler = logging.FileHandler(logfile)
+ f = logging.Formatter('%(asctime)s %(levelname)-8s [%(name)6s] '+\
+ '%(filename)s %(lineno)s: %(message)s')
+ handler.setFormatter(f)
+ log.addHandler(handler)
+elif detach:
+ print 'unable to detach without setting a logfile'
+ usage(1)
+
+# fork thumbnailer
+pid = os.fork()
+
+if not pid:
+ # thumbnailer child
+ import kaa
+ import kaa.notifier
+
+ import kaa.vfs.thumbnail.server
+
+ # create tmp dir and change directory to it
+ tmpdir = os.path.join(kaa.TEMP, 'thumb')
+ if not os.path.isdir(tmpdir):
+ os.mkdir(tmpdir)
+ os.chdir(tmpdir)
+
+ # create thumbnailer object
+ try:
+ thumbnailer = kaa.vfs.thumbnail.server.Thumbnailer(tmpdir)
+ except IOError, e:
+ log.error('thumbnail: %s' % e)
+ time.sleep(0.1)
+ sys.exit(0)
+
+ # loop
+ kaa.notifier.loop()
+ log.info('stop thumbnail server')
+ sys.exit(0)
+
+
+
+# vfs server
+import kaa
+import kaa.notifier
+import kaa.ipc
+
+import kaa.vfs.server
+import kaa.vfs.thumbnail.thumbnail
+
+def garbage_collect():
+ g = gc.collect()
+ if g:
+ log.info('gc: deleted %s objects' % g)
+ if gc.garbage:
+ log.warning('gc: found %s garbage objects' % len(gc.garbage))
+ for g in gc.garbage:
+ log.warning(g)
+ return True
+
+
+# connect thumbnailer
+log.info('connect to thumbnailer')
+kaa.vfs.thumbnail.thumbnail.connect()
+
+try:
+ ipc = kaa.ipc.IPCServer('vfs')
+except IOError, e:
+ kaa.vfs.thumbnail.thumbnail.disconnect()
+ log.error('vfs: %s' % e)
+ time.sleep(0.1)
+ sys.exit(0)
+
+log.info('start vfs server')
+ipc.register_object(kaa.vfs.server.connect, 'vfs')
+ipc.register_object(kaa.notifier.shutdown, 'shutdown')
+ipc.signals["client_closed"].connect(kaa.vfs.server._client_closed)
+
+# start garbage collector
+kaa.notifier.Timer(garbage_collect).start(10)
+if shutdown:
+ log.info('set autoshutdown timer to 3 seconds')
+ kaa.vfs.server.autoshutdown(3)
+
+kaa.notifier.loop()
+log.info('stop vfs server')
+try:
+ os.kill(pid, 15)
+except OSError:
+ pass
Modified: trunk/WIP/vfs/setup.py
==============================================================================
--- trunk/WIP/vfs/setup.py (original)
+++ trunk/WIP/vfs/setup.py Sat Mar 25 17:21:46 2006
@@ -62,6 +62,6 @@
setup (module = 'vfs',
version = '0.1',
description = "Media-oriented VFS",
- scripts = [ 'bin/kaa-thumb' ],
+ scripts = [ 'bin/kaa-thumb', 'bin/kaa-vfs' ],
ext_modules = [ ext ]
)
Modified: trunk/WIP/vfs/src/__init__.py
==============================================================================
--- trunk/WIP/vfs/src/__init__.py (original)
+++ trunk/WIP/vfs/src/__init__.py Sat Mar 25 17:21:46 2006
@@ -36,35 +36,33 @@
from kaa import ipc
from client import Client
-
+import thumbnail
from thumbnail import Thumbnail, NORMAL, LARGE
+# get logging object
+log = logging.getLogger('vfs')
+
# connected client object
_client = None
-def connect(vfsdb, logfile=None, loglevel=logging.INFO):
+def connect(vfsdb=None):
"""
- Connect to the vfs database dir given by 'vfsdb'. A server will be started
- if no server is running. The new server will print debug output to the
- given logfile. If a server is already running, logfile has no effect. If
- a loglevel is given and the server will be started, it will use the given
- loglevel. If no logfile is given, the server will log to vfsdb/log.
- The server can be used by different clients in different applications if
- the are started by the same user. It will shutdown if no client is
connected
- for over 5 seconds.
+ Connect to the vfs database dir given by 'vfsdb'. Id 'vfsdb' is None, the
+ client will only connect to the thumbnailer. A kaa-vfs program must be
running.
"""
global _client
if _client:
return _client
- # check logfile
- if not logfile:
- logfile = os.path.join(vfsdb, 'log')
- # get server filename
- server = os.path.join(os.path.dirname(__file__), 'server.py')
+ log.info('connect to thumbnailer')
+ thumbnail.connect()
- _client = ipc.launch([server, logfile, str(loglevel)], 5, Client, vfsdb)
+ if not vfsdb:
+ return None
+
+ log.info('connect to %s' % vfsdb)
+ _client = Client(vfsdb)
return _client
Modified: trunk/WIP/vfs/src/server.py
==============================================================================
--- trunk/WIP/vfs/src/server.py (original)
+++ trunk/WIP/vfs/src/server.py Sat Mar 25 17:21:46 2006
@@ -38,16 +38,10 @@
import sys
import logging
-# insert kaa path information
-__site__ = '../lib/python%s.%s/site-packages' % sys.version_info[:2]
-__site__ = os.path.normpath(os.path.join(os.path.dirname(__file__), __site__))
-if not __site__ in sys.path:
- sys.path.insert(0, __site__)
-
# kaa imports
from kaa import ipc
from kaa.weakref import weakref
-from kaa.notifier import OneShotTimer
+from kaa.notifier import OneShotTimer, Timer
# kaa.vfs imports
import parser
@@ -267,70 +261,20 @@
_num_client -= 1
-if __name__ == "__main__":
-
- # python imports
- import gc
- import sys
-
- # kaa imports
- from kaa.notifier import Timer, execute_in_timer, loop
-
- @execute_in_timer(Timer, 1)
- def garbage_collect():
- g = gc.collect()
- if g:
- log.info('gc: deleted %s objects' % g)
- if gc.garbage:
- log.warning('gc: found %s garbage objects' % len(gc.garbage))
- for g in gc.garbage:
- log.warning(g)
+def autoshutdown_step(timeout):
+ global shutdown_timer
+ if _num_client > 0:
+ shutdown_timer = timeout
return True
-
-
- shutdown_timer = 5
-
- @execute_in_timer(Timer, 1)
- def autoshutdown():
- global shutdown_timer
- if _num_client > 0:
- shutdown_timer = 5
- return True
- shutdown_timer -= 1
- if shutdown_timer == 0:
- log.info('shutdown vfs server')
- sys.exit(0)
- return True
-
- # setup logger
- f = logging.Formatter('%(asctime)s %(levelname)-8s [%(name)6s] '+\
- '%(filename)s %(lineno)s: '+\
- '%(message)s')
- if not os.path.isdir(os.path.dirname(sys.argv[1])):
- os.makedirs(os.path.dirname(sys.argv[1]))
-
- handler = logging.FileHandler(sys.argv[1])
- handler.setFormatter(f)
- logging.getLogger().addHandler(handler)
-
- try:
- # detach for parent using a new sesion
- os.setsid()
- except OSError:
- # looks like we are started from the shell
- # TODO: start some extra debug here and disable autoshutdown
- pass
+ shutdown_timer -= 1
+ if shutdown_timer == 0:
+ log.info('vfs server timeout')
+ sys.exit(0)
+ return True
- # set log level
- logging.getLogger().setLevel(int(sys.argv[2]))
- log.info('start vfs server')
+def autoshutdown(timeout):
+ global shutdown_timer
+ shutdown_timer = timeout
+ Timer(autoshutdown_step, timeout).start(1)
- # connect to the ipc code
- _ipc = ipc.IPCServer('vfs')
- _ipc.register_object(connect, 'vfs')
- _ipc.signals["client_closed"].connect(_client_closed)
-
- garbage_collect()
- autoshutdown()
- loop()
Modified: trunk/WIP/vfs/src/thumbnail/__init__.py
==============================================================================
--- trunk/WIP/vfs/src/thumbnail/__init__.py (original)
+++ trunk/WIP/vfs/src/thumbnail/__init__.py Sat Mar 25 17:21:46 2006
@@ -1 +1 @@
-from thumbnail import Thumbnail, NORMAL, LARGE
+from thumbnail import Thumbnail, NORMAL, LARGE, connect, disconnect
Modified: trunk/WIP/vfs/src/thumbnail/server.py
==============================================================================
--- trunk/WIP/vfs/src/thumbnail/server.py (original)
+++ trunk/WIP/vfs/src/thumbnail/server.py Sat Mar 25 17:21:46 2006
@@ -34,12 +34,6 @@
import sys
import logging
-# insert kaa path information
-__site__ = '../lib/python%s.%s/site-packages' % sys.version_info[:2]
-__site__ = os.path.normpath(os.path.join(os.path.dirname(__file__), __site__))
-if not __site__ in sys.path:
- sys.path.insert(0, __site__)
-
# kaa imports
from kaa import ipc
import kaa.notifier
@@ -192,48 +186,3 @@
self.videothumb._jobs.remove(j)
self.clients.remove(client_info)
return
-
-
-if __name__ == "__main__":
-
- shutdown_timer = 5
-
- @kaa.notifier.execute_in_timer(kaa.notifier.Timer, 1)
- def autoshutdown(server):
- global shutdown_timer
- if len(server.clients) > 0:
- shutdown_timer = 5
- return True
- shutdown_timer -= 1
- if shutdown_timer == 0:
- sys.exit(0)
- return True
-
- try:
- # detach for parent using a new sesion
- os.setsid()
- except OSError:
- # looks like we are started from the shell
- # TODO: start some extra debug here and disable autoshutdown
- pass
-
- # create tmp dir and change directory to it
- tmpdir = os.path.join(kaa.TEMP, 'thumb')
- if not os.path.isdir(tmpdir):
- os.mkdir(tmpdir)
- os.chdir(tmpdir)
-
- # Setup logger. This module should produce no output at all, but a crash
- # will result in a backtrace which is nice to have.
- handler = logging.FileHandler('log')
- handler.setFormatter(logging.Formatter('%(filename)s %(lineno)s:
%(message)s'))
- logging.getLogger().addHandler(handler)
-
- # create thumbnailer object
- thumbnailer = Thumbnailer(tmpdir)
-
- # start autoshutdown
- autoshutdown(thumbnailer)
-
- # loop
- kaa.notifier.loop()
Modified: trunk/WIP/vfs/src/thumbnail/thumbnail.py
==============================================================================
--- trunk/WIP/vfs/src/thumbnail/thumbnail.py (original)
+++ trunk/WIP/vfs/src/thumbnail/thumbnail.py Sat Mar 25 17:21:46 2006
@@ -29,7 +29,7 @@
#
# -----------------------------------------------------------------------------
-__all__ = [ 'Thumbnail', 'NORMAL', 'LARGE' ]
+__all__ = [ 'Thumbnail', 'NORMAL', 'LARGE', 'connect', 'disconnect' ]
NORMAL = 'normal'
LARGE = 'large'
@@ -37,10 +37,11 @@
# python imports
import os
import md5
+import time
import logging
# kaa imports
-from kaa import ipc
+import kaa
from kaa.weakref import weakref
from kaa.notifier import Signal, step
@@ -56,6 +57,9 @@
# sizes for the thumbnails
SIZE = { NORMAL: (128, 128), LARGE: (256, 256) }
+# internal variables for ipc
+_server = _client_id = _schedule = _remove = None
+
class Job(object):
all = []
@@ -156,10 +160,44 @@
_remove((_client_id, job.id), __ipc_oneway=True,
__ipc_noproxy_args=True)
-# connect to ipc
-_server = os.path.join(os.path.dirname(__file__), 'server.py')
-_server = ipc.launch(_server, 5, ipc.IPCClient,
'thumb/socket').get_object('thumb')
-
-_client_id = _server.connect(_callback)
-_schedule = _server.schedule
-_remove = _server.remove
+def connect():
+ """
+ Connect to server.
+ """
+ global _server
+ global _client_id
+ global _schedule
+ global _remove
+
+ if _server:
+ return True
+
+ start = time.time()
+ while True:
+ try:
+ _server = kaa.ipc.IPCClient('thumb/socket').get_object('thumb')
+ break
+ except Exception, e:
+ if start + 3 < time.time():
+ # start time is up, something is wrong here
+ raise RuntimeError('unable to connect to thumbnail server')
+ time.sleep(0.01)
+
+ _client_id = _server.connect(_callback)
+ _schedule = _server.schedule
+ _remove = _server.remove
+ kaa.signals['shutdown'].connect(disconnect)
+ return True
+
+
+def disconnect():
+ """
+ Shutdown connection.
+ """
+ global _server
+ global _client_id
+ global _schedule
+ global _remove
+
+ # delete all objects
+ _server = _client_id = _schedule = _remove = None
Modified: trunk/WIP/vfs/test/cache.py
==============================================================================
--- trunk/WIP/vfs/test/cache.py (original)
+++ trunk/WIP/vfs/test/cache.py Sat Mar 25 17:21:46 2006
@@ -4,9 +4,6 @@
import kaa.vfs
import logging
-# full parameter set for connect
-# kaa.vfs.connect('vfsdb', 'logfile', logging.INFO)
-
# simple connect
kaa.vfs.connect(os.path.expanduser("~/.vfs"))
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog