Esempio n. 1
0
def update_test_specs(files_path, assignment_id, markus_address, test_specs):
    """
    Copy new test scripts for a given assignment to from the files_path
    to a new location. Indicate that these new test scripts should be used instead of
    the old ones. And remove the old ones when it is safe to do so (they are not in the
    process of being copied to a working directory).

    This function should be used by an rq worker.
    """
    # TODO: catch and log errors
    test_script_dir_name = "test_scripts_{}".format(int(time.time()))
    clean_markus_address = clean_dir_name(markus_address)
    new_dir = os.path.join(*stringify(TEST_SCRIPT_DIR, clean_markus_address,
                                      assignment_id, test_script_dir_name))
    new_files_dir = os.path.join(new_dir, FILES_DIRNAME)
    move_tree(files_path, new_files_dir)
    if "hooks_file" in test_specs:
        src = os.path.join(new_files_dir, test_specs["hooks_file"])
        if os.path.isfile(src):
            os.rename(src, os.path.join(new_dir, HOOKS_FILENAME))
    test_specs = create_tester_environments(new_files_dir, test_specs)
    settings_filename = os.path.join(new_dir, SETTINGS_FILENAME)
    with open(settings_filename, "w") as f:
        json.dump(test_specs, f)
    old_test_script_dir = test_script_directory(markus_address, assignment_id)
    test_script_directory(markus_address, assignment_id, set_to=new_dir)

    if old_test_script_dir is not None:
        with fd_open(old_test_script_dir) as fd:
            with fd_lock(fd, exclusive=True):
                destroy_tester_environments(old_test_script_dir)
                shutil.rmtree(old_test_script_dir,
                              onerror=ignore_missing_dir_error)
def get_test_script_key(markus_address, assignment_id):
    """
    Return unique key for each assignment used for
    storing the location of test scripts in Redis
    """
    clean_markus_address = file_management.clean_dir_name(markus_address)
    return f"{clean_markus_address}_{assignment_id}"
Esempio n. 3
0
def _store_results(
        client: ClientType, results_data: Dict[str, Union[List[ResultData],
                                                          str, int]]) -> None:
    """
    Write the results of multiple test script runs to an output file as a json string.
    """
    destination = os.path.join(
        TEST_RESULT_DIR, clean_dir_name(client.unique_run_str())) + ".json"
    with open(destination, "w") as f:
        json.dump(results_data, f, indent=4)
Esempio n. 4
0
def store_results(results_data, markus_address, assignment_id, group_id,
                  submission_id):
    """
    Write the results of multiple test script runs to an output file as a json string.
    The output file is located at:
        {TEST_RESULT_DIR}/{markus_address}/{assignment_id}/{group_id}/{submission_id}/ouput.json
    """
    clean_markus_address = clean_dir_name(markus_address)
    run_time = "run_{}".format(int(time.time()))
    destination = os.path.join(*stringify(
        TEST_RESULT_DIR,
        clean_markus_address,
        assignment_id,
        group_id,
        "s{}".format(submission_id or ""),
        run_time,
    ))
    os.makedirs(destination, exist_ok=True)
    with open(os.path.join(destination, "output.json"), "w") as f:
        json.dump(results_data, f, indent=4)
Esempio n. 5
0
def update_test_specs(client_type: str, client_data: Dict) -> None:
    """
    Download test script files and test specs for the given client.
    Indicate that these new test scripts should be used instead of
    the old ones. Remove the old ones when it is safe to do so (they are not in the
    process of being copied to a working directory).
    """
    # TODO: catch and log errors
    client = get_client(client_type, client_data)
    test_script_dir_name = "test_scripts_{}".format(int(time.time()))
    unique_script_str = client.unique_script_str()
    new_dir = os.path.join(TEST_SCRIPT_DIR, clean_dir_name(unique_script_str),
                           test_script_dir_name)
    test_specs = client.get_test_specs()

    new_files_dir = os.path.join(new_dir, FILES_DIRNAME)
    os.makedirs(new_files_dir, exist_ok=True)
    client.write_test_files(new_files_dir)
    filenames = [
        os.path.relpath(path, new_files_dir)
        for fd, path in recursive_iglob(new_files_dir) if fd == "f"
    ]
    try:
        validate_against_schema(test_specs, filenames)
    except Exception as e:
        sys.stderr.write(f"Form Validation Error: {str(e)}")
        sys.exit(1)

    test_specs = _create_tester_environments(new_files_dir, test_specs)
    settings_filename = os.path.join(new_dir, SETTINGS_FILENAME)
    with open(settings_filename, "w") as f:
        json.dump(test_specs, f)
    old_test_script_dir = test_script_directory(unique_script_str)
    test_script_directory(unique_script_str, set_to=new_dir)

    if old_test_script_dir is not None and os.path.isdir(old_test_script_dir):
        _destroy_tester_environments(old_test_script_dir)
        shutil.rmtree(old_test_script_dir, onerror=ignore_missing_dir_error)
Esempio n. 6
0
 def test_with_forward_slash_modified(self, name: str):
     """ Should replace forward slashes with underscores """
     assert fm.clean_dir_name(name).replace("/", "_")
Esempio n. 7
0
 def test_no_forward_slash(self, name: str):
     """ Should not change a name that does not contain a forward slash character """
     assert fm.clean_dir_name(name) == name