Beispiel #1
0
 def test_bad_file_hash_type(self):
     try:
         reusables.file_hash("test_file", hash_type="There is no way this is a valid hash type")
     except ValueError:
         return True
     else:
         assert False
Beispiel #2
0
 def test_bad_file_hash_type(self):
     try:
         reusables.file_hash(
             "test_file",
             hash_type="There is no way this is a valid hash type")
     except ValueError:
         return True
     else:
         assert False
def perform_backup(manifest):
    corrupt_files = []
    for file in save_game_folder.glob("*/*.sav"):
        if manifest[file.parent.name][file.name].corrupt:
            continue

        contents = file.read_bytes()
        if contents.count(NUL) > (len(contents) / 2) or len(contents) < 100:
            logger.warning(f"DANGER {file.parent.name}\\{file.name} corrupt!")
            manifest[file.parent.name][file.name].corrupt = True
            corrupt_files.append(file)
            continue

        manifest[file.parent.name][file.name].corrupt = False
        current_hash = reusables.file_hash(file)
        if current_hash == manifest[file.parent.name][file.name].last_hash:
            logger.debug(f"{file.parent.name}\\{file.name} has not changed")
            continue
        manifest[file.parent.name][file.name].last_hash = current_hash
        backup_path = save_game_backup_folder / file.parent.name / f"{file.stem}_{dt()}.sav.bak"
        backup_path.parent.mkdir(exist_ok=True, parents=True)
        shutil.copy(file, backup_path)
        if "files" not in manifest[file.parent.name][file.name]:
            manifest[file.parent.name][file.name].files = [str(backup_path)]
        else:
            manifest[file.parent.name][file.name].files.append(str(backup_path))
            if len(manifest[file.parent.name][file.name].files) > 2:
                old_file = manifest[file.parent.name][file.name].files.pop(0)
                Path(old_file).unlink()
        logger.info(f"Backing up {file.parent.name}\\{file.name} to {backup_path.parent.name}\\{backup_path.name}")
    return corrupt_files
Beispiel #4
0
 def test_hash_file(self):
     valid = "81dc9bdb52d04dc20036dbd8313ed055"
     hash_file = "1234"
     with open(os.path.join(test_root, "test_hash"), 'w') as out_hash:
         out_hash.write(hash_file)
     resp = reusables.file_hash(os.path.join(test_root, "test_hash"))
     os.unlink(os.path.join(test_root, "test_hash"))
     assert resp == valid, (resp, valid)
Beispiel #5
0
 def test_hash_file(self):
     valid = "81dc9bdb52d04dc20036dbd8313ed055"
     hash_file = "1234"
     with open(os.path.join(test_root, "test_hash"), 'w') as out_hash:
         out_hash.write(hash_file)
     resp = reusables.file_hash(os.path.join(test_root, "test_hash"))
     os.unlink(os.path.join(test_root, "test_hash"))
     assert resp == valid, (resp, valid)
Beispiel #6
0
    def file_info(self, file: str) -> tuple:
        """
        Returns file information.

        :param file: Path to file as string.
        :return:
        """

        sha256 = reusables.file_hash(file, "sha256")
        ext = self.file_extension(file)
        size = os.path.getsize(file)

        return sha256, ext, size
def remove_duplicates(directory):
    sizes = {}

    for image in reusables.find_files(directory):
        file_size = os.path.getsize(image)
        if file_size not in sizes:
            sizes[file_size] = [image]
        else:
            sizes[file_size].append(image)

    size_matches = [v for v in sizes.values() if len(v) > 1]

    hashes = {}
    for possible_duplicates in size_matches:
        for dd in possible_duplicates:
            file_hash = reusables.file_hash(dd)
            if file_hash not in hashes:
                hashes[file_hash] = [dd]
            else:
                hashes[file_hash].append(dd)

    return [v for v in hashes.values() if len(v) > 1]
Beispiel #8
0
    def file_info(self, file):
        sha256 = reusables.file_hash(file, "sha256")
        ext = self.file_extension(file)
        size = os.path.getsize(file)

        return sha256, ext, size
Beispiel #9
0
    def file_info(self, file):
        sha256 = reusables.file_hash(file, "sha256")
        ext = self.file_extension(file)
        size = os.path.getsize(file)

        return sha256, ext, size