Esempio n. 1
0
    def __init__(self, local_unbox_dirpath):
        """Instantiates a new module to manage the local Unbox directory

        Keyword Args:
        local_unbox_dirpath -- path to the local Unbox directory
        """
        local_unbox_dirpath = unbox_filesystem.abs_path(local_unbox_dirpath)

        # Test if local Unbox directory exists and create if not
        if not self._is_valid_local_dir(local_unbox_dirpath):
            try:
                self._make_local_dir(local_unbox_dirpath)
            except OSError as e:
                raise ValueError("Could not create local Unbox directory: " + str(e))

        # Register input variables
        self._local_unbox_dirpath = local_unbox_dirpath

        # Read backup index file
        backup_index_filepath = os.path.join(local_unbox_dirpath, self._BACKUP_DIRNAME, self._BACKUP_INDEX_FILENAME)
        if os.path.isfile(backup_index_filepath):
            backup_index_fp = open(backup_index_filepath, "r")
            self._backup_index = json.load(backup_index_fp)
            backup_index_fp.close()

        # Read local index file
        local_index_filepath = os.path.join(self._local_unbox_dirpath, self._INDEX_FILENAME)
        if os.path.isfile(local_index_filepath):
            dropbox_index_fp = open(dropbox_index_filepath, "r")
            self._dropbox_index = json.load(dropbox_index_fp)
            dropbox_index_fp.close()
Esempio n. 2
0
    def __init__(self, local_unbox_dirpath):
        """Instantiates a new module to manage the local Unbox directory

        Keyword Args:
        local_unbox_dirpath -- path to the local Unbox directory
        """
        local_unbox_dirpath = unbox_filesystem.abs_path(local_unbox_dirpath)

        # Test if local Unbox directory exists and create if not
        if not self._is_valid_local_dir(local_unbox_dirpath):
            try:
                self._make_local_dir(local_unbox_dirpath)
            except OSError as e:
                raise ValueError("Could not create local Unbox directory: " +
                                 str(e))

        # Register input variables
        self._local_unbox_dirpath = local_unbox_dirpath

        # Read backup index file
        backup_index_filepath = os.path.join(local_unbox_dirpath,
                                             self._BACKUP_DIRNAME,
                                             self._BACKUP_INDEX_FILENAME)
        if os.path.isfile(backup_index_filepath):
            backup_index_fp = open(backup_index_filepath, "r")
            self._backup_index = json.load(backup_index_fp)
            backup_index_fp.close()

        # Read local index file
        local_index_filepath = os.path.join(self._local_unbox_dirpath,
                                            self._INDEX_FILENAME)
        if os.path.isfile(local_index_filepath):
            dropbox_index_fp = open(dropbox_index_filepath, "r")
            self._dropbox_index = json.load(dropbox_index_fp)
            dropbox_index_fp.close()
Esempio n. 3
0
    def backup_delete(self, path):
        """Deletes a resource in the backup system

        Keyword Args:
        path -- local path to resource

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not self.backup_exists(path):
            raise ValueError("Cannot delete file from backup; file does not exist")


        # Remove the resource and the directory holding it
        resource_filename = os.path.basename(path)
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath, self._BACKUP_DIRNAME)
        resource_parent_dirpath = os.path.join(BACKUP_DIRPATH, self._backup_index[path]) 
        resource_parent_filepath = os.path.join(resource_parent_dirpath, resource_filename)
        if os.path.isdir(resource_parent_filepath):
            shutil.rmtree(saved_local_filepath)
        else:
            os.remove(path)
        os.rmdir(resource_parent_dirpath)

        # Register the removal in the backup index 
        del(self._backup_index[path])
        self._write_backup_index()
Esempio n. 4
0
    def backup_add(self, path):
        """Moves the given file/directory tree into the backup system

        Keyword Args:
        path -- local path to the file 

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not os.path.exists(path):
            raise ValueError("Cannot add file to backup; file does not exist")
        if self.backup_exists(path):
            raise ValueError("Cannot add file to backup; file already exists in backup")

        # Hashes the resource's full path, creates a directory with the hash name, and places the resource inside
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath, self._BACKUP_DIRNAME)
        upstream, basename = os.path.split(path)
        dest_dir = str(uuid.uuid4())
        dest_path = os.path.join(BACKUP_DIRPATH, dest_dir)
        os.mkdir(dest_path)
        shutil.move(path, dest_path)

        # Register the addition in the backup index
        self._backup_index[path] = dest_dir
        self._write_backup_index()
