Exemplo n.º 1
0
 def _find_relevant_files(self, extracted_files_dir: Path):
     result = []
     for path in safe_rglob(extracted_files_dir):
         if path.is_file() and not path.is_symlink():
             file_type = get_file_type_from_path(path.absolute())
             if self._has_relevant_type(file_type):
                 result.append(('/{}'.format(path.relative_to(Path(self.root_path))), file_type['full']))
     return result
def test_safe_rglob(symlinks, directories, expected_number):
    test_dir_path = Path(
        TestFailSafeFileOperations.get_directory_of_current_file(
        )).parent / 'tests' / 'data'
    result = list(
        safe_rglob(test_dir_path,
                   include_symlinks=symlinks,
                   include_directories=directories))
    assert len(result) == expected_number
Exemplo n.º 3
0
def add_unpack_statistics(extraction_dir: Path, meta_data: Dict):
    unpacked_files, unpacked_directories = 0, 0
    for extracted_item in safe_rglob(extraction_dir):
        if extracted_item.is_file():
            unpacked_files += 1
        elif extracted_item.is_dir():
            unpacked_directories += 1

    meta_data['number_of_unpacked_files'] = unpacked_files
    meta_data['number_of_unpacked_directories'] = unpacked_directories
Exemplo n.º 4
0
    def extract_files_from_file(self, file_path, tmp_dir):
        self._initialize_shared_folder(tmp_dir)
        shutil.copy2(file_path, str(Path(tmp_dir, 'input', Path(file_path).name)))

        output, return_code = execute_shell_command_get_return_code(
            'docker run --privileged -m {}m -v /dev:/dev -v {}:/tmp/extractor --rm fkiecad/fact_extractor'.format(self.config.get('unpack', 'memory_limit', fallback='1024'), tmp_dir)
        )
        if return_code != 0:
            error = 'Failed to execute docker extractor with code {}:\n{}'.format(return_code, output)
            logging.error(error)
            raise RuntimeError(error)

        self.change_owner_back_to_me(tmp_dir)
        return [item for item in safe_rglob(Path(tmp_dir, 'files')) if not item.is_dir()]
Exemplo n.º 5
0
 def _analyze_metadata_of_mounted_dir(self, mounted_dir: Path):
     for file_ in safe_rglob(
             mounted_dir, False,
             False):  # FIXME files with PermissionError could be ignored
         if file_.is_file() and not file_.is_symlink():
             self._enter_results_for_mounted_file(file_)
def test_safe_rglob_empty_dir():
    assert EMPTY_FOLDER.exists()
    result = safe_rglob(EMPTY_FOLDER)
    assert len(list(result)) == 0
def test_safe_rglob_invalid_path():
    test_path = Path('foo', 'bar')
    assert not test_path.exists()
    result = safe_rglob(test_path)
    assert len(list(result)) == 0