Exemple #1
0
def samefile_different_casing(path1, path2):
    """Returns True if path1 and path2 refer to the same file, but differ in casing of the filename.
    Returns False if path1 and path2 refer to different files or there case is identical.
    """
    path1 = os.path.normpath(path1)
    path2 = os.path.normpath(path2)
    if path1 == path2 or not os.path.exists(path1) or not os.path.exists(path2):
        return False
    dir1 = os.path.realpath(os.path.normcase(os.path.dirname(path1)))
    dir2 = os.path.realpath(os.path.normcase(os.path.dirname(path2)))
    if dir1 != dir2 or not samefile(path1, path2):
        return False
    file1 = os.path.basename(path1)
    file2 = os.path.basename(path2)
    return file1 != file2 and file1.lower() == file2.lower()
Exemple #2
0
def get_music_rename_filename(new_path, old_path):
    # We only allow moving if the file doesn't already exist
    if os.path.exists(new_path):
        if old_path and not samefile(old_path, new_path):
            raise Exception(f"File '{new_path}' already exists.")

    # If another file matches with a different extension, try to rename it
    old_dirname = os.dirname(old_path)

    dirname = os.dirname(new_path)
    basename = os.path.basename(new_path)
    filename, ext = os.path.splitext(basename)
    ext_lower = ext.lower()

    for list_basename in os.listdir(dirname):
        list_path = os.path.join(dirname, list_basename)
        list_filename, list_ext = os.path.splitext(list_basename)
        list_ext_lower = list_ext.lower()

        # We only care about audio extensions
        if list_ext_lower not in ('mp3', 'flac', 'm4a', 'wav'):
            continue

        # We only care about different extensions
        if ext_lower == list_ext_lower:
            continue

        # Warn if different extension and different filename
        if list_filename != filename:
            log.warn(f"Found file with different extension '{list_path}'.")
            continue

        # Fail if different extension and same name, and we aren't upgrading to FLAC
        if ext_lower != ".flac" or list_ext_lower == ".flac":
            raise Exception(
                f"File with different extension '{list_path}' already exists.")

        # Otherwise backup the old file, and proceed
        backup_path = os.path.join(old_dirname, list_basename + '.bak')
        log.warn("Backing up different extension %r => %r", list_path,
                 backup_path)
        os.rename(list_path, backup_path)

    return new_path
Exemple #3
0
    def _rename(self, old_filename, metadata):
        new_filename, ext = os.path.splitext(
            self.make_filename(old_filename, metadata))

        if old_filename == new_filename + ext:
            return old_filename

        new_dirname = os.path.dirname(new_filename)
        if not os.path.isdir(new_dirname):
            os.makedirs(new_dirname)
        tmp_filename = new_filename
        i = 1
        while (os.path.exists(new_filename + ext)
               and not samefile(old_filename, new_filename + ext)):
            new_filename = "%s (%d)" % (tmp_filename, i)
            i += 1
        new_filename = new_filename + ext
        log.debug("Moving file %r => %r", old_filename, new_filename)
        move_ensure_casing(old_filename, new_filename)
        return new_filename
Exemple #4
0
def get_available_filename(new_path, old_path=None):
    """Returns an available file name.

    If new_path does already exist it appends " (N)" to the file name, where
    N is an integer counted upwards.

    If `old_path` is given the `new_path` is only changed if it does not point
    to the same file location.

    Args:
      new_path: The requested file name for the file
      old_path: The previous name of the file

    Returns: A unique available file name.
    """
    tmp_filename, ext = os.path.splitext(new_path)
    i = 1
    compare_old_path = old_path and os.path.exists(old_path)
    while (os.path.exists(new_path)
           and (not compare_old_path or not samefile(old_path, new_path))):
        new_path = "%s (%d)%s" % (tmp_filename, i, ext)
        i += 1
    return new_path