Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / cmpviews
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         cmpview
5 # Description:  This script will produce listing of the delta between two
6 #               different views
7 # Author:       Andrew@DeFaria.com
8 # Created:      Wed Apr  2 15:21:21 PST 2003
9 # Modified:
10 # Language:     Perl
11 #
12 # (c) Copyright 2003, Salira Optical Network Systems, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16
17 my $me = substr ($0, rindex ($0, "/") + 1);
18 my (@view1out, @view2out);
19 my ($view1, $view2);
20 my ($view1_tag, $view2_tag);
21 my $cwd = $ENV {PWD};
22 my $dynamic;
23
24 sub usage {
25   print "Usage: $me: [-g] <view 1> <view 2>\n";
26   print "\nWhere:\n";
27   print "\n\t-g\t\tGenerate lists - otherwise <view 1>.list and\n";
28   print "\t\t\t<view 2>.list files in the current directory are\n";
29   print "\t\t\tsearched. If the file is not found then a list file\n";
30   print "\t\t\tis generated.\n";
31   print "\t<view 1>\tClearcase view to compare from\n";
32   print "\t<view 2>\tClearcase view to compare to\n";
33   exit 1;
34 } # usage
35
36 sub ProduceListFile {
37   # This subroutine will simply produce a file listing all Clearcase elements
38   # elements into a file.
39   my $view     = shift;
40   my $view_tag = shift;
41
42   chdir $view or die "Unable to change directory to $view\n";
43
44   # Produce listing into array
45   print "Producing a list of elements in view $view_tag...\n";
46   `cleartool ls -recurse -short -visible -vob_only salira > $cwd/$view_tag.list`;
47 } # ProduceListFile
48
49 sub PrintArray {
50   # This subroutine merely prints the array passed to it.
51   foreach (@_) {
52     print "$_\n";
53   } # foreach
54 } # PrintArray
55
56 sub CheckView {
57   # This subroutine checks to see if the view passed is a Clearcase view.
58   my $view = shift;
59
60   if (-d $view) {
61     return 0;
62   } else {
63     return 1;
64   } # if
65 } # CheckView
66
67 sub ProduceLists {
68   my $view1   = shift;
69   my $view2   = shift;
70
71   # This subrourtine produces a list of all elements in the view. These
72   # lists are placed into arrays and saved as files for future runs.
73   if ($dynamic eq 1) {
74     ProduceListFile $view1, $view1_tag;
75     ProduceListFile $view2, $view2_tag;
76   } else {
77     if (! -f "$cwd/$view1_tag.list") {
78       ProduceListFile $view1, $view1_tag;
79     } # if
80     if (! -f "$cwd/$view2_tag.list") {
81       ProduceListFile $view2, $view2_tag;
82     } # if
83   } # if
84
85   # Load up arrays from listing files
86   open (VIEW1OUT, "<$cwd/$view1_tag.list") || die "Unable to open $cwd/$view1_tag.list\n";
87   open (VIEW2OUT, "<$cwd/$view2_tag.list") || die "Unable to open $cwd/$view2_tag.list\n";
88   @view1out = <VIEW1OUT>;
89   @view2out = <VIEW2OUT>;
90
91   # Now ensure their in sorted order
92   @view1out = sort (@view1out);
93   @view2out = sort (@view2out);
94 } # ProduceLists
95
96 sub ProcessLists {
97   # This subroutine will process the two list arrays producing the report.
98   # Now enter a loop comparing items
99   my $view1line = shift (@view1out);
100   my $view2line = shift (@view2out);
101
102   my (@diffs, @view1only, @view2only);
103   my $nbrelements;
104
105   if (!$view1line && !$view2line) {
106     print "$me: Warning: No elements found for either view $view1_tag nor $view2_tag\n";
107     print "Nothing to compare!\n";
108     exit;
109   } # if
110
111   if (!$view1line) {
112     print "$me: Warning: No elements found for view $view1_tag\n";
113     $view1line = " ";
114   } # if
115
116   if (!$view2line) {
117     print "$me: Warning: No elements found for view $view2_tag\n";
118     $view2line = " ";
119   } # if
120
121   while ($view1line && $view2line) {
122     # Extract versions and elements
123     my $version_start = rindex ($view1line, "@") + 1;
124     my $version1      = substr ($view1line, $version_start);
125     my $element1      = substr ($view1line, 0, $version_start - 2);
126     my $version_start = rindex ($view2line, "@") + 1;
127     my $version2      = substr ($view2line, $version_start);
128     my $element2      = substr ($view2line, 0, $version_start - 2);
129
130     # Change "\"'s to "/"'s
131     $element1 =~ s/\\/\//g;
132     $version1 =~ s/\\/\//g;
133     $element2 =~ s/\\/\//g;
134     $version2 =~ s/\\/\//g;
135
136     # Remove leading vob pathname
137     #$element1 =~ s/$vob\///;
138     #$element2 =~ s/$vob\///;
139
140     # Remove trailing carriage return and linefeed
141     chop ($version1); chop ($version1);
142     chop ($version2); chop ($version2);
143
144     # Compare element names
145     if ($element1 eq $element2) {
146       # Element is the same
147       if ($version1 ne $version2) {
148         # Element has different version
149         push (@diffs, "$element1 $version1 -> $version2");
150       } # if
151
152       # Get next element/version
153       $view1line = shift (@view1out);
154       $view2line = shift (@view2out);
155     } else {
156       # Elements names are different
157       if (!$element1) {
158         # Element 1 is blank indicating there are no items in view1out array
159         # Push all of element 2
160         push (@view2only, $element2 . " " . $version2);
161         $view2line = shift (@view2out);
162       } elsif (!$element2) {
163         # Element 2 is blank indicating there are no items in view1out array
164         # Push all of element 1
165         push (@view1only, $element1 . " " . $version1);
166         $view1line = shift (@view1out);
167       } elsif ($element1 lt $element2) {
168         # Element 1 is less so get next element 1
169         push (@view1only, $element1 . " " . $version1);
170         $view1line = shift (@view1out);
171       } else {
172         # Element 2 is less so get next element 2
173         push (@view2only, $element2 . " " . $version2);
174         $view2line = shift (@view2out);
175       } # if
176     } # if
177   } # while
178
179   # Output comparison listing
180   print "Comparison of $view1_tag -> $view2_tag\n";
181
182   if ($#diffs ne -1) {
183     $nbrelements = $#diffs + 1;
184     if ($nbrelements eq 1) {
185       print "\nThe following $nbrelements element appears in only one view:\n\n";
186     } else {
187       print "\nThe following $nbrelements elements appear in both views:\n\n";
188     } # if
189     PrintArray (@diffs);
190   } # if
191
192   if ($#view1only ne -1) {
193     $nbrelements = $#view1only + 1;
194     if ($nbrelements eq 1) {
195       print "\nThe following element only appears in the view $view1_tag:\n\n";
196     } else {
197       print "\nThe following $nbrelements elements only appear in the view $view1_tag:\n\n";
198     } # if
199     PrintArray (@view1only);
200   } # if
201
202   if ($#view2only ne -1) {
203     $nbrelements = $#view2only + 1;
204     if ($nbrelements eq 1) {
205       print "\nThe following element only appear in the view $view2_tag:\n\n";
206     } else {
207       print "\nThe following $nbrelements elements only appear in the view $view2_tag:\n\n";
208     } # if
209     PrintArray (@view2only);
210   } # if
211 } # ProcessLists
212
213 # Get options...
214 if ($ARGV[0] eq "-g") {
215   shift (@ARGV);
216   $dynamic = 1;
217 } else {
218   $dynamic = 0;
219 } # if
220
221 usage if $ARGV [0] eq "" || $ARGV [1] eq "";;
222
223 $view1=$ARGV[0];
224 $view2=$ARGV[1];
225
226 $view1_tag = substr ($view1, rindex ($view1, "/") + 1);
227 $view2_tag = substr ($view2, rindex ($view2, "/") + 1);
228
229 if (CheckView ($view1)) {
230   print "$me: Error: $view1 is not a view\n";
231   exit 1;
232 } # if
233
234 if (CheckView ($view2)) {
235   print "$me: Error: $view2 is not a view\n";
236   exit 1;
237 } # if
238
239 ProduceLists $view1, $view2;
240 ProcessLists;