FreeCalypso > hg > tcs211-fcmodem
comparison g23m/system/busyb/tools/cmp_objs.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 object files in two directories. Comparisons is done bytewise ignoring | |
4 # the first 8 bytes of the file ( COFF magic number and time stamp). | |
5 # | |
6 # The directories must be specified in a configuration file read by the script. The filename is | |
7 # passed as the one and only command line parameter. | |
8 # | |
9 # The script expects the utility cmp to be in the path. | |
10 # | |
11 # The syntax of each line in the configuration file is the following (comments start with #) | |
12 # <left_directory> <right_directory> <excluded_files> <explicit_files> | |
13 # <excluded_files> = [-<filename_without_path>]* | |
14 # <explicit_files> = [+<filename_without_path>]* | |
15 # | |
16 # default behaviour (no excluded or explicit files) is to compare all similar named files | |
17 # inthe two directories | |
18 # if <excluded_files> are specified these files are excluded from the comparison | |
19 # if <explicit_files> are specified ONY THESE FILES are included in the comparison | |
20 # | |
21 # Attention: file and directory names MUST NOT contain spaces or other white space | |
22 | |
23 use strict; | |
24 | |
25 use File::Glob ':glob'; | |
26 use File::Spec; | |
27 | |
28 # define global variables | |
29 my $cnfFile=$ARGV[0]; | |
30 my $logFile="cmp_report.log"; | |
31 my $firstComment = 1; | |
32 | |
33 # find the config file | |
34 @ARGV == 1 || die "Usage:\tcmp_objs.pl config_file\n"; | |
35 | |
36 # check for cmp.exe | |
37 system("cmp -v > cmp_report.log 2>&1") && die "ERROR: cmp must be in the path \n"; | |
38 | |
39 # open the config file | |
40 # | |
41 open (IN,"<$cnfFile")||die "Can't open $cnfFile !\n"; | |
42 | |
43 # parse the config file | |
44 # | |
45 my $line; | |
46 | |
47 while(defined($line = <IN>)) | |
48 { | |
49 # reset variable for next loop | |
50 my $leftDir; | |
51 my $rightDir; | |
52 my @files; | |
53 my @excludedFiles; | |
54 my @explicitFiles; | |
55 | |
56 # ignore comments (starting with #) | |
57 if($line =~ /^\s*\#/) { | |
58 # print out special comments starting with ## | |
59 if ($' =~ /^\s*\#/) { | |
60 if ($firstComment == 1) { | |
61 print "\n"; | |
62 $firstComment = 0; | |
63 } | |
64 print "[COMMENT] $'"; | |
65 } | |
66 else { | |
67 $firstComment = 1; | |
68 } | |
69 next; | |
70 } | |
71 | |
72 # ignore empty lines | |
73 if($line =~ /^\s+$/) { | |
74 next; | |
75 } | |
76 | |
77 # remove trailing \n | |
78 chomp($line); | |
79 | |
80 # put everything SEPARATED BY WHITESPACE in an array | |
81 @files = split(/\s+/,$line); | |
82 | |
83 # find the directories (and remove them from the array) | |
84 $leftDir = $files[0]; shift(@files); | |
85 $rightDir = $files[0]; shift(@files); | |
86 | |
87 # check whether the directories are wildcards | |
88 # if ($leftDir =~ s/(.*?)(\/\*)/$1/ && $rightDir =~ s/(.*?)(\/\*)/$1/ ) { | |
89 if ($leftDir =~ s:(.*?)([/\\]\*):$1: && $rightDir =~ s:(.*?)([/\\]\*):$1: ) { | |
90 my @dirs; | |
91 my @leftDirs; | |
92 my @rightDirs; | |
93 | |
94 # check whether the parent directories exist | |
95 (-e $leftDir) || die "Directory $leftDir does not exist !\n"; | |
96 (-e $rightDir) || die "Directory $rightDir does not exist !\n"; | |
97 | |
98 # find all sub directories left | |
99 @dirs = bsd_glob("$leftDir/*"); | |
100 @dirs = sort @dirs; | |
101 foreach (@dirs) { | |
102 my ($drv,$dir); | |
103 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
104 if (-d "$leftDir/$_") { | |
105 push (@leftDirs, "$_"); | |
106 } | |
107 } | |
108 | |
109 # reset variables | |
110 splice (@dirs); | |
111 | |
112 # find all sub directories right | |
113 @dirs = bsd_glob("$rightDir/*"); | |
114 @dirs = sort @dirs; | |
115 foreach (@dirs) { | |
116 my ($drv,$dir); | |
117 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
118 if (-d "$rightDir/$_") { | |
119 push (@rightDirs, "$_"); | |
120 } | |
121 } | |
122 | |
123 # create a hash that uses the filename as the key and an integer as value | |
124 # to find those directories that are found in both directories or only in one | |
125 # of them | |
126 my %count; | |
127 my $element; | |
128 | |
129 foreach $element (@leftDirs) { $count{$element}++; } | |
130 foreach $element (@rightDirs) { $count{$element}+=2; } | |
131 | |
132 # delete used arrays for later reuse | |
133 splice (@dirs); | |
134 splice (@leftDirs); | |
135 splice (@rightDirs); | |
136 | |
137 foreach $element (keys %count) { | |
138 if ($count{$element} == 1) { | |
139 push @leftDirs, $element; | |
140 } | |
141 elsif ($count{$element} == 2) { | |
142 push @rightDirs, $element; | |
143 } | |
144 elsif ($count{$element} == 3) { | |
145 push @dirs, $element; | |
146 } | |
147 } | |
148 print "[LEFT ] $leftDir/*\n"; | |
149 print "[RIGHT ] $rightDir/*\n"; | |
150 foreach $element (sort(@leftDirs)) { | |
151 print "[WARNING - directory missing right] $element\n"; | |
152 } | |
153 foreach $element (sort(@rightDirs)) { | |
154 print "[WARNING - directory missing left] $element\n"; | |
155 } | |
156 print "\n"; | |
157 foreach $element (sort(@dirs)) { | |
158 # ignore some commonly used version files | |
159 splice (@excludedFiles); | |
160 push (@excludedFiles,"ver.obj"); | |
161 push (@excludedFiles,"$element\_version.obj"); | |
162 cmp_dirs ("$leftDir/$element", "$rightDir/$element", \@excludedFiles, \@explicitFiles); | |
163 } | |
164 | |
165 } | |
166 else { | |
167 # check whether the directories exist | |
168 (-e $leftDir) || die "Directory $leftDir does not exist !\n"; | |
169 (-e $rightDir) || die "Directory $rightDir does not exist !\n"; | |
170 | |
171 if (defined($files[0])) { | |
172 # some files are are specified or excluded explicitely | |
173 foreach (@files) { | |
174 if (/^-/) { | |
175 push(@excludedFiles,$'); | |
176 } | |
177 elsif(/^\+/) { | |
178 (-e "$leftDir/$'") || die "File $rightDir/$' does not exist !\n"; | |
179 (-e "$rightDir/$'") || die "File $rightDir/$' does not exist !\n"; | |
180 push(@explicitFiles,$'); | |
181 } | |
182 } | |
183 } | |
184 cmp_dirs ($leftDir, $rightDir, \@excludedFiles, \@explicitFiles); | |
185 } | |
186 | |
187 } | |
188 | |
189 | |
190 # close the config file | |
191 # | |
192 close IN; | |
193 | |
194 # delete the log file | |
195 # | |
196 #unlink($logFile); | |
197 | |
198 ################### | |
199 # | |
200 sub cmp_dirs { | |
201 | |
202 my $leftDir = $_[0]; | |
203 my $rightDir = $_[1]; | |
204 my @excludedFiles = @{$_[2]}; | |
205 my @explicitFiles = @{$_[3]}; | |
206 | |
207 my @files; | |
208 my @leftFiles; | |
209 my @rightFiles; | |
210 my $element; | |
211 | |
212 if (defined($explicitFiles[0])) { | |
213 @files = @explicitFiles; | |
214 } | |
215 else { | |
216 @leftFiles = bsd_glob("$leftDir/*.obj"); | |
217 @leftFiles = sort @leftFiles; | |
218 foreach (@leftFiles) { | |
219 my ($drv,$dir); | |
220 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
221 } | |
222 | |
223 @rightFiles = bsd_glob("$rightDir/*.obj"); | |
224 @rightFiles = sort @rightFiles; | |
225 foreach (@rightFiles) { | |
226 my ($drv,$dir); | |
227 ($drv,$dir,$_) = File::Spec->splitpath($_); | |
228 } | |
229 | |
230 # create a hash that uses the filename as the key and an integer as value | |
231 # to find those files that are found in both directories or only in one | |
232 # of them | |
233 my %count; | |
234 | |
235 foreach $element (@leftFiles) { $count{$element}++; } | |
236 foreach $element (@rightFiles) { $count{$element}+=2; } | |
237 | |
238 # delete used arrays for later reuse | |
239 splice (@files); | |
240 splice (@leftFiles); | |
241 splice (@rightFiles); | |
242 | |
243 # remove all exclude files | |
244 foreach $element (@excludedFiles) { | |
245 if (exists($count{$element})) { | |
246 # print "Skipping file: $element \n"; | |
247 delete($count{$element}); | |
248 } | |
249 } | |
250 | |
251 foreach $element (keys %count) { | |
252 if ($count{$element} == 1) { | |
253 push @leftFiles, $element; | |
254 } | |
255 elsif ($count{$element} == 2) { | |
256 push @rightFiles, $element; | |
257 } | |
258 elsif ($count{$element} == 3) { | |
259 push @files, $element; | |
260 } | |
261 } | |
262 } | |
263 | |
264 | |
265 print "[LEFT ] $leftDir/\n"; | |
266 print "[RIGHT] $rightDir/\n"; | |
267 | |
268 if(defined($leftFiles[0]) || defined($rightFiles[0]) ) { | |
269 my %tmp; | |
270 | |
271 foreach $element (@leftFiles) { $tmp{$element} = " [WARNING - file missing right] $element\n" }; | |
272 foreach $element (@rightFiles) { $tmp{$element} = " [WARNING - file missing left ] $element\n"; } | |
273 | |
274 foreach $element (sort(keys %tmp)) { | |
275 print $tmp{$element}; | |
276 } | |
277 } | |
278 | |
279 | |
280 foreach (sort(@files)) { | |
281 if (system("cmp -i8 $leftDir/$_ $rightDir/$_ >>$logFile 2>&1")) { | |
282 print " [ERROR - files differ] $_\n"; | |
283 cmp_cmdFiles ($leftDir , $rightDir , $_ ); | |
284 } | |
285 } | |
286 | |
287 print "\n"; | |
288 } | |
289 | |
290 | |
291 | |
292 | |
293 ################### | |
294 ## | |
295 sub cmp_cmdFiles { | |
296 | |
297 my $leftDir = $_[0]; | |
298 my $rigthDir = $_[1]; | |
299 my $cmdFile = $_[2]; | |
300 | |
301 my @leftDef; | |
302 my @rightDef; | |
303 | |
304 $cmdFile =~ s/\.obj/\.cl470cmd/; | |
305 | |
306 my $leftFile = $leftDir . "\\" . $cmdFile; | |
307 my $rightFile = $rigthDir . "\\" . $cmdFile; | |
308 | |
309 print " [Checking cmd files ] $cmdFile\n"; | |
310 | |
311 if (!(-e $leftFile) || !(-e $rightFile)){ | |
312 print " [Cmdfile(s) missing]\n\n"; | |
313 return; | |
314 } | |
315 | |
316 my @AoA = parse_cmdFile($leftFile); | |
317 push(@leftDef, @{$AoA[0]}); | |
318 | |
319 @AoA = parse_cmdFile($rightFile); | |
320 push(@rightDef, @{$AoA[0]}); | |
321 | |
322 my %defines; | |
323 my $element; | |
324 my %defSorted; | |
325 | |
326 foreach $element (@leftDef) { $defines{$element}++; } | |
327 foreach $element (@rightDef) { $defines{$element}+=2; } | |
328 | |
329 foreach $element (keys %defines) { | |
330 if ($defines{$element} == 1) { | |
331 $defSorted{" [LEFT ] $element\n"} = 0; | |
332 } | |
333 elsif ($defines{$element} == 2) { | |
334 $defSorted{" [RIGHT] $element\n"} = 0; | |
335 } | |
336 } | |
337 foreach $element (sort(keys(%defSorted))) { | |
338 print $element; | |
339 } | |
340 | |
341 print "\n"; | |
342 } | |
343 | |
344 ################### | |
345 ## | |
346 sub parse_cmdFile { | |
347 my @defines; | |
348 my %def; | |
349 my $line; | |
350 my $element; | |
351 | |
352 open (CMD,"<$_[0]")||die "Can't open $_[0] !\n"; | |
353 while(defined($line = <CMD>)) | |
354 { | |
355 chomp($line); | |
356 my @tmp = split(/\s+/,$line); | |
357 foreach $element (@tmp) { | |
358 if ($element =~ /^-d/i ) { | |
359 if ($' =~ /^$/) { | |
360 #ignore empty defines | |
361 } | |
362 else { | |
363 $def{"-D$'"}++; | |
364 } | |
365 } | |
366 elsif ($element =~ /^-u/i ) { | |
367 if ($' =~ /^$/) { | |
368 #ignore empty defines | |
369 } | |
370 else { | |
371 $def{"-U$'"}++; | |
372 } | |
373 } | |
374 } | |
375 } | |
376 | |
377 # check for double defines | |
378 # foreach $element (keys %def) { | |
379 # print "$element\n"; | |
380 # } | |
381 | |
382 @defines = sort(keys %def); | |
383 | |
384 close CMD; | |
385 return (\@defines); | |
386 } |