Example #1
0
def delete_patch_sd(path):
    """ Attempts to delete a patch located on an inserted, ZOIA
    formatted, SD card.

    This method relies on the user leaving the SD card inserted
    while the deletion occurs. Otherwise, corruption is likely
    to occur. In such situations, either a DeletionError, or
    BadPathError may be raised.

    path: A string representing the path to the patch on the inserted
          SD card. This path must include the name of the file to be
          deleted, otherwise the method will not know which file to
          delete and a DeletionError will be raised.
    Raises BadPatchError if path does not lead to a patch.
    Raises a DeletionError if patch_dir is malformed, cannot find a
    a patch to delete, or the SD card is removed during deletion.
    """

    global backend_path
    if backend_path is None:
        backend_path = determine_backend_path()

    if path is None:
        raise errors.DeletionError(None)

    if not len(path.split(".")) > 1:
        # There should be a file extension.
        raise errors.DeletionError(path, 403)

    # Delete the patch.
    try:
        os.remove(path)
    except FileNotFoundError:
        # Couldn't find the patch at the supplied path.
        raise errors.BadPathError(path, 301)
Example #2
0
def delete_full_patch_directory(patch_dir):
    """ Forces the deletion of an entire patch directory, should
    one exist.

    Please note that this method will not attempt to correct invalid
    input. Please ensure that the patch parameter is exclusively the
    name of the patch directory that will be deleted.

    patch_dir: A string representing the patch directory to be deleted.
    Raises a DeletionError if patch_dir is malformed.
    Raises a BadPathError if patch_dir does not lead to a patch.
    """

    global backend_path
    if backend_path is None:
        backend_path = determine_backend_path()

    if patch_dir is None:
        raise errors.DeletionError(None)

    if len(patch_dir.split(".")) > 1:
        # There shouldn't be a file extension.
        raise errors.DeletionError(patch_dir, 401)
    if len(patch_dir.split("_")) > 1:
        # There shouldn't be a version extension.
        raise errors.DeletionError(patch_dir, 402)

    try:
        shutil.rmtree(os.path.join(backend_path, patch_dir))
    except FileNotFoundError:
        # Couldn't find the patch directory that was passed.
        raise errors.BadPathError(patch_dir, 301)
Example #3
0
    def delete_patch_sd(index, sd_path):
        """Attempts to delete a patch located on an inserted SD card.

        This method relies on the user leaving the SD card inserted
        while the deletion occurs. Otherwise, corruption is likely
        to occur. In such situations, either a DeletionError, or
        BadPathError may be raised.

        index: The index number that will be searched for among the
               patches currently present in the supplied sd_path. Should
               a patch be found that matches the passed index, it will
               be deleted.
        sd_path: A string representing the path to the inserted SD card.

        raise: BadPatchError if path does not lead to a patch.
        raise: DeletionError if patch_dir is malformed, cannot find a
               a patch to delete, or the SD card is removed during
               deletion.
        """

        if sd_path is None:
            raise errors.DeletionError(None)

        if len(index) != 3:
            raise errors.DeletionError(index, 404)

        # Delete the patch.
        try:
            for pch in os.listdir(sd_path):
                if pch[:3] == index:
                    os.remove(os.path.join(sd_path, pch))
                    break
        except FileNotFoundError:
            # Couldn't find the patch at the supplied path.
            raise errors.BadPathError(sd_path, 301)
Example #4
0
    def delete_patch(self, patch):
        """Attempts to delete a patch and its metadata from
        the backend ZoiaLibraryApp directory.

        patch: A string representing the path to the patch to be
               deleted.

        raise: RenamingError if the file could not be renamed
               correctly.
        raise: BadPathError if patch was not a valid path.
        """

        if patch is None:
            raise errors.DeletionError(None)

        # Remove any file extension if it is included.
        if os.path.sep in patch:
            patch = patch.split(os.path.sep)[-1]
        patch = patch.split(".")[0]

        # Try to delete the file and metadata file.
        try:
            # Should the patch directory not exist, a BadPathError is raised.
            new_path = os.path.join(self.back_path, patch.split("_")[0])
            os.remove(os.path.join(new_path, patch + ".bin"))
            os.remove(os.path.join(new_path, patch + ".json"))
            if new_path is not None and len(os.listdir(new_path)) == 2:
                # If there aren't multiple patches left, drop the version
                # extension on the remaining patch.
                for left_files in os.listdir(new_path):
                    try:
                        os.rename(
                            os.path.join(new_path, left_files),
                            os.path.join(
                                new_path,
                                "{}.{}".format(
                                    left_files.split("_")[0], left_files.split(".")[-1]
                                ),
                            ),
                        )
                    except FileNotFoundError or FileExistsError:
                        raise errors.RenamingError(left_files, 601)
            elif new_path is not None and len(os.listdir(new_path)) == 0:
                # Special case: There are no more patches left in the
                # patch directory. As such, the directory should be removed.
                os.rmdir(new_path)
        except FileNotFoundError:
            raise errors.BadPathError(patch, 301)
Example #5
0
def delete_patch(patch):
    """Attempts to delete a patch and its metadata from
    the backend ZoiaLibraryApp directory.

    patch: A string representing the patch to be deleted.
    Raises a RenamingError if the file could not be renamed correctly.
    Raises a BadPathError if patch was not a valid path.
    """

    global backend_path
    if backend_path is None:
        backend_path = determine_backend_path()

    if patch is None:
        raise errors.DeletionError(None)

    # Remove any file extension if it is included.
    if os.path.sep in patch:
        patch = patch.split(os.path.sep)[-1]
    patch = patch.split(".")[0]

    # Try to delete the file and metadata file.
    try:
        # Should the patch directory not exist, a BadPathError is raised.
        new_path = os.path.join(backend_path, patch.split("_")[0])
        os.remove(os.path.join(new_path, patch + ".bin"))
        os.remove(os.path.join(new_path, patch + ".json"))
        if new_path is not None and len(os.listdir(new_path)) == 2:
            for left_files in os.listdir(new_path):
                try:
                    front = left_files.split("_")[0]
                    end = left_files.split(".")[1]
                    os.rename(
                        os.path.join(new_path, left_files),
                        os.path.join(new_path, "{}.{}".format(front, end)))
                except FileNotFoundError or FileExistsError:
                    raise errors.RenamingError(left_files, 601)
        elif new_path is not None and len(os.listdir(new_path)) == 0:
            # Special case: There are no more patches left in the
            # patch directory. As such, the directory should be removed.
            os.rmdir(new_path)
    except FileNotFoundError:
        raise errors.BadPathError(patch, 301)
Example #6
0
    def delete_full_patch_directory(self, patch_dir):
        """Forces the deletion of an entire patch directory, should
        one exist.

        Please note that this method will not attempt to correct invalid
        input. Please ensure that the patch parameter is exclusively the
        name of the patch directory that will be deleted.

        patch_dir: A string representing the patch directory to be deleted.

        raise: DeletionError if patch_dir is malformed.
        raise: BadPathError if patch_dir does not lead to a patch.
        """

        if patch_dir is None:
            raise errors.DeletionError(None)

        try:
            shutil.rmtree(os.path.join(self.back_path, patch_dir))
        except FileNotFoundError:
            # Couldn't find the patch directory that was passed.
            raise errors.BadPathError(patch_dir, 301)