Пример #1
0
def validate_folder_name(release: Release,
                         violations: List[Violation],
                         folder_name: str,
                         skip_comparison: bool,
                         group_by_category: bool = False,
                         codec_short: bool = True) -> None:
    if not release.can_validate_folder_name():
        violations.append(
            Violation(ViolationType.FOLDER_NAME,
                      "Cannot validate folder name"))
        return

    valid_folder_name = release.get_folder_name(
        codec_short=codec_short, group_by_category=group_by_category)
    if valid_folder_name != folder_name and not skip_comparison:
        violations.append(
            Violation(
                ViolationType.FOLDER_NAME,
                "Invalid folder name '{folder_name}' should be '{valid_folder_name}'"
                .format(folder_name=folder_name,
                        valid_folder_name=valid_folder_name)))
Пример #2
0
def move_rename_folder(release: Release, unique_releases: Set[Tuple],
                       curr_dir: str, dest_folder: str, duplicate_folder: str,
                       args: argparse.Namespace) -> str:
    """Rename a release folder, and move to a destination folder"""

    # if a dry run,or the folder name cannot be validated, do nothing
    if args.dry_run or not release.can_validate_folder_name():
        return curr_dir

    moved_dir = curr_dir

    # rename the release folder
    codec_short = not args.full_codec_names
    fixed_dir = os.path.join(
        os.path.split(curr_dir)[0],
        release.get_folder_name(codec_short=codec_short,
                                group_by_category=args.group_by_category))
    if curr_dir != fixed_dir:
        if not os.path.exists(fixed_dir) or os.path.normcase(
                curr_dir) == os.path.normcase(fixed_dir):
            while True:
                try:
                    os.rename(curr_dir, fixed_dir)
                    break
                except PermissionError:
                    logging.getLogger(__name__).error(
                        "PermissionError: could not rename directory to {0}".
                        format(fixed_dir))
                    time.sleep(1)

            moved_dir = fixed_dir
        else:
            logging.getLogger(__name__).error(
                "Release folder already exists: {0}".format(fixed_dir))

    # move the release folder to a destination
    moved_duplicate = False
    if dest_folder and release.num_violations == 0:
        artist_folder = flatten_artists(release.validate_release_artists()) \
            if args.group_by_artist and not release.is_va() else ""

        category_folder = str(
            release.category.value) if args.group_by_category else ""
        curr_dest_parent_folder = os.path.join(dest_folder, category_folder,
                                               artist_folder)
        curr_dest_folder = os.path.join(
            curr_dest_parent_folder,
            release.get_folder_name(codec_short=codec_short,
                                    group_by_category=args.group_by_category))

        if os.path.normcase(moved_dir) != os.path.normcase(curr_dest_folder):
            if not os.path.exists(curr_dest_parent_folder):
                os.makedirs(curr_dest_parent_folder, exist_ok=True)
            if not os.path.exists(curr_dest_folder):
                os.rename(moved_dir, curr_dest_folder)
                moved_dir = curr_dest_folder

                # clean up empty directories
                curr_src_parent_folder = os.path.split(fixed_dir)[0]
                while not os.listdir(curr_src_parent_folder):
                    os.rmdir(curr_src_parent_folder)
                    curr_src_parent_folder = os.path.split(
                        curr_src_parent_folder)[0]
            else:
                if duplicate_folder:
                    release_folder_name = release.get_folder_name(
                        codec_short=codec_short,
                        group_by_category=args.group_by_category)
                    moved_dir = move_duplicate(duplicate_folder, moved_dir,
                                               release_folder_name)
                    moved_duplicate = True

                else:
                    logging.getLogger(__name__).error(
                        "Destination folder already exists: {0}".format(
                            fixed_dir))

    # deduplicate versions of the same release
    unique_release = UniqueRelease(
        release.validate_release_artists(),
        release.validate_release_date().split("-")[0],
        release.validate_release_title(), release.validate_codec(),
        release.get_codec_rank(), moved_dir)

    if duplicate_folder and release.num_violations == 0 and not moved_duplicate:
        if unique_release in unique_releases:
            existing = [x for x in unique_releases if x == unique_release][0]
            if unique_release > existing:
                # move the existing one
                release_folder_name = os.path.split(existing.path)[1]
                moved_dir = move_duplicate(duplicate_folder, existing.path,
                                           release_folder_name)
                unique_releases.remove(unique_release)
                unique_releases.add(unique_release)
            else:
                # move the current one
                release_folder_name = release.get_folder_name(
                    codec_short=codec_short,
                    group_by_category=args.group_by_category)
                moved_dir = move_duplicate(duplicate_folder, moved_dir,
                                           release_folder_name)

        else:
            unique_releases.add(unique_release)

    return moved_dir