Exemplo n.º 1
0
def _do_extract(filename, args):
    stu_location = args['student_location']
    parts = filename.split()
    if stu_location:
        assignment_file = "{} {}".format(os.path.join(stu_location, parts[0]), parts[1])
    else:
        assignment_file = "{} {}".format(os.path.basename(parts[0]), parts[1])
    assignment_runner = assignment_file + 'run.py'

    if not os.path.exists(assignment_file):
        os.makedirs(assignment_file)

    if os.path.exists('run.py'):
        mv_file('run.py', assignment_runner)

    extract_archive(filename, assignment_file)
    if not args['cancel_archiving']:
        process_archive(assignment_file, filename)

    assignment_file = (stu_location if stu_location else '') + os.path.basename(assignment_file)
    create_student_dirs(assignment_file)
    return assignment_file
Exemplo n.º 2
0
def create_student_dirs(assignment):

    """
    Goes the given assignment directory and creates a new directory for each student. If the student
    submitted a python file then that is the only content of their directory. If they submitted a zip file then
    the entrie zip is extracted to theit directory.

    :param assignment: Assignment Directory Name
    :return: None
    """

    os.chdir(os.path.join('.', assignment))

    # Support File Extensions
    file_handlers = {
        '.zip': lambda fn, targ, sname: extract_archive(fn, sname),
        '.py': lambda fn, targ, sname: shutil.move(fn, targ)
    }

    for filename in [x for x in os.listdir('.')]:
        filename_with_ext = filename
        filename, ext = os.path.splitext(filename)
        filename = os.path.basename(filename)

        if ext not in file_handlers:
            continue

        parts = filename.split(' - ')
        sname, actual_filename = fix_name(parts[1]), parts[2].strip() + ext
        actual_path = os.path.join(sname, actual_filename)

        if not os.path.exists(sname):
            os.makedirs(sname)

        if os.path.exists(actual_path):
            os.remove(actual_path)

        file_handlers[ext](filename_with_ext, actual_path, sname)
        rm_file(filename_with_ext)

    os.chdir('..')