コード例 #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)
コード例 #2
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)
コード例 #3
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)
コード例 #4
0
    def delete_file(path):
        """Deletes a file at the specified path.

        path: The path at which the file to be deleted is located.

        raise: BadPathError if path did not lead to a file.
        """

        # Delete the patch.
        try:
            os.remove(path)
        except FileNotFoundError:
            # Couldn't find the file at the supplied path.
            raise errors.BadPathError(path, 301)
コード例 #5
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)
コード例 #6
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)
コード例 #7
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)
コード例 #8
0
    def export_patch_bin(self, patch, dest, slot=-1, overwrite=False):
        """ Attempts to export a patch from the application to the
        specified supplied path. Normally, this will be a user's SD
        card, but it could be used for a location on a user's machine
        as well.
        This method is specifically for the exporting of .bin patch
        files.

        patch: A string representing the patch that is to be exported.
               This does not need a file extension, but supplying one
               will not cause the method to fail.
        dest: A string representing the destination that the patch will
              be exported to.
        slot: Optional. If a patch is being exported to an SD card, set
              this to the desired slot that you wish to see the patch
              appear in on a ZOIA. Valid slot numbers are 0 - 63. Should
              the slot be negative, a slot identifier will not be added
              to the name.
        overwrite: Optional. This should be set to True when the export
                   will conflict with a file that is already in the
                   dest, but the user has specified that it is okay to
                   overwrite said file.

        raise: BadPathError if path did not lead to a file.
        raise: ExportingError if an invalid slot number is provided or
               shutil is unable to copy the file over to the dest.
        """

        # Check to see if there is already another patch with that slot #
        # in the destination.
        if not overwrite:
            for pch in os.listdir(dest):
                if pch[:3] == "00{}".format(slot) or pch[:3] == "0{}".format(
                        slot):
                    raise errors.ExportingError(slot, 703)
        else:
            # Delete the previous patch that occupied the slot.
            for pch in os.listdir(dest):
                if pch[:3] == "00{}".format(slot) or pch[:3] == "0{}".format(
                        slot):
                    os.remove(os.path.join(dest, pch))
                    break

        # Prepare the name of the patch. We need to access the metadata.
        # Extract the patch id from the supplied patch parameter.
        patch = patch.split(".")[0]
        idx = patch.split(".")[0]
        idx = idx.split("_")[0]

        # Get the metadata for this patch.
        try:
            with open(
                    os.path.join(self.back_path, idx, "{}.json".format(patch)),
                    "r") as f:
                metadata = json.loads(f.read())
        except FileNotFoundError:
            raise errors.BadPathError(301)

        # Extract the filename attribute.
        name = metadata["files"][0]["filename"]
        # Check to see if the patch author included a ZOIA prefix.
        # (and drop it from the name).
        if "_" in name and len(name.split("_")[0]) == 3:
            try:
                float(name.split("_")[0])
                name = name[4:]
            except ValueError:
                pass
        try:
            if -1 < slot < 10:
                # one digit
                name = "00{}_".format(slot) + name
            elif slot >= 10 < 64:
                # two digits
                name = "0{}_".format(slot) + name
            elif slot < 0:
                # No slot, not going to an sd card, no need for digits.
                pass
            else:
                # Incorrect slot number provided.
                raise errors.ExportingError(slot, 701)
        except FileNotFoundError or FileExistsError:
            pass

        # Add the file extension if need be.
        if "." not in patch:
            patch += ".bin"

        # If the dest is not a directory, create one.
        if not os.path.isdir(dest):
            os.mkdir(dest)

        # Rename the patch and export.
        try:
            shutil.copy(os.path.join(self.back_path, idx, patch),
                        os.path.join(dest, name))
        except FileNotFoundError or FileExistsError:
            raise errors.ExportingError(patch)