Esempio n. 5
0
    def backup_delete(self, path):
        """Deletes a resource in the backup system

        Keyword Args:
        path -- local path to resource

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not self.backup_exists(path):
            raise ValueError(
                "Cannot delete file from backup; file does not exist")

        # Remove the resource and the directory holding it
        resource_filename = os.path.basename(path)
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath,
                                      self._BACKUP_DIRNAME)
        resource_parent_dirpath = os.path.join(BACKUP_DIRPATH,
                                               self._backup_index[path])
        resource_parent_filepath = os.path.join(resource_parent_dirpath,
                                                resource_filename)
        if os.path.isdir(resource_parent_filepath):
            shutil.rmtree(saved_local_filepath)
        else:
            os.remove(path)
        os.rmdir(resource_parent_dirpath)

        # Register the removal in the backup index
        del (self._backup_index[path])
        self._write_backup_index()
Esempio n. 6
0
    def backup_restore(self, path):
        """Retrieves the file/diretory tree from the backup system

        Keyword Args:
        path -- local path to retrieve

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not self.backup_exists(path):
            raise ValueError(
                "Cannot restore file from backup; file does not exist")

        # Restore backed-up resource into original location
        resource_filename = os.path.basename(path)
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath,
                                      self._BACKUP_DIRNAME)
        resource_parent_dirpath = os.path.join(BACKUP_DIRPATH,
                                               self._backup_index[path])
        resource_parent_filepath = os.path.join(resource_parent_dirpath,
                                                resource_filename)
        shutil.move(resource_parent_filepath, path)
        os.rmdir(resource_parent_dirpath)

        # Register the removal in the backup index
        del (self._backup_index[path])
        self._write_backup_index()
