forked from OTRS/module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileCheck.pl
executable file
·381 lines (304 loc) · 9.78 KB
/
FileCheck.pl
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/perl
# --
# module-tools/FileCheck.pl - searchs for mistakes in the list of files registered in SOPM
# Copyright (C) 2001-2012 OTRS AG, http://otrs.org/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# 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 Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
use strict;
use warnings;
use Getopt::Std;
use File::Find;
use XML::Simple;
=head1 NAME
FileCheck.pl - searchs for mistakes in the list of files registered in SOPM
=head1 SYNOPSIS
FileCheck.pl -m <source-module-folder> [-v]
[-v] optional to see intermediate results
=head1 DESCRIPTION
This script searchs for mistakes in the list of files registered
in SOPM file. It reads the complete sopm list and tries to find every file
in the main module directory or even look for all files in main module directory
and compare against the sopm list to see if you forgot to include some in the list.
=cut
# get options
my %Opt;
getopt( 'hm', \%Opt );
if ( exists $Opt{h} ) {
Usage();
exit;
}
# some globals
my $ModulePath;
my $CleanSOPMFile;
# look for module main directory in parameters and look for it.
if ( $Opt{m} && -d $Opt{m} ) {
# find and read sopm in main path.
$ModulePath = $Opt{m};
$ModulePath =~ s{ /* \z}{}xms;
my $SOPMFile = glob "$ModulePath/*.sopm";
$CleanSOPMFile = $SOPMFile;
$CleanSOPMFile =~ s{\Q$ModulePath\E/?}{}xms;
if ($SOPMFile) {
# get list of files from SOPM file.
my @SOPM = GetPackageFileList( SOPM => $SOPMFile );
# look in path for every file in module directory.
my @Directory = GetDirectoryFileList( ModuleDirectory => $ModulePath );
# list all files found and those which not.
DiffList(
SOPM => \@SOPM,
Directory => \@Directory,
);
}
else {
Usage('sopm file not found');
}
exit;
}
elsif ( !$Opt{m} ) {
Usage('needed module path!');
}
else {
Usage('non existent path!');
}
sub GetPackageFileList {
my %Param = @_;
# look for needed parameters
for my $Parameter (qw( SOPM )) {
if ( !$Param{$Parameter} ) {
die "\n Needed parameter, $Parameter! \n";
}
}
my $SOPM = $Param{SOPM};
die "\n can't find sopm file!: $SOPM \n" if !-e $SOPM;
print "\n >> reading sopm file: $CleanSOPMFile ... \n";
# build xml object
my $XMLObject = XML::Simple->new(
ForceArray => [qw(File)],
);
# read XML file
my $XMLContent = $XMLObject->XMLin($SOPM);
# find the list of needed files inside sopm.
my @FileList = map { $_->{Location} } @{ $XMLContent->{Filelist}->{File} };
if ( $Opt{v} ) {
PrintFiles(
Source => 'SOPM file',
ListOfFiles => \@FileList,
);
}
return @FileList;
}
sub GetDirectoryFileList {
my %Param = @_;
# look for needed parameters
for my $Needed (qw( ModuleDirectory )) {
if ( !$Param{$Needed} ) {
die "\n Needed parameter, $Needed! \n";
}
}
# files and directories to ignore
my @IgnoreFiles = (
'.cvsignore', '.project', '.includepath', '.*\.sopm', '\.tmp.*', '.filecheckignore'
, '.filecheckdirignore'
);
my @IgnoreDirs = ( 'CVS', '.settings', );
my $Dir = $Param{ModuleDirectory};
# set custom files to ignore file location
my $CustomIgnoreFilesLocation = $Param{ModuleDirectory} . '/.filecheckignore';
# set custom dirs to ignore file location
my $CustomIgnoreDirsLocation = $Param{ModuleDirectory} . '/.filecheckdirignore';
# read and add custom files to ignore
if ( -e $CustomIgnoreFilesLocation ) {
# check if file can not open
my $FH;
if ( !open $FH, '<', $CustomIgnoreFilesLocation ) {
print "Can't open or read '$CustomIgnoreFilesLocation': $!"
}
else {
my @CustomIgnoreFiles = <$FH>;
for my $File (@CustomIgnoreFiles) {
chomp($File);
push @IgnoreFiles, $File;
}
}
}
# read and add custom files to ignore
if ( -e $CustomIgnoreDirsLocation ) {
# check if file can not open
my $FH;
if ( !open $FH, '<', $CustomIgnoreDirsLocation ) {
print "Can't open or read '$CustomIgnoreDirsLocation': $!"
}
else {
my @CustomIgnoreDirs = <$FH>;
for my $Dir (@CustomIgnoreDirs) {
chomp($Dir);
push @IgnoreDirs, $Dir;
}
}
}
my @FileList;
my $WantedFunction = sub {
my $FullName = $File::Find::name;
my $Name = $_;
my $CleanName = $FullName;
# cleaning FullName to store
$CleanName =~ s{\A\Q$ModulePath\E/?}{};
# filtering results
# ignore directories
return if -d $Name;
# ignore files inside preconfigured directories
my $MatchDir = 0;
my $MatchFile = 0;
my $MatchCustomFile = 0;
for my $IgnoreDirectory (@IgnoreDirs) {
$MatchDir = $FullName =~ m{$IgnoreDirectory/}xms;
last if ($MatchDir);
}
for my $IgnoreFile (@IgnoreFiles) {
$MatchFile = $Name =~ m{\A$IgnoreFile\z}xms;
$MatchCustomFile = $CleanName =~ m{\A$IgnoreFile\z}xms;
last if ( $MatchFile || $MatchCustomFile );
}
if ( !$MatchDir && !$MatchFile && !$MatchCustomFile ) {
push @FileList, $CleanName;
}
};
print " >> reading file system: $Dir ... \n";
find( $WantedFunction, $Dir );
if (@FileList) {
if ( $Opt{v} ) {
PrintFiles(
Source => 'file system',
ListOfFiles => \@FileList,
);
}
return sort @FileList;
}
else {
return 0;
}
}
sub PrintFiles() {
my %Param = @_;
print "\n+ List of files in $Param{Source} +\n";
print "---------\n";
my $Counter = 0;
for my $file ( sort @{ $Param{ListOfFiles} } ) {
print "$file \n";
$Counter++;
}
print "---------\n"
. "Count: $Counter \n"
. "---------\n"
. "\n";
}
sub DiffList {
my %Param = @_;
# look for needed parameters
for my $Needed (qw( SOPM Directory )) {
if ( !$Param{$Needed} ) {
die "\n Needed parameter, $Needed! \n";
}
}
# convert array to hash to compare faster
my %SOPM = map { $_ => 1 } @{ $Param{SOPM} };
my %Directory = map { $_ => 1 } @{ $Param{Directory} };
# get files in SOPM and not in File System
# diff Hashes
my @SOPMvsDIR;
for my $key ( keys %SOPM ) {
if ( !$Directory{$key} ) {
push @SOPMvsDIR, $key;
}
}
# get files in File System and not in SOPM
# diff Hashes
my @DIRvsSOPM;
for my $key ( keys %Directory ) {
if ( !$SOPM{$key} ) {
push @DIRvsSOPM, $key;
}
}
if ( @DIRvsSOPM || @SOPMvsDIR ) {
#Diff
print "\n"
. "\n"
. "------------------------------------------------------\n"
. " DIFF RESULTS \n"
. "------------------------------------------------------\n"
. " SOPM: $CleanSOPMFile \n"
. " Directory: $ModulePath \n"
. "------------------------------------------------------\n";
# print diff
if (@SOPMvsDIR) {
print "----------------\n"
. "This files are in SOPM but were not found in File System.\n"
. "Please check for syntax errors\n"
. "----------------\n";
my $Counter = 0;
for my $file (sort @SOPMvsDIR) {
print " " . $file . "\n";
$Counter++;
}
print "----------------\n"
. "Files: $Counter \n"
. "----------------\n";
}
if (@DIRvsSOPM) {
print "\n----------------\n"
. "This files are in File System but were not found in SOPM.\n"
. "Some times this is ok, but maybe you forgot to register them\n"
. "in SOPM file\n"
. "----------------\n";
my $Counter = 0;
for my $file (sort @DIRvsSOPM) {
print " $file\n";
$Counter++;
}
print "----------------\n"
. "Files: $Counter \n"
. "----------------\n";
}
}
else {
print <<'END_OF_HERE';
----------------
Good Job, all files are well registered!
----------------
END_OF_HERE
}
}
# exit after printing the usage message
sub Usage {
my ($Message) = @_;
$Message ||= '';
print <<"END_OF_HERE";
$Message
USAGE:
FileCheck.pl -m <source-module-path> -v (optional to see intermediate results)
You can add custom files to ignore in source-module-path/.filecheckignore
.filecheckignore content Example:
doc/en/ModuleDoc.xml
Changes.MyModule
You can add custom directories to ignore in source-module-path/.filecheckdirignore
.filecheckdirignore content Example:
development
testing
END_OF_HERE
exit 1;
}
1;