Skip to content

init

The challenge init subcommand

init_challenge(challenge, base_dir, mkdir, config_ctx=ConfigContext())

Setups the basic challenge files

Parameters:

Name Type Description Default
challenge str

one of

  • [int] challenge id
  • [str] challenge slug
  • [str] challenge url
required
base_dir str

directory for setting up challenge

required
mkdir bool

should the base directory be created if it doesn't exist?

required
config_ctx ConfigContext

CLI Config

ConfigContext()

Examples:

To create a challenge directory named example for example-challenge

from aicrowd import challenge
challenge.init("example-challenge", "example", True)
Source code in aicrowd/challenge/init.py
def init_challenge(
    challenge: str,
    base_dir: str,
    mkdir: bool,
    config_ctx: ConfigContext = ConfigContext(),
):
    """
    Setups the basic challenge files

    Args:
        challenge: one of

            - [`int`] challenge id
            - [`str`] challenge slug
            - [`str`] challenge url
        base_dir: directory for setting up challenge
        mkdir: should the base directory be created if it doesn't exist?
        config_ctx: CLI Config

    Example:
        To create a challenge directory named `example` for `example-challenge`

        ```python
        from aicrowd import challenge
        challenge.init("example-challenge", "example", True)
        ```
    """
    log = logging.getLogger()

    api_key = must_get_api_key(config_ctx)

    challenge_id, challenge_slug = parse_cli_challenge(challenge, api_key)

    if challenge_id is None or challenge_slug is None:
        log.error("Failed to parse challenge")
        raise ChallengeNotFoundException(
            "Challenge with the given details could not be found",
            exit_code=INVALID_PARAMETER,
        )

    # create the challenge directory
    if base_dir is None:
        challenge_dir = Path.cwd()
    else:
        challenge_dir = base_dir

    if mkdir:
        challenge_dir = create_challenge_dir(challenge_dir, challenge_slug)

    if not validate_challenge_dir(challenge_dir):
        raise ChallengeNotFoundException(
            "Challenge dir should be empty and not reside within an existing git repository.",
            INVALID_PARAMETER,
        )

    repo = Repo.init(challenge_dir)

    with repo.config_writer() as git_conf:
        git_conf.add_value(
            ChallengeConstants.CONFIG_SECTION_NAME,
            ChallengeConstants.CONFIG_ID_KEY,
            challenge_id,
        )
        git_conf.add_value(
            ChallengeConstants.CONFIG_SECTION_NAME,
            ChallengeConstants.CONFIG_SLUG_KEY,
            challenge_slug,
        )