# 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
def extract_repository(container, archive_path, destination_path, user=None):
    """
    Extracts a repository in the directory that it's located in. Optionally changes ownership of
    the extracted repository to USER.

    """
    result = container.bash("""if [[ -f {0} ]]; then
                                   mkdir -p {1} &>/dev/null
                                   tar -xf {0} -C {1} --strip-components=1 &>/dev/null
                                   if [[ $? = 0 ]]; then
                                       echo -n "pass"
                                   fi
                               fi
                            """.format(bash_quote(archive_path), bash_quote(destination_path)))
    if result != "pass":
        raise JobFailedError("I tried to extract your code from GitHub, but was unable to do " +
                             "so. Someone should probably investigate this.")
    if user:
        take_ownership(container, destination_path, user)


def take_ownership(container, path, user, group=None):
    if group is None:
        group = user
    result = container.bash("""chown -R %s:%s -- %s &>/dev/null
                               if [[ $? = 0 ]]; then
                                   echo -n "pass"
                               fi
                            """ % (bash_quote(user), bash_quote(group), bash_quote(path)))
    if result != "pass":

# 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

def extract_repository(container, archive_path, destination_path, user=None):
    """
    Extracts a repository in the directory that it's located in. Optionally changes ownership of
    the extracted repository to USER.

    """
    result = container.bash("""if [[ -f {0} ]]; then
                                   mkdir -p {1} &>/dev/null
                                   tar -xf {0} -C {1} --strip-components=1 &>/dev/null
                                   if [[ $? = 0 ]]; then
                                       echo -n "pass"
                                   fi
                               fi
                            """.format(bash_quote(archive_path), bash_quote(destination_path)))
    if result != "pass":
        raise JobFailedError("I tried to extract your code from GitHub, but was unable to do " +
                             "so. Someone should probably investigate this.")
    if user:
        take_ownership(container, destination_path, user)


def take_ownership(container, path, user, group=None):
    if group is None:
        group = user
    result = container.bash("""chown -R %s:%s -- %s &>/dev/null
                               if [[ $? = 0 ]]; then
                                   echo -n "pass"
                               fi
                            """ % (bash_quote(user), bash_quote(group), bash_quote(path)))
    if result != "pass":

        raise JobFailedError("I tried taking ownership of this directory, but it didn't work. " +