Пример #1
0
def get_storagedir_contents(courseId, assignmentId, username):
    """Get the content of a the archive coresponding to a
    MD5Submission-type homework"""
    client = paramiko.SSHClient()
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        assignments = vmcfg.assignments()
        storage_hostname = assignments.get(assignmentId, "AssignmentStorageHost")
        storage_username = assignments.get(assignmentId, "AssignmentStorageQueryUser")
        storage_basepath = assignments.storage_basepath(assignmentId, username)

        client.load_system_host_keys(vmcfg.known_hosts_file())
        client.connect(
            storage_hostname, username=storage_username, key_filename=vmcfg.storer_sshid(), look_for_keys=False
        )

        cmd = "find " + storage_basepath + "/" + username + " \( ! -regex '.*/\..*' \) -type f"

        stdin, stdout, stderr = client.exec_command(cmd)
        result = []
        for d in stdout.readlines():
            result.append({"fileName": d})
        for f in [stdin, stdout, stderr]:
            f.close()

        return json.dumps(result)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({"errorTrace": strout.get()}, indent=4)
    finally:
        client.close()
Пример #2
0
def get_test_queue_contents(courseId):
    """Get the contents of the test queues for all testers configured
    in the system."""
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        tstcfg = vmcfg.testers()
        queue_contents = []  # array of strings
        for tester_id in tstcfg:
            # connect to the tester
            client = paramiko.SSHClient()
            try:
                client.load_system_host_keys(vmcfg.known_hosts_file())
                client.connect(
                    tstcfg.hostname(tester_id),
                    username=tstcfg.login_username(tester_id),
                    key_filename=vmcfg.storer_sshid(),
                    look_for_keys=False,
                )

                # run 'ls' in the queue_path and get it's output.
                cmd = "ls -ctgG " + tstcfg.queue_path(tester_id)
                stdin, stdout, stderr = client.exec_command(cmd)
                data = stdout.readlines()
                for f in [stdin, stdout, stderr]:
                    f.close()
                if len(data) > 0:
                    data = data[1:]
                    queue_contents.append(data)

            finally:
                client.close()

        # print the concatenation of all 'ls' instances
        return json.dumps(queue_contents, indent=4)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({"errorTrace": strout.get()}, indent=4)
Пример #3
0
def get_test_queue_contents(courseId):
    """Get the contents of the test queues for all testers configured
    in the system."""
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        tstcfg = vmcfg.testers()
        queue_contents = []  # array of strings
        for tester_id in tstcfg:
            # connect to the tester
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            try:
                client.load_system_host_keys(vmcfg.known_hosts_file())
                client.connect(tstcfg.hostname(tester_id),
                               username=tstcfg.login_username(tester_id),
                               key_filename=vmcfg.storer_sshid(),
                               look_for_keys=False)

                # run 'ls' in the queue_path and get it's output.
                cmd = 'ls -ctgG ' + tstcfg.queue_path(tester_id)
                stdin, stdout, stderr = client.exec_command(cmd)
                data = stdout.readlines()
                for f in [stdin, stdout, stderr]:
                    f.close()
                if len(data) > 0:
                    data = data[1:]
                    queue_contents.append(data)

            finally:
                client.close()

        # print the concatenation of all 'ls' instances
        return json.dumps(queue_contents, indent=4)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({'errorTrace': strout.get()}, indent=4)
Пример #4
0
def get_storagedir_contents(courseId, assignmentId, username):
    """Get the content of a the archive coresponding to a
    MD5Submission-type homework"""
    client = paramiko.SSHClient()
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        assignments = vmcfg.assignments()
        storage_hostname = assignments.get(assignmentId,
                                           'AssignmentStorageHost')
        storage_username = assignments.get(assignmentId,
                                           'AssignmentStorageQueryUser')
        storage_basepath = assignments.storage_basepath(assignmentId, username)

        client.load_system_host_keys(vmcfg.known_hosts_file())
        client.connect(storage_hostname,
                       username=storage_username,
                       key_filename=vmcfg.storer_sshid(),
                       look_for_keys=False)

        cmd = "find " + storage_basepath + '/' + username + \
            " \( ! -regex '.*/\..*' \) -type f"

        stdin, stdout, stderr = client.exec_command(cmd)
        result = []
        for d in stdout.readlines():
            result.append({'fileName': d})
        for f in [stdin, stdout, stderr]:
            f.close()

        return json.dumps(result)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({'errorTrace': strout.get()}, indent=4)
    finally:
        client.close()
Пример #5
0
def validate_md5_submission(courseId, assignmentId, username, archiveFileName):
    """Checks whether a MD5Submission is valid:
       * checks that the uploaded md5 corresponds to the one of the machine
       * checks that the archive uploaded by the student is a zip file

       On success returns 'ok'.
       On failure reports the source of the failure:
       - 'md5' - the uploaded md5 does not match the one computed on the archive
       - 'zip' - the uploaded archive is not zip.
    """

    md5_calculated = ""
    md5_uploaded = ""
    archive_file_type = ""

    client = paramiko.SSHClient()
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        assignments = vmcfg.assignments()
        storage_hostname = assignments.get(assignmentId,
                                           'AssignmentStorageHost')
        storage_username = assignments.get(assignmentId,
                                           'AssignmentStorageQueryUser')
        storage_basepath = assignments.storage_basepath(assignmentId, username)

        client.load_system_host_keys(vmcfg.known_hosts_file())
        client.connect(storage_hostname,
                       username=storage_username,
                       key_filename=vmcfg.storer_sshid(),
                       look_for_keys=False)

        archive_abs = os.path.join(storage_basepath, username, archiveFileName)

        # XXX: This will take ages to compute! I wonder how many
        # connections will Apache hold.
        stdin, stdout, stderr = client.exec_command("md5sum " +
                                                    QuoteForPOSIX(archive_abs))
        md5_calculated = stdout.readline().split()[0]
        for f in [stdin, stdout, stderr]:
            f.close()

        stdin, stdout, stderr = client.exec_command("file " +
                                                    QuoteForPOSIX(archive_abs))
        archive_file_type = stdout.readline()[len(archive_abs):].split(
        )[1].lower()
        for f in [stdin, stdout, stderr]:
            f.close()

        vmpaths = paths.VmcheckerPaths(vmcfg.root_path())
        submission_dir = vmpaths.dir_cur_submission_root(
            assignmentId, username)
        md5_fpath = paths.submission_md5_file(submission_dir)

        if os.path.isfile(md5_fpath):
            with open(md5_fpath, 'r') as f:
                md5_uploaded = f.read(32)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({'errorTrace': strout.get()}, indent=4)
    finally:
        client.close()

    if not md5_calculated == md5_uploaded:
        return "md5"  # report the type of the problem

    if not archive_file_type == "zip":
        return "zip"  # report the type of the problem

    return "ok"  # no problemo
Пример #6
0
def validate_md5_submission(courseId, assignmentId, username, archiveFileName):
    """Checks whether a MD5Submission is valid:
       * checks that the uploaded md5 corresponds to the one of the machine
       * checks that the archive uploaded by the student is a zip file

       On success returns 'ok'.
       On failure reports the source of the failure:
       - 'md5' - the uploaded md5 does not match the one computed on the archive
       - 'zip' - the uploaded archive is not zip.
    """

    md5_calculated = ""
    md5_uploaded = ""
    archive_file_type = ""

    client = paramiko.SSHClient()
    try:
        vmcfg = CourseConfig(CourseList().course_config(courseId))
        assignments = vmcfg.assignments()
        storage_hostname = assignments.get(assignmentId, "AssignmentStorageHost")
        storage_username = assignments.get(assignmentId, "AssignmentStorageQueryUser")
        storage_basepath = assignments.storage_basepath(assignmentId, username)

        client.load_system_host_keys(vmcfg.known_hosts_file())
        client.connect(
            storage_hostname, username=storage_username, key_filename=vmcfg.storer_sshid(), look_for_keys=False
        )

        archive_abs = os.path.join(storage_basepath, username, archiveFileName)

        # XXX: This will take ages to compute! I wonder how many
        # connections will Apache hold.
        stdin, stdout, stderr = client.exec_command("md5sum " + QuoteForPOSIX(archive_abs))
        md5_calculated = stdout.readline().split()[0]
        for f in [stdin, stdout, stderr]:
            f.close()

        stdin, stdout, stderr = client.exec_command("file " + QuoteForPOSIX(archive_abs))
        archive_file_type = stdout.readline()[len(archive_abs) :].split()[1].lower()
        for f in [stdin, stdout, stderr]:
            f.close()

        vmpaths = paths.VmcheckerPaths(vmcfg.root_path())
        submission_dir = vmpaths.dir_cur_submission_root(assignmentId, username)
        md5_fpath = paths.submission_md5_file(submission_dir)

        if os.path.isfile(md5_fpath):
            with open(md5_fpath, "r") as f:
                md5_uploaded = f.read(32)
    except:
        strout = OutputString()
        traceback.print_exc(file=strout)
        return json.dumps({"errorTrace": strout.get()}, indent=4)
    finally:
        client.close()

    if not md5_calculated == md5_uploaded:
        return "md5"  # report the type of the problem

    if not archive_file_type == "zip":
        return "zip"  # report the type of the problem

    return "ok"  # no problemo