Esempio n. 7
0
    def backup_add(self, path):
        """Moves the given file/directory tree into the backup system

        Keyword Args:
        path -- local path to the file 

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not os.path.exists(path):
            raise ValueError("Cannot add file to backup; file does not exist")
        if self.backup_exists(path):
            raise ValueError(
                "Cannot add file to backup; file already exists in backup")

        # Hashes the resource's full path, creates a directory with the hash name, and places the resource inside
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath,
                                      self._BACKUP_DIRNAME)
        upstream, basename = os.path.split(path)
        dest_dir = str(uuid.uuid4())
        dest_path = os.path.join(BACKUP_DIRPATH, dest_dir)
        os.mkdir(dest_path)
        shutil.move(path, dest_path)

        # Register the addition in the backup index
        self._backup_index[path] = dest_dir
        self._write_backup_index()
Esempio n. 8
0
    def __init__(self, dropbox_dirpath, unbox_dirname):
        """Instantiates a new Dropbox filesystem module at the given location

        Keyword Args:
        dropbox_dirpath -- path to the user's Dropbox directory
        unbox_dirname -- name of Unbox directory in the Dropbox folder
        """
        # Ensure argument validity
        if dropbox_dirpath == None or unbox_dirname == None:
            raise TypeError("Cannot use 'None' type")
        if len(dropbox_dirpath.strip()) == 0 or len(unbox_dirname.strip()) == 0:
            raise ValueError("Cannot have empty arguments")
        dropbox_dirpath = unbox_filesystem.abs_path(dropbox_dirpath)

        # Ensure valid Dropbox path
        if not os.path.isdir(dropbox_dirpath):
            raise ValueError("Invalid Dropbox path")

        # Test if Dropbox Unbox directory exists and create if not
        unbox_dirpath = os.path.join(dropbox_dirpath, unbox_dirname)
        if not os.path.isdir(unbox_dirpath):
            try:
                os.mkdir(unbox_dirpath)
            except OSError as e:
                raise ValueError("Could not create Dropbox Unbox directory: " + str(e))
        self._unbox_dirpath = unbox_dirpath # Path to Dropbox Unbox directory

        # Read Dropbox index file
        dropbox_index_filepath = os.path.join(self._unbox_dirpath, self._INDEX_FILENAME)
        if os.path.isfile(dropbox_index_filepath):
            dropbox_index_fp = open(dropbox_index_filepath, "r")
            self._dropbox_index = pickle.load(dropbox_index_fp)
            dropbox_index_fp.close()
        else:
            self._dropbox_index = dict()
Esempio n. 9
0
    def backup_restore(self, path):
        """Retrieves the file/diretory tree from the backup system

        Keyword Args:
        path -- local path to retrieve

        """
        # Check validity
        path = unbox_filesystem.abs_path(path)
        if not self.backup_exists(path):
            raise ValueError("Cannot restore file from backup; file does not exist")

        # Restore backed-up resource into original location
        resource_filename = os.path.basename(path)
        BACKUP_DIRPATH = os.path.join(self._local_unbox_dirpath, self._BACKUP_DIRNAME)
        resource_parent_dirpath = os.path.join(BACKUP_DIRPATH, self._backup_index[path]) 
        resource_parent_filepath = os.path.join(resource_parent_dirpath, resource_filename)
        shutil.move(resource_parent_filepath, path)
        os.rmdir(resource_parent_dirpath)

        # Register the removal in the backup index 
        del(self._backup_index[path])
        self._write_backup_index()
Esempio n. 10
0
    def add_resource(self, local_path, version="1.0", dependencies=None):
        """Copies the given resource into the Dropbox system
        NOTE: The resource must not already be in the system

        Keyword Args:
        path -- path to resource to add
        version -- version to give the resource (default: 1.0)
        dependencies -- dependencies the resource depends on (default: None)

        Return:
        Path to the resource in Dropbox
        """
        if dependencies == None:
            dependencies = set()

        # Sanity checks
        if local_path == None:
            raise ValueError("Cannot add resource to Dropbox directory; cannot use null resource")
        local_path = unbox_filesystem.abs_path(local_path)
        upstream, resource_filename = os.path.split(local_path)
        if not os.path.exists(local_path):
            raise ValueError("Cannot add resource to Dropbox; resource does not exist")
        if not (os.path.isdir(local_path) or os.path.isfile(local_path)):
            raise ValueError("Cannot add resource to Dropbox; resource is not a file or directory")
        if resource_filename in self._dropbox_index:
            raise ValueError("Cannot add resource to Dropbox; resource with same name already exists")
        if version == None or len(version.strip()) == 0:
            raise ValueError("Cannot have empty version name")
        version = version.strip()
        if version == self._CURRENT_RSRC_VERSION_KEYWORD:
            raise ValueError("Version name '" + self._CURRENT_RSRC_VERSION_KEYWORD + "' is a reserved name")

        # Creates directory structure to copy resource to
        upstream, resource_filename = os.path.split(local_path)
        parent_dirname = str(uuid.uuid4())
        parent_dirpath = os.path.join(self._unbox_dirpath, parent_dirname)
        os.mkdir(parent_dirpath)
        version_dirpath = os.path.join(parent_dirpath, str(version))
        os.mkdir(version_dirpath)

        # Creates symlink to current version
        current_version_linkpath = os.path.join(parent_dirpath, self._CURRENT_RSRC_VERSION_KEYWORD)
        os.symlink(version_dirpath, current_version_linkpath)

        # Copies resource to proper spot
        dest_dirpath = os.path.join(parent_dirpath, str(version))
        if os.path.isdir(local_path):
            shutil.copytree(local_path, dest_dirpath)
        else:
            shutil.copy(local_path, dest_dirpath)

        # Register the addition in the Dropbox index
        version_info = {
            self._VERSION_INFO_KEY_DEPENDENCIES : dependencies
        }
        resource_info = {
            self._RSRC_INFO_KEY_PARENT_DIRNAME : parent_dirname, 
            self._RSRC_INFO_KEY_VERSIONS_INFO : { str(version) : version_info },
            self._RSRC_INFO_KEY_CURRENT_VERSION : version
        }
        self._dropbox_index[resource_filename] = resource_info
        self._write_index()

        return dest_dirpath