Ticket #874: mythrename-subdirectories.patch

File mythrename-subdirectories.patch, 3.7 KB (added by sphery <mtdean@…>, 18 years ago)

Updated patch--should be used instead of mythrename-sub.diff

  • contrib/mythrename.pl

     
    2020    use DBI;
    2121    use Getopt::Long;
    2222    use File::Path;
     23    use File::Basename;
     24    use File::Find;
    2325
    2426# Some variables we'll use here
    2527    our ($dest, $format, $usage, $underscores, $live);
     
    6668
    6769    /var/video/show_names/
    6870
     71    WARNING: ALL symlinks within the destination directory and its
     72    subdirectories (recursive) will be removed when using the --link option.
     73
    6974--live
    7075
    71     Include live tv recordings, affects both linking and renaming. This assumes
    72     that recgroup = "LiveTV" for live tv recordings.
     76    Include live tv recordings, affects both linking and renaming.
    7377
    7478    default: do not link/rename live tv recordings
    7579
     
    98102    \%a = am/pm
    99103    \%A = AM/PM
    100104    \%- = separator character
     105    \%F = directory/folder (path separator)
    101106
    102107    * For end time, prepend an "e" to the appropriate time/date format code
    103108      above; i.e. "\%eG" gives the 24-hour hour for the end time.
     
    108113
    109114    * A suffix of .mpg or .nuv will be added where appropriate.
    110115
     116    * To separate links into subdirectories, include the \%F format specifier
     117      between the appropriate fields.  For example, "\%T\%F\%S" would create
     118      a directory for each title containing links for each recording named
     119      by subtitle.  You may use any number of subdirectories in your format
     120      specifier.  If used without the --link option, "\%F" will be replaced
     121      with the "\%-" separator character.
     122
    111123--separator
    112124
    113125    The string used to separate sections of the link name.  Specifying the
     
    240252    # Bad path
    241253        die "$dest is not a directory.\n" unless (-d $dest);
    242254    # Delete any old links
    243         foreach my $file (<$dest/*>) {
    244             next unless (-l $file);
    245             unlink $file or die "Couldn't remove old symlink $file:  $!\n";
    246         }
     255        find sub { if (-l $_) {
     256                       unlink $_ or die "Couldn't remove old symlink $_: $!\n";
     257                   }
     258                 }, $dest;
     259    # Delete empty directories (should this be an option?)
     260    # Let this fail silently for non-empty directories
     261        finddepth sub { rmdir $_; }, $dest;
    247262    }
    248263
    249264# Prepare a database queries
     
    339354    # Literals
    340355        $fields{'%'}  = '%';
    341356        ($fields{'-'}  = $separator) =~ s/%/%%/g;
     357        $fields{'F'} = $dest ? "\0" : "$separator";
    342358    # Make the substitution
    343359        my $keys = join('|', sort keys %fields);
    344360        my $name = $format;
     
    352368        $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg;
    353369        $name =~ s/^($safe_sep|$safe_rep|\ )+//s;
    354370        $name =~ s/($safe_sep|$safe_rep|\ )+$//s;
     371        $name =~ s/\0($safe_sep|$safe_rep|\ )+/\0/s;
     372        $name =~ s/($safe_sep|$safe_rep|\ )+\0/\0/s;
    355373    # Underscores?
    356374        if ($underscores) {
    357375            $name =~ tr/ /_/s;
    358376        }
     377    # Folders
     378        $name =~ s/\0/\//sg;
    359379    # Get a shell-safe version of the filename (yes, I know it's not needed in this case, but I'm anal about such things)
    360380        my $safe_file = $info{'basename'};
    361381        $safe_file =~ s/'/'\\''/sg;
     
    375395            }
    376396            $name .= $suffix;
    377397        # Create the link
     398            my $directory = dirname("$dest/$name");
     399            unless (-e $directory) {
     400                mkpath($directory, 0, 0755)
     401                    or die "Failed to create $directory:  $!\n";
     402            }
    378403            symlink "$video_dir/".$info{'basename'}, "$dest/$name"
    379404                or die "Can't create symlink $dest/$name:  $!\n";
    380405            if (defined($verbose)) {