blib/lib/ClearCase/CRDB.pm untested

filter: Statementstmt 0.0% 0 / 420 Branchbran 0.0% 0 / 202 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 0.0% 0 / 50 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 832 CC 101 SCAR 61.6
CC
101
Cov
0%
catcr_handle
1560.0
init
420.0
absify
90.0
CRAP 473.4
1 package ClearCase::CRDB;
2  
3 $VERSION = '0.15';
4  
5 # This schema version is stored in the flat-file form via ->store
6 # and compared during ->load. A warning is issued if they don't match.
7 my $mod_schema = 1;
8  
9 # I don't know exactly what Perl version is the minimum required but I
10 # have a report that it works with 5.005. I use 5.8 myself.
11 require 5.005;
12  
13 use File::Basename;
14 use File::Spec 0.82;
15 use Getopt::Long;
16 use Cwd;
17  
18 # For convenience, load up known subclasses. We 'eval' these to ignore
19 # the error msg if the underlying serialization modules
20 # Data::Dumper/Storable/etc aren't installed.
21 eval { require ClearCase::CRDB::Dumper; };
22 eval { require ClearCase::CRDB::Storable; };
23  
24 use strict;
25  
26 use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0;
27  
28 my $prog = basename($0, qw(.pl));
29  
30 # Defining multiple constants at once isn't supported before 5.8.
31 use constant SKIP => 0;
32 use constant NOTES => 1;
33 use constant OBJECTS => 2;
34 use constant VARS => 3;
35 use constant SCRIPT => 4;
36  
37 sub new {
38 my $proto = shift;
39 my($class, $self);
40 if ($class = ref($proto)) {
41 # Make a (deep) clone of the original
42 require Clone;
43 Clone->VERSION(0.11); # there's a known bug in 0.10
44 return Clone::clone($proto);
45 }
46 $class = $proto;
47 $self = {};
48 bless $self, $class;
49 # HACK - this doesn't actually use @_, it's just an indication
50 # that we want to parse @ARGV.
51 $self->init if @_;
52 return $self;
53 }
54  
55 sub usage {
56 my $self = shift;
57 return "TBD";
58 }
59  
60 sub init {
61 my $self = shift;
62 my %opt;
63 my $ext = 'crdb';
64 my $delim = MSWIN ? ',;' : ',;:';
65  
66 # Any options matched here are removed from the global @ARGV.
67 local $Getopt::Long::passthrough = 1;
68 GetOptions(\%opt, qw(cr|cache=s do=s@ db=s@ lsprivate recurse save));
69  
70 if ($opt{do} || $opt{lsprivate} || $ENV{CRDB_DO}) {
71 my @dos;
72 if ($opt{do}) {
73 @dos = @{$opt{do}};
74 if (@dos == 1 && $dos[0] eq '-') {
75 chomp(@dos = <STDIN>);
76 }
77 } elsif ($opt{lsprivate}) {
78 @dos = qx(cleartool lsprivate -do);
79 chomp @dos;
80 } elsif ($ENV{CRDB_DO}) {
81 @dos = map {split /[$delim]/} $ENV{CRDB_DO};
82 }
83 $self->crdo(@dos);
84 if ($opt{recurse}) {
85 $self->catcr_recurse;
86 } else {
87 $self->catcr_flat;
88 }
89  
90 my $savefile;
91 if ($opt{db}) {
92 die "$prog: Error: can write to only one database file"
93 if @{$opt{db}} != 1;
94 $savefile = $opt{db}->[0];
95 } elsif ($opt{save}) {
96 $savefile = $dos[0];
97 }
98 if ($savefile) {
99 $savefile .= ".$ext" unless $savefile =~ m%$ext$%;
100 $self->store($savefile);
101 }
102 } elsif ($opt{cr}) {
103 open(CR, $opt{cr}) || die "$prog: Error: $opt{cr}: $!";
104 $self->catcr_handle(\*CR);
105 close(CR);
106 if ($opt{save}) {
107 (my $savefile = $opt{cr}) =~ s%\.\w+?$%.$ext%;
108 $self->store($savefile);
109 }
110 } elsif ($opt{db} || $ENV{CRDB_DB}) {
111 my $dblist = $opt{db} ? join(',', @{$opt{db}}) : $ENV{CRDB_DB};
112 my @dbs = map {split /[$delim]/} $dblist;
113 for (@dbs) {
114 $_ .= ".$ext" unless m%$ext$%;
115 }
116 $self->load(@dbs);
117 } else {
118 my $path = $opt{dir} || $ENV{CRDB_PATH} || '.';
119 my @found;
120 for my $dir (split /[$delim]/, $path) {
121 push(@found, glob("$dir/.*.$ext"), glob("$dir/*.$ext"));
122 }
123 die "$prog: Error: no CRDB databases found" unless @found;
124 my $s = @found > 1 ? 's' : '';
125 $self->load(@found);
126 }
127 }
128  
129 sub crdo {
130 my $self = shift;
131 if (@_) {
132 push(@{$self->{CRDB_CRDO}}, map {File::Spec->rel2abs($_)} @_);
133 if (MSWIN) {
134 s%^[a-z]:%%i for (@{$self->{CRDB_CRDO}});
135 }
136 }
137 return @{$self->{CRDB_CRDO}};
138 }
139  
140 sub check {
141 my $self = shift;
142 $self->crdo(@_) if @_;
143 my @objects = $self->crdo;
144 die "Error: no derived objects specified to check" unless @objects;
145 return system(qw(cleartool catcr -check -union), @objects);
146 }
147  
148 # Useful in development; allows a pre-generated catcr output to be
149 # stashed in a file and passed in here repeatedly.
150 sub catcr_handle {
151 my $self = shift;
152 my $handle = shift;
153 my($tgt, @notes);
154 my $state = SKIP;
155 for (<$handle>) {
156 chomp;
157  
158 # State machine - set $state according to the CR section.
159 if (m%^-{28}%) {
160 next;
161 } elsif (m%^Derived object:\s+(.*)@@%) {
162 $tgt = $1;
163 $tgt =~ s%^[a-z]:%%i if MSWIN;
164 if (exists $self->{CRDB_FILES}->{$tgt} &&
165 (exists $self->{CRDB_FILES}->{$tgt}->{CR_DO} ||
166 exists $self->{CRDB_FILES}->{$tgt}->{CR_SIBLING_OF})) {
167 $state = SKIP;
168 } elsif (!MSWIN && basename($tgt) eq 'core') {
169 # This is a hack, but some builds produce lots of core
170 # files and we generally want to ignore the spurious
171 # DOs thus produced.
172 $state = SKIP;
173 } else {
174 $state = NOTES;
175 }
176 next;
177 } elsif (m%^Target\s+(\S*)\s+built\s%) {
178 $state = SKIP if $1 eq 'ClearAudit_Shell';
179 next;
180 } elsif ($state == SKIP) {
181 next;
182 } elsif (($state == NOTES || $state == OBJECTS) && m%MVFS objects:%) {
183 @{$self->{CRDB_FILES}->{$tgt}->{CR_DO}->{CR_NOTES}} = @notes;
184 @notes = ();
185 $state = OBJECTS;
186 next;
187 } elsif ($state == OBJECTS && m%^Variables and Options:%) {
188 $state = VARS;
189 next;
190 } elsif (($state == VARS || $state == OBJECTS) && m%^Build Script:%) {
191 $state = SCRIPT;
192 next;
193 }
194  
195 # Accumulate data from section according to $state.
196 if ($state == NOTES) {
197 push(@notes, $_);
198 if (my($iwd) = m%^Initial working directory was (\S+)%) {
199 $self->{CRDB_FILES}->{$tgt}->{CR_DO}->{CR_IWD} = $iwd;
200 }
201 } elsif ($state == OBJECTS) {
202 my($path, $vers, $date, $cmnts);
203 my $inmakefile = 1;
204  
205 if (m%^(?:directory version|view directory)\s%) {
206 # We don't care about directories.
207 next;
208 } elsif (($path, $vers, $date, $cmnts) =
209 m%^(?:version|derived object version)\s+(\S+?.+?)@@(\S+)\s+<(\S+)>\s*(.*)$%) {
210 # We do not currently distinguish between regular elements
211 # and versioned derived objects.
212 $path =~ s%^[a-z]:%%i if MSWIN;
213 $self->{CRDB_FILES}->{$path}->{CR_TYPE} = 'ELEM';
214 $self->{CRDB_FILES}->{$path}->{CR_ELEM}->{CR_VERS} = $vers;
215 $self->{CRDB_FILES}->{$path}->{CR_DATE} = $date;
216 } elsif ((($path, $date, $cmnts) =
217 m%^derived object\s+(\S.+?)@@(\S+)\s*?(.*)$%) ||
218 (($path, $date, $cmnts) =
219 m%^derived object\s+(\S.+?)\s+<(\S+)>\s*?(.*)$%)) {
220 $path =~ s%^[a-z]:%%i if MSWIN;
221 $self->{CRDB_FILES}->{$path}->{CR_TYPE} = 'DO';
222 if ($date =~ s%\.(\d+)$%%) {
223 $self->{CRDB_FILES}->{$path}->{CR_DBID} = $1;
224 }
225 $self->{CRDB_FILES}->{$path}->{CR_DATE} = $date;
226 # In the case of siblings, each secondary sibling gets
227 # a pointer back to the primary, while the primary
228 # gets a list of its siblings.
229 if ($cmnts =~ m%new derived object%) {
230 if ($path ne $tgt) {
231 $self->{CRDB_FILES}->{$path}->{CR_SIBLING_OF} = $tgt;
232 $self->{CRDB_FILES}->{$tgt}->{CR_SIBLINGS}->{$path} = 1;
233 }
234 }
235 $inmakefile = ($cmnts =~ m%in makefile%) ? 1 : 0;
236 } elsif (($path, $vers, $cmnts) =
237 m%^derived object\s+(\S+?.+?)@@(\S+)\s*(.*)$%) {
238 warn "Warning: unhandled DO type: '$_'";
239 } elsif (($path, $date, $cmnts) =
240 m%^view file\s+(\S.+\S)\s+<(\S+)>\s*(.*)$%) {
241 $path =~ s%^[a-z]:%%i if MSWIN;
242 $self->{CRDB_FILES}->{$path}->{CR_TYPE} = 'VP';
243 $self->{CRDB_FILES}->{$path}->{CR_DATE} = $date;
244 } elsif (m%^view private object%) {
245 # These seem to come up only for symlinks, which we can
246 # ignore since the target be recorded elsewhere.
247 next;
248 } elsif (m%^branch%) {
249 # This appears to be reported only for non-visible DO's
250 next;
251 } elsif (m%^file element%) {
252 # This appears to be reported only for non-visible DO's
253 next;
254 } elsif (m%^directory element%) {
255 # This appears to be reported only for non-visible DO's
256 next;
257 } elsif (m%^symbolic link%) {
258 next;
259 } else {
260 warn "Warning: unrecognized CR line: '$_'";
261 next;
262 }
263 if (MSWIN) {
264 # Must compare paths case-insensitively on Windows.
265 next if lc($path) eq lc($tgt);
266 } else {
267 next if $path eq $tgt;
268 }
269 # The value of the NEEDS->{path} key indicated whether
270 # the prereq is explicit in the makefile.
271 if (!exists $self->{CRDB_FILES}->{$tgt}->{CR_SIBLINGS}->{$path}) {
272 $self->{CRDB_FILES}->{$tgt}->{CR_DO}->{CR_NEEDS}->{$path} = $inmakefile;
273 }
274 # If this is a younger sibling, no need to record what it
275 # contributes to - the older sibling has that data.
276 if (!exists $self->{CRDB_FILES}->{$path}->{CR_SIBLING_OF}) {
277 $self->{CRDB_FILES}->{$path}->{CR_MAKES}->{$tgt} = 1;
278 }
279 } elsif ($state == VARS && (my($var, $val) = m%^(.+?)=(.*)%)) {
280 $self->{CRDB_FILES}->{$tgt}->{CR_DO}->{CR_VARS}->{$var} = $val;
281 } elsif ($state == SCRIPT) {
282 push(@{$self->{CRDB_FILES}->{$tgt}->{CR_DO}->{CR_SCRIPT}},
283 substr($_, 1));
284 } else {
285 warn "Warning: unrecognized CR line: '$_'";
286 }
287 }
288 close ($handle);
289 return $self;
290 }
291  
292 sub catcr_cmd {
293 my $self = shift;
294 my $cmd = shift;
295  
296 # The preferred directory for temp files.
297 my $tmpd = MSWIN ?
298 ($ENV{TEMP} || $ENV{TMP} || ( -d "$ENV{SYSTEMDRIVE}/temp" ?
299 "$ENV{SYSTEMDRIVE}/temp" : $ENV{SYSTEMDRIVE}))
300 :
301 ($ENV{TMPDIR} || '/tmp');
302 $tmpd =~ s%\\%/%g;
303  
304 # The command line here can get awfully long, enough to blow the
305 # limit on some systems. So we hack around it by reading stdin.
306 my $dolist = "$tmpd/dolist.$$.tmp";
307 if (open(DOLIST, ">$dolist")) {
308 print DOLIST $cmd;
309 print DOLIST " '$_'" for @{[$self->crdo]};
310 print DOLIST "\n";
311 close(DOLIST);
312 } else {
313 warn "Warning: $dolist: $!";
314 return;
315 }
316 my $handle;
317 open($handle, "cleartool <$dolist |") || die "Error: $dolist: $!";
318 $self->catcr_handle($handle);
319 unlink $dolist;
320 return $self;
321 }
322  
323 sub catcr_flat {
324 my $self = shift;
325 $self->crdo(@_) if @_;
326 my $cmd = "catcr -l";
327 return $self->catcr_cmd($cmd);
328 }
329  
330 sub catcr_recurse {
331 my $self = shift;
332 $self->crdo(@_) if @_;
333 my $cmd = "catcr -r -l";
334 return $self->catcr_cmd($cmd);
335 }
336  
337 # Backward compatibility.
338 sub catcr {
339 my $self = shift;
340 return $self->catcr_recurse(@_);
341 }
342  
343 # Internal func to recursively merge two hash refs
344 sub hash_merge {
345 my($to, $from, @source) = @_;
346 for (keys %{$from}) {
347 if (! $to->{$_}) {
348 $to->{$_} = $from->{$_};
349 next;
350 }
351 my($ttype, $ftype) = (ref $to->{$_}, ref $from->{$_});
352 if ($ttype ne $ftype) {
353 warn "Warning: key type conflict: @source: $_ $ttype/$ftype"
354 } elsif (! $ttype) {
355 warn "Warning: @source: $_: can't merge non-references";
356 } elsif ($ttype eq 'ARRAY') {
357 push(@{$to->{$_}}, @{$from->{$_}});
358 } elsif ($ttype eq 'HASH') {
359 hash_merge($to->{$_}, $from->{$_}, @source);
360 } else {
361 warn "Warning: @source: $_: can't merge type: $ttype";
362 }
363 }
364 }
365  
366 sub store {
367 my $self = shift;
368 my $file = shift || '-';
369 $self->{CRDB_SCHEMA} = $mod_schema;
370 my $d = Data::Dumper->new([$self], ['_DO']);
371 $d->Indent(1);
372 open(DUMP, ">$file") || die "$file: $!\n";
373 printf DUMP "# Produced by Perl module %s using %s format.\n",
374 ref $self, ref $d;
375 print DUMP "# It is valid Perl syntax and may be read into memory via\n";
376 print DUMP "# 'do <file>' or by eval-ing its contents.\n\n";
377 print DUMP $d->Dumpxs;
378 close(DUMP);
379 return $self;
380 }
381  
382 sub load {
383 my $self = shift;
384 for my $db (@_) {
385 my $hashref;
386 die "Error: $db: incorrect format" if -e $db && -B $db;
387 $hashref = do $db; # eval's $db and returns the obj ref
388 if (!defined($hashref)) {
389 warn "Error: $db: " . (-r $db ? $@ : $!);
390 return undef;
391 }
392 my $file_schema = $hashref->{CRDB_SCHEMA};
393 die "Error: $db: stored schema ($file_schema) != current ($mod_schema)"
394 unless $file_schema == $mod_schema;
395 $self->hash_merge($hashref, @{$hashref->{CRDB_CRDO}});
396 }
397 return $self;
398 }
399  
400 sub iwd {
401 my $self = shift;
402 if (@_ == 1) {
403 my $do = $self->primary_sibling(shift);
404 return $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_IWD};
405 } elsif (@_ == 0) {
406 my %iwds;
407 for (keys %{$self->{CRDB_FILES}}) {
408 next unless exists $self->{CRDB_FILES}->{$_}->{CR_DO};
409 my $iwd = $self->iwd($_); # recursion of a sort
410 $iwds{$iwd}++ if $iwd;
411 }
412 return sort keys %iwds;
413 }
414 return undef;
415 }
416  
417 sub files {
418 my $self = shift;
419 return keys %{$self->{CRDB_FILES}};
420 }
421  
422 sub type_is {
423 my $self = shift;
424 my $type = shift;
425 return grep {
426 exists $self->{CRDB_FILES}->{$_} &&
427 $self->{CRDB_FILES}->{$_}->{CR_TYPE} eq $type
428 } keys %{$self->{CRDB_FILES}};
429 }
430  
431 sub versioned_elements {
432 my $self = shift;
433 return $self->type_is('ELEM');
434 }
435  
436 sub derived_objects {
437 my $self = shift;
438 return $self->type_is('DO');
439 }
440  
441 sub view_privates {
442 my $self = shift;
443 return $self->type_is('VP');
444 }
445  
446 sub targets {
447 my $self = shift;
448 my $siblings_too = shift;
449 my $key = $siblings_too ? 'CR_DBID' : 'CR_DO';
450 return grep {exists $self->{CRDB_FILES}->{$_}->{$key}}
451 keys %{$self->{CRDB_FILES}};
452 }
453  
454 sub terminals {
455 my $self = shift;
456 return grep {
457 exists $self->{CRDB_FILES}->{$_}->{CR_DO} &&
458 !exists $self->{CRDB_FILES}->{$_}->{CR_MAKES}
459 } keys %{$self->{CRDB_FILES}};
460 }
461  
462 sub vars {
463 my $self = shift;
464 my $do = shift;
465 if (wantarray) {
466 return keys %{$self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_VARS}};
467 } else {
468 return $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_VARS};
469 }
470 }
471  
472 sub val {
473 my $self = shift;
474 my($do, $var) = @_;
475 return undef unless exists $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_VARS};
476 return $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_VARS}->{$var};
477 }
478  
479 sub notes {
480 my $self = shift;
481 my $do = shift;
482 return undef unless exists $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_NOTES};
483 return @{$self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_NOTES}};
484 }
485  
486 sub script {
487 my $self = shift;
488 my $do = $self->primary_sibling(shift);
489 return undef
490 if !exists $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_SCRIPT};
491 my @script = @{$self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_SCRIPT}};
492 return @script;
493 }
494  
495 sub matches {
496 my $self = shift;
497 my @matched;
498 for my $re (@_) {
499 push(@matched, grep m%$re%, keys %{$self->{CRDB_FILES}});
500 }
501 return @matched;
502 }
503 *matches_do = \&matches;
504  
505 sub needs {
506 my $self = shift;
507 my @results;
508 for my $do (@_) {
509 next unless exists $self->{CRDB_FILES}->{$do};
510 push(@results, keys %{$self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_NEEDS}});
511 }
512 return @results;
513 }
514 *needs_do = \&needs;
515  
516 sub makes {
517 my $self = shift;
518 my @results;
519 for my $do (@_) {
520 push(@results, keys %{$self->{CRDB_FILES}->{$do}->{CR_MAKES}})
521 if exists $self->{CRDB_FILES}->{$do};
522 }
523 return @results;
524 }
525 *makes_do = \&makes;
526  
527 sub unmentioned {
528 my $self = shift;
529 my @results;
530 for my $do (@_) {
531 for my $prq (keys %{$self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_NEEDS}}) {
532 next if $self->{CRDB_FILES}->{$do}->{CR_DO}->{CR_NEEDS}->{$prq};
533 push(@results, $prq)
534 }
535 }
536 return @results;
537 }
538  
539 # If the supplied path is a sibling DO, replace it with the "primary"
540 # DO record which contains the useful data such as iwd, script, etc.
541 sub primary_sibling {
542 my $self = shift;
543 my $do = shift;
544 $do = $self->{CRDB_FILES}->{$do}->{CR_SIBLING_OF}
545 if exists $self->{CRDB_FILES}->{$do}->{CR_SIBLING_OF};
546 return $do;
547 }
548  
549 sub sibling_of {
550 my $self = shift;
551 my $do = shift;
552 return $self->{CRDB_FILES}->{$do}->{CR_SIBLING_OF};
553 }
554  
555 sub siblings {
556 my $self = shift;
557 my $do = shift;
558 if (!exists $self->{CRDB_FILES}->{$do}->{CR_SIBLINGS} &&
559 exists $self->{CRDB_FILES}->{$do}->{CR_SIBLING_OF}) {
560 $do = $self->{CRDB_FILES}->{$do}->{CR_SIBLING_OF};
561 }
562 if (exists $self->{CRDB_FILES}->{$do}->{CR_SIBLINGS}) {
563 return keys %{$self->{CRDB_FILES}->{$do}->{CR_SIBLINGS}};
564 } else {
565 return ();
566 }
567 }
568  
569 sub recognizes {
570 my $self = shift;
571 my $do = shift;
572 return exists $self->{CRDB_FILES}->{$do};
573 }
574  
575 sub iwd_targets {
576 my $self = shift;
577 my $iwd = shift;
578 my @tgts = grep {
579 exists($self->{$_}->{CR_IWD}) && $self->{$_}->{CR_IWD} eq $iwd
580 } keys %$self;
581 return @tgts;
582 }
583  
584 sub absify {
585 my $self = shift;
586 my($iwd, $orig) = @_;
587  
588 my $line = '';
589 my @chunks = split ' ', $orig;
590 for my $i (0 .. $#chunks) {
591 my $chunk = $chunks[$i];
592 if ($chunk =~ m%^[\\/]%) {
593 $line .= $chunk;
594 next;
595 } else {
596 if ($chunk =~ m%^-%) {
597 if ($chunk =~ s%^(-[ILR])%%) {
598 $line .= $1;
599 if ($chunk =~ m%^[\\/]%) {
600 $line .= $chunk;
601 next;
602 }
603 } else {
604 $line .= $chunk;
605 next;
606 }
607 }
608 }
609 my $full = File::Spec->canonpath("$iwd/$chunk");
610 if ($self->recognizes($full)) {
611 $line .= $full;
612 } elsif (-d $full) {
613 $line .= $full;
614 } elsif (-e $full) {
615 $line .= File::Spec->join(Cwd::abs_path(dirname $full),
616 basename $full);;
617 #warn "Warning: not recognized: $full\n";
618 } else {
619 $line .= $chunk;
620 #warn "Unrecognized: '$chunk'\n";
621 }
622 } continue {
623 $line .= ' ' if $i < $#chunks;
624 }
625 return $line;
626 }
627  
628 1;
629  
630 __END__