示例#1
0
def contest_id_from_args(args_contest_id, ask_contest):
    """Return a valid contest_id from the arguments or None if multicontest
    mode should be used

    If the passed value is missing, ask the admins with ask_contest.
    If the contest id is invalid, print a message and exit.

    args_contest_id (int|str|None): the contest_id passed as argument.
    ask_contest (function): a function that returns a contest_id.

    """
    assert ask_contest is not None

    if args_contest_id == "ALL":
        return None
    if args_contest_id is not None:
        try:
            contest_id = int(args_contest_id)
        except ValueError:
            logger.critical("Unable to parse contest id '%s'", args_contest_id)
            sys.exit(1)
    else:
        contest_id = ask_contest()

    # Test if there is a contest with the given contest id.
    from cms.db import is_contest_id
    if not is_contest_id(contest_id):
        logger.critical("There is no contest with the specified id. "
                        "Please try again.")
        sys.exit(1)
    return contest_id
示例#2
0
def main():
    """Parses arguments and launch service.

    """
    parser = argparse.ArgumentParser(
        description="Resource monitor and service starter for CMS.")
    parser.add_argument("-a", "--autorestart", metavar="CONTEST_ID",
                        help="restart automatically services on its machine",
                        nargs="?", type=int, const=-1)
    parser.add_argument("shard", type=int, nargs="?", default=-1)
    args = parser.parse_args()

    # If the shard is -1 (i.e., unspecified) we find it basing on the
    # local IP addresses
    if args.shard == -1:
        addrs = find_local_addresses()
        args.shard = get_shard_from_addresses("ResourceService", addrs)

    if args.autorestart is not None:
        if args.autorestart == -1:
            ResourceService(args.shard,
                            contest_id=ask_for_contest()).run()
        else:
            if is_contest_id(args.autorestart):
                ResourceService(args.shard, contest_id=args.autorestart).run()
            else:
                import sys
                print >> sys.stderr, "There is no contest " \
                      "with the specified id. Please try again."
                sys.exit(1)
    else:
        ResourceService(args.shard).run()
示例#3
0
文件: util.py 项目: WPettersson/cms
def contest_id_from_args(args_contest_id, ask_contest):
    """Return a valid contest_id from the arguments or None if multicontest
    mode should be used

    If the passed value is missing, ask the admins with ask_contest.
    If the contest id is invalid, print a message and exit.

    args_contest_id (int|str|None): the contest_id passed as argument.
    ask_contest (function): a function that returns a contest_id.

    """
    assert ask_contest is not None

    if args_contest_id == "ALL":
        return None
    if args_contest_id is not None:
        try:
            contest_id = int(args_contest_id)
        except ValueError:
            logger.critical("Unable to parse contest id '%s'", args_contest_id)
            sys.exit(1)
    else:
        contest_id = ask_contest()

    # Test if there is a contest with the given contest id.
    from cms.db import is_contest_id
    if not is_contest_id(contest_id):
        logger.critical("There is no contest with the specified id. "
                        "Please try again.")
        sys.exit(1)
    return contest_id
示例#4
0
def default_argument_parser(description, cls, ask_contest=None):
    """Default argument parser for services.

    This has two versions, depending on whether the service needs a
    contest_id, or not.

    description (string): description of the service.
    cls (type): service's class.
    ask_contest (function|None): None if the service does not require
        a contest, otherwise a function that returns a contest_id
        (after asking the admins?)

    return (object): an instance of a service.

    """
    parser = ArgumentParser(description=description)
    parser.add_argument("shard", action="store", type=int, nargs="?")

    # We need to allow using the switch "-c" also for services that do
    # not need the contest_id because RS needs to be able to restart
    # everything without knowing which is which.
    contest_id_help = "id of the contest to automatically load"
    if ask_contest is None:
        contest_id_help += " (ignored)"
    parser.add_argument("-c",
                        "--contest-id",
                        action="store",
                        type=int,
                        help=contest_id_help)
    args = parser.parse_args()

    try:
        args.shard = get_safe_shard(cls.__name__, args.shard)
    except ValueError:
        raise ConfigError("Couldn't autodetect shard number and "
                          "no shard specified for service %s, "
                          "quitting." % (cls.__name__))

    if ask_contest is not None:
        if args.contest_id is not None:
            # Test if there is a contest with the given contest id.
            from cms.db import is_contest_id
            if not is_contest_id(args.contest_id):
                print(
                    "There is no contest with the specified id. "
                    "Please try again.",
                    file=sys.stderr)
                sys.exit(1)
            return cls(args.shard, args.contest_id)
        else:
            return cls(args.shard, ask_contest())
    else:
        return cls(args.shard)
示例#5
0
文件: __init__.py 项目: shaunren/cms
def default_argument_parser(description, cls, ask_contest=None):
    """Default argument parser for services - in two versions: needing
    a contest_id, or not.

    description (string): description of the service.
    cls (type): service's class.
    ask_contest (function): None if the service does not require a
                            contest, otherwise a function that returns
                            a contest_id (after asking the admins?)

    return (object): an instance of a service.

    """
    parser = ArgumentParser(description=description)
    parser.add_argument("shard", nargs="?", type=int, default=-1)

    # We need to allow using the switch "-c" also for services that do
    # not need the contest_id because RS needs to be able to restart
    # everything without knowing which is which.
    contest_id_help = "id of the contest to automatically load"
    if ask_contest is None:
        contest_id_help += " (ignored)"
    parser.add_argument("-c",
                        "--contest-id",
                        help=contest_id_help,
                        nargs="?",
                        type=int)
    args = parser.parse_args()

    # If the shard is -1 (i.e., unspecified) we find it basing on the
    # local IP addresses
    if args.shard == -1:
        addrs = find_local_addresses()
        args.shard = get_shard_from_addresses(cls.__name__, addrs)
        if args.shard == -1:
            logger.critical("Couldn't autodetect shard number and "
                            "no shard specified for service %s, "
                            "quitting." % (cls.__name__))
            sys.exit(1)

    if ask_contest is not None:
        if args.contest_id is not None:
            # Test if there is a contest with the given contest id.
            from cms.db import is_contest_id
            if not is_contest_id(args.contest_id):
                print >> sys.stderr, "There is no contest " \
                    "with the specified id. Please try again."
                sys.exit(1)
            return cls(args.shard, args.contest_id)
        else:
            return cls(args.shard, ask_contest())
    else:
        return cls(args.shard)
