0

After doing some research I have a much better idea as to what the error is. Pardon me if I'm wrong but it has to do with the pipe/socket you're connecting to closing before all of the data can be transferred? I'm unclear as to the best way to fix this. How do you prevent it from closing prematurely? My program connects to a network attached storage device, checks if certain folders are present (if not creates them), and then moves images and mp3s to those folders. The error seems to happen most often when transferring large amounts of files. The last one I tried included 64 tracks and 15 images of various sizes. The largest being 180kb. And each step of the way I got the broken pipe error. I connect to the server once when the application starts. I'm using PySMB to do this. I know there are probably ways I can optimize this code, which is possibly part of the problem as well.

Here is some of my code. For moving the images and checking if the folders to store the images exist or not on the NAS.

def split_into_folders(art, first_check):
    """Alphabetically organizes a string into folders based on the character
    it starts with and creates those folders if they don't already exist.

    :param art: the jpg file containing the album art itself as well as
    metadata about it
    """
    service_name = "websites"
    covers_folder = "Covers"
    original = "original"
    small = 75
    medium = 155
    large = 256
    default = 300
    if _platform == "darwin" or _platform == "linux" or _platform == "linux2":
        a_f = "/a-f/"
        g_m = "/g-m/"
        n_s = "/n-s/"
        t_z = "/t-z/"
        slash = "/"
    else:
        a_f = "\\a-f\\"
        g_m = "\\g-m\\"
        n_s = "\\n-s\\"
        t_z = "\\t-z\\"
        slash = "\\"
    if first_check:
        # Checks if a particular folder on the service_name exists and if it
        # doesn't creates it.
        print("Covers folder exists" + " | " + str(does_directory_exist(
                service_name, slash, covers_folder)))
        # Checks to see if 75 (small), 155 (medium), 255 (large), 300 (default)
        # and original folders exists and if they don't creates them. Using
        # slash very loosely here to accomplish my checks.
        covers_folder = covers_folder + slash
        print("Small folder exists" + " | " + str(does_directory_exist(
                service_name, covers_folder, str(small))))
        print("Medium folder exists" + " | " + str(does_directory_exist(
                service_name, covers_folder, str(medium))))
        print("Large folder exists" + " | " + str(does_directory_exist(
                service_name, covers_folder, str(large))))
        print("Default folder exists" + " | " + str(does_directory_exist(
                service_name, covers_folder, str(default))))
        print("Original folder exists" + " | " + str(does_directory_exist(
                service_name, covers_folder, original)))
    if art is not None:
        s = os.path.basename(art)
        size = Image.open(art)
        covers_folder = covers_folder + slash
        if int(size.size[1]) == small:
            album_art_size_folder = str(small)
        elif int(size.size[1]) == medium:
            album_art_size_folder = str(medium)
        elif int(size.size[1]) == large:
            album_art_size_folder = str(large)
        elif int(size.size[1]) == default:
            album_art_size_folder = str(default)
        else:
            album_art_size_folder = original
        if s[0].lower() >= "g" and s[0].lower() <= "m":
            album_art_size_destination_folder = covers_folder + \
                album_art_size_folder + slash
            print(
                    "g-m folder exists" + " | " +
                    str(does_directory_exist(
                        service_name, album_art_size_destination_folder,
                        g_m.replace("/", "").replace("\\", ""))))
            album_art_size_destination_folder = covers_folder + \
                album_art_size_folder
            return album_art_size_destination_folder + g_m, service_name
        elif s[0].lower() >= "n" and s[0].lower() <= "s":
            album_art_size_destination_folder = \
                covers_folder + album_art_size_folder + slash
            print(
                    "n-s folder exists" + " | " +
                    str(does_directory_exist(
                        service_name, album_art_size_destination_folder,
                        n_s.replace("/", "").replace("\\", ""))))
            album_art_size_destination_folder = covers_folder + \
                album_art_size_folder
            return album_art_size_destination_folder + n_s, service_name
        elif s[0].lower() >= "t" and s[0].lower() <= "z":
            album_art_size_destination_folder = \
                covers_folder + album_art_size_folder + slash
            print(
                "t-z folder exists" + " | " +
                str(does_directory_exist(
                    service_name, album_art_size_destination_folder,
                    t_z.replace("/", "").replace("\\", ""))))
            album_art_size_destination_folder = covers_folder + \
                album_art_size_folder
            return album_art_size_destination_folder + t_z, service_name
        else:
            album_art_size_destination_folder = \
                covers_folder + album_art_size_folder + slash
            print(
                "a-album_art_size_destination_folder folder exists" + " | " +
                str(does_directory_exist(
                    service_name, album_art_size_destination_folder,
                    a_f.replace("/", "").replace("\\", ""))))
            album_art_size_destination_folder = covers_folder + \
                album_art_size_folder
            return album_art_size_destination_folder + a_f, service_name
    else:
        return covers_folder, service_name    

    def does_directory_exist(service_name, path, directory_check):
        """
        :param service_name: the name of the shared folder for the path,
        should be a string or unicode
        :param path: path relative to the service_name where we are
        interested to learn about its files/sub-folders,
        should be a string or unicode
        :param directory_check: the directory or file we are checking whether or
        not exists, should be a string
        :returns: a boolean, either true if the directory or file already exists
        or false if a new one needed to be created
        """
        global IS_CONNECTED
        # Get a list of all of the items (directories and files)
        # in the shared folder.
        try:
            results_list = CONN.listPath(service_name, path, timeout=6000)
            for item in results_list:
                if directory_check == item.filename and item.isDirectory:
                    # If the directory or file we're looking for already exists on
                    # the network storage device than we're golden.
                    return True
                elif item is results_list[-1] and directory_check != item.filename:
                    # If the last item in the shared folder is still not the file
                    # we are looking for than that means we need to create it.
                    try:
                        CONN.createDirectory(
                            service_name, "%s" % (
                                path + directory_check), timeout=6000)
                    except BaseException as exception:
                        print("does_directory_exist" + " | createDirectory | "
                                                       "" + str(exception))
                    return False
        except BaseException as exception:
            print("does_directory_exist" + " | results_list | " + str(exception)) 
terratunaz
  • 614
  • 3
  • 9
  • 19

0 Answers0