Exemplo n.º 1
0
def main():
    """Entry point for RWS.

    return (int): exit code (0 on success, 1 on error)

    """
    parser = argparse.ArgumentParser(
        description="Ranking for CMS.")
    parser.add_argument("--config", type=argparse.FileType("rt"),
                        help="override config file")
    parser.add_argument("-d", "--drop", action="store_true",
                        help="drop the data already stored")
    args = parser.parse_args()

    config = Config()
    config.load(args.config)

    if args.drop:
        ans = input("Are you sure you want to delete directory %s? [y/N]" %
                    config.lib_dir).strip().lower()
        if ans in ['y', 'yes']:
            print("Removing directory %s." % config.lib_dir)
            shutil.rmtree(config.lib_dir)
        else:
            print("Not removing directory %s." % config.lib_dir)
        return 1

    stores = dict()

    stores["subchange"] = Store(
        Subchange, os.path.join(config.lib_dir, 'subchanges'), stores)
    stores["submission"] = Store(
        Submission, os.path.join(config.lib_dir, 'submissions'), stores,
        [stores["subchange"]])
    stores["user"] = Store(
        User, os.path.join(config.lib_dir, 'users'), stores,
        [stores["submission"]])
    stores["team"] = Store(
        Team, os.path.join(config.lib_dir, 'teams'), stores,
        [stores["user"]])
    stores["task"] = Store(
        Task, os.path.join(config.lib_dir, 'tasks'), stores,
        [stores["submission"]])
    stores["contest"] = Store(
        Contest, os.path.join(config.lib_dir, 'contests'), stores,
        [stores["task"]])

    stores["contest"].load_from_disk()
    stores["task"].load_from_disk()
    stores["team"].load_from_disk()
    stores["user"].load_from_disk()
    stores["submission"].load_from_disk()
    stores["subchange"].load_from_disk()

    stores["scoring"] = ScoringStore(stores)
    stores["scoring"].init_store()

    toplevel_handler = RoutingHandler(
        DataWatcher(stores, config.buffer_size),
        ImageHandler(
            os.path.join(config.lib_dir, '%(name)s'),
            os.path.join(config.web_dir, 'img', 'logo.png')),
        ScoreHandler(stores),
        HistoryHandler(stores))

    wsgi_app = SharedDataMiddleware(DispatcherMiddleware(
        toplevel_handler, {
            '/contests': StoreHandler(
                stores["contest"],
                config.username, config.password, config.realm_name),
            '/tasks': StoreHandler(
                stores["task"],
                config.username, config.password, config.realm_name),
            '/teams': StoreHandler(
                stores["team"],
                config.username, config.password, config.realm_name),
            '/users': StoreHandler(
                stores["user"],
                config.username, config.password, config.realm_name),
            '/submissions': StoreHandler(
                stores["submission"],
                config.username, config.password, config.realm_name),
            '/subchanges': StoreHandler(
                stores["subchange"],
                config.username, config.password, config.realm_name),
            '/faces': ImageHandler(
                os.path.join(config.lib_dir, 'faces', '%(name)s'),
                os.path.join(config.web_dir, 'img', 'face.png')),
            '/flags': ImageHandler(
                os.path.join(config.lib_dir, 'flags', '%(name)s'),
                os.path.join(config.web_dir, 'img', 'flag.png')),
            '/sublist': SubListHandler(stores),
        }), {'/': config.web_dir})

    servers = list()
    if config.http_port is not None:
        http_server = WSGIServer(
            (config.bind_address, config.http_port), wsgi_app)
        servers.append(http_server)
    if config.https_port is not None:
        https_server = WSGIServer(
            (config.bind_address, config.https_port), wsgi_app,
            certfile=config.https_certfile, keyfile=config.https_keyfile)
        servers.append(https_server)

    try:
        gevent.joinall(list(gevent.spawn(s.serve_forever) for s in servers))
    except KeyboardInterrupt:
        pass
    finally:
        gevent.joinall(list(gevent.spawn(s.stop) for s in servers))
    return 0