示例#6
0
文件: __init__.py 项目: ronalchn/cms
def default_argument_parser(description, cls, ask_contest=None):
    """Default argument parser for services - in two versions: needing
    a contest_id, or not.

    description (string): description of the service.
    cls (type): service's class.
    ask_contest (function): None if the service does not require a
                            contest, otherwise a function that returns
                            a contest_id (after asking the admins?)

    return (object): an instance of a service.

    """
    parser = ArgumentParser(description=description)
    parser.add_argument("shard", nargs="?", type=int, default=-1)

    # We need to allow using the switch "-c" also for services that do
    # not need the contest_id because RS needs to be able to restart
    # everything without knowing which is which.
    contest_id_help = "id of the contest to automatically load"
    if ask_contest is None:
        contest_id_help += " (ignored)"
    parser.add_argument("-c", "--contest-id", help=contest_id_help,
                        nargs="?", type=int)
    args = parser.parse_args()

    # If the shard is -1 (i.e., unspecified) we find it basing on the
    # local IP addresses
    if args.shard == -1:
        addrs = find_local_addresses()
        args.shard = get_shard_from_addresses(cls.__name__, addrs)
        if args.shard == -1:
            logger.critical("Couldn't autodetect shard number and "
                            "no shard specified for service %s, "
                            "quitting." % (cls.__name__))
            sys.exit(1)

    if ask_contest is not None:
        if args.contest_id is not None:
            # Test if there is a contest with the given contest id.
            from cms.db import is_contest_id
            if not is_contest_id(args.contest_id):
                print >> sys.stderr, "There is no contest " \
                    "with the specified id. Please try again."
                sys.exit(1)
            return cls(args.shard, args.contest_id)
        else:
            return cls(args.shard, ask_contest())
    else:
        return cls(args.shard)
示例#7
0
文件: util.py 项目: Corea/cms
def default_argument_parser(description, cls, ask_contest=None):
    """Default argument parser for services.

    This has two versions, depending on whether the service needs a
    contest_id, or not.

    description (string): description of the service.
    cls (type): service's class.
    ask_contest (function|None): None if the service does not require
        a contest, otherwise a function that returns a contest_id
        (after asking the admins?)

    return (object): an instance of a service.

    """
    parser = ArgumentParser(description=description)
    parser.add_argument("shard", action="store", type=int, nargs="?")

    # We need to allow using the switch "-c" also for services that do
    # not need the contest_id because RS needs to be able to restart
    # everything without knowing which is which.
    contest_id_help = "id of the contest to automatically load"
    if ask_contest is None:
        contest_id_help += " (ignored)"
    parser.add_argument("-c", "--contest-id", action="store", type=int,
                        help=contest_id_help)
    args = parser.parse_args()

    try:
        args.shard = get_safe_shard(cls.__name__, args.shard)
    except ValueError:
        raise ConfigError("Couldn't autodetect shard number and "
                          "no shard specified for service %s, "
                          "quitting." % (cls.__name__))

    if ask_contest is not None:
        if args.contest_id is not None:
            # Test if there is a contest with the given contest id.
            from cms.db import is_contest_id
            if not is_contest_id(args.contest_id):
                print("There is no contest with the specified id. "
                      "Please try again.", file=sys.stderr)
                sys.exit(1)
            return cls(args.shard, args.contest_id)
        else:
            return cls(args.shard, ask_contest())
    else:
        return cls(args.shard)
示例#8
0
def contest_id_from_args(args_contest_id, ask_contest):
    """Return a valid contest_id from the arguments

    If the passed value is missing, ask the admins with ask_contest.
    If the contest id is invalid, print a message and exit.

    args_contest_id (int|None): the contest_id passed as argument.
    ask_contest (function|None): a function that returns a contest_id.

    """
    assert ask_contest is not None

    if args_contest_id is not None:
        contest_id = args_contest_id
    else:
        contest_id = ask_contest()

    # Test if there is a contest with the given contest id.
    from cms.db import is_contest_id
    if not is_contest_id(contest_id):
        logger.critical("There is no contest with the specified id. "
                        "Please try again.")
        sys.exit(1)
    return contest_id
示例#9
0
文件: util.py 项目: PJeBeK/cms
def contest_id_from_args(args_contest_id, ask_contest):
    """Return a valid contest_id from the arguments

    If the passed value is missing, ask the admins with ask_contest.
    If the contest id is invalid, print a message and exit.

    args_contest_id (int|None): the contest_id passed as argument.
    ask_contest (function|None): a function that returns a contest_id.

    """
    assert ask_contest is not None

    if args_contest_id is not None:
        contest_id = args_contest_id
    else:
        contest_id = ask_contest()

    # Test if there is a contest with the given contest id.
    from cms.db import is_contest_id
    if not is_contest_id(contest_id):
        logger.critical("There is no contest with the specified id. "
                        "Please try again.")
        sys.exit(1)
    return contest_id