From d6826e52c115e8254b01d09457c2fa6cc03d6f91 Mon Sep 17 00:00:00 2001
From: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Date: Wed, 9 Apr 2014 22:30:35 -0300
Subject: [PATCH] Bug 12061 - tmpl_process3.pl - Include/exclude file by name

This patch adds an option to tmpl_process3.pl
for exclude some files by matching their
names. Also modifies current code to include
only selected files to check only filenames.
Checking is case insensitive !!

To test after patch:

A) Include only 'normarc'
1. create
cd misc/translator
perl tmpl_process3.pl create -i ../../koha-tmpl/opac-tmpl/bootstrap/en -s normarc.po -r -f normarc
- check provenance of strings
egrep "^#:" normarc.po | cut -d":" -f2 | sort | uniq
- only files with normarc in their names must be present

2. update
perl tmpl_process3.pl update -i ../../koha-tmpl/opac-tmpl/bootstrap/en -s normarc.po -r -f normarc
- repeat check

3. install
mkdir test
perl tmpl_process3.pl install -i ../../koha-tmpl/opac-tmpl/bootstrap/en -o ./test -s normarc.po -r -f normarc
- check name of created files

rm -rf test normarc.po

B) Exclude
4. create
perl tmpl_process3.pl create -i ../../koha-tmpl/opac-tmpl/bootstrap/en -s xnormarc.po -r -n normarc
- check provenance
egrep "^#:" xnormarc.po | cut -d":" -f2 | sort | uniq | grep -i normarc
- there must be no results

5. update
perl tmpl_process3.pl update -i ../../koha-tmpl/opac-tmpl/bootstrap/en -s xnormarc.po -r -n normarc
- check provenance

6. install
mkdir test
perl tmpl_process3.pl install -i ../../koha-tmpl/opac-tmpl/bootstrap/en -o ./test -s xnormarc.po -r -n normarc
- check files
find test | grep -i normarc
- there must be no results

You can also try another combination, use for example "-f patron -f user -f bottom" (or use -n)
or mixed "-f marc -n normarc", do create/install and look filenames

Changed to adapt current functionality (-f)

http://bugs.koha-community.org/show_bug.cgi?id=12292
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
---
 misc/translator/tmpl_process3.pl | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/misc/translator/tmpl_process3.pl b/misc/translator/tmpl_process3.pl
index 4c23a8f..899ec7f 100755
--- a/misc/translator/tmpl_process3.pl
+++ b/misc/translator/tmpl_process3.pl
@@ -22,7 +22,7 @@ use VerboseWarnings qw( :warn :die );
 
 ###############################################################################
 
-use vars qw( @in_dirs @filenames $str_file $out_dir $quiet );
+use vars qw( @in_dirs @filenames @nofilenames $str_file $out_dir $quiet );
 use vars qw( @excludes $exclude_regex );
 use vars qw( $recursive_p );
 use vars qw( $pedantic_p );
@@ -133,7 +133,9 @@ sub text_replace (**) {
 }
 
 sub listfiles {
-    my($dir, $type, $action, $filenames) = @_;
+    my($dir, $type, $action) = @_;
+    my $filenames   = join ('|', @filenames);
+    my $nofilenames = join ('|', @nofilenames);
     my @it = ();
     if (opendir(DIR, $dir)) {
         my @dirent = readdir DIR;   # because DIR is shared when recursing
@@ -144,12 +146,13 @@ sub listfiles {
             || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) {
             ;
             } elsif (-f $path) {
-                my $basename = basename $path;
+                my $basename = fileparse( $path );
                 push @it, $path
-                    if ( not @$filenames or ( grep { $path =~ /$_/ } @$filenames ) )
+                    if     ( not @filenames   or $basename =~ /($filenames)/i   )
+                       and ( not @nofilenames or $basename !~ /($nofilenames)/i ) # files not to include
                        and (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install';
             } elsif (-d $path && $recursive_p) {
-                push @it, listfiles($path, $type, $action, $filenames);
+                push @it, listfiles($path, $type, $action);
             }
         }
     } else {
@@ -167,7 +170,7 @@ sub mkdir_recursive ($) {
     my ($prefix, $basename) = ($dir =~ /\/([^\/]+)$/s)? ($`, $1): ('.', $dir);
     mkdir_recursive($prefix) if $prefix ne '.' && !-d $prefix;
     if (!-d $dir) {
-    print STDERR "Making directory $dir..." unless $quiet;
+    print STDERR "Making directory $dir...\n" unless $quiet;
     # creates with rwxrwxr-x permissions
     mkdir($dir, 0775) || warn_normal "$dir: $!", undef;
     }
@@ -192,11 +195,13 @@ Create or update PO files from templates, or install translated templates.
       --pedantic-warnings     Issue warnings even for detected problems
                               which are likely to be harmless
   -r, --recursive             SOURCE in the -i option is a directory
-  -f, --filename=FILE         FILE is a specific filaneme.
+  -f, --filename=FILE         FILE is a specific filename or part of it.
                               If given, only these files will be processed.
+  -n, --nofilename=FILE       FILE is a specific filename or part of it.
+                              If given, these files will not be processed.
   -s, --str-file=FILE         Specify FILE as the translation (po) file
                               for input (install) or output (create, update)
-  -x, --exclude=REGEXP        Exclude files matching the given REGEXP
+  -x, --exclude=REGEXP        Exclude dirs matching the given REGEXP
       --help                  Display this help and exit
   -q, --quiet                 no output to screen (except for errors)
 
@@ -221,6 +226,7 @@ sub usage_error (;$) {
 GetOptions(
     'input|i=s'             => \@in_dirs,
     'filename|f=s'          => \@filenames,
+    'nofilename|n=s'        => \@nofilenames,
     'outputdir|o=s'         => \$out_dir,
     'recursive|r'           => \$recursive_p,
     'str-file|s=s'          => \$str_file,
@@ -260,13 +266,13 @@ my @in_files;
 # Generate the list of input files if a directory is specified
 # input is a directory, generates list of files to process
 
-for my $fn ( @filenames ) {
+for my $fn ( ( @filenames, @nofilenames ) ) {
     die "You cannot specify input files and directories at the same time.\n"
         if -d $fn;
 }
 for my $in_dir ( @in_dirs ) {
     $in_dir =~ s/\/$//; # strips the trailing / if any
-    @in_files = ( @in_files, listfiles($in_dir, $type, $action, \@filenames) );
+    @in_files = ( @in_files, listfiles($in_dir, $type, $action));
 }
 
 # restores the string list from file
-- 
1.9.1