-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-not-in-dir.plx
executable file
·99 lines (88 loc) · 3.33 KB
/
find-not-in-dir.plx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl
# find-not-in-dir.plx -*- Perl -*-
# Possible bug: only -type f and -type d are checked
# Copyright (C) 2001, 2002, 2003, 2004, 2008, 2011 Bradley M. Kuhn <[email protected]>
# Copyright (C) 2011 Denver Gingerich <[email protected]>
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU General Public License (GPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the GPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file in the toplevel directory called
# "GPLv3". If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use warnings;
######################################################################
sub FindAndSortOutput {
use File::Find;
my($type, $dir, $output, $ignoreRegex, $includeRegex, $filterRewrite) = @_;
my @files;
my $buildList = sub {
my $val = $_;
chomp $val;
$val =~ s/$filterRewrite// if defined $filterRewrite;
if ($type eq "NON-REGULAR") {
push(@files, $val) unless -f $_;
} elsif ($type eq "FILES") {
push(@files, $val) if -f $_;
} elsif ($type eq "DIRECTORY") {
push(@files, $val) if -d $_;
} else {
die "Unknown type requested: $type";
}
};
find({ wanted => $buildList, no_chdir => 1}, $dir);
if (defined $output) {
open(FILE_OUTPUT, ">$output") or
die "$0: unable to open temporary output file, $output: $!";
}
my @sortedChompedFiles;
foreach my $file (sort {$a cmp $b } @files) {
chomp $file;
next if defined $ignoreRegex and $file =~ /$ignoreRegex/;
next if defined $includeRegex and $file !~ /$includeRegex/;
push(@sortedChompedFiles, $file);
print FILE_OUTPUT "$file\n" if defined $output;
}
close FILE_OUTPUT if defined $output;
die "unable to write to output file: $output: $! ($?)"
if $? != 0 and defined $output;
return @sortedChompedFiles;
}
######################################################################
if (@ARGV < 2) {
print STDERR "usage: $0 <DIRECTORY_OF_FILES_TO_EXCLUDE> <DIRECTORY> ... <DIRECTORY>\n";
exit 1;
}
my $excludeDirectory = shift @ARGV;
my(@directories) = @ARGV;
my($type, $dir, $output, $ignoreRegex, $includeRegex, $filterRewrite) = @_;
my(@ignoredFiles) = FindAndSortOutput("FILES", $excludeDirectory, undef,
'(?:/\.svn/|~$)');
my %ignoredFiles;
foreach my $file (@ignoredFiles) {
$file =~ s%.*/([^/]+)$%$1%;
$ignoredFiles{$file} = 1;
}
my @files;
foreach my $dir (@directories) {
push(@files, FindAndSortOutput("FILES", $dir, undef, '(?:/\.svn/|~$)'));
}
foreach my $file (@files) {
print "$file\n" unless defined $ignoredFiles{$file};
$ignoredFiles{$file} = 1; # Ignore the ones we've already printed so they aren't printed again.
}
###############################################################################
#
# Local variables:
# compile-command: "perl -c find-not-in-dir.plx"
# End: