Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure no wrongful file collisions #8

Open
github-actions bot opened this issue Oct 26, 2022 · 0 comments
Open

Ensure no wrongful file collisions #8

github-actions bot opened this issue Oct 26, 2022 · 0 comments
Assignees
Labels

Comments

@github-actions
Copy link

Ensure no wrongful file collisions

Use database name, type, ip address, and project path/folder structure in project manager

to create a unique hash for the backup name. Keep the timestamp. So backups are still unique.

# TODO: Ensure no wrongful file collisions

import logging
import os
from datetime import datetime
from rich import print
from rich import traceback as rich_tracebacks

from rex.settings.manager import SettingsManager
import hashlib
from pydavinci import davinci

rich_tracebacks.install()
settings = SettingsManager()

logger = logging.getLogger(__name__)
logger.setLevel(settings["app"]["loglevel"])

resolve = davinci.Resolve()


class Backup:
    def __init__(self):

        # TODO: Ensure no wrongful file collisions
        # Use database name, type, ip address, and project path/folder structure in project manager
        # to create a unique hash for the backup name. Keep the timestamp. So backups are still unique.

        self.project = resolve.project
        self.db_name = resolve.project_manager.db["DbName"]
        self.timestamp = datetime.now().strftime("%H%M%S")

        self.backup_filename = (
            f"{self.db_name}_{self.project.name}_{self.timestamp}.drp"
        )
        self.static_dir = os.path.normpath(settings["backup"]["static_dir"])
        self.backup_filepath = os.path.join(self.static_dir, self.backup_filename)

        print(f"Backup Name: '{self.backup_filename}'")
        print(f"Backup Path: '{self.static_dir}'")

    def run(self, generate_checksum: bool = True) -> bool:
        """
        Run the backup routine

        Args:
            generate_checksum (bool, optional): Generate an md5 checksum file alongside project backup .drp. Defaults to True.
        """

        logger.info("Exporting project backup...")
        if not self.export_project():
            return False

        if generate_checksum:
            logger.info("Generating checksum...")
            if not self.generate_checksum():
                return False

        return True

    def export_project(self) -> bool:
        return resolve.project_manager.export_project(
            project_name=self.project.name,
            path=self.backup_filepath,
            stills_and_luts=True,
        )

    def generate_checksum(self) -> bool:
        try:
            hash_md5 = hashlib.md5()
            with open(self.backup_filepath, "rb") as f:
                for chunk in iter(lambda: f.read(4096), b""):
                    hash_md5.update(chunk)

            with open(self.backup_filepath + ".md5", "x") as checksum_file:
                checksum_file.write(hash_md5.hexdigest())

            return True

        except Exception as e:
            logger.error(e)
            return False

e054bc8da325d3c4621eb0b5abfa5baa2f244659

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant