Example #1
0
def _setup_files(client: ClientType, tests_path: str,
                 test_username: str) -> None:
    """
    Copy test script files and student files to the working directory tests_path,
    then make it the current working directory.
    The following permissions are also set:
        - tests_path directory:     rwxrwx--T
        - test subdirectories:      rwxrwx--T
        - test files:               rw-r-----
        - student subdirectories:   rwxrwx---
        - student files:            rw-rw----
    """
    os.chmod(tests_path, 0o1770)
    client.write_student_files(tests_path)
    for fd, file_or_dir in recursive_iglob(tests_path):
        if fd == "d":
            os.chmod(file_or_dir, 0o770)
        else:
            os.chmod(file_or_dir, 0o770)
        shutil.chown(file_or_dir, group=test_username)
    script_files = _copy_test_script_files(client, tests_path)
    for fd, file_or_dir in script_files:
        if fd == "d":
            os.chmod(file_or_dir, 0o1770)
        else:
            os.chmod(file_or_dir, 0o750)
        shutil.chown(file_or_dir, group=test_username)
Example #2
0
 def test_order_is_correct(self, structure):
     """ Should navigate the directory breadth first """
     with nested_dirs(structure) as d:
         visited = [d.name]
         for fd, name in fm.recursive_iglob(d.name):
             dir_name = os.path.dirname(name)
             assert dir_name in visited  # yielded child before parent
             if fd == "d":
                 visited.append(name)
Example #3
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)
Example #4
0
 def test_yield_all_dirs(self, structure):
     """ Should find all files recursively in the directory """
     with nested_dirs(structure) as d:
         dirs = [dname for fd, dname in fm.recursive_iglob(d.name) if fd == "d"]
         assert len(dirs) + 1 == self.dir_counter(structure)  # +1 to include the root directory
Example #5
0
 def test_yield_all_files(self, structure):
     """ Should find all files recursively in the directory """
     with nested_dirs(structure) as d:
         files = [fname for fd, fname in fm.recursive_iglob(d.name) if fd == "f"]
         assert len(files) == self.file_counter(structure)