# Path: ob2/dockergrader/job.py
# class JobFailedError(Exception):
#     def __init__(self, message, critical=False):
#         super(JobFailedError, self).__init__(message)
#         self.args = (message, critical)
# 
#     def __str__(self):
#         return self.args[0]


import errno
import logging
import re
import shutil
import ob2.util.github_api as github_api
from contextlib import contextmanager
from tempfile import mkdtemp
from ob2.dockergrader.job import JobFailedError

@contextmanager
def get_working_directory():
    """
    Creates a temporary directory and deletes it when finished. You should probably wrap your
    autograder script within one of these things:

    with get_working_directory() as wd:
        do_stuff_with(wd)
        ...etc...

    """
    try:
        directory = mkdtemp()
        yield directory
    finally:
        try:
            shutil.rmtree(directory)
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise


def download_repository(*args, **kwargs):
    """
    Downloads a repository to a local directory. Raises JobFailedError if the download was not
    successful.

    """
    if not github_api.download_archive(*args, **kwargs):

# Path: ob2/dockergrader/job.py
# class JobFailedError(Exception):
#     def __init__(self, message, critical=False):
#         super(JobFailedError, self).__init__(message)
#         self.args = (message, critical)
# 
#     def __str__(self):
#         return self.args[0]


# Path: ob2/dockergrader/helpers.py
import errno
import logging
import re
import shutil
import ob2.util.github_api as github_api
from contextlib import contextmanager
from tempfile import mkdtemp
from ob2.dockergrader.job import JobFailedError


@contextmanager
def get_working_directory():
    """
    Creates a temporary directory and deletes it when finished. You should probably wrap your
    autograder script within one of these things:

    with get_working_directory() as wd:
        do_stuff_with(wd)
        ...etc...

    """
    try:
        directory = mkdtemp()
        yield directory
    finally:
        try:
            shutil.rmtree(directory)
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise


def download_repository(*args, **kwargs):
    """
    Downloads a repository to a local directory. Raises JobFailedError if the download was not
    successful.

    """
    if not github_api.download_archive(*args, **kwargs):

        raise JobFailedError("I tried to download your code from GitHub, but was unable to do " +