#!/usr/bin/env python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/bin/expire_session.py $
$Id: expire_session.py 29387 2007-02-12 21:04:47Z dbinger $

Delete sessions whose access time is older than the age passed in as an
argument (in hours).

This script is meant to be run by cron.
"""
import sys
from datetime import timedelta
from qp.lib.site import Site
from qp.pub.common import site_now, get_connection

def main (prog, args):
    usage = "usage: %s [site] [expire time]" % prog

    if len(args) != 2:
        sys.exit(usage)

    sessions = Site(args[0]).get_publisher().get_sessions()
    expiration_hours = float(args[1])
    cutoff = site_now() - timedelta(hours=expiration_hours)
    for key, session in sessions.items():
        if session.get_access_time() < cutoff:
            del sessions[key]
    get_connection().commit()

if __name__ == '__main__':
    main(sys.argv[0], sys.argv[1:])
