FreeCalypso > hg > tcs211-fcmodem
comparison g23m/system/busyb/tools/cmp_maks.pl @ 0:509db1a7b7b8
initial import: leo2moko-r1
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Mon, 01 Jun 2015 03:24:05 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:509db1a7b7b8 |
---|---|
1 #!perl -w | |
2 | |
3 # This script compares similar named control files (makefile, *.mak *.mk *.pl) in two directories. | |
4 # | |
5 # | |
6 # The script expects the utility diff to be in the path. | |
7 # | |
8 # The syntax of each line in the configuration file is the following (comments start with #) | |
9 # <left_directory> <right_directory> | |
10 # | |
11 # Attention: file and directory names MUST NOT contain spaces or other white space | |
12 use strict; | |
13 | |
14 use File::Glob ':glob'; | |
15 use File::Spec; | |
16 | |
17 # define global variables | |
18 my $cnfFile=$ARGV[0]; | |
19 my $logFile="cmp_report.log"; | |
20 my $firstComment = 1; | |
21 | |
22 # find the config file | |
23 @ARGV == 1 || die "Usage:\tcmp_maks.pl config_file\n"; | |
24 | |
25 # check for diff.exe | |
26 system("diff -v > cmp_report.log 2>&1") && die "ERROR: diff must be in the path \n"; | |
27 | |
28 # open the config file | |
29 # | |
30 open (IN,"<$cnfFile")||die "Can't open $cnfFile !\n"; | |
31 | |
32 # parse the config file | |
33 # | |
34 my $line; | |
35 | |
36 while(defined($line = <IN>)) | |
37 { | |
38 # reset variable for next loop | |
39 my $leftDir; | |
40 my $rightDir; | |
41 my @files; | |
42 my @excludedFiles; | |
43 my @explicitFiles; | |
44 | |
45 # ignore comments (starting with #) | |
46 if($line =~ /^\s*\#/) { | |
47 # print out special comments starting with ## | |
48 if ($' =~ /^\s*\#/) { | |
49 if ($firstComment == 1) { | |
50 print "\n"; | |
51 $firstComment = 0; | |
52 } | |
53 print "[COMMENT] $'"; | |
54 } | |
55 else { | |
56 $firstComment = 1; | |
57 } | |
58 next; | |
59 } | |
60 | |
61 # ignore empty lines | |
62 if($line =~ /^\s+$/) { | |
63 next; | |
64 } | |
65 | |
66 # remove trailing \n | |
67 chomp($line); | |
68 | |
69 # put everything SEPARATED BY WHITESPACE in an array | |
70 @files = split(/\s+/,$line); | |
71 | |
72 # find the directories (and remove them from the array) | |
73 $leftDir = $files[0]; shift(@files); | |
74 $rightDir = $files[0]; shift(@files); | |
75 | |
76 # check whether the directories are wildcards | |
77 # if ($leftDir =~ s/(.*?)(\/\*)/$1/ && $rightDir =~ s/(.*?)(\/\*)/$1/ ) { | |
78 if ($leftDir =~ s:(.*?)([/\\]\*):$1: && $rightDir =~ s:(.*?)([/\\]\*):$1: ) { | |
79 my @dirs; | |
80 my @leftDirs; | |
81 my @rightDirs; | |
82 | |
83 # check whether the parent directories exist | |
84 (-e $leftDir) || die "Directory $leftDir does not exist !\n"; | |
85 (-e $rightDir) || die "Directory $rightDir does not exist !\n"; | |
86 | |
87 # find all sub directories left | |
88 @dirs = bsd_glob("$leftDir/*"); | |
89 @dirs = sort @dirs; | |
90 foreach (@dirs) { | |
91 my ($drv,$dir); | |
92 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
93 if (-d "$leftDir/$_") { | |
94 push (@leftDirs, "$_"); | |
95 } | |
96 } | |
97 | |
98 # reset variables | |
99 splice (@dirs); | |
100 | |
101 # find all sub directories right | |
102 @dirs = bsd_glob("$rightDir/*"); | |
103 @dirs = sort @dirs; | |
104 foreach (@dirs) { | |
105 my ($drv,$dir); | |
106 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
107 if (-d "$rightDir/$_") { | |
108 push (@rightDirs, "$_"); | |
109 } | |
110 } | |
111 | |
112 # create a hash that uses the filename as the key and an integer as value | |
113 # to find those directories that are found in both directories or only in one | |
114 # of them | |
115 my %count; | |
116 my $element; | |
117 | |
118 foreach $element (@leftDirs) { $count{$element}++; } | |
119 foreach $element (@rightDirs) { $count{$element}+=2; } | |
120 | |
121 # delete used arrays for later reuse | |
122 splice (@dirs); | |
123 splice (@leftDirs); | |
124 splice (@rightDirs); | |
125 | |
126 foreach $element (keys %count) { | |
127 if ($count{$element} == 1) { | |
128 push @leftDirs, $element; | |
129 } | |
130 elsif ($count{$element} == 2) { | |
131 push @rightDirs, $element; | |
132 } | |
133 elsif ($count{$element} == 3) { | |
134 push @dirs, $element; | |
135 } | |
136 } | |
137 print "[LEFT ] $leftDir/*\n"; | |
138 print "[RIGHT ] $rightDir/*\n"; | |
139 foreach $element (sort(@leftDirs)) { | |
140 print "[WARNING - directory missing right] $element\n"; | |
141 } | |
142 foreach $element (sort(@rightDirs)) { | |
143 print "[WARNING - directory missing left] $element\n"; | |
144 } | |
145 print "\n"; | |
146 foreach $element (sort(@dirs)) { | |
147 # ignore some commonly used version files | |
148 splice (@excludedFiles); | |
149 # push (@excludedFiles,"ver.obj"); | |
150 # push (@excludedFiles,"$element\_version.obj"); | |
151 cmp_dirs ("$leftDir/$element", "$rightDir/$element", \@excludedFiles, \@explicitFiles); | |
152 } | |
153 | |
154 } | |
155 else { | |
156 # check whether the directories exist | |
157 (-e $leftDir) || die "Directory $leftDir does not exist !\n"; | |
158 (-e $rightDir) || die "Directory $rightDir does not exist !\n"; | |
159 | |
160 if (defined($files[0])) { | |
161 # some files are are specified or excluded explicitely | |
162 foreach (@files) { | |
163 if (/^-/) { | |
164 push(@excludedFiles,$'); | |
165 } | |
166 elsif(/^\+/) { | |
167 (-e "$leftDir/$'") || die "File $rightDir/$' does not exist !\n"; | |
168 (-e "$rightDir/$'") || die "File $rightDir/$' does not exist !\n"; | |
169 push(@explicitFiles,$'); | |
170 } | |
171 } | |
172 } | |
173 cmp_dirs ($leftDir, $rightDir, \@excludedFiles, \@explicitFiles); | |
174 } | |
175 | |
176 } | |
177 | |
178 | |
179 # close the config file | |
180 # | |
181 close IN; | |
182 | |
183 # delete the log file | |
184 # | |
185 #unlink($logFile); | |
186 | |
187 ################### | |
188 # | |
189 sub cmp_dirs { | |
190 | |
191 my $leftDir = $_[0]; | |
192 my $rightDir = $_[1]; | |
193 my @excludedFiles = @{$_[2]}; | |
194 my @explicitFiles = @{$_[3]}; | |
195 | |
196 my @files; | |
197 my @leftFiles; | |
198 my @rightFiles; | |
199 my $element; | |
200 | |
201 if (defined($explicitFiles[0])) { | |
202 @files = @explicitFiles; | |
203 } | |
204 else { | |
205 @leftFiles = bsd_glob("$leftDir/*.mk"); | |
206 push (@leftFiles, bsd_glob("$leftDir/*.mak")); | |
207 push (@leftFiles, bsd_glob("$leftDir/*makefile")); | |
208 push (@leftFiles, bsd_glob("$leftDir/*.pl")); | |
209 | |
210 @leftFiles = sort @leftFiles; | |
211 foreach (@leftFiles) { | |
212 my ($drv,$dir); | |
213 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
214 } | |
215 | |
216 @rightFiles = bsd_glob("$rightDir/*.mk"); | |
217 push (@rightFiles, bsd_glob("$rightDir/*.mak")); | |
218 push (@rightFiles, bsd_glob("$rightDir/*makefile")); | |
219 push (@rightFiles, bsd_glob("$rightDir/*.pl")); | |
220 @rightFiles = sort @rightFiles; | |
221 foreach (@rightFiles) { | |
222 my ($drv,$dir); | |
223 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
224 } | |
225 | |
226 # create a hash that uses the filename as the key and an integer as value | |
227 # to find those files that are found in both directories or only in one | |
228 # of them | |
229 my %count; | |
230 | |
231 foreach $element (@leftFiles) { $count{$element}++; } | |
232 foreach $element (@rightFiles) { $count{$element}+=2; } | |
233 | |
234 # delete used arrays for later reuse | |
235 splice (@files); | |
236 splice (@leftFiles); | |
237 splice (@rightFiles); | |
238 | |
239 # remove all exclude files | |
240 foreach $element (@excludedFiles) { | |
241 if (exists($count{$element})) { | |
242 # print "Skipping file: $element \n"; | |
243 delete($count{$element}); | |
244 } | |
245 } | |
246 | |
247 foreach $element (keys %count) { | |
248 if ($count{$element} == 1) { | |
249 push @leftFiles, $element; | |
250 } | |
251 elsif ($count{$element} == 2) { | |
252 push @rightFiles, $element; | |
253 } | |
254 elsif ($count{$element} == 3) { | |
255 push @files, $element; | |
256 } | |
257 } | |
258 } | |
259 | |
260 | |
261 print "[LEFT ] $leftDir/\n"; | |
262 print "[RIGHT] $rightDir/\n"; | |
263 | |
264 if(defined($leftFiles[0]) || defined($rightFiles[0]) ) { | |
265 my %tmp; | |
266 | |
267 foreach $element (@leftFiles) { $tmp{$element} = " [WARNING - file missing right] $element\n" }; | |
268 foreach $element (@rightFiles) { $tmp{$element} = " [WARNING - file missing left ] $element\n"; } | |
269 | |
270 foreach $element (sort(keys %tmp)) { | |
271 print $tmp{$element}; | |
272 } | |
273 } | |
274 | |
275 | |
276 foreach (sort(@files)) { | |
277 if (system("cmp $leftDir/$_ $rightDir/$_ >>$logFile 2>&1")) { | |
278 print " [ERROR - files differ] $_\n"; | |
279 print " [Diff'ing files] $leftDir/$_ $rightDir/$_\n"; | |
280 | |
281 my $leftFile = "$leftDir/$_"; | |
282 $leftFile =~ s:/:\\:g ; | |
283 my $rightFile = "$rightDir/$_"; | |
284 $rightFile =~ s:/:\\:g ; | |
285 | |
286 open(DIFFOUT, '-|', "diff -I ^#.*\$ -t -w -y --suppress-common-lines --width=120 $leftFile $rightFile"); | |
287 #indent output | |
288 while (defined($line = <DIFFOUT>)) { | |
289 print " $line"; | |
290 } | |
291 | |
292 close(DIFFOUT); | |
293 print " \n"; | |
294 | |
295 } | |
296 } | |
297 print "\n"; | |
298 } |