Exemplo n.º 2
0
def main():
    """Entry point for RWS.

    return (int): exit code (0 on success, 1 on error)

    """
    parser = argparse.ArgumentParser(
        description="Ranking for CMS.")
    parser.add_argument("--config", type=argparse.FileType("rt"),
                        help="override config file")
    parser.add_argument("-d", "--drop", action="store_true",
                        help="drop the data already stored")
    parser.add_argument("-y", "--yes", action="store_true",
                        help="do not require confirmation on dropping data")
    args = parser.parse_args()

    config = Config()
    config.load(args.config)

    if args.drop:
        if args.yes:
            ans = 'y'
        else:
            ans = input("Are you sure you want to delete directory %s? [y/N] " %
                        config.lib_dir).strip().lower()
        if ans in ['y', 'yes']:
            print("Removing directory %s." % config.lib_dir)
            shutil.rmtree(config.lib_dir)
        else:
            print("Not removing directory %s." % config.lib_dir)
        return 0

    stores = dict()

    stores["subchange"] = Store(
        Subchange, os.path.join(config.lib_dir, 'subchanges'), stores)
    stores["submission"] = Store(
        Submission, os.path.join(config.lib_dir, 'submissions'), stores,
        [stores["subchange"]])
    stores["user"] = Store(
        User, os.path.join(config.lib_dir, 'users'), stores,
        [stores["submission"]])
    stores["team"] = Store(
        Team, os.path.join(config.lib_dir, 'teams'), stores,
        [stores["user"]])
    stores["task"] = Store(
        Task, os.path.join(config.lib_dir, 'tasks'), stores,
        [stores["submission"]])
    stores["contest"] = Store(
        Contest, os.path.join(config.lib_dir, 'contests'), stores,
        [stores["task"]])

    stores["contest"].load_from_disk()
    stores["task"].load_from_disk()
    stores["team"].load_from_disk()
    stores["user"].load_from_disk()
    stores["submission"].load_from_disk()
    stores["subchange"].load_from_disk()

    stores["scoring"] = ScoringStore(stores)
    stores["scoring"].init_store()

    toplevel_handler = RoutingHandler(
        RootHandler(config.web_dir),
        DataWatcher(stores, config.buffer_size),
        ImageHandler(
            os.path.join(config.lib_dir, '%(name)s'),
            os.path.join(config.web_dir, 'img', 'logo.png')),
        ScoreHandler(stores),
        HistoryHandler(stores))

    wsgi_app = SharedDataMiddleware(DispatcherMiddleware(
        toplevel_handler, {
            '/contests': StoreHandler(
                stores["contest"],
                config.username, config.password, config.realm_name),
            '/tasks': StoreHandler(
                stores["task"],
                config.username, config.password, config.realm_name),
            '/teams': StoreHandler(
                stores["team"],
                config.username, config.password, config.realm_name),
            '/users': StoreHandler(
                stores["user"],
                config.username, config.password, config.realm_name),
            '/submissions': StoreHandler(
                stores["submission"],
                config.username, config.password, config.realm_name),
            '/subchanges': StoreHandler(
                stores["subchange"],
                config.username, config.password, config.realm_name),
            '/faces': ImageHandler(
                os.path.join(config.lib_dir, 'faces', '%(name)s'),
                os.path.join(config.web_dir, 'img', 'face.png')),
            '/flags': ImageHandler(
                os.path.join(config.lib_dir, 'flags', '%(name)s'),
                os.path.join(config.web_dir, 'img', 'flag.png')),
            '/sublist': SubListHandler(stores),
        }), {'/': config.web_dir})

    servers = list()
    if config.http_port is not None:
        http_server = WSGIServer(
            (config.bind_address, config.http_port), wsgi_app)
        servers.append(http_server)
    if config.https_port is not None:
        https_server = WSGIServer(
            (config.bind_address, config.https_port), wsgi_app,
            certfile=config.https_certfile, keyfile=config.https_keyfile)
        servers.append(https_server)

    try:
        gevent.joinall(list(gevent.spawn(s.serve_forever) for s in servers))
    except KeyboardInterrupt:
        pass
    finally:
        gevent.joinall(list(gevent.spawn(s.stop) for s in servers))
    return 0