Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
392 dpurdie 1
########################################################################
2
# Copyright (C) 1998-2012 Vix Technology, All rights reserved
3
#
4
# Module name   : cc2svn_importpackage.pl
5
# Module type   : Makefile system
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Get package information for a package name specified on the
10
#                 command line.
11
#
12
#                 Determine the package id
13
#                 Locate all packages that have the same package name
14
#                 Determine essential packages
15
#                 Prune uneeded packages
16
#
17
#                 Pump it into SVN
18
#
19
#                 Project Based Pumping, creating branches as needed
20
#
21
#......................................................................#
22
 
23
require 5.006_001;
24
use strict;
25
use warnings;
26
use JatsError;
27
use JatsRmApi;
28
use FileUtils;
29
use JatsSystem;
30
use HTTP::Date;
31
use JatsProperties;
32
use JatsEnv;
33
use ConfigurationFile;
34
use JatsSvn qw(:All);
1197 dpurdie 35
use JatsLocateFiles;
392 dpurdie 36
 
37
 
38
#use Data::Dumper;
39
use Fcntl ':flock'; # import LOCK_* constants
40
use Cwd;
41
use DBI;
42
use Getopt::Long;
43
use Pod::Usage;                             # required for help support
44
 
45
#
46
#   Options
47
#
48
my $opt_help = 0;
49
my $opt_manual = 0;
50
my $opt_verbose = 0;
51
my $opt_repo_base = 'AUPERASVN01/';
52
my $opt_repo = '';
53
my $opt_flat;
54
my $opt_test;
55
my $opt_reuse;
56
my $opt_age;
57
my $opt_dump = 0;
58
my $opt_images = 0;
59
my $opt_retaincount = 2;
60
my $opt_pruneModeString;
61
my $opt_listTags;
62
my $opt_name;
63
my $opt_log = 0;
64
my @opt_tip;
65
my $opt_postimage = 1;
66
my $opt_workDir = '/work';
67
my $opt_vobMap;
1197 dpurdie 68
my $opt_preserveProjectBase;
69
my $opt_ignoreProjectBaseErrors;
1270 dpurdie 70
my $opt_delete;
1272 dpurdie 71
my $opt_recentAge = 14;             # Days
392 dpurdie 72
 
73
################################################################################
74
#   List of Projects Suffixes and Branch Names to be used within SVN
75
#
76
#       Name        - Name of branch for the project
77
#       Trunk       - Can be a trunk project
78
#                     First one seen will be placed on the trunk
79
#                     Others will create project branches
80
#
81
my $ProjectTrunk;
82
my %ProjectsBaseCreated;
83
my %Projects = (
84
    '.sea'      => { Name => 'Seattle' },
85
    '.coct'     => { Name => 'CapeTown' },
86
    '.sls'      => { Name => 'Stockholm' },
87
    '.syd'      => { Name => 'Sydney' },
88
    '.vtk'      => { Name => 'Vasttrafik' },
89
    '.bei'      => { Name => 'Beijing' },
90
    '.bkk'      => { Name => 'Bangkok' },
91
    '.ndl'      => { Name => 'NewDelhi' },
92
    '.nzs'      => { Name => 'NewZealandStageCoach' },
93
    '.wdc'      => { Name => 'Washington' },
94
    '.oso'      => { Name => 'Oslo' },
95
    '.lvs'      => { Name => 'LasVegas' },
96
    '.mlc'      => { Name => 'BeijingMlc' },
97
    '.sfo'      => { Name => 'SanFrancisco' },
98
    '.sg'       => { Name => 'Singapore' },
99
    '.gmp'      => { Name => 'GmpteProject' },
100
    '.ssw'      => { Name => 'UkStageCoach' },
101
    '.uk'       => { Name => 'UkProject' },
102
    '.pmb'      => { Name => 'Pietermaritzburg' },
103
    '.vps'      => { Name => 'VixPayments' },
104
    '.ncc'      => { Name => 'NSWClubCard' },
105
    '.rm'       => { Name => 'Rome' },
106
    'unknown'   => { Name => 'UnknownProject' },
107
 
108
    '.ebr'      => { Name => 'eBrio' , Trunk => 1 },
109
    '.mas'      => { Name => 'Mass'  , Trunk => 1 },
110
    '.cr'       => { Name => 'Core'  , Trunk => 1 },
111
    '.cots'     => { Name => 'Cots'  , Trunk => 1 },
112
    '.tool'     => { Name => 'Tools' , Trunk => 1 },
113
);
114
 
115
my %suffixFixup = (
116
    '.sf'           => '.sfo',
117
    '.vt'           => '.vtk',
118
    '.lv'           => '.lvs',
119
    '.was'          => '.wdc',
120
    '.uk.1'         => '.uk',
121
    '.ssts.demo'    => '.ssts',
122
    '.u244.syd'     => '.syd',
123
    '.pxxx.sea'     => '.sea',
124
    '.pxxx.syd'     => '.syd',
125
    '.pxxx.sydddd'  => '.syd',
126
    '.oslo'         => '.oso',
1272 dpurdie 127
    '.osl'          => '.oso',
392 dpurdie 128
);
129
 
130
my %specialPackages = (
404 dpurdie 131
    'core_devl' =>  ',all,protected,',
1197 dpurdie 132
#    'core_devl' =>  ',all,',
392 dpurdie 133
    'daf_utils_mos' => ',flat,',
134
    'mos_packager'  => ',all,',
395 dpurdie 135
 
136
    # Need to be handled in a special manner
137
    # Not done by this utility
138
    #
139
    'linux_drivers_eb5600'  => ',protected,',
140
    'linux_drivers_viper'   => ',protected,',
141
    'linux_drivers_cobra'   => ',protected,',
142
    'linux_drivers_bcp4600' => ',protected,',
143
    'linux_drivers_etx86'   => ',protected,',
144
    'linux_drivers_tp5600'  => ',protected,',
145
 
1197 dpurdie 146
    'ftp'                   => 'SetProjectBase,',
147
 
148
    'icl'                   => 'IgnoreProjectBase,',
149
    'itso'                  => 'IgnoreProjectBase,',
150
    'daf_osa_mos'           => 'IgnoreProjectBase,',
151
    'daf_utils_mos'         => 'IgnoreProjectBase,',
152
    'itso_ud'               => 'IgnoreProjectBase,',
153
#    'mos_api'               => 'IgnoreProjectBase,',
154
#    'mos_fonts'             => 'IgnoreProjectBase,',
155
#    'sntp'                  => 'IgnoreProjectBase,',
156
#    'time_it'               => 'IgnoreProjectBase,',
157
 
392 dpurdie 158
);
159
 
160
my %notCots = (
161
    'isl'       => 1,
162
);
163
 
164
################################################################################
165
#   Global data
166
#
167
my $VERSION = "1.0.0";
168
my $RM_DB;
169
my $last_pv_id;
170
my %pkg_ids;
171
my $first_pkg_id;
172
my %versions;
173
my %suffixes;
174
my @processOrder;
175
my @startPoints;
176
my @allStartPoints;
177
my @endPoints;
178
my $now = time();
179
my $logSummary;
180
my $firstVersionCreated;
181
my @EssentialPackages;
182
my $createBranch;
183
my $createSuffix;
184
my $currentBranchName;
185
my $singleProject;
186
my $pruneCount = 0;
187
my $trimCount = 0;
188
my $badVcsCount = 0;
189
my $ProjectCount = 0;
190
my $totalVersions = 0;
191
my $initialTrees = 0;
192
my $globalError;
193
my @unknownProjects;
194
my %knownProjects;
195
my $badSingletonCount = 0;
196
my @flatOrder;
197
my $pruneMode;
198
my $pruneModeString;
199
my $threadId = 0;
200
my $threadCount;
201
my %tipVersions;
202
my $allSvn;
203
my @multiplePaths;
204
my @badEssentials;
205
my %svnData;
206
my $cwd;
207
 
208
my $packageNames;
209
my @packageNames;
210
my $multiPackages = -1;
211
my $visitId = 0;
212
my $noTransfer;
213
my $rippleCount = 0;
214
my $svnRepo;
1270 dpurdie 215
my $processCount = 0;
216
my $processTotal = 0;
1272 dpurdie 217
my $recentCount = 0;
392 dpurdie 218
 
219
our $GBE_RM_URL;
220
my $UNIX = $ENV{'GBE_UNIX'};
221
 
222
my $result = GetOptions (
223
                "help+"         => \$opt_help,          # flag, multiple use allowed
224
                "manual:3"      => \$opt_help,
225
                "verbose:+"     => \$opt_verbose,       # Versose
226
                "repository:s"  => \$opt_repo,          # Name of repository
1272 dpurdie 227
                'rbase:s'       => \$opt_repo_base,     # Base of the repo
392 dpurdie 228
                "flat!"         => \$opt_flat,          # Flat structure
229
                "test!"         => \$opt_test,          # Test operations
230
                "reuse!"        => \$opt_reuse,         # Reuse ClearCase views
231
                "age:i"         => \$opt_age,           # Only recent versions
232
                "dump:1"        => \$opt_dump,          # Dump Data
233
                "images:1"      => \$opt_images,        # Create DOT images
234
                "retain:i"      => \$opt_retaincount,   # Retain N packages
235
                "pruneMode:s"   => \$opt_pruneModeString,
236
                "listtags:i"    => \$opt_listTags,
237
                "name:s"        => \$opt_name,          # Alternate output
238
                "tip:s"         => \@opt_tip,           # Force tip version(s)
239
                "log!"          => \$opt_log,
1270 dpurdie 240
                "delete!"       => \$opt_delete,
392 dpurdie 241
                "postimage!"    => \$opt_postimage,
242
                'workdir:s'     => \$opt_workDir,
243
                );
244
 
245
#
246
#   Process help and manual options
247
#
248
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
249
pod2usage(-verbose => 1)  if ($opt_help == 2 );
250
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));
251
 
252
#
253
#   Configure the error reporting process now that we have the user options
254
#
255
SystemConfig ('ExitOnError' => 1);
256
ErrorConfig( 'name'    =>'CC2SVN_IMPORT',
257
             'verbose' => $opt_verbose,
258
              );
259
 
260
Error("Workdir does not exist" ) unless ( -d $opt_workDir );
261
Error("Specify a package as 'name'" ) unless ( defined $ARGV[0] );
262
EnvImport('GBE_RM_URL');
263
$cwd = Getcwd();
264
 
265
#
266
#   Init the pruning mode
267
#
268
setPruneMode( $opt_pruneModeString || 'ripple');
269
 
270
#
271
#   Get data for all packages
272
#
273
foreach my $packageName ( @ARGV )
274
{
275
    next unless ( $packageName );
276
    Verbose( "Base Package: $packageName");
277
 
278
    my $pkg_id = GetPkgIdByName ( $packageName );
279
    GetData_by_pkg_id ( $pkg_id, $packageName  );
280
    $pkg_ids{$pkg_id} = 1;
281
    $first_pkg_id = $pkg_id unless ( $first_pkg_id );
282
    push @packageNames, $packageName;
283
    $multiPackages++;
284
}
285
 
286
{
287
    #
288
    #   Delete entries that have been created as we read in
289
    #   data, but don't exist in RM. They will not have a pvid.
290
    #
291
    foreach my $entry ( keys(%versions) )
292
    {
293
        delete $versions{$entry}
294
            unless ( exists $versions{$entry}{pvid} );
295
    }
296
}
297
 
298
$totalVersions = scalar keys %versions;
299
Error ("No packages specified") unless ( $multiPackages >= 0 );
300
Warning ("Multiple Packages being processed") if ( $multiPackages > 1 );
301
$packageNames = join ('_', @packageNames );
302
$packageNames = $opt_name if ( defined $opt_name );
303
Message ("PackageName: $packageNames" );
304
 
305
#
306
#   Save logging data
307
#
308
if ( $opt_log )
309
{
310
    my $opt_logfile = $packageNames . '.import';
311
    Message ("Logging outout: $opt_logfile" );
312
    open STDOUT, '>', $opt_logfile  or die "Can't redirect STDOUT: $!";
313
    open STDERR, ">&STDOUT"         or die "Can't dup STDOUT: $!";
314
}
315
 
316
#
317
#   Prepare tip version hash
318
#
319
$tipVersions{$_} = 1 foreach ( @opt_tip );
320
 
321
#
322
#   Read in external data and massage it all
323
#
324
getEssenialPackageVersions();
325
getVobMapping();
326
smartPackageType();                 # Determine special prune mode
327
ReportPathVariance();
328
massageData();
329
getSvnData();
330
smartPackageType();                 # Have another go
331
 
332
my @missedTips = keys %tipVersions;
333
Error ("Specified tip version not found: @missedTips") if ( @missedTips );
334
 
335
if ( $opt_flat )
336
{
337
#    @flatOrder = sort {$versions{$a}{version} cmp $versions{$b}{version}} keys(%versions);
338
#    @flatOrder = sort {$versions{$a}{created} cmp $versions{$b}{created}} keys(%versions);
339
    @flatOrder = sort {$a <=> $b} keys(%versions);
340
    my $tip = $flatOrder[-1];
341
    $versions{$tip}{Tip} = 1 if $tip;
342
}
343
 
344
#
345
#   Generate dumps and images
346
#
347
if ( $opt_images )
348
{
349
    createImages();
350
}
351
 
352
if ( $opt_dump )
353
{
354
    DebugDumpData ("Versions", \%versions );
355
    DebugDumpData ("Starts", \@startPoints );
356
    DebugDumpData ("Ends", \@endPoints );
357
    DebugDumpData ("Suffixes", \%suffixes );
358
}
359
 
360
 
361
#   Display VCS tags
362
#
363
if ( $opt_listTags )
364
{
365
    foreach my $entry (sort {$versions{$a}{version} cmp $versions{$b}{version}} keys(%versions) )
366
    {
367
        print $versions{$entry}{vcsTag} || '-' ,"\n";
368
    }
369
}
370
exit if ( ($opt_dump > 1) || ($opt_images > 1) );
371
 
372
transferPackageToSvn();
373
 
374
if ( $opt_postimage )
375
{
376
    getSvnData();
377
    createImages();
378
}
379
 
380
exit 0;
381
 
382
#-------------------------------------------------------------------------------
383
# Function        : transferPackageToSvn
384
#
385
# Description     : Transfer the package to SVN
386
#
387
# Inputs          : 
388
#
389
# Returns         : 
390
#
391
sub transferPackageToSvn
392
{
393
    Error ("Repository Path not setup")
394
        unless ( $svnRepo );
395
 
396
    #
397
    #   Going to do serious work
398
    #   Need to ensure we have more arguments
399
    #
400
    if ( $noTransfer )
401
    {
402
        Warning("Protected Package not transferred: $packageNames[0]");
403
        exit 0;
404
    }
405
 
406
    #
407
    #   Perform all the work in a package specific subdirectory
408
    #
409
    my $workDir = $opt_workDir . '/' . $packageNames;
410
    mkdir $workDir unless ( -d $workDir );
411
    chdir $workDir || Error ("Cannot cd to $workDir");
412
 
413
    #
414
    #   Process all packages
415
    #       Going to create versions based on RM structure
416
    #       May have several starting points: Process each
417
    #
418
    newPackage();
419
    if ( $opt_flat )
420
    {
421
        newProject();
422
        foreach my $entry (@flatOrder )
423
        {
424
            newPackageVersion( $entry, $versions{$entry}{suffix} );
425
        }
426
    }
427
    else
428
    {
429
        processBranch(@allStartPoints);
430
    }
431
    endPackage();
432
 
433
    chdir $cwd || Error ("Cannot cd back to $cwd");
434
    rmdir $workDir;
435
    Warning ("Work Directory still exists: $workDir");
436
    saveData();
437
}
438
 
439
#-------------------------------------------------------------------------------
440
# Function        : setPruneMode
441
#
442
# Description     : Set the pruning mode
443
#
444
# Inputs          : mode                    - Text mode value
445
#
446
# Returns         : Nothing
447
#
448
sub setPruneMode
449
{
450
    my ($mode) = @_;
451
    my $value;
452
    if ( $mode )
453
    {
454
        if ( $mode =~ m/none/i) {
455
            $value = 0;
456
        } elsif ( $mode =~ m/ripple/i) {
457
            $value = 1;
458
        } elsif ( $mode =~ m/retain/i) {
459
            $value = 2;
460
        } elsif ( $mode =~ m/severe/i) {
461
            $value = 3;
462
        } else {
463
            Error ("Unknown pruning mode", "Use: none, ripple, retain or severe");
464
        }
465
 
466
        $pruneModeString = $mode;
467
        $pruneMode = $value;
468
    }
469
}
470
 
471
#-------------------------------------------------------------------------------
472
# Function        : smartPackageType
473
#
474
# Description     : Have a look at the projects in the package set and
475
#                   attempt to determine what sort of mechanism to use
476
#
477
# Inputs          : Uses %suffixes data
478
#
479
# Returns         : 
480
#
481
my $packageType = 'UNKNOWN';
482
sub smartPackageType
483
{
484
    #
485
    #   Rebuild suffixes hash based on  post massaged versions
486
    #
487
    my %suffixes;
488
    my @unknown;
489
    foreach my $entry ( keys %versions )
490
    {
491
        my $suffix = $versions{$entry}{suffix} || '';
492
        push (@unknown, $entry) if ($suffix eq 'unknown');
493
 
494
        next if ( exists $suffixes{$suffix} );
495
        next if ( $versions{$entry}{badSingleton} );
496
        next if ( $versions{$entry}{locked} eq 'N' || $versions{$entry}{isaWip} );
497
        $suffixes{$suffix} = 1;
498
        $knownProjects{$suffix}{seen} = 1;
499
 
500
    }
501
 
502
    #
503
    #   The 'unknown' suffix is really an 'empty' suffix
504
    #   Try to be clever
505
    #       Map unknown to 'cr' or 'mas' if present
506
    #
507
    #
508
    if ( exists $suffixes{'unknown'}  )
509
    {
510
        my $new_suffix;
511
        if ( exists $suffixes{'.cr'} ) {
512
            $new_suffix = '.cr';
513
        } elsif ( exists $suffixes{'.mas'} ) {
514
            $new_suffix = '.mas';
515
        }
516
 
517
        if ( $new_suffix )
518
        {
519
            foreach my $entry ( @unknown )
520
            {
521
                $versions{$entry}{suffix} = $new_suffix;
522
            }
523
            delete $suffixes{'unknown'};
524
            delete $knownProjects{'unknown'}{seen};
525
        }
526
    }
527
 
528
    if ( exists $suffixes{'.cots'} && !exists ($notCots{$packageNames}) ) {
529
        $packageType = 'COTS';
530
        $Projects{'.cots'}{Trunk} = 1;
531
        $singleProject = 1;
532
        $opt_flat = 1 unless defined $opt_flat;
533
        setPruneMode('none') unless (defined $opt_pruneModeString);
534
 
535
    } elsif ( exists $suffixes{'.tool'} ) {
536
        $packageType = 'TOOL';
537
        $Projects{'.tool'}{'Trunk'} = 1;
538
        $singleProject = 1;
539
        setPruneMode('none') unless (defined $opt_pruneModeString);
540
#        $opt_flat = 1;
541
 
542
    } elsif ( scalar (keys %suffixes ) == 1 ) {
543
        $packageType = 'SINGLE_PROJECT';
544
        $singleProject = 1;
545
 
546
    } else {
547
        $packageType = 'MULTIPLE_PROJECT';
548
    }
549
 
550
    #
551
    #   Some packages are special
552
    #
553
 
554
    if ( $packageNames[0] =~ m'^br_applet_' )
555
    {
556
        $opt_flat = 1 unless defined $opt_flat;
557
    }
558
 
559
    if ( exists $specialPackages{$packageNames[0]} )
560
    {
561
        my $data = $specialPackages{$packageNames[0]};
562
        if ( index( $data, ',all' ) >= 0) {
563
            setPruneMode('none') unless (defined $opt_pruneModeString);
564
        }
565
 
566
        if ( index( $data, 'protected,' ) >= 0) {
567
            $noTransfer = 1;
568
        }
569
 
570
        if ( index( $data, 'flat,' ) >= 0) {
571
            $opt_flat = 1;
572
        }
1197 dpurdie 573
 
574
        if ( index( $data, 'SetProjectBase,' ) >= 0) {
575
            $opt_preserveProjectBase = 1;
576
            $opt_ignoreProjectBaseErrors = 1;
577
            Message ("Preserving ProjectBase");
578
        }
579
 
580
        if ( index( $data, 'IgnoreProjectBase,' ) >= 0) {
581
            $opt_ignoreProjectBaseErrors = 1;
582
            Message ("Ignore ProjectBase Errors");
583
        }
584
 
392 dpurdie 585
    }
586
 
587
    Message("Package Type: $packageType, $pruneModeString");
588
}
589
 
590
 
591
#-------------------------------------------------------------------------------
592
# Function        : massageData
593
#
594
# Description     : Massage all the data to create a tree of package versions
595
#                   that can be used to create images as well as an import order
596
#
597
# Inputs          : 
598
#
599
# Returns         : 
600
#
601
my $reprocess=0;
602
sub calcLinks
603
{
604
    #
605
    #   Process the 'versions' hash and:
606
    #   Add back references
607
    #   Find starts and ends
608
    #       Entry with no previous
609
    #       Entry with no next
610
    #
611
    $reprocess = 0;
612
    foreach my $entry ( keys(%versions) )
613
    {
614
        foreach ( @{ $versions{$entry}{next}} )
615
        {
616
            $versions{$_}{last} = $entry;
617
        }
618
    }
619
    @allStartPoints = ();
620
    @startPoints = ();
621
    @endPoints = ();
622
    foreach my $entry ( keys(%versions) )
623
    {
624
        push @startPoints, $entry
625
            unless ( exists $versions{$entry}{last} || $versions{$entry}{badSingleton} );
626
 
627
        push @allStartPoints, $entry
628
            unless ( exists $versions{$entry}{last} );
629
 
630
        push @endPoints, $entry
631
            unless ( @{$versions{$entry}{next}} > 0  )
632
    }
633
}
634
 
635
sub massageData
636
{
637
    #
638
    #   Report unknown suffixes
639
    #   Handle bad, or little known project suffixes by creating them
640
    #
641
    foreach my $suffix ( keys %suffixes )
642
    {
643
        if ( exists $Projects{$suffix} )
644
        {
645
            next;
646
        }
647
        Message ("Unknown project suffix: '$suffix'");
648
        push @unknownProjects, $suffix;
649
        my $cleanSuffix = ucfirst(lc(substr( $suffix, 1)));
650
        $Projects{$suffix}{Name} = 'Project_' . $cleanSuffix;
651
    }
652
 
653
    calcLinks();
654
 
655
    $initialTrees = scalar @allStartPoints;
656
    Message ('Total RM versions: ' . $totalVersions );
657
    Message ('Initial trees: ' . $initialTrees );
658
    #
659
    #   Attempt to glue all the start points into one chain.
660
    #   This should allow us to track projects that branch from each other
661
    #   in cases where the RM data is incorrect/incomplete
662
    #       Strays are those that have no next or last
663
    #
664
    #   Glue based on Name, then PVID (Creation Order)
665
    #
666
    {
667
        #
668
        #   Examine threads. If it is a single entry thats bad then drop it
669
        #   This is simple to do. Should examine all entries, but thats a
670
        #   bit harder. Perhaps later.
671
        #
672
        if (1) {
673
            Message ("Dropping Bad Singletons");
674
            my $badSingletons;
675
            foreach my $entry ( sort {$a <=> $b} @startPoints )
676
            {
677
                my $ep = $versions{$entry};
678
                unless ( $ep->{last} || $ep->{next}[0] )
679
                {
680
#                    if (  $ep->{isaWip}  )
681
                    if ( (exists $ep->{badVcsTag} && $ep->{badVcsTag}) || $ep->{isaWip}  )
682
                    {
683
                        $ep->{badSingleton} = 1;
684
                        $reprocess = 1;
685
                        $badSingletonCount++;
686
 
687
                        # Add to a list of its own.
688
                        if ( $badSingletons )
689
                        {
690
                            push @{$versions{$badSingletons}{next}}, $entry;
691
                        }
692
                        $badSingletons = $entry;
693
                    }
694
                }
695
            }
696
            calcLinks()
697
                if ( $reprocess );
698
        }
699
 
700
        #
701
        #   Create simple trees out of the chains
702
        #   Tree is based on suffix (project) and version
703
        #
704
        {
705
            my %trees;
706
            Message ("Entries into trees");
707
            foreach my $single ( @startPoints )
708
            {
709
                my $suffix = $versions{$single}{suffix} || '';
710
                push @{$trees{$suffix}}, $single;
711
            }
712
 
713
            foreach  ( keys %trees )
714
            {
715
                my $last;
716
                foreach my $entry ( sort { $versions{$a}{version} cmp $versions{$b}{version}  } @{$trees{$_}} )
717
                {
718
                    if ( $last )
719
                    {
720
                        $versions{$last}{MakeTree} = 1;
721
                        push @{$versions{$last}{next}}, $entry;
722
                        $reprocess = 1;
723
                    }
724
                    $last = $entry;
725
                }
726
            }
727
            calcLinks()
728
                if ( $reprocess );
729
        }
730
 
731
        #
732
        #   Have a number of trees that are project related
733
        #   Attempt to create a single tree by inserting
734
        #   Secondary trees into the main line at suitable points
735
        #
736
        my @AllVersions = sort { $a <=> $b } @startPoints;
737
        my $lastEntry = shift @AllVersions;
738
        Error ("Oldest entry has a previous version") if ( $versions{$lastEntry}{last}  );
739
#print "Oldest: $lastEntry\n";
740
        #
741
        #   Insert remaining entries into out list, which is now sorted
742
        #
743
        my @completeList;
744
        foreach my $base ( @AllVersions  )
745
        {
746
            push @completeList, recurseList($lastEntry);
747
            @completeList = sort {$a <=> $b} @completeList;
748
#            Message("Complete List: ", @completeList);
749
#            Message("Complete List($completeList[0]) Length: " . scalar @completeList);
750
            $lastEntry = $base;
751
 
752
            my $last;
753
            foreach my $entry ( @completeList )
754
            {
755
                if ( $entry > $base )
756
                {
757
                    Error ("Not expecting last to be empty. $base, $entry") unless ( $last );
758
                    last;
759
                }
760
                $last = $entry;
761
            }
762
 
763
            #
764
            #   Insert at end if point not yet found
765
            #
766
#print "Inserting $base at $last\n";
767
            push @{$versions{$last}{next}}, $base;
768
            $versions{$base}{GluedIn} = 1;
769
            $reprocess = 1;
770
        }
771
 
772
        #
773
        #   Recalc basic links if any processing done
774
        #
775
        calcLinks()
776
            if ( $reprocess );
777
 
778
    }
779
 
780
 
781
    #
782
    #   Remove Dead Ends
783
    #   Packages that were never released
784
    #       Not locked, unless essential or a branchpoint
785
    #   Won't consider these to be mainline path.
786
    #
787
    {
788
        Message ("Remove Dead Ends");
789
        foreach my $entry ( @endPoints )
790
        {
791
            my $deadWood;
792
            while ( $entry )
793
            {
794
                last if ( $versions{$entry}{Essential} );
795
 
796
                my @next = @{$versions{$entry}{next}};
797
                my $count = @next;
798
                last if ( $count > 1 );
799
 
800
                last unless ( $versions{$entry}{locked} eq 'N' || $versions{$entry}{isaWip} );
801
 
802
                $versions{$entry}{DeadWood} = 1;
803
                $trimCount++;
804
            } continue {
805
                $entry = $versions{$entry}{last};
806
            }
807
        }
808
    }
809
 
810
    #
811
    #   Walk each starting point list and determine new Projects
812
    #   branchpoints.
813
    #
814
    Message ("Locate Projects branch points");
815
    foreach my $bentry ( keys(%versions) )
816
    {
817
        my $baseSuffix = $versions{$bentry}{suffix};
818
        foreach my $entry ( @{$versions{$bentry}{next}} )
819
        {
820
            if ( $baseSuffix ne $versions{$entry}{suffix})
821
            {
822
                unless ( exists $versions{$entry}{DeadWood} || $versions{$entry}{badSingleton} )
823
                {
824
#print "--- Project Branch $versions{$entry}{vname}\n";
825
                    $versions{$entry}{branchPoint} = 1;
826
                    $versions{$entry}{newSuffix} = 1;
827
                }
828
            }
829
        }
830
    }
831
 
832
    #
833
    #   Prune
834
    #   Marks paths to root for all essential packages
835
    #   Marks the last-N from all essential packages
836
    #
837
    if ( $pruneMode )
838
    {
839
        Message ("Prune Tree: $pruneModeString");
840
        foreach ( @EssentialPackages )
841
        {
842
            #next unless ( exists $versions{$_} );      # Aleady deleted
843
 
844
            # Mark previous-N to be retained as well
845
            my $entry = $_;
846
            my $count = 0;
847
            while ( $entry )
848
            {
849
                last if ( $versions{$entry}{KeepMe} );
850
                unless ( $versions{$entry}{isaRipple} )
851
                {
852
                    my $keepFlag = ($count++ < $opt_retaincount);
853
                    last unless ( $keepFlag );
854
                    $versions{$entry}{KeepMe} = $keepFlag;
855
                }
856
                $entry = $versions{$entry}{last}
857
            }
858
        }
859
 
860
        #
1272 dpurdie 861
        #   Keep recent versions
862
        #       Keep versions created in the last N days
863
        #       Will keep recent ripples too
864
        #
865
        if ( $pruneMode == 1 )
866
        {
867
            foreach my $entry ( keys(%versions) )
868
            {
869
                next unless ( $versions{$entry}{Age} <= $opt_recentAge  );
870
                $versions{$entry}{keepRecent} = 1;
871
                $recentCount++;
872
#print "--- Recent version $versions{$entry}{vname}, $versions{$entry}{Age} <= $opt_recentAge\n";
873
            }
874
 
875
#            #
876
#            #   Keep the tip of each branch
877
#            #
878
#            foreach my $entry ( @endPoints )
879
#            {
880
#                next if exists $versions{$entry}{keepRecent};
881
#                $versions{$entry}{keepRecent} = 1;
882
##print "--- Tip version $versions{$entry}{vname}\n";
883
#            }
884
        }
885
 
886
        #
392 dpurdie 887
        #   Keep versions that are common parents to Essential Versions
888
        #       Mark paths through the tree to essential versions
889
        #       Mark nodes with the number of essential versions that they sprout
890
        #   Don't do it if we are ripple pruning
891
        #
892
        Message ("Prune Tree keep common parents");
893
        if ( $pruneMode != 1 )
894
        {
895
            foreach my $entry ( @endPoints )
896
            {
897
                my $hasEssential = 0;
898
                $visitId++;
899
                while ( $entry )
900
                {
901
                    $hasEssential = 1 if ( exists ($versions{$entry}{Essential}) && $versions{$entry}{Essential} );
902
                    if ( $hasEssential )
903
                    {
904
                        if ( @{$versions{$entry}{next}} > 1 )
905
                        {
906
                            $versions{$entry}{EssentialSplitPoint}++;
907
                        }
908
                        last if ( exists $versions{$entry}{EssentialPath} );
909
                        $versions{$entry}{EssentialPath} = 1;
910
                    }
911
 
912
                    if ( ($versions{$entry}{visitId} || 0) == $visitId )
913
                    {
914
                        DebugDumpData ("Versions", \%versions );
915
                        Warning ("Circular dependency");
916
                        last;
917
                    }
918
                    $versions{$entry}{visitId} = $visitId;
919
 
920
                    $entry = $versions{$entry}{last};
921
                }
922
            }
923
        }
924
 
925
        #
926
        #   Keep first version of each ripple. Must keep first
927
        #   Group ripples together so that they can be proccessed at the same time
928
        #
929
        calcRippleGroups()
930
            if ( $pruneMode == 1);
931
 
932
        #
933
        #   Delete all nodes that are not marked for retention
934
        #   This is rough on the tree
935
        #
936
        Message ("Prune Tree Deleting");
937
 
938
        # 0 - Keep me
939
        # 1 - Prune me
940
        sub pruneMe
941
        {
942
            my ($entry) = @_;
943
 
944
            return 0 unless ( exists $versions{$entry} );
945
            return 0 unless ( $versions{$entry}{last} );
946
            return 0 if ( ($pruneMode == 2) && exists $versions{$entry}{KeepMe} );
947
            return 0 if ( exists $versions{$entry}{Essential} );
948
            return 0 if ( $versions{$entry}{newSuffix} );
949
            return 0 if ( $versions{$entry}{newSuffix} && (exists $versions{$entry}{EssentialPath}) );
950
#            return 1 if ( exists $versions{$entry}{DeadWood} );
951
            return 0 if ( exists $versions{$entry}{EssentialSplitPoint} && $versions{$entry}{EssentialSplitPoint} > 1 );
952
            return 0 if ( exists $versions{$entry}{keepLowestRipple} &&  $versions{$entry}{keepLowestRipple} );
953
            return 0 if ( ($pruneMode == 1) && ! $versions{$entry}{isaRipple} );
1272 dpurdie 954
            return 0 if ( exists $versions{$entry}{keepRecent} && $versions{$entry}{keepRecent} );
392 dpurdie 955
            return 1;
956
        }
957
 
958
        foreach my $entry ( keys(%versions) )
959
        {
960
#last;
961
            next unless ( pruneMe($entry) );
962
#print "--- Prune: $versions{$entry}{vname}\n";
963
 
964
            # Delete the current node
965
            #
966
            my @newNext;
967
            $pruneCount++;
968
            my $last = $versions{$entry}{last};
969
            foreach ( @{$versions{$last}{next}} )
970
            {
971
                next if ( $_ == $entry );
972
                push @newNext, $_;
973
            }
974
            foreach ( @{$versions{$entry}{next}} )
975
            {
976
                push @newNext, $_;
977
                $versions{$_}{last} = $last;
978
            }
979
 
980
            @{$versions{$last}{next}} = @newNext;
981
            delete $versions{$entry};
982
        }
983
 
984
        # Recalculate endpoints
985
        calcLinks();
986
    }
987
    else
988
    {
989
        #   No rippling happening
990
        #   Some process still need to happen
991
        #
992
        calcRippleGroups();
993
    }
994
 
995
    #
996
    #   Calculate best through-path for branches in the tree
997
    #   Attempt to keep that 'max' version on the mainline
998
    #   May be modified by -tip=nnnn
999
    #
1000
    #   For each leaf (end point), walk backwards and mark each node with the
1001
    #   max version see. If we get to a node which already has been marked then
1002
    #   stop if our version is greater. We want the value to be the max version
1003
    #   to a leaf
1004
    #
1005
    #   Account for 'suffix'. When suffix changes, then the 'max' version must
1006
    #   be recalculated
1007
    #
1008
 
1009
    Message ("Calculate Max Version");
1010
    my $maxVersion;
1011
 
1012
    foreach my $entry ( @endPoints )
1013
    {
1014
        my $lastSuffix;
1015
        my $forceTip;
1016
        while ( $entry )
1017
        {
1018
            if (!defined($lastSuffix) || ($versions{$entry}{suffix} ne $lastSuffix) )
1019
            {
1020
                $maxVersion = '0';
1021
                $visitId++;
1022
                $forceTip = ( exists $tipVersions{$versions{$entry}{vname}} );
1023
                delete $tipVersions{$versions{$entry}{vname}};
1024
                $maxVersion = '999.999.999.999.zzz' if ( $forceTip );
1025
                $lastSuffix = $versions{$entry}{suffix};
1026
#print "---Tip Found\n" if $forceTip;
1027
            }
1028
 
1029
            # Detect circular dependencies
1030
            if ( ($versions{$entry}{visitId} || 0) == $visitId )
1031
            {
1032
                DebugDumpData ("Circular dependency: Versions", \%versions );
1033
                Warning ("Circular dependency");
1034
                last;
1035
            }
1036
            $versions{$entry}{visitId} = $visitId;
1037
 
1038
            my $thisVersion = $versions{$entry}{version} || '';
1039
            if ( $thisVersion gt $maxVersion )
1040
            {
1041
                $maxVersion = $thisVersion;
1042
            }
1043
 
1044
            if ( exists $versions{$entry}{maxVersion} )
1045
            {
1046
                if ( $versions{$entry}{maxVersion} gt $maxVersion )
1047
                {
1048
                    last;
1049
                }
1050
            }
1051
 
1052
            $versions{$entry}{maxVersion} = $maxVersion;
1053
            $entry = $versions{$entry}{last};
1054
        }
1055
    }
1056
 
1057
 
1058
    #
1059
    #   Locate all instances where a package-version branches
1060
    #   Determine the version that should be on the non-branching path
1061
    #
1062
    #   Reorder the 'next' list so that the first item is the non-branching
1063
    #   path. This will be used in the data-insertion phase to simplify the
1064
    #   processing.
1065
    #
1066
    Message ("Calculate package version branches");
1067
    foreach my $entry ( sort {$a <=> $b} keys(%versions) )
1068
    {
1069
        calculateWalkOrder($entry);
1070
    }
1071
 
1072
    #
1073
    #   Mark Project Branch Tips as they will be in the Repository
1074
    #   Find each project head and walk primary entry to the end.
1075
    #
1076
    foreach my $entry ( keys(%versions) )
1077
    {
1078
        #
1079
        #   Root of each tree is 'new'
1080
        #
1081
        unless ( defined $versions{$entry}{last})
1082
        {
1083
            unless ( $versions{$entry}{badSingleton} )
1084
            {
1085
                $versions{$entry}{newSuffix} = 1;
1086
            }
1087
        }
1088
 
1089
        #
1090
        #   Update stats
1091
        #
1092
        $badVcsCount++ if ( $versions{$entry}{badVcsTag} );
1093
        $ProjectCount++ if ( $versions{$entry}{newSuffix} );
1094
        next if ( $opt_flat );
1095
 
1096
        next unless ($versions{$entry}{newSuffix} );
1097
#print "--- Project new Suffix $versions{$entry}{vname}\n";
1098
 
1099
 
1100
        my $suffix = $versions{$entry}{suffix};
1101
        $knownProjects{$suffix}{count}++;
1102
 
1103
        my $next = $versions{$entry}{next}[0];
1104
        my $tip;
1105
        while ( $next )
1106
        {
1107
            last if ( $suffix ne $versions{$next}{suffix} );
1108
            $tip = $next unless (exists ($versions{$next}{DeadWood}) || $versions{$next}{badSingleton});
1109
            $next = $versions{$next}{next}[0];
1110
        }
1111
 
1112
        $versions{$tip}{Tip} = 1 if $tip;
1113
    }
1114
 
1115
    unless ( $opt_flat )
1116
    {
1117
        my $finalTrees = scalar @startPoints;
1118
        Warning ("Still have multiple trees: $finalTrees") unless ( $finalTrees == 1 );
1119
    }
1120
 
1121
    #
1122
    #   Display warnings about multiple
1123
    #
1124
    foreach ( sort keys %knownProjects )
1125
    {
1126
        my $count = $knownProjects{$_}{count} || 0;
1127
        Warning ("Multiple Project Roots: $_ ($count)" )
1128
            if ( $count > 1 );
1129
    }
1130
 
1131
    #
1132
    #   Display warnings about Bad Essential Packages
1133
    #
1134
    $allSvn = 1;
1135
    foreach my $entry ( keys(%versions) )
1136
    {
1137
        $rippleCount++ if ( exists($versions{$entry}{isaRipple}) && $versions{$entry}{isaRipple} );
1138
        $allSvn = 0 unless ( $versions{$entry}{isSvn} );
1139
        next unless ( exists $versions{$entry}{Essential}  );
1140
        next unless ( $versions{$entry}{badVcsTag}  );
1141
        push @badEssentials, $entry;
1142
        Warning ("BadVCS Essential: " . GetVname($entry))
1143
    }
1144
 
1145
    #
1146
    #   All done
1147
    #
1270 dpurdie 1148
    $processTotal = scalar keys %versions;
1149
    Message("Retained entries: $processTotal" );
392 dpurdie 1150
    Message("Pruned entries: $pruneCount");
1151
    Message("Deadwood entries: $trimCount");
1152
    Message("Bad Singletons: $badSingletonCount");
1153
    Message("Ripples: $rippleCount");
1272 dpurdie 1154
    Message("Recent entries: $recentCount");
392 dpurdie 1155
}
1156
 
1157
sub calculateWalkOrder
1158
{
1159
    my ($entry) = @_;
1160
    my @next = @{$versions{$entry}{next}};
1161
    my $count = @next;
1162
    my @ordered;
1163
    my $main;
1164
 
1165
    if ( $count > 1 )
1166
    {
1167
        # Array to hash to simplify removal
1168
        my %nexts = map { $_ => 1 } @next;
1169
        foreach my $e ( @next )
1170
        {
1171
 
1172
            #
1173
            #   Locate branch points that are not a part of a new project
1174
            #   These will not be preferred paths for walking
1175
            #
1176
            if ( !defined($versions{$e}{branchPoint}) && $versions{$entry}{suffix} ne $versions{$e}{suffix} )
1177
            {
1178
                unless ( exists $versions{$e}{DeadWood} || $versions{$e}{badSingleton}  )
1179
                {
1180
#print "--- Project Branch (1) $versions{$e}{vname}\n";
1181
                    $versions{$e}{branchPoint} = 1;
1182
                    $versions{$e}{newSuffix} = 1;
1183
                }
1184
            }
1185
 
1186
            #
1187
            #   Remove those that already have a branch,
1188
            #
1189
            if ( $versions{$e}{branchPoint} || $versions{$e}{newSuffix} || $versions{$e}{DeadWood}  )
1190
            {
1191
                push @ordered, $e;
1192
                delete $nexts{$e};
1193
            }
1194
        }
1195
        #
1196
        #   Select longest arm as the non-branching path
1197
        #   Note: Reverse sort order
1198
        #         Done so that 'newest' item is given preference
1199
        #         to the main trunk in cases where all subtrees are
1200
        #         the same length
1201
        #
1202
        my $maxData = '';
1203
        my $countEntry;
1204
        foreach my $e ( sort {$b <=> $a} keys %nexts )
1205
        {
1206
            if ( $versions{$e}{maxVersion} gt $maxData )
1207
            {
1208
                $maxData = $versions{$e}{maxVersion};
1209
                $countEntry = $e;
1210
            }
1211
        }
1212
        if ($countEntry)
1213
        {
1214
            $main = $countEntry;
1215
            delete $nexts{$countEntry};
1216
        }
1217
 
1218
        #
1219
        #   Append the remaining
1220
        #
1221
        push @ordered, keys %nexts;
1222
 
1223
        #
1224
        #   Re-order 'next' so that the main path is first
1225
        #   Sort (non main) by number
1226
        #
1227
        @ordered = sort {$a <=> $b} @ordered;
1228
        unshift @ordered, $main if ( $main );
1229
        @{$versions{$entry}{next}} = @ordered;
1230
 
1231
        #
1232
        #   Ensure all except the first are a branch point
1233
        #   First may still be a branch point
1234
        #
1235
        shift @ordered;
1236
        foreach my $e ( @ordered )
1237
        {
1238
            $versions{$e}{branchPoint} = 1;
1239
        }
1240
    }
1241
}
1242
 
1243
#-------------------------------------------------------------------------------
1244
# Function        : calcRippleGroups
1245
#
1246
# Description     : Locate and mark ripple groups
1272 dpurdie 1247
#                   packages that are ripples of each other
392 dpurdie 1248
#                       Keep first version of each ripple. Must keep first
1249
#                       Group ripples together so that they can be
1250
#                       proccessed at the same time
1251
#
1252
# Inputs          : 
1253
#
1254
# Returns         : 
1255
#
1256
sub calcRippleGroups
1257
{
1258
    my %rippleVersions;
1259
    foreach my $entry ( keys(%versions) )
1260
    {
1261
        my $ep = $versions{$entry};
1262
        if ( defined $ep->{buildVersion} )
1263
        {
1264
            my $suffix = $ep->{suffix};
1265
            my ($major, $minor, $patch, $build) = @{$ep->{buildVersion}};
1266
#print "--- $major, $minor, $patch, $build, $suffix\n";
1267
            $rippleVersions{$suffix}{"$major.$minor.$patch"}{count}++;
1268
            my $rp = $rippleVersions{$suffix}{"$major.$minor.$patch"};
1269
            $rp->{list}{$entry} = 1;
1270
 
1271
            next if ( $ep->{badVcsTag} );
1272
            next if ( $ep->{locked} eq 'N');
1273
            if (!defined ($rp->{min}) || $rp->{min} > $build )
1274
            {
1275
                $rp->{pvid} = $entry;
1276
                $rp->{min} = $build;
1277
            }
1278
        }
1279
    }
1280
#            DebugDumpData("rippleVersions", \%rippleVersions );
1281
 
1282
    while ( my($suffix, $e1) = each %rippleVersions )
1283
    {
1284
        while ( my( $mmp, $e2) = each %{$e1} )
1285
        {
1286
            next unless ( exists  $e2->{pvid} );
1287
            my $entry = $e2->{pvid};
1288
            if ( !exists $versions{$entry} )
1289
            {
1290
                Error ("Internal: Expected entry not found: $entry, $mmp");
1291
            }
1292
 
1293
            $versions{$entry}{keepLowestRipple} = 1;
1294
#print "--- Keep Riple $versions{$entry}{vname}\n";
1295
 
1296
            #
1297
            #   Update entry with list of associated ripples, removing lowest
1298
            #
1299
            delete $e2->{list}{$entry};
1300
            my @rippleList = sort keys %{$e2->{list}};
1301
            if ( @rippleList)
1302
            {
1303
#DebugDumpData("LIST: $entry", $e2->{list}, \@rippleList  );
1304
                @{$versions{$entry}{rippleList}} = @rippleList;
1305
            }
1306
        }
1307
    }
1308
}
1309
 
1310
#-------------------------------------------------------------------------------
1311
# Function        : processBranch
1312
#
1313
# Description     : Process one complete branch within the tree of versions
1314
#                   May be called recursivly to walk the tree
1315
#
1316
# Inputs          : Array of package-version ID to process
1317
#
1318
# Returns         : Nothing
1319
#
1320
 
1321
sub processBranch
1322
{
1323
    foreach my $entry ( @_ )
1324
    {
1325
        #
1326
        #   Do we need to create a branch before we can process this package
1327
        #
1328
        if ( $versions{$entry}{newSuffix} || $versions{$entry}{branchPoint} )
1329
        {
1330
            newProject();
1331
            $createBranch = 1;
1332
            $createSuffix = 1 if $versions{$entry}{newSuffix};
1333
        }
1334
 
1335
        newPackageVersion( $entry );
1336
no warnings "recursion";
1337
        processBranch (@{$versions{$entry}{next}});
1338
    }
1339
}
1340
 
1341
#-------------------------------------------------------------------------------
1342
# Function        : newPackageVersion
1343
#
1344
# Description     : Create a package version
1345
#
1346
# Inputs          : $entry              - Ref to entry being proccessed
1347
#
1348
# Returns         :
1349
#
1350
sub newPackageVersion
1351
{
1352
    my ($entry) = @_;
1353
    my %data;
1354
    my $flags = 'e';
1355
    my $rv = 1;
1356
    my $startTime = time();
1357
    my $timestamp = localtime;
1358
 
1359
    $data{rmRef} = 'ERROR';
1360
    $data{tag} = 'ERROR';
1361
 
1362
    #
1363
    #   If its been processed then fake that its been done
1364
    #   May have been a ripple that we processed
1365
    #
1270 dpurdie 1366
    return if ($versions{$entry}{Processed});
1367
    $processCount++;
392 dpurdie 1368
    Message ("------------------------------------------------------------------" );
1270 dpurdie 1369
    Message ("Package $processCount of $processTotal");
1370
 
392 dpurdie 1371
    Message ("New package-version: " . GetVname($entry) . " Tag: " . $versions{$entry}{vcsTag} );
1372
 
1328 dpurdie 1373
    #
1374
    #   Detect user abort
1375
    #
1376
    if ( -f $cwd . '/stopfile' )
1377
    {
1378
        $globalError = 1;
1379
        Message ("Stop file located");
1380
    }
392 dpurdie 1381
 
1382
    #
1383
    #   If we have a global error,then we pretend to process, but we
1384
    #   report errors for the logging system
1385
    #
1386
    unless ( $globalError )
1387
    {
1388
        #
1389
        #   Call worker function
1390
        #   It will exist on any error so that it can be logged
1391
        #
1392
        $rv = newPackageVersionBody( \%data, @_ );
1393
        $globalError = 1 if ( $rv >= 10 );
1394
    }
1395
 
1396
    #
1397
    #   Highlight essential packages that failed to transfer
1398
    #
1399
    if ( $globalError ) {
1400
        $flags = 'e';
1401
    } elsif ( $rv && ( exists $versions{$entry}{Essential} ) ) {
1402
        $flags = 'X';
1403
    } elsif ( $rv ) {
1404
        $flags = 'E';
1405
    } else {
1406
        $flags = 'G';
1407
    }
1408
 
1409
    #
1410
    #   Always log results to a file
1411
    #   Flags:
1412
    #       e - Error: Global Fatal causes other versions to be ignored
1413
    #       X - Essential Package NOT proccessed
1414
    #       E - Error processing package
1415
    #       G - Good
1416
    #
1417
    my $duration = time() - $startTime;
1418
    my $line = join(';',
1419
            $flags,
1420
            $entry,
1421
            $packageNames,
1422
            $versions{$entry}{vname},
1423
            $data{rmRef},
1424
            $data{tag},
1425
            $timestamp,
1426
            $duration,
1427
            $data{errStr} || ''
1428
            );
1429
    logToFile( $cwd . '/importsummary.txt', ";$line;");
1430
    #
1431
    #   Sava data
1432
    #
1270 dpurdie 1433
    $data{errFlags} = $flags;
1434
    $data{duration} = $duration;
392 dpurdie 1435
    $versions{$entry}{rmRef} = $data{rmRef};
1270 dpurdie 1436
    delete $data{rmRef};
1437
    delete $data{tag};
1340 dpurdie 1438
    ##delete $data{ViewRoot};
1270 dpurdie 1439
    $versions{$entry}{data} = \%data;
1440
 
392 dpurdie 1441
    #
1442
    #   Delete the created view
1443
    #   Its just a directory, so delete it
1444
    #
1445
    if ( $data{ViewRoot} && -d $data{ViewRoot})
1446
    {
1340 dpurdie 1447
        if ( !$opt_reuse || $rv )
1448
        {
1449
            Message ("Delete View: $data{ViewRoot}");
1450
            RmDirTree ($data{ViewRoot} );
1451
        }
1452
        else
1453
        {
1454
            Message ("Retaining View: $data{ViewRoot}");
1455
        }
1456
 
392 dpurdie 1457
    }
1340 dpurdie 1458
    else
1459
    {
1460
        Message ("No view to delete");
1461
    }
392 dpurdie 1462
 
1463
 
1464
    #
1465
    #   If this version has any 'ripples' then process them while we have the
1466
    #   main view. Note the ripple list may contain entries that do not
1467
    #   exist - they will have been pruned.
1468
    #
1469
    foreach my $rentry ( @{$versions{$entry}{rippleList}} )
1470
    {
1471
        next unless( exists $versions{$rentry} );
1472
 
1473
        if ($versions{$rentry}{Processed})
1474
        {
1475
            Warning ("Ripple Processed before main entry");
1476
            $versions{$rentry}{rippleProcessed} = 1;
1477
        }
1478
 
1479
        Message ("Proccessing associated Ripple: " . GetVname($rentry));
1480
        newPackageVersion($rentry);
1481
    }
1482
}
1483
 
1484
#-------------------------------------------------------------------------------
1485
# Function        : newPackageVersionBody
1486
#
1487
# Description     : Perform the bulk of the work in creating a new PackageVersion
1488
#                   Designed to return on error and have error processing
1489
#                   performed by caller
1490
#
1491
# Inputs          : $data               - Shared data
1492
#                   $entry              - Package entry to process
1493
#
1494
# Returns         : Error Code
1495
#                         0 - All is well
1272 dpurdie 1496
#                       <10 - Recoverable error
392 dpurdie 1497
#                       >10 - Fatal error
1498
#
1499
sub newPackageVersionBody
1500
{
1501
    my ($data, $entry) = @_;
1502
    my $rv;
1503
    my $cc_label;
1504
    my $cc_path;
1505
 
1506
    #
1507
    #   Init Data
1508
    #
1509
    $data->{rmRef} = 'ERROR';
1510
    $data->{tag} = '';
1511
    $data->{ViewRoot} = undef;
1512
    $data->{ViewPath} = undef;
1513
    $data->{errStr} = '';
1514
    $versions{$entry}{Processed} = 1;
1515
 
1516
 
1517
    SystemConfig ('ExitOnError' => 0);
1518
 
1519
    push @processOrder, $entry;
1520
    return 0 if ( $opt_test );
1521
 
1522
#   Keep DeadWood. May be a WIP
1523
#    if ( exists $versions{$entry}{DeadWood} && $versions{$entry}{DeadWood} )
1524
#    {
1525
#        $data->{errStr} = 'Package is DeadWood';
1526
#        return 3;
1527
#    }
1528
 
1529
    #
1530
    #   Determine version information
1531
    #
1532
    $data->{tag} = $versions{$entry}{vcsTag} || '';
1533
    if ( $versions{$entry}{badVcsTag} )
1534
    {
1535
        Warning ("Error: Bad VcsTag for: " . GetVname($entry),
1536
                 "Tag: $data->{tag}" );
1537
        $data->{errStr} = 'VCS Tag Marked as Bad';
1538
        return 1;
1539
    }
1540
 
1541
 
1542
    $data->{tag} =~ m~^(.+?)::(.*?)(::(.+))?$~;
1543
    $cc_label = $4;
1544
    $cc_path = $2;
1545
    $cc_path = '/' . $cc_path;
1546
    $cc_path =~ tr~\\/~/~s;
1547
 
1548
    #
1549
    #   Correct well known path mistakes
1550
    #
394 dpurdie 1551
    $cc_path =~ s~/MASS_Dev/Bus/~/MASS_Dev_Bus/~i;
392 dpurdie 1552
    $cc_path =~ s~/MASS_Dev_Bus/Cbp/~/MASS_Dev_Bus/CBP/~i;
1553
    $cc_path =~ s~/MREF_Package/ergpostmongui$~/MREF_Package/ergpostmongui~i;
1197 dpurdie 1554
    $cc_path =~ s~/MREF_21/MREF_Package/~/MREF_Package/~i;
392 dpurdie 1555
 
1197 dpurdie 1556
 
392 dpurdie 1557
#print "--- Path: $cc_path, Label: $cc_label\n";
1558
 
1559
    #
1560
    #   Create CC view
1561
    #   Import into Subversion View
1562
    #
1563
    $data->{ViewRoot} = $opt_name ? $opt_name : "$cc_label";
1197 dpurdie 1564
    $data->{ViewPath} =  $data->{ViewRoot} . $cc_path;
1565
 
1566
    if ( $opt_preserveProjectBase )
1567
    {
1568
        my $cc_vob = $cc_path;
1569
        $cc_vob =~ s~^/~~;
1570
        $cc_vob =~ s~/.*~~;
1571
        $data->{ViewPath} =  $data->{ViewRoot} . '/' . $cc_vob;
1572
        Message ("Preserving Project Base");
1573
    }
392 dpurdie 1574
    $data->{ViewPath} =~  tr~/~/~s;
1575
 
1576
    if ( $opt_reuse && -d $data->{ViewPath}  )
1577
    {
1578
        Message ("Reusing view: $cc_label");
1579
    }
1580
    else
1581
    {
1582
        my @args;
1583
        push (@args, '-view', $opt_name ) if ( defined $opt_name );
1584
        $rv = JatsToolPrint ( 'jats_ccrelease', '-extractfiles', '-root=.' , '-noprefix',
1585
                    "-label=$cc_label" ,
1586
                    "-path=$cc_path",
1587
                    @args
1588
                    );
1589
 
1590
        unless ( -d $data->{ViewPath}  )
1591
        {
1592
            $data->{errStr} = 'Failed to extract files from CC';
1593
            return 2;
1594
        }
1595
    }
1596
 
1197 dpurdie 1597
 
392 dpurdie 1598
    #
1197 dpurdie 1599
    #   Some really ugly packages make use of a Jats feature called 'SetProjectBase'
1600
    #   Detect such packages as we will need to handle them differently
1601
    #   Can't really handle it on the fly
1602
    #   All we can do is detct it and report it - at the moment
1603
    #
1604
    if (detectProjectBaseUsage($data, $cc_path) )
1605
    {
1606
        unless ( $opt_ignoreProjectBaseErrors )
1607
        {
1608
            $data->{BadProjectBase}++;
1609
            $data->{errStr} = 'Bad usage of ProjectBase detected';
1610
            return 14;
1611
        }
1612
    }
1613
 
1270 dpurdie 1614
 
1197 dpurdie 1615
    #
1270 dpurdie 1616
    #   Developers have been slack
1617
    #       Sometime the mark the source path as 'GMTPE2005'
1618
    #       Sometimes as 'GMTPE2005/Package/Fred/Jill/Harry'
1619
    #
1620
    #   Attempt to suck up empty directories below the specified
1621
    #   source path
1622
    #
1623
    unless ( $opt_preserveProjectBase )
1624
    {
1625
        #
1626
        #   Look in ViewPath
1627
        #   If it contains only ONE directory then we can suck it up
1628
        #
1629
        my $testDir = findDirWithStuff( $data->{ViewPath} );
1328 dpurdie 1630
 
1270 dpurdie 1631
        unless ( $data->{ViewPath} eq $testDir  )
1632
        {
1633
            Message ("Adjust Base Dir: $testDir");
1634
            $data->{adjustedPath} = $data->{ViewPath};
1635
            $data->{ViewPath} = $testDir;
1636
        }
1637
    }
1638
 
1639
 
1640
    #
392 dpurdie 1641
    #   Have a CC view
1642
    #   Now we can create the SVN package and branching point before we
1643
    #   import the CC data into SVN
1644
    #
1645
    my @args;
1646
 
1647
    #
1648
    #   Calculate args for functions
1649
    #
1650
    my $author = $versions{$entry}{created_id};
1651
    if ( $author )
1652
    {
1653
        push @args, '-author', $author;
1654
    }
1655
    my $created = $versions{$entry}{created};
1656
    if ( $created )
1657
    {
1658
        $created =~ s~ ~T~;
1659
        $created .= '00000Z';
1660
        push @args, '-date', $created;
1661
    }
1662
 
1663
    my $log = $versions{$entry}{comment};
1664
    if ( $log )
1665
    {
1666
        push @args, '-log', $log;
1667
    }
1668
 
1669
    #
1670
    #   Create package skeleton if needed
1671
    #
1672
    $rv = createPackage( $author, $created);
1673
    if ( $rv )
1674
    {
1675
        $data->{errStr} = 'Failed to create Package';
1676
        return 10;
1677
    }
1678
 
1679
    #
1680
    #   Calculate the label for the target package
1681
    #   Use format <packageName>_<PackageVersion>
1682
    #   Need to handle WIPs too.
1683
    #
1684
    my $import_label = saneLabel($entry);
1685
 
1686
    #
1687
    #   May need to create the branchpoint
1688
    #   The process is delayed until its needed so avoid creating unneeded
1689
    #   branch points
1690
    #
1691
    if ( $createBranch )
1692
    {
1693
        $rv = createBranchPoint ($entry, $author, $created);
1694
        $createBranch = 0;
1695
        $createSuffix = 0;
1696
        if ( $rv )
1697
        {
1698
            $data->{errStr} = 'Failed to create Branch Point';
1699
            return 11;
1700
        }
1701
    }
1702
    push @args, "-branch=$currentBranchName" if ( defined $currentBranchName );
1703
 
1704
    my $datafile = "importdata.$import_label.properties";
1705
    $rv = JatsToolPrint ( 'jats_svn', 'import', '-reuse' ,
1706
                    "-package=$svnRepo/$packageNames",
1707
                    "-dir=$data->{ViewPath}",
1708
                    "-label=$import_label",
1709
                    "-datafile=$datafile",
1710
                    @args,
1711
                     );
1712
 
1713
    if ( $rv )
1714
    {
1715
        $data->{errStr} = 'Failed to import to SVN';
1716
        return 12;
1717
    }
1718
 
1719
    $versions{$entry}{TagCreated} = 1;
1720
    $firstVersionCreated = $entry unless ( $firstVersionCreated );
1721
 
1722
    #
1723
    #   Read in the Rm Reference
1724
    #   Retain entries in a global file
1725
    #
1726
    if ( -f $datafile  )
1727
    {
1728
        my $rmData = JatsProperties::New($datafile);
1729
        $data->{rmRef} = 'SVN::' . $rmData->getProperty('subversion.tag');
1730
    }
1731
 
1732
    unless ( $data->{rmRef}  )
1733
    {
1272 dpurdie 1734
        $data->{errStr} = 'Failed to determine Rm Reference';
392 dpurdie 1735
        return 13;
1736
    }
1737
 
1341 dpurdie 1738
 
1739
    #
1740
    #   Add supplemental tags if this version is in a 'Release'
1741
    #
1742
    foreach my $rname ( keys %{$versions{$entry}{Releases}}  )
1743
    {
1744
        my $rtext = 'Release_' . saneString($versions{$entry}{Releases}{$rname});
1745
        Message ("Adding Release Tag: $rname");
1746
        $rv = JatsToolPrint ( 'jats_svnlabel' ,
1747
                    $data->{rmRef},
1748
                    '-clone',
1749
                    $rtext,
1750
#                    @args,
1751
                     );
1752
        if ( $rv )
1753
        {
1754
            $data->{errStr} = 'Failed to add Release tag';
1755
            return 14;
1756
        }
1757
 
1758
    }
1759
 
1760
 
392 dpurdie 1761
    Message ("RM Ref: $data->{rmRef}");
1762
    unlink $datafile;
1763
 
1764
    #
1765
    #   All is good
1766
    #
1767
    $data->{errStr} = '';
1768
    return 0;
1769
}
1770
 
1771
 
1772
#-------------------------------------------------------------------------------
1773
# Function        : newProject
1774
#
1775
# Description     : Start a new project within a package
1776
#
1777
# Inputs          : 
1778
#
1779
# Returns         : 
1780
#
1781
sub newProject
1782
{
1783
#    Message ("---- New Project");
1784
    $createSuffix = 0;
1785
 
1786
    #
1787
    #   New project
1788
    #   Kill the running import directory
1789
    #
1790
    RmDirTree ('SvnImportDir');
1791
}
1792
 
1793
#-------------------------------------------------------------------------------
1794
# Function        : newPackage
1795
#
1796
# Description     : Start processing a new package
1797
#
1798
# Inputs          : 
1799
#
1800
# Returns         : 
1801
#
1802
my $createPackageDone;
1803
sub newPackage
1804
{
1805
#    Message( "---- New Package");
1806
 
1807
    #
1808
    #   Create a package specific log file
1809
    #
1810
    $logSummary = $packageNames . ".summary.log";
1811
    unlink $logSummary;
1812
    Message( "PackageName: $packageNames");
1813
    $createPackageDone = 1;
1814
    $createBranch = 0;
1815
    $createSuffix = 0;
1816
 
1817
    #
1818
    #   First entry being created
1819
    #   Prime the work area
1820
    #
1821
    RmDirTree ('SvnImportDir');
1822
}
1823
 
1824
#-------------------------------------------------------------------------------
1825
# Function        : createPackage
1826
#
1827
# Description     : Create a new Package in SVN
1828
#                   Called before any serious SVN operation to ensure that the
1829
#                   package has been created. Don't create a package until
1830
#                   we expect to put something into it.
1831
#
1832
#                   Will only create a package once
1833
 
1834
#
1835
# Inputs          : $author         - Who done it
1836
#                   $date           - When
1837
#
1838
# Returns         : 
1839
#
1840
sub createPackage
1841
{
1842
    my ($author, $date) = @_;
1843
    my @opts;
1844
    push (@opts, '-date', $date) if ( $date );
1845
    push (@opts, '-author', $author) if ( $author );
1846
    #
1847
    #   Only do once
1848
    #
1849
    return unless ( $createPackageDone );
1850
    $createPackageDone = 0;
1851
 
1197 dpurdie 1852
    #
1853
    #   Real import
1854
    #       Do not Delete package if it exists
1855
    #       Package must NOT exist
1856
    #
392 dpurdie 1857
    Message ("Creating new SVN package: $packageNames");
1270 dpurdie 1858
    if ( $opt_delete )
1859
    {
1860
        Message ("Delete existing version of package: $packageNames");
1861
        JatsToolPrint ( 'jats_svn', 'delete-package', '-noerror',  "$svnRepo/$packageNames" );
1862
    }
1197 dpurdie 1863
    JatsToolPrint ( 'jats_svn', 'create', "$svnRepo/$packageNames", '-new', @opts );
392 dpurdie 1864
}
1865
 
1866
 
1867
#-------------------------------------------------------------------------------
1868
# Function        : createBranchPoint
1869
#
1870
# Description     : Create a branch point for the current work
1871
#                   Perform the calculation to determine the details of
1872
#                   the branch point. The work will only be done when its
1873
#                   needed. This will avoid the creation of branchpoints
1874
#                   that are not used.
1875
#
1876
# Inputs          : $entry                  Entry being processed
1877
#                   $author         - Who done it
1878
#                   $date           - When
1879
#
1880
# Returns         : 
1881
#
1882
sub createBranchPoint
1883
{
1884
    my ($entry, $author, $date) = @_;
1885
    my $forceNewProject;
1886
 
1887
#    Message ("---- Create Branch Point");
1888
 
1889
    #
1890
    #   Find previous good tag
1891
    #   We are walking a tree so something should have been created, but
1892
    #   the one we want may have had an error
1893
    #
1894
    #   Walk backwards looking for one that has been created
1895
    #
1896
    my $last = $versions{$entry}{last};
1897
    while ( $last )
1898
    {
1899
        unless ( $versions{$last}{TagCreated} )
1900
        {
1901
            $last = $versions{$last}{last};
1902
        }
1903
        else
1904
        {
1905
            last;
1906
        }
1907
    }
1908
 
1909
    #
1910
    #   If we have walked back to the base of the tree
1911
    #   If we transferred any software at all, then use the first
1912
    #   version as the base for this disconnected version
1913
    #
1914
    #   Otherwise we create a new, and empty, view
1915
    #
1916
    unless ( $last )
1917
    {
1918
        if ( $firstVersionCreated )
1919
        {
1920
            Warning ("Cannot find previous version to branch. Use first version");
1921
            $last = $firstVersionCreated;
1922
        }
1923
        else
1924
        {
1925
            Warning ("Forcing First instance of a Project");
1926
            $forceNewProject = 1;
1927
        }
1928
    }
1929
 
1930
    #
1931
    #   Determine source name
1932
    #   This MUST have been created before we can branch
1933
    #
1934
    my $src_label;
1935
    $src_label = saneLabel($last) if $last;
1936
 
1937
    #
1938
    #   Create target name
1939
    #
1940
    my $tgt_label;
1941
    if ( $forceNewProject || $versions{$entry}{newSuffix} || $createSuffix || !defined $src_label )
1942
    {
1943
        #
1944
        #   Create target name based on project
1945
        #
1946
        return if ( $singleProject );
1947
 
1948
        my $suffix = $versions{$entry}{suffix};
1949
        if ( $suffix )
1950
        {
1951
            Error ("Unknown Project: $suffix") unless ( defined $Projects{$suffix} );
1952
 
1953
            #
1954
            #   If this project can be considered to be a truck, then 'claim' the
1955
            #   truck for the first created element.
1956
            #
1957
            if ( $Projects{$suffix}{Trunk} )
1958
            {
1959
                # This project can use the trunk, if it has not been allocated.
1960
                $ProjectTrunk = $suffix unless ( defined $ProjectTrunk );
1961
 
1962
                #
1963
                #   If this package has multiple instances of the potential
1964
                #   trunk, then don't place either of them on the trunk as it
1965
                #   may cause confusion
1966
                #
1967
                if ($knownProjects{$suffix}{count} < 2 )
1968
                {
1969
                    if ( $suffix eq $ProjectTrunk )
1970
                    {
1971
                        return unless $currentBranchName;
1972
                    }
1973
                }
1974
            }
1975
 
1976
            $tgt_label = $Projects{$suffix}{Name};
1977
            $tgt_label = $versions{$entry}{name} . '_' . $tgt_label if ($multiPackages);
1978
            if ( !exists $ProjectsBaseCreated{$tgt_label}  )
1979
            {
1980
                $ProjectsBaseCreated{$tgt_label} = 1;
1981
            }
1982
            else
1983
            {
1984
                #   Project Base Already taken
1985
                #   Have disjoint starting points
1986
                $tgt_label .= '.' . $ProjectsBaseCreated{$tgt_label} ++;
1987
            }
1988
        }
1989
        else
1990
        {
1991
            #
1992
            #   No suffix in use
1993
            #
1994
            #   Currently not handled
1995
            #   May have to force the use of the trunk
1996
            #
1997
            Error ("INTERNAL ERROR: No suffix present");
1998
        }
1999
    }
2000
    else
2001
    {
2002
        $tgt_label = saneLabel($entry, $src_label . '_for_');
2003
    }
2004
 
2005
    #
2006
    #   Save branch name for use when populating sandbox
2007
    #
2008
    $currentBranchName = $tgt_label;
2009
 
2010
    #
2011
    #   Perform the branch
2012
    #
2013
    if ( defined $src_label )
2014
    {
1328 dpurdie 2015
        #
1341 dpurdie 2016
        #   The 'clone' operation will backtrack the branch point
2017
        #   to the source of the label. This will make the output version
2018
        #   tree much prettier
1328 dpurdie 2019
        #
392 dpurdie 2020
        my @opts;
2021
        push (@opts, '-date', $date) if ( $date );
2022
        push (@opts, '-author', $author) if ( $author );
2023
 
2024
        JatsToolPrint ( 'jats_svnlabel',
2025
                        '-packagebase', "$svnRepo/$packageNames",
1341 dpurdie 2026
                        'tags/' . $src_label,
392 dpurdie 2027
                        '-branch',
2028
                        '-clone', $tgt_label,
2029
                        @opts
2030
                      );
2031
    }
2032
}
2033
 
2034
 
2035
#-------------------------------------------------------------------------------
2036
# Function        : endPackage
2037
#
2038
# Description     : End of package processing
2039
#                   Clean up and display problems
2040
#
2041
# Inputs          : 
2042
#
2043
# Returns         : 
2044
#
2045
sub endPackage
2046
{
2047
    RmDirTree ('SvnImportDir');
2048
 
2049
    #
2050
    #   Display versions that did get captured
2051
    #
2052
    foreach my $entry ( @processOrder )
2053
    {
2054
        $versions{$entry}{Scanned} = 1;
2055
        next unless ( $versions{$entry}{TagCreated} );
2056
        Warning ("Processed: " . GetVname($entry) . ' :: ' . $versions{$entry}{rmRef} || $versions{$entry}{errStr} || '???' );
2057
    }
2058
 
2059
    #
2060
    #   Display versions that did not get created
2061
    #
2062
    foreach my $entry ( @processOrder )
2063
    {
2064
        $versions{$entry}{Scanned} = 1;
2065
        next if ( $versions{$entry}{TagCreated} );
2066
        Warning ("Not Processed: " . GetVname($entry) );
2067
    }
2068
 
2069
    foreach my $entry ( keys(%versions) )
2070
    {
2071
        next if ( $versions{$entry}{Scanned} );
2072
        Warning ("(E) INTERNAL ERROR. Package Not Processed: " . GetVname($entry) );
2073
    }
2074
 
2075
    Message ("All Done");
2076
}
2077
 
1197 dpurdie 2078
#-------------------------------------------------------------------------------
2079
# Function        : detectProjectBaseUsage
2080
#
2081
# Description     : Detect and report usage of the SetProjectBase directive
2082
#
2083
# Inputs          : $data               - Ref to a hash of bits
2084
#                   $cc_path            - Packages cc_path
2085
#
2086
# Returns         : true    - Bad usage (Really good usage not detected)
2087
#                   false   - Good usage detected
2088
#
2089
sub detectProjectBaseUsage
2090
{
2091
    my ($data, $cc_path) = @_;
2092
    my $retval = 0;
2093
    my $eSuf = $opt_ignoreProjectBaseErrors ? '' : 'Error';
2094
 
2095
    #
2096
    #   Find makefile.pl
2097
    #
2098
    Message ("Locate JATS makefiles");
2099
    my $usesProjectBase = 0;
2100
    my $definesProjectBase = 0;
2101
    my $definitionError = 0;
2102
 
2103
    my $search = JatsLocateFiles->new("--Recurse=1",
2104
                                       "--FilterIn=makefile.pl",
2105
                                       );
2106
    my @makefiles = $search->search($data->{ViewRoot});
2107
    foreach my $file ( @makefiles )
2108
    {
2109
        if ( open( my $fh, '<', "$data->{ViewRoot}/$file" ) )
2110
        {
2111
            while ( <$fh> )
2112
            {
2113
                s~\s+$~~;
2114
                s~^\s+~~;
2115
                next if ( m~^#~ );
2116
 
2117
                if ( m~\$ProjectBase~ )
2118
                {
2119
                    $usesProjectBase++;
2120
                    Message ("Project Base Use: $_");
2121
                    $data->{UsesProjectBase}++;
2122
                }
2123
 
2124
                if ( m~^SetProjectBase~ )
2125
                {
2126
                    $definesProjectBase++;
2127
                    $data->{DefinesProjectBase}++;
2128
                    Warning ("Package uses SetProjectBase:",
2129
                             "Line: " . $_,
2130
                             "Root: " . "$data->{ViewRoot}",
2131
                             "File: " . "$data->{ViewRoot}/$file",
2132
                            );
2133
 
2134
                    # The only problem is if the user attempts to escape
2135
                    # from the root of the view.
2136
                    #
2137
                    # Examine the depth of the makefile with the directive
2138
                    # Examine the depth of the view base
2139
                    #
2140
                    #
2141
                    # Locate the build.pl file
2142
                    # This is the basis for for the directive
2143
                    #
2144
                    my $blevel;
2145
                    my @bpaths = split ('/', $file );
2146
                    while ( @bpaths )
2147
                    {
2148
                        $bpaths[-1] = 'build.pl';
2149
                        my $bfile = join '/', @bpaths ;
2150
                        if ( -f "$data->{ViewRoot}/$bfile" )
2151
                        {
2152
                            $blevel = scalar @bpaths;
2153
                            last;
2154
                        }
2155
                        pop @bpaths;
2156
                    }
2157
                    unless (defined $blevel)
2158
                    {
2159
                        Warning ("SetProjectBase$eSuf calculation failed - can't find build.pl");
2160
                        $retval = 1;
2161
                    }
2162
                    else
2163
                    {
2164
                        #
2165
                        #   Determine the depth of the view root
2166
                        #   This is given by cc_path, but cc_path has a leading /
2167
                        #
2168
                        my @cpaths = split ('/', $cc_path );
2169
                        my $clevel = (scalar @cpaths) - 1;
2170
                        my $max_up = $blevel - $clevel - 1;
2171
 
2172
                        m~--Up=(\d+)~i;
2173
                        my $ulevel = $1;
2174
                        if ( defined $ulevel )
2175
                        {
2176
                            my @paths = split ('/', $file );
2177
                            my $plevel = scalar @paths;
2178
 
2179
#print "--- blevel: $blevel\n";
2180
#print "--- bpaths: @bpaths\n";
2181
#print "--- ulevel: $ulevel\n";
2182
#print "--- paths: @paths\n";
2183
#print "--- plevel: $plevel\n";
2184
#print "--- cpaths: @cpaths\n";
2185
#print "--- clevel: $clevel\n";
2186
#print "--- max_up: $max_up\n";
2187
 
2188
                            if ( $ulevel > $max_up )
2189
                            {
2190
                                Warning ("SetProjectBase escapes view. MaxUp: $max_up, Up: $ulevel");
2191
                                $definitionError++;
2192
                            }
2193
                        }
2194
                        else
2195
                        {
2196
                            $retval = 1;
2197
                            Warning ("SetProjectBase$eSuf MAY escape view - can't detect level")
2198
                        }
2199
                    }
2200
                }
2201
            }
2202
            close $fh;
2203
        }
2204
        else
2205
        {
2206
            Warning ("SetProjectBase$eSuf - Cannot open makefile: $file");
2207
            $retval = 1;
2208
        }
2209
    }
2210
 
2211
    #
2212
    #   Detect defined, but not used
2213
    #
2214
    if ( $usesProjectBase && ! $definesProjectBase )
2215
    {
2216
        Warning ("SetProjectBase - Uses ProjectBase without defining it");
2217
    }
2218
 
2219
    if ( ! $usesProjectBase && $definesProjectBase )
2220
    {
2221
        Warning ("SetProjectBase - Defines ProjectBase without using it");
2222
    }
2223
 
2224
    if ( $usesProjectBase && $definesProjectBase && $definitionError )
2225
    {
2226
        Warning ("SetProjectBase$eSuf - Problem detected");
2227
        $retval = 1;
2228
    }
2229
    return $retval;
2230
}
2231
 
1270 dpurdie 2232
#-------------------------------------------------------------------------------
2233
# Function        : findDirWithStuff
2234
#
2235
# Description     : Find a directory that contains more than just another subdir
2236
#
2237
# Inputs          : $base               - Start of the scan
2238
#
2239
# Returns         : Path to dir with more than just a single dir in it
2240
#
2241
sub findDirWithStuff
2242
{
2243
    my ($base) = @_;
1197 dpurdie 2244
 
1270 dpurdie 2245
    while ( $base )
2246
    {
2247
    my $fileCount = 0;
2248
    my $dirCount = 0;
1272 dpurdie 2249
    my $firstDir;
1270 dpurdie 2250
 
2251
    my @list = glob( $base . '/*');
2252
    foreach ( @list )
2253
    {
2254
        next if ( $_ eq '.' );
2255
        next if ( $_ eq '..' );
2256
        if ( -d $_ )
2257
        {
2258
            $dirCount++;
1272 dpurdie 2259
            $firstDir = $_ unless ( defined $firstDir );
1270 dpurdie 2260
            return $base if ( $dirCount > 1  );
2261
        }
1272 dpurdie 2262
        elsif ( -e $_ )
1270 dpurdie 2263
        {
2264
            return $base;
2265
        }
1272 dpurdie 2266
 
2267
        # else its probably a dead symlink
1270 dpurdie 2268
    }
2269
    return $base unless ( $dirCount == 1  );
1272 dpurdie 2270
    $base = $firstDir;
1270 dpurdie 2271
    }
2272
}
2273
 
2274
 
1197 dpurdie 2275
#-------------------------------------------------------------------------------
2276
# Function        : JatsToolPrint
2277
#
2278
# Description     : Print and Execuate a JatsTool command
2279
#
2280
# Inputs          : 
2281
#
2282
# Returns         : 
2283
#
2284
 
392 dpurdie 2285
sub JatsToolPrint
2286
{
2287
    Information ("Command: @_");
2288
    JatsTool @_;
2289
}
2290
 
2291
sub GetVname
2292
{
2293
    my ($entry) = @_;
2294
    my $me = 'NONE';
2295
    if ( $entry )
2296
        {
2297
        $me = $versions{$entry}{vname};
2298
        unless ( $me )
2299
        {
2300
            $me = 'Unknown-' . $entry;
2301
        }
2302
    }
2303
    return $me;
2304
}
2305
 
2306
sub saneLabel
2307
{
2308
    my ($entry, $pkgname) = @_;
1272 dpurdie 2309
    my $me;
2310
    $me = $versions{$entry}{vname};
392 dpurdie 2311
    $pkgname = $versions{$entry}{name} unless ( defined $pkgname );
2312
 
2313
    Error ("Package does have a version string: pvid: $entry")
2314
        unless ( defined $me );
2315
 
2316
    #
2317
    #   Convert Wip format (xxxx) into a string that can be used for a label
2318
    #
2319
    if ( $me =~ m~^(.*)\((.*)\)(.*)$~ )
2320
    {
2321
        $me = $1 . '_' . $2 . '_' . $3 . '.WIP';
2322
        $me =~ s~_\.~.~;
2323
        $me =~ s~^_~~;
2324
    }
2325
 
2326
    #
2327
    #   Allow for WIPS
2328
    #   Get rid of multiple '_'
2329
    #   Replace space with -
2330
    #
2331
    $me = $pkgname . '_' . $me;
2332
    $me =~ tr~ ~-~s;
2333
    $me =~ tr~-~-~s;
2334
    $me =~ tr~_~_~s;
2335
 
2336
    return $me;
2337
}
2338
 
1341 dpurdie 2339
sub saneString
2340
{
2341
    my ($string) = @_;
2342
    #
2343
    #   Get rid of multiple '_'
2344
    #   Replace space with -
2345
    #
2346
    $string =~ s~\W~_~g;
2347
    $string =~ tr~ ~_~s;
2348
    $string =~ tr~_-~-~s;
2349
    $string =~ tr~-_~-~s;
2350
    $string =~ tr~-~-~s;
2351
    $string =~ tr~_~_~s;
2352
    $string =~ s~-$~~;
2353
    $string =~ s~_$~~;
392 dpurdie 2354
 
1341 dpurdie 2355
    return $string;
2356
}
2357
 
2358
 
392 dpurdie 2359
exit 0;
2360
 
2361
 
2362
#-------------------------------------------------------------------------------
2363
# Function        : GetPkgIdByName
2364
#
2365
# Description     :
2366
#
2367
# Inputs          : pkg_name
2368
#
2369
# Returns         : pkg_id
2370
#
2371
sub GetPkgIdByName
2372
{
2373
    my ( $pkg_name ) = @_;
2374
    my (@row);
2375
    my $pv_id;
2376
    my $pkg_id;
2377
 
2378
    #
2379
    #   Establish a connection to Release Manager
2380
    #
2381
    connectRM(\$RM_DB) unless ( $RM_DB );
2382
 
2383
    #
2384
    #   Extract data from Release Manager
2385
    #
2386
    my $m_sqlstr = "SELECT pkg.PKG_NAME, pkg.PKG_ID" .
2387
                   " FROM RELEASE_MANAGER.PACKAGES pkg" .
2388
                   " WHERE pkg.PKG_NAME = \'$pkg_name\'";
2389
 
2390
    my $sth = $RM_DB->prepare($m_sqlstr);
2391
    if ( defined($sth) )
2392
    {
2393
        if ( $sth->execute( ) )
2394
        {
2395
            if ( $sth->rows )
2396
            {
2397
                while ( @row = $sth->fetchrow_array )
2398
                {
2399
                    Verbose( "DATA: " . join(',', @row) );
2400
                    $pkg_id = $row[1] || 0;
2401
                    last;
2402
                }
2403
            }
2404
            else
2405
            {
2406
                Error ("GetPkgIdByName:No Data for package: $pkg_name");
2407
            }
2408
            $sth->finish();
2409
        }
2410
    }
2411
    else
2412
    {
2413
        Error("GetPkgIdByName:Prepare failure" );
2414
    }
2415
 
2416
    return $pkg_id;
2417
}
2418
 
2419
#-------------------------------------------------------------------------------
2420
# Function        : GetData_by_pkg_id
2421
#
2422
# Description     :
2423
#
2424
# Inputs          : pv_id
2425
#
2426
# Returns         :
2427
#
2428
sub GetData_by_pkg_id
2429
{
2430
    my ( $pkg_id, $packageName ) = @_;
2431
    my (@row);
2432
 
2433
    #
2434
    #   Establish a connection to Release Manager
2435
    #
2436
    Message ("Extract package versions from Release Manager: $packageName");
2437
    connectRM(\$RM_DB) unless ( $RM_DB );
2438
 
2439
    #
2440
    #   Extract data from Release Manager
2441
    #
2442
    my $m_sqlstr = "SELECT pkg.PKG_NAME, pv.PKG_VERSION, pkg.PKG_ID, pv.PV_ID, pv.LAST_PV_ID, pv.MODIFIED_STAMP, release_manager.PK_RMAPI.return_vcs_tag(pv.PV_ID), amu.USER_NAME, pv.COMMENTS, pv.DLOCKED, pv.CREATOR_ID ".
2443
                   " FROM RELEASE_MANAGER.PACKAGES pkg, RELEASE_MANAGER.PACKAGE_VERSIONS pv, ACCESS_MANAGER.USERS amu" .
2444
                   " WHERE pv.PKG_ID = \'$pkg_id\' AND pkg.PKG_ID = pv.PKG_ID AND amu.USER_ID (+) = pv.CREATOR_ID";
2445
 
2446
 
2447
    my $sth = $RM_DB->prepare($m_sqlstr);
2448
    if ( defined($sth) )
2449
    {
2450
        if ( $sth->execute( ) )
2451
        {
2452
            if ( $sth->rows )
2453
            {
2454
                while ( @row = $sth->fetchrow_array )
2455
                {
2456
                    Verbose( "DATA: " . join(',', @row) );
2457
                    my $pkg_name = $row[0] || 'Unknown';
2458
                    my $pkg_ver = $row[1] || 'Unknown';
2459
                       $pkg_ver =~ s~\s+$~~;
2460
                       $pkg_ver =~ s~^\s+~~;
2461
                    my $pv_id = $row[3] || 'Unknown';
2462
                    my $last_pv_id = $row[4];
2463
                    my $created =  $row[5] || 'Unknown';
2464
                    my $vcstag =  $row[6] || 'Unknown';
395 dpurdie 2465
 
392 dpurdie 2466
                    my $created_id =  $row[7] || ($row[10] ? "Userid_$row[10]" :'Unknown');
2467
                    my $comment =  $row[8] || '';
2468
                    my $locked =  $row[9] || 'N';
2469
 
2470
                    #
2471
                    #   Some developers have a 'special' package version
2472
                    #   We really need to ignore them
2473
                    #
2474
                    next if ( $pkg_ver eq '23.23.23.ssw' );
2475
 
2476
                    #
2477
                    #   Add data to the hash
2478
                    #       Remove entries that address themselves
2479
                    #
2480
                    push (@{$versions{$last_pv_id}{next}}, $pv_id) unless ($pv_id == $last_pv_id || $last_pv_id == 0) ;
2481
                    $versions{$pv_id}{name} = $pkg_name;
2482
                    $versions{$pv_id}{pvid} = $pv_id;
2483
                    $versions{$pv_id}{vname} = $pkg_ver;
2484
                    $versions{$pv_id}{vcsTag} = $vcstag;
2485
                    $versions{$pv_id}{created} = $created;
2486
                    $versions{$pv_id}{created_id} = $created_id;
2487
                    $versions{$pv_id}{comment} = $comment;
2488
                    $versions{$pv_id}{locked} = $locked;
2489
                    $versions{$pv_id}{TimeStamp} = str2time( $created );
2490
                    $versions{$pv_id}{Age} = ($now - $versions{$pv_id}{TimeStamp}) / (60 * 60 * 24);
2491
                    $versions{$pv_id}{TooOld} = 1 if ( $opt_age && $opt_age <= $versions{$pv_id}{Age} );
2492
                    examineVcsTag($pv_id);
2493
 
2494
                    #
2495
                    #   Process version number
2496
                    #
2497
                    my ($suffix, $version, $isaR, $isaWip, $buildVersion ) = massageVersion($pkg_ver, $pkg_name);
2498
 
2499
                    $versions{$pv_id}{version} = $version;
2500
                    $versions{$pv_id}{buildVersion} = $buildVersion;
2501
                    $versions{$pv_id}{isaRipple} = 1 if ( $isaR );
2502
                    $versions{$pv_id}{isaWip} = 1 if ( $isaWip );
2503
 
2504
                    #
2505
                    #   Process suffix
2506
                    #
2507
                    $suffix = 'Unknown' unless ( $suffix );
2508
                    $suffix = lc ($suffix);
2509
                    $versions{$pv_id}{suffix} = $suffix;
2510
                    push @{$suffixes{$suffix}}, $pv_id;
2511
 
2512
#                    print "$pkg_name, $pkg_ver, $pv_id, $last_pv_id, $locked, $created, $created_id, $suffix\n";
2513
                }
2514
            }
2515
            else
2516
            {
2517
                Error ("GetData_by_pkg_id: No Data: $m_sqlstr");
2518
            }
2519
            $sth->finish();
2520
        }
2521
        else
2522
        {
2523
                Error ("GetData_by_pkg_id: Execute: $m_sqlstr");
2524
        }
2525
    }
2526
    else
2527
    {
2528
        Error("GetData_by_pkg_id:Prepare failure" );
2529
    }
2530
}
2531
 
2532
#-------------------------------------------------------------------------------
2533
# Function        : massageVersion
2534
#
2535
# Description     : Process a version number and return usful bits
2536
#
2537
# Inputs          : Version Number
2538
#                   Package Name - debug only
2539
#
2540
# Returns         : An array
2541
#                       suffix
2542
#                       multipart version string useful for text comparisons
2543
#
2544
sub massageVersion
2545
{
2546
    my ($version, $name) = @_;
2547
    my ($major, $minor, $patch, $build, $suffix);
2548
    my $result;
2549
    my $buildVersion;
2550
    my $isaRipple;
2551
    my $isaWIP;
2552
    $build = 0;
2553
 
2554
#print "--- $name, $version\n";
2555
    $version =~ s~^_~~;
2556
    $version =~ s~^${name}_~~;
2557
 
2558
    #
2559
    #   xxxxxxxxx.nnnn.cots
2560
    #
2561
    if ( $version =~ m~(.*)\.cots$~ ) {
2562
        my $cots_base = $1;
2563
        $suffix = '.cots';
2564
        if ( $version =~ m~(.*?)\.([0-9]{4})\.cots$~ )
2565
        {
2566
            $result = $1 . sprintf (".%4.4d", $2) . $suffix;
2567
        }
2568
        else
2569
        {
2570
            $result = $cots_base . '.0000.cots';
2571
        }
2572
    }
2573
    #
2574
    #   Convert version into full form for comparisions
2575
    #       nnn.nnn.nnn.[p]nnn.xxx
2576
    #       nnn.nnn.nnn.[p]nnn-xxx
2577
    #       nnn.nnn.nnn-[p]nnn.xxx
2578
    #       nnn.nnn.nnn-[p]nnn-xxx
2579
    #       nnn.nnn.nnn[p]nnn-xxx
2580
    #   Don't flag as ripples - they are patches
2581
    #
2582
    elsif ( $version =~ m~^(\d+)\.(\d+)\.(\d+)[-.p][p]?(\d+)([-.](.*))?$~ ) {
2583
        $major = $1;
2584
        $minor = $2;
2585
        $patch = $3;
2586
        $build = $4;
2587
        $suffix = defined $6 ? ".$6" : '';
2588
        $isaRipple = 0;
2589
    }
2590
    #
2591
    #       nn.nnn.nnnnn.xxx
2592
    #       nn.nnn.nnnnn-xxx
2593
    #       nnn.nnn.nnnx.xxx
2594
    #   Don't flag as ripples - they are patches
2595
    #
2596
    elsif ( $version =~ m~^(\d+)\.(\d+)\.(\d+)\w?([-.](.*))?$~ ) {
2597
        $major = $1;
2598
        $minor = $2;
2599
        $patch = $3;
2600
        if ( length( $patch) >= 4 )
2601
        {
2602
            $build = substr( $patch, -3 ,3);
2603
            $patch = substr( $patch,  0 ,length($patch)-3);
2604
        }
2605
        $suffix = defined $5 ? ".$5" : '';
2606
    }
2607
 
2608
    #
2609
    #       nnn.nnn.nnn
2610
    #       nnn.nnn-nnn
2611
    #       nnn.nnn_nnn
2612
    #
2613
    elsif ( $version =~ m~^(\d+)\.(\d+)[-._](\d+)$~ ) {
2614
        $major = $1;
2615
        $minor = $2;
2616
        $patch = $3;
2617
        $suffix = '';
2618
    }
2619
 
2620
    #
2621
    #       nnn.nnn.nnn.nnn
2622
    #       nnn.nnn.nnn-nnn
2623
    #       nnn.nnn.nnn_nnn
2624
    #
2625
    elsif ( $version =~ m~^(\d+)\.(\d+)\.(\d+)[-._](\d+)$~ ) {
2626
        $major = $1;
2627
        $minor = $2;
2628
        $patch = $3;
2629
        $build = $4;
2630
        $suffix = '';
2631
        $isaRipple = 0;
2632
    }
2633
 
2634
 
2635
    #
2636
    #       nnn.nnn
2637
    #
2638
    elsif ( $version =~ m~^(\d+)\.(\d+)$~ ) {
2639
        $major = $1;
2640
        $minor = $2;
2641
        $patch = 0;
2642
        $suffix = '';
2643
    }
2644
    #
2645
    #       nnn.nnn.xxx
2646
    #
2647
    elsif ( $version =~ m~^(\d+)\.(\d+)(\.\w+)$~ ) {
2648
        $major = $1;
2649
        $minor = $2;
2650
        $patch = 0;
2651
        $suffix = $3;
2652
    }
2653
 
2654
    #
2655
    #       nnn.nnn.nnnz
2656
    #
2657
    elsif ( $version =~ m~^(\d+)\.(\d+)\.(\d+)([a-z])$~ ) {
2658
        $major = $1;
2659
        $minor = $2;
2660
        $patch = $3;
2661
        $build = ord($4) - ord('a');
2662
        $suffix = '.cots';
2663
        $isaRipple = 0;
2664
    }
2665
    #
2666
    #       ???REV=???
2667
    #
2668
    elsif ( $version =~ m~REV=~ ) {
2669
        $suffix = '.cots';
2670
        $result = $version . '.0000.cots';
2671
    }
2672
 
2673
    #
2674
    #   Wip Packages
2675
    #   (nnnnnn).xxx
2676
    #   Should be essential, but want to sort very low
2677
    #
2678
    elsif ($version =~ m~\((.*)\)(\..*)?~) {
2679
        $suffix = $2 || '';
2680
        $result = "000.000.000.000$suffix";
2681
        $isaWIP = 1;
2682
    }
2683
 
2684
    #
2685
    #   !current
2686
    #
2687
    elsif ($version eq '!current' || $version eq 'current_$USER' || $version eq 'current' || $version eq 'beta' || $version eq 'latest' || $version eq 'beta.cr' || $version eq 'CREATE') {
2688
        $suffix = '';
2689
        $result = "000.000.000.000$suffix";
2690
        $isaWIP = 1;
2691
    }
2692
 
2693
    #
2694
    #   Also WIP: FINRUN.103649.BEI.WIP
2695
    elsif ($version =~ m~(\.[a-zA-Z]+)\.WIP$~) {
2696
        $suffix = lc($1);
2697
        $result = "000.000.000.000$suffix";
2698
        $isaWIP = 1;
2699
    }
2700
 
2701
    #
2702
    #   Also ERGOFSSLS190100_015
2703
    #   Don't flag as a ripple
2704
    elsif ($version =~ m~^ERG[A-Z]+(\d\d)(\d\d)(\d\d)[-_](\d+)(\.\w+)?$~) {
2705
        $major = $1;
2706
        $minor = $2;
2707
        $patch = $3;
2708
        $build = $4;
2709
        $suffix = $5 || '.sls';
2710
        $isaRipple = 0;
2711
    }
2712
 
2713
    #
2714
    #   Stuff we don't yet handle
2715
    #
2716
    else  {
2717
        Warning ("Unknown version number: $name,$version");
2718
        $version =~ m~(\.\w+)$~;
2719
        $suffix = $1 || '';
2720
        $result = $version;
2721
    }
2722
 
2723
    $isaRipple = ($build > 0) unless defined $isaRipple;
2724
    unless ( $result )
2725
    {
2726
        # Major and minor of 99.99 are normally funy versions
2727
        # Don't make important desicions on them
2728
        #
2729
        if ( $major == 99 && $minor == 99 )
2730
        {
2731
            $major = 0;
2732
            $minor = 0;
2733
            $patch = 0;
2734
        }
2735
 
2736
        $result = sprintf("%3.3d.%3.3d.%3.3d.%3.3d%s", $major,$minor,$patch,$build,$suffix || '.0000');
2737
        $buildVersion = [ $major, $minor, $patch, $build ];
2738
    }
2739
 
2740
    $suffix = lc( $suffix );
2741
    if ( exists $suffixFixup{$suffix} )
2742
    {
2743
        $suffix = $suffixFixup{$suffix} ;
2744
    }
2745
 
2746
    return ($suffix, $result, $isaRipple, $isaWIP, $buildVersion );
2747
}
2748
 
2749
#-------------------------------------------------------------------------------
395 dpurdie 2750
# Function        : vcsCleanup
2751
#
2752
# Description     : Cleanup and rewrite a vcstag
2753
#
2754
#                   DUPLICATED IN:
2755
#                       - cc2svn_procdata
2756
#                       - cc2svn_importpackage
2757
#
2758
# Inputs          : vcstag
2759
#
2760
# Returns         : Cleaned up vcs tag
2761
#
2762
sub vcsCleanup
2763
{
2764
    my ($tag) = @_;
2765
    $tag =~ tr~\\/~/~;
2766
    if ( $tag =~ m~^CC::~ )
2767
    {
2768
        $tag =~ s~CC::\s+~CC::~;
2769
        $tag =~ s~MASS_Dev_Bus/Cbp/~MASS_Dev_Bus/CBP/~i;
2770
        $tag =~ s~MASS_Dev_Bus~MASS_Dev_Bus~i;
2771
        $tag =~ s~/MASS_Dev/Infra~MASS_Dev_Infra~i;
2772
        $tag =~ s~/MASS_Dev/Bus/web~/MASS_Dev_Bus/web~i;
2773
 
2774
        $tag =~ s~/Vastraffik/~/Vasttrafik/~;
2775
        $tag =~ s~/MREF_Package/ergpostmongui$~/MREF_Package/ergpostmongui~i;
2776
        $tag =~ s~DPC_SWCode/~DPG_SWCode/~i;
2777
    }
2778
    return $tag;
2779
}
2780
 
2781
#-------------------------------------------------------------------------------
392 dpurdie 2782
# Function        : examineVcsTag
2783
#
2784
# Description     : Examine a VCS Tag and determine if it looks like rubbish
395 dpurdie 2785
#                   Give it a clean
392 dpurdie 2786
#
2787
# Inputs          : $entry
2788
#
2789
# Returns         : Will add Data to the $entry
2790
#
2791
sub examineVcsTag
2792
{
2793
    my ($entry) = @_;
2794
    my $bad = 0;
395 dpurdie 2795
 
2796
    $versions{$entry}{vcsTag} = vcsCleanup($versions{$entry}{vcsTag});
392 dpurdie 2797
    my $vcstag = $versions{$entry}{vcsTag};
395 dpurdie 2798
 
392 dpurdie 2799
    if ( $vcstag =~ m~^SVN::~ ) {
2800
        $versions{$entry}{isSvn} = 1;
2801
 
2802
    } elsif ( $vcstag =~ m~^CC::(.*?)(::(.+))?$~ ) {
2803
        my $path = $1  || '';
2804
        my $label = $2 || '';
2805
        $bad = 1 unless ( $label );
2806
        $bad = 1 if ( $label =~ m~^N/A$~i || $label  =~ m~^na$~i );
2807
 
2808
        $bad = 1 unless ( $path );
2809
        $bad = 1 if ( $path =~ m~^N/A$~i || $path  =~ m~^na$~i );
2810
        $bad = 1 if ( $path =~ m~^/dpkg_archive~ || $path  =~ m~^dpkg_archive~ );
2811
        $bad = 1 if ( $path =~ m~^/devl/~ || $path  =~ m~^devl/~ );
395 dpurdie 2812
        $bad = 1 if ( $path =~ m~^CVS~ );
392 dpurdie 2813
        $bad = 1 if ( $path =~ m~^http:~i );
2814
        $bad = 1 if ( $path =~ m~^[A-Za-z]\:~ );
2815
        $bad = 1 if ( $path =~ m~^//~ );
395 dpurdie 2816
        $bad = 1 if ( $path =~ m~^/*none~i );
2817
        $bad = 1 if ( $path =~ m~^/*NoWhere~i );
2818
        $bad = 1 if ( $path =~ m~^-$~i );
392 dpurdie 2819
        $bad = 1 if ( $path =~ m~^cvsserver:~ );
2820
        $bad = 1 if ( $path =~ m~,\s*module:~ );
2821
#        $bad = 1 unless ( $path =~ m~^/~ );
2822
    }
2823
    else
2824
    {
2825
        $bad = 1;
2826
    }
2827
    $versions{$entry}{badVcsTag} = 1 if ( $bad );
2828
}
2829
 
2830
#-------------------------------------------------------------------------------
2831
# Function        : logToFile
2832
#
2833
# Description     : Log some data to a named file
2834
#                   Use file locking to allow multiple process to log
2835
#
2836
# Inputs          : $filename           - Name of file to log
2837
#                   ...                 - Data to log
2838
#
2839
# Returns         : Nothing
2840
#
2841
sub logToFile
2842
{
2843
    my ($file, @data) = @_;
2844
 
2845
    open  (LOGFILE, '>>', $file);
2846
    flock (LOGFILE, LOCK_EX);
2847
    print  LOGFILE "@data\n";
2848
    flock (LOGFILE, LOCK_UN);
2849
    close (LOGFILE);
2850
}
2851
 
2852
#-------------------------------------------------------------------------------
2853
# Function        : createImages
2854
#
2855
# Description     : Create nice images of the RM version tree
2856
#
2857
# Inputs          : 
2858
#
2859
# Returns         : 
2860
#
2861
sub createImages
2862
{
2863
 
2864
    my $filebase = "${packageNames}";
2865
    open (FH, '>', "$filebase.dot" ) or die "Cannot open output";
2866
    print FH "digraph \"${packageNames}\" {\n";
2867
    #print FH "rankdir=LR;\n";
2868
    print FH "node[fontsize=16];\n";
2869
    print FH "node[target=_graphviz];\n";
2870
#    print FH "subgraph cluster_A {\n";
2871
#    print FH "node[fontsize=12];\n";
2872
 
2873
    {
2874
        my @text;
2875
        push @text, $packageNames;
2876
        push @text, 'HyperLinked to Release Manager';
2877
        push @text, 'Created:' . localtime();
2878
        push @text, '|';
2879
 
2880
        push @text, 'Total RM versions: ' . $totalVersions;
2881
        push @text, 'Essential Entries: ' . scalar @EssentialPackages;
2882
        push @text, 'Initial trees: ' . $initialTrees;
2883
 
1270 dpurdie 2884
        push @text, 'Number of Entries: ' . $processTotal;
392 dpurdie 2885
        push @text, 'Type : ' . $packageType;
2886
        push @text, 'All versions in Subversion' if ( $allSvn );
2887
 
2888
        push @text, '|';
2889
        push @text, 'Total Project Branches: ' . $ProjectCount;
2890
        foreach ( sort keys %knownProjects )
2891
        {
2892
            my $count = $knownProjects{$_}{count} || 0;
2893
            if ( $count )
2894
            {
2895
                my $text = 'Project Branch: ' . $_;
2896
                $text .= " (" . $count . ")" if ( $count > 1 );
2897
                push @text, $text;
2898
            }
2899
        }
2900
 
2901
        push @text, '|';
2902
        push @text, 'Bad VCS : ' . $badVcsCount;
2903
        push @text, 'Bad Singletions : ' . $badSingletonCount;
2904
        push @text, 'Deadwood entries : ' . $trimCount;
2905
        push @text, 'Walking Mode : Flat' if ($opt_flat);
2906
        push @text, 'Pruned Mode : ' . $pruneModeString;
2907
        push @text, 'Pruned entries : ' . $pruneCount;
1272 dpurdie 2908
        push @text, 'Recent entries : ' . $recentCount;
392 dpurdie 2909
 
2910
        if ( @unknownProjects )
2911
        {
2912
            push @text, '|';
2913
            push @text, 'Unknown Projects';
2914
            push @text, 'Unknown Project: ' . $_ foreach (sort @unknownProjects );
2915
        }
2916
 
2917
        #
2918
        #   Multiple Paths
2919
        #
2920
        if ( scalar @multiplePaths > 1 )
2921
        {
2922
            push @text, '|';
2923
            push @text, 'Multiple Paths';
2924
            push @text, @multiplePaths;
2925
        }
2926
 
2927
        #
2928
        #   Bad essentials
2929
        #
2930
        if ( @badEssentials  )
2931
        {
2932
            push @text, '|';
2933
            push @text, 'Bad Essential Versions';
2934
            push @text, GetVname($_) foreach ( @badEssentials );
2935
        }
2936
 
2937
        #
2938
        #   Subversion Data
2939
        #
2940
        if ( %svnData )
2941
        {
2942
            push @text, '|';
2943
            push @text, 'Subversion';
2944
            push @text, 'Trunk used' if exists $svnData{branches}{trunk} ;
2945
            push @text, 'Labels: ' . scalar keys %{$svnData{tags}} ;
2946
            push @text, 'Branches: ' . scalar keys %{$svnData{branches}} ;
2947
        }
2948
 
2949
        push @text, '';
2950
        my $text = join '\l', @text;
2951
        $text =~ s~\|\\l~|~g;
2952
 
2953
        my @attributes;
2954
        push @attributes, "shape=record";
2955
        push @attributes, "label=\"{$text}\"";
2956
        push @attributes, "tooltip=\"$packageNames\"";
1272 dpurdie 2957
        push (@attributes, "URL=\"" . $GBE_RM_URL . "/view_by_version.asp?pkg_id=$first_pkg_id" . "\"" )if $first_pkg_id;
392 dpurdie 2958
        push @attributes, "color=red";
2959
        my $attr = join( ' ', @attributes);
2960
 
2961
        my $tld_done = 'TitleBlock';
2962
        print FH "$tld_done [$attr]\n";
2963
    }
2964
 
2965
    #
2966
    #   Generate Legend
2967
    #
2968
    {
2969
        my @text;
2970
        push @text, 'Legend';
2971
        push @text, '|';
2972
        push @text, 'Node Content';
2973
        push @text, 'Package Version';
2974
#        push @text, 'Release Manager Ref (pvid)';
2975
        push @text, 'Creation Date: yyyy-mm-dd';
2976
        push @text, '(Coded information)';
2977
        push @text, '|{Code';
2978
        push @text, '|{N: Not Locked';
2979
        push @text, 'b: Bad Singleton';
2980
        push @text, 'B: Bad VCS Tag';
2981
        push @text, 'D: DeadWood';
1272 dpurdie 2982
        push @text, 'E: Essential Release Version';
392 dpurdie 2983
        push @text, 'G: Glued into Version Tree';
1272 dpurdie 2984
        push @text, 'r: Recent version';
1328 dpurdie 2985
        push @text, 'R: Ripple';
392 dpurdie 2986
        push @text, 'S: Splitpoint';
2987
        push @text, 't: Glued into Project Tree';
2988
        push @text, 'T: Tip version';
2989
        push @text, 'V: In SVN';
2990
        push @text, '+: In Subversion';
2991
        push @text, '}}';
2992
 
2993
        push @text, '|';
2994
        push @text, 'Outline';
2995
        push @text, 'Red: Dead or Bad VCS Tag';
2996
        push @text, 'Orange: Project Branch Root';
2997
        push @text, 'Green: Ripple Build Version';
2998
        push @text, 'Blue: Essential Version';
2999
        push @text, 'Darkmagenta: Entry Glued into tree';
3000
        push @text, 'Magenta: Entry added to project tree';
3001
 
3002
 
3003
        push @text, '|';
3004
        push @text, 'Fill';
3005
        push @text, 'PowderBlue: Essential Version';
3006
        push @text, 'Red: Bad Essential Version';
3007
        push @text, 'Light Green: Migrated to SVN';
3008
#        push @text, 'Red: Entry Glued into tree';
3009
#        push @text, 'Green: Entry added to project tree';
3010
 
3011
        push @text, '|';
3012
        push @text, 'Shape';
3013
        push @text, 'Oval: Normal Package Version';
3014
        push @text, 'Invhouse: Project Branch Root';
3015
        push @text, 'Octagon: Branch Point';
3016
        push @text, 'Box: Bad Single version with no history';
3017
        push @text, 'Doublecircle: Tip of a Project Branch';
3018
 
3019
        push @text, '';
3020
        my $text = join '\l', @text;
3021
        $text =~ s~\|\\l~|~g;
3022
        $text =~ s~\}\\l~}~g;
3023
 
3024
        my @attributes;
3025
        push @attributes, "shape=record";
3026
        push @attributes, "label=\"{$text}\"";
3027
        push @attributes, "color=red";
3028
        my $attr = join( ' ', @attributes);
3029
 
3030
        my $tld_done = 'LegendBlock';
3031
        print FH "$tld_done [$attr]\n";
3032
    }
3033
 
3034
#    print FH "\n}\n";
3035
    print FH "TitleBlock -> LegendBlock [style=invis]\n";
3036
 
3037
    sub genLabelText
3038
    {
3039
        my ($entry) = @_;
3040
        my @label;
3041
        push @label, $versions{$entry}{name} if ( $multiPackages );
3042
        push @label, $versions{$entry}{vname};
3043
#        push @label, $entry;       # Add PVID
3044
        push @label, substr( $versions{$entry}{created}, 0, 10); #  2008-02-19
3045
#        push @label, 'V=' . $versions{$entry}{maxVersion};
3046
#        push @label, 'B=' . $versions{$entry}{svnBranchTip} if ( exists $versions{$entry}{svnBranchTip} );
3047
 
3048
 
3049
        my $stateText = '';
3050
        $stateText .= 'N' if ($versions{$entry}{locked} eq 'N');
3051
        $stateText .= 'b' if (exists $versions{$entry}{badSingleton});
3052
        $stateText .= 'B' if (exists $versions{$entry}{badVcsTag});
3053
        $stateText .= 'G' if (exists $versions{$entry}{GluedIn});
3054
        $stateText .= 't' if (exists $versions{$entry}{MakeTree});
3055
        $stateText .= 'E' if (exists $versions{$entry}{Essential});
3056
        $stateText .= 'D' if (exists $versions{$entry}{DeadWood});
1328 dpurdie 3057
        $stateText .= 'R' if ( $versions{$entry}{isaRipple} );
1272 dpurdie 3058
        $stateText .= 'r' if (exists $versions{$entry}{keepRecent} && $versions{$entry}{keepRecent} );
392 dpurdie 3059
        $stateText .= 'S' if (exists $versions{$entry}{EssentialSplitPoint} && $versions{$entry}{EssentialSplitPoint} > 1 );
3060
        $stateText .= 'T' if (exists $versions{$entry}{Tip} );
3061
        $stateText .= 'V' if (exists $versions{$entry}{isSvn} );
3062
        $stateText .= '+' if (exists $versions{$entry}{svnVersion} );
3063
#        $stateText .= 's' if (exists $versions{$entry}{branchPoint} );
3064
#        $stateText .= ' T='. $versions{$entry}{threadId} if (exists $versions{$entry}{threadId});
3065
#        $stateText .= ' EssentalPath' if (exists $versions{$entry}{EssentialPath});
3066
#        $stateText .= ' Count='. $versions{$entry}{EssentialSplitPoint} if (exists $versions{$entry}{EssentialSplitPoint});
3067
#        $stateText .= ' M='. $versions{$entry}{maxVersion} if (exists $versions{$entry}{maxVersion});
3068
 
3069
        push @label, "(${stateText})" if ( $stateText );
3070
 
1341 dpurdie 3071
##       Insert Release Names
3072
#        foreach my $rname ( keys %{$versions{$entry}{Releases}}  ) {
3073
#            push @label, "Release: $versions{$entry}{Releases}{$rname}";
3074
#        }
3075
 
392 dpurdie 3076
        return join ('\n', @label );
3077
    }
3078
 
3079
    sub genAttributes
3080
    {
3081
        my ($entry) = @_;
3082
        my @attributes;
3083
        push @attributes, 'label="' . genLabelText($entry) . '"';
3084
        push @attributes, 'URL="' . dotUrl($entry) . '"';
3085
        push @attributes, 'tooltip="' . "Goto: $versions{$entry}{vname}, PVID=$entry" ,'"';
3086
        my $shape;
3087
            $shape = 'box' if ($versions{$entry}{badSingleton});
3088
            $shape = 'octagon'  if ($versions{$entry}{branchPoint});
3089
            $shape = 'invhouse' if ($versions{$entry}{newSuffix});
3090
            $shape = 'doublecircle' if ($versions{$entry}{Tip});
3091
 
3092
 
3093
        push @attributes, 'shape=' . $shape if ( $shape );
3094
 
3095
        my $color;
3096
        my $fill;
3097
           $color = 'color=green style=bold' if ( $versions{$entry}{isaRipple} );
3098
           $color = 'color=orange style=bold' if ( $versions{$entry}{newSuffix} );
3099
           $color = 'color=red style=bold' if ( $versions{$entry}{DeadWood} || $versions{$entry}{badVcsTag} );
3100
           $color = 'color=blue style=bold' if ( $versions{$entry}{Essential} );
3101
           $color = 'color=darkmagenta style=bold' if ( $versions{$entry}{GluedIn} );
3102
           $color = 'color=magenta style=bold' if ( $versions{$entry}{MakeTree} );
3103
 
3104
           $fill = 'style=filled fillcolor=powderblue' if ( $versions{$entry}{Essential} );
3105
           $fill = 'style=filled fillcolor=red' if ( $versions{$entry}{Essential} && $versions{$entry}{badVcsTag} );
3106
           $fill = 'style=filled fillcolor="#99FF99"' if ( exists $versions{$entry}{svnVersion} );
3107
 
3108
 
3109
        push @attributes, $color if ( $color );
3110
        push @attributes, $fill if ( $fill );
3111
 
3112
        return '[' . join( ' ', @attributes) . ']';
3113
    }
3114
 
3115
    sub genArrowAttributes
3116
    {
3117
        my ($not_first, $entry) = @_;
3118
        my @attributes;
3119
 
3120
        push @attributes, 'arrowhead=empty' if ( $not_first );
3121
        push ( @attributes, 'label="' . $versions{$entry}{svnBranchTip} .'"' ) if ( exists $versions{$entry}{svnBranchTip} );
3122
 
3123
        return ('[' . join( ' ', @attributes) . ']') if ( @attributes ) ;
3124
        return '';
3125
    }
3126
 
3127
    #
3128
    #   Flat
3129
    #
3130
    if ( $opt_flat )
3131
    {
3132
        my $last = 0;
3133
        foreach my $entry (@flatOrder )
3134
        {
3135
            if ( $last )
3136
            {
3137
                my $me = dotTag($last);
3138
 
3139
                print FH pentry($me)  ,' -> ', pentry(dotTag($entry)), genArrowAttributes(0, $entry) ,";\n";
3140
                print FH pentry($me)  ,genAttributes($last) . ";\n";
3141
            }
3142
            $last = $entry;
3143
        }
3144
        print FH pentry(dotTag($last))  ,genAttributes($last) . ";\n";
3145
 
3146
    }
3147
    else
3148
    {
3149
        foreach my $entry ( sort {$a <=> $b} keys(%versions) )
3150
        {
3151
            my $me = dotTag($entry);
3152
            my @versions = @{ $versions{$entry}{next}};
3153
            my $ii = 0;
3154
            foreach ( @versions )
3155
            {
3156
                print FH pentry($me)  ," -> ",pentry(dotTag($_)), genArrowAttributes($ii++, $_), ";\n";
3157
            }
3158
 
3159
            print FH pentry($me)  ,genAttributes($entry) . ";\n";
3160
        }
3161
    }
3162
 
3163
    print FH "\n};\n";
3164
    close FH;
3165
 
3166
    #
3167
    #   Convert DOT to a SVG
3168
    #
3169
    unless ( $UNIX )
3170
    {
3171
    print "Generating graphical images\n";
3172
#    system( "dot $filebase.dot -Tjpg -o$filebase.jpg" );  # -v
3173
    system( "dot $filebase.dot -Tsvg -o$filebase.svg" );  # -v
3174
#    unlink("$filebase.dot");
3175
 
3176
    #
3177
    #   Display a list of terminal packages
3178
    #   These are packages that are not used by any other package
3179
    #
3180
    print "\n";
3181
#    print "Generated: $filebase.dot\n";
3182
#    print "Generated: $filebase.jpg\n";
3183
    print "Generated: $filebase.svg\n";
3184
    }
3185
    else
3186
    {
3187
        print "Generated: $filebase.dot\n";
3188
    }
3189
}
3190
 
3191
sub dotTag
3192
{
3193
    my ($entry) = @_;
3194
 
3195
    my $label = '';
3196
    $label .= $versions{$entry}{name} if $multiPackages;
3197
    $label .= $versions{$entry}{vname};
3198
    $label =~ s~[-() ]~_~g;
3199
    return $label;
3200
}
3201
 
3202
sub dotUrl
3203
{
3204
    my ($entry) = @_;
3205
 
3206
    my $pv_base = $GBE_RM_URL . "/fixed_issues.asp?pv_id=$entry";
3207
}
3208
 
3209
#-------------------------------------------------------------------------------
3210
# Function        : pentry
3211
#
3212
# Description     : Generate an entry list as text
3213
#                   Replace "." with "_" since DOT doesn't like .'s
3214
#                   Seperate the arguments
3215
#
3216
# Inputs          : @_          - An array of entries to process
3217
#
3218
# Returns         : A string
3219
#
3220
sub pentry
3221
{
3222
    my ($data) = @_;
3223
    $data =~ s~\.~_~g;
3224
    $result = '"' . $data . '"' ;
3225
    return $result;
3226
}
3227
 
3228
#-------------------------------------------------------------------------------
3229
# Function        : getVobMapping
3230
#
3231
# Description     : Read in Package to Repository Mapping
3232
#
3233
# Inputs          : 
3234
#
3235
# Returns         : Populates %VobMapping
3236
#                             Mapping of PackageName to RepoName[/Subdir]
3237
#
3238
our %ScmRepoMap;
3239
sub getVobMapping
3240
{
3241
    Message ("Read in Vob Mapping");
3242
 
3243
    my $fname = 'cc2svn.repo.dat';
3244
    Error "Cannot locate $fname" unless ( -f $fname );
3245
    require $fname;
3246
 
3247
    Error "Data in $fname is not valid\n"
3248
        unless ( keys(%ScmRepoMap) >= 0 );
3249
 
3250
    $opt_vobMap = $ScmRepoMap{$packageNames}{repo}
3251
        if (exists $ScmRepoMap{$packageNames});
3252
 
3253
    #
3254
    #   Free the memory
3255
    #
3256
    %ScmRepoMap = ();
3257
 
3258
    #
3259
    #   Calculate Target Repo
3260
    #
3261
    Warning ("No VOB Mapping found")
3262
        unless ($opt_vobMap);
3263
    Error("No repository specified. ie -repo=DevTools or -repo=COTS")
3264
        unless ( $opt_repo || $opt_vobMap );
3265
 
3266
    my $r1 = ($opt_repo || '') . '/' . ($opt_vobMap || '');
3267
    $r1 =~ s~^/~~;
3268
    $r1 =~ s~/$~~;
3269
    $svnRepo = $opt_repo_base . $r1;
3270
 
3271
    Verbose( "Repo URL: $svnRepo");
3272
}
3273
 
3274
 
3275
#-------------------------------------------------------------------------------
3276
# Function        : getEssenialPackageVersions
3277
#
3278
# Description     : Determine the 'Essental' Package Versions
3279
#                   Read the data in from an external file
3280
#
3281
# Inputs          : 
3282
#
3283
# Returns         : Populates @EssentialPackages
3284
#
3285
 
3286
our %ScmReleases;
3287
our %ScmPackages;
3288
our %ScmSuffixes;
3289
sub getEssenialPackageVersions
3290
{
3291
    Message ("Read in Essential Package Versions");
3292
 
3293
    my $fname = 'cc2svn.raw.txt';
3294
    Error "Cannot locate $fname" unless ( -f $fname );
3295
    require $fname;
3296
 
3297
    Error "Data in $fname is not valid\n"
3298
        unless ( keys(%ScmReleases) >= 0 );
3299
 
3300
#    DebugDumpData("ScmReleases", \%ScmReleases );
3301
#    DebugDumpData("ScmPackages", \%ScmPackages );
3302
#    DebugDumpData("ScmSuffixes", \%ScmSuffixes );
3303
 
3304
    #
3305
    #   Create a list of essential packages
3306
    #   Retain packages-versions used in this program
3307
    #
3308
    foreach ( keys %ScmPackages )
3309
    {
3310
        next unless ( exists  $pkg_ids{ $ScmPackages{$_}{pkgid} } );
3311
        push @EssentialPackages, $_;
3312
        Error ("Essential Package Version not in extracted Release Manager Data: $_")
3313
            unless ( exists $versions{$_} );
3314
        $versions{$_}{Essential} = 1;
1341 dpurdie 3315
 
3316
        # Retain which RM Release this package-version is the tip
3317
        # Release of
3318
        foreach my $release ( @{$ScmPackages{$_}{'release'}} )
3319
        {
3320
            $versions{$_}{Releases}{$release} = $ScmReleases{$release}{name};
3321
        }
3322
 
392 dpurdie 3323
        #print "ESSENTIAL: $versions{$_}{name} $versions{$_}{vname}\n";
3324
    }
3325
 
1341 dpurdie 3326
 
392 dpurdie 3327
    #
3328
    #   Free memory
3329
    #
3330
    %ScmReleases = ();
3331
    %ScmPackages = ();
3332
    %ScmSuffixes = ();
3333
 
3334
#    DebugDumpData("Essential", \@EssentialPackages );
3335
    Message ("Essential Versions: " . scalar @EssentialPackages );
3336
}
3337
 
3338
#-------------------------------------------------------------------------------
3339
# Function        : ReportPathVariance
3340
#
3341
# Description     : Report variance in paths used by the versions
3342
#
3343
# Inputs          : 
3344
#
3345
# Returns         : 
3346
#
3347
my %VobPaths;
3348
sub ReportPathVariance
3349
{
3350
    Message ("Detect Multiple Paths");
3351
    foreach my $entry ( keys(%versions) )
3352
    {
3353
        my $e = $versions{$entry};
3354
        next if ( isSet ($e, 'DeadWood' ) );
3355
        next if ( isSet ($e, 'badVcsTag') );
3356
        next if ( isSet ($e, 'isSvn') );
3357
        my $tag = $e->{vcsTag};
3358
        next unless ( $tag );
3359
 
3360
        $tag =~ m~^(.+?)::(.*?)(::(.+))?$~;
3361
        my $vcsType = $1;
3362
        my $cc_label = $4;
3363
        my $cc_path = $2;
3364
        $cc_path = '/' . $cc_path;
3365
        $cc_path =~ tr~\\/~/~s;
3366
 
3367
        $VobPaths{$cc_path}++;
3368
    }
3369
 
3370
    @multiplePaths = sort keys %VobPaths;
3371
    if ( scalar @multiplePaths > 1 )
3372
    {
3373
        Warning ("Multiple Paths:" . $_ ) foreach (@multiplePaths);
3374
    }
3375
}
3376
 
3377
sub isSet
3378
{
3379
    my ($base, $element) = @_;
3380
    return 0 unless ( exists $base->{$element} );
3381
    return $base->{$element};
3382
}
3383
 
3384
 
3385
#-------------------------------------------------------------------------------
3386
# Function        : recurseList
3387
#
3388
# Description     : Return a list of all element below a given head element
3389
#
3390
# Inputs          : $head               - Head element
3391
#
3392
# Returns         : A list, not in any particular order
3393
#
3394
 
3395
our @recurseList;
3396
sub recurseList
3397
{
3398
    @recurseList = ();
3399
    recurseListBody (@_);
3400
    return @recurseList;
3401
}
3402
sub recurseListBody
3403
{
3404
    foreach my $entry ( @_ )
3405
    {
3406
        push @recurseList, $entry;
3407
no warnings "recursion";
3408
        recurseListBody (@{$versions{$entry}{next}});
3409
    }
3410
}
3411
 
3412
#-------------------------------------------------------------------------------
3413
# Function        : getSvnData
3414
#
3415
# Description     : Read the SVN tree and see what we have
3416
#
3417
# Inputs          : 
3418
#
3419
# Returns         : 
3420
#
3421
my @svnDataItems;
3422
sub getSvnData
3423
{
3424
    Message ("Examine Subversion Tree");
3425
 
3426
    #
3427
    #   Re-init data
3428
    #
3429
    @svnDataItems = ();
3430
    %svnData = ();
3431
 
3432
    #
3433
    #   Create an SVN session
3434
    #
3435
    return unless ( $svnRepo );
3436
    my $svn = NewSessionByUrl ( "$svnRepo/$packageNames" );
3437
    return unless ( $svn );
3438
 
3439
    #
3440
    #   extract data
3441
    #
3442
#    DebugDumpData("SVN", $svn );
3443
    $svn->SvnCmd ( 'log', '-v', '--xml', '--stop-on-copy', $svn->Full()
3444
                    , { 'credentials' => 1,
3445
                        'process' => \&ProcessSvnLog,
3446
                         }
3447
                        );
3448
 
3449
    #
3450
    #   Process data
3451
    foreach my $entry ( @svnDataItems )
3452
    {
3453
        my $name;
3454
        my $isaBranch;
3455
        my $target = $entry->{target};
3456
        if ( $target =~ m~/tags/(.*)~ ) {
3457
            $name = $1;
3458
            $svnData{tags}{$name} = 1;
3459
        } elsif ( $target =~ m~/branches/(.*)~ )  {
3460
            $name = $1;
3461
    #        $branches{$1} = 1;
3462
        } else {
3463
            $svnData{nonTag}{$target} = 1;
3464
        }
3465
 
3466
        my $fromBranch;
3467
        if ( $entry->{fromPath} =~ m~/trunk$~  ) {
3468
            $fromBranch = 'trunk';
3469
        } elsif ( $entry->{fromPath} =~ m~/branches/(.*)~ ) {
3470
            $fromBranch = $1;
3471
        }
3472
 
1341 dpurdie 3473
        # largest Rev number on branch
3474
        if ( exists $svnData{max}{$fromBranch} )
392 dpurdie 3475
        {
1341 dpurdie 3476
            if ( $svnData{max}{$fromBranch}{rev} <  $entry->{fromRev} )
392 dpurdie 3477
            {
1341 dpurdie 3478
                $svnData{max}{$fromBranch}{rev} =  $entry->{fromRev};
3479
                $svnData{max}{$fromBranch}{name} = $name;
392 dpurdie 3480
            }
3481
        }
1341 dpurdie 3482
        else
3483
        {
3484
            $svnData{max}{$fromBranch}{rev} =  $entry->{fromRev};
3485
            $svnData{max}{$fromBranch}{name} = $name;
3486
        }
392 dpurdie 3487
    }
1341 dpurdie 3488
 
3489
    foreach my $branch ( keys %{$svnData{max}} )
3490
    {
3491
        $svnData{tips}{$svnData{max}{$branch}{name}} = $branch;
3492
    }
3493
#    DebugDumpData("svnDataItems", \@svnDataItems);
392 dpurdie 3494
#    DebugDumpData("SvnData", \%svnData);
3495
 
1341 dpurdie 3496
 
392 dpurdie 3497
    foreach my $entry ( keys(%versions) )
3498
    {
3499
        my $import_label = saneLabel($entry);
3500
        delete $versions{$entry}{svnVersion};
3501
        delete $versions{$entry}{svnBranchTip};
3502
 
3503
        if ( exists $svnData{tags}{$import_label} )
3504
        {
3505
            $versions{$entry}{svnVersion} = 1;
3506
        }
3507
 
3508
        if ( exists $svnData{tips}{$import_label} )
3509
        {
3510
            $versions{$entry}{svnBranchTip} = $svnData{tips}{$import_label};
3511
        }
3512
    }
3513
 
1341 dpurdie 3514
    Message ( 'Trunk used: ' . (exists $svnData{'max'}{trunk} ? 'Yes' : 'No') );
392 dpurdie 3515
    Message ( 'Labels    : ' . scalar keys %{$svnData{tags}} );
1341 dpurdie 3516
    Message ( 'Branches  : ' . scalar keys %{$svnData{'max'}} );
392 dpurdie 3517
}
3518
 
3519
#-------------------------------------------------------------------------------
3520
# Function        : ProcessSvnLog
3521
#
3522
# Description     :
3523
#                   Parse
3524
#                       <logentry
3525
#                          revision="24272">
3526
#                       <author>bivey</author>
3527
#                       <date>2005-07-25T15:45:35.000000Z</date>
3528
#                       <paths>
3529
#                       <path
3530
#                          prop-mods="false"
3531
#                          text-mods="false"
3532
#                          kind="dir"
3533
#                          copyfrom-path="/enqdef/branches/Stockholm"
3534
#                          copyfrom-rev="24271"
3535
#                          action="A">/enqdef/tags/enqdef_24.0.1.sls</path>
3536
#                       </paths>
3537
#                       <msg>COTS/enqdef: Tagged by Jats Svn Import</msg>
3538
#                       </logentry>
3539
#
3540
# Inputs          : 
3541
#
3542
# Returns         : 
3543
#
3544
my $entryData;
3545
sub  ProcessSvnLog
3546
{
3547
    my ($self, $line ) = @_;
3548
#print "----- $line\n";
3549
    if ( $line =~ m~^<logentry~ ) {
3550
        $entryData = ();
3551
 
3552
    } elsif ( $line =~ m~^\s+revision="(\d+)"~ ) {
3553
        $entryData->{Rev} = $1;
3554
 
3555
    } elsif ( $line =~ m~^\s+copyfrom-path="(.*)"~ ) {
3556
        $entryData->{fromPath} = $1;
3557
 
3558
    } elsif ( $line =~ m~^\s+copyfrom-rev="(\d+)"~ ) {
3559
        $entryData->{fromRev} = $1;
3560
 
3561
    } elsif ( $line =~ m~\s+action=.*?>(.*)</path~ ) {
3562
        $entryData->{target} = $1;
3563
 
3564
    } elsif ( $line =~ m~</logentry~ ) {
3565
        if ( exists $entryData->{fromPath} )
3566
        {
3567
#            DebugDumpData("Data", $entryData);
3568
            push @svnDataItems, $entryData;
3569
        }
3570
    }
3571
 
3572
    #
3573
    #   Return 0 to keep on going
3574
    return 0;
3575
}
3576
 
1328 dpurdie 3577
#-------------------------------------------------------------------------------
392 dpurdie 3578
# Function        : saveData
3579
#
3580
# Description     : Save essential data
3581
#
3582
# Inputs          : 
3583
#
3584
# Returns         : 
3585
#
3586
sub saveData
3587
{
3588
    my $file = $cwd . "/${packageNames}.data";
3589
 
3590
    Message ("Create: $file");
3591
    my $fh = ConfigurationFile::New( $file );
3592
 
3593
    $fh->DumpData(
3594
        "\n# ScmVersions.\n#\n",
3595
        "ScmVersions", \%versions );
3596
 
3597
    #
3598
    #   Close out the file
3599
    #
3600
    $fh->Close();
3601
}
3602
 
3603
 
3604
#-------------------------------------------------------------------------------
3605
#   Documentation
3606
#
3607
 
3608
=pod
3609
 
3610
=for htmltoc    SYSUTIL::cc2svn::
3611
 
3612
=head1 NAME
3613
 
3614
cc2svn_gendata - CC2SVN tool to import an entire package into SVN
3615
 
3616
=head1 SYNOPSIS
3617
 
3618
  jats cc2svn_importpackage [options] package_name
3619
 
3620
 Options:
3621
    -help              - brief help message
3622
    -help -help        - Detailed help message
3623
    -man               - Full documentation
3624
    -repository=name   - Specify target repository
3625
    -[no]flat          - Do not create project tree. Def: -noflat
3626
    -prunemode=mode    - Mode: none, ripple, retain, severe, Def=ripple
3627
    -retain=N          - Specify retain count for pruning. Def=2
3628
    -[no]test          - Do not create packages. Def:-notest
3629
    -[no]reuse         - Keep and reuse ClearCase views
3630
    -age=nnDays        - Only keep recent package
3631
    -dump[=n]          - Dump raw data. N=0,1,2
3632
    -images[=n]        - Create SVG of version tree. N=0,1,2
3633
    -name=aaa          - Alternate output package name. Test Only
3634
    -[no]log           - Write output to log file. Def: -nolog
3635
    -[no]postimage     - Create image after transger: Def: -post
3636
    -workdir=path      - Use for temp storage (def:/work)
1270 dpurdie 3637
    -delete            - Delete SVN package before test
392 dpurdie 3638
 
3639
=head1 OPTIONS
3640
 
3641
=over 8
3642
 
3643
=item B<-help>
3644
 
3645
Print a brief help message and exits.
3646
 
3647
=item B<-help -help>
3648
 
3649
Print a detailed help message with an explanation for each option.
3650
 
3651
=item B<-man>
3652
 
3653
Prints the manual page and exits.
3654
 
3655
=item B<-prunemode=mode>
3656
 
3657
This option control the manner in which excess versions will be pruned. Valid
3658
modes are:
3659
 
3660
=over 8
3661
 
3662
=item   none
3663
 
3664
No pruning will be performed
3665
 
3666
=item   ripple
3667
 
3668
Non-Essential packages that are ripple builds will be removed.
3669
 
3670
=item   retain
3671
 
3672
Versions that preceed an Essential version will be retained.
3673
 
3674
=item   severe
3675
 
3676
Only Essential Versions, and Branching points will be retained.
3677
 
3678
=back
3679
 
3680
=back
3681
 
3682
=head1 DESCRIPTION
3683
 
3684
This program is a tool used in the conversion of ClearCase VOBS to subversion.
3685
It will take a complete package and all relevent versions from ClearCase and
3686
insert them into subversion in a sessible manner. It will attempt to retain
3687
file change order and history.
3688
 
3689
It will:
3690
 
3691
=over 8
3692
 
3693
=item *
3694
 
3695
Read in the Essential Package Version list.
3696
 
3697
=item *
3698
 
3699
Extract, from Release Manager, all known versions of the specified package.
3700
 
3701
=item *
3702
 
3703
It will attempt to determine the type of package: COTS, TOOL, CORE, PROJECT
3704
and alter the processing accordingly.
3705
 
3706
=item *
3707
 
3708
It will create a version dependency tree and determine 'new' project branch
3709
points. It will remove (prune) versions that are excess to requirements.
3710
 
3711
=item *
3712
 
3713
It will extract source from ClearCase and insert it into SVN, creating
3714
branches and tags as it goes.
3715
 
3716
=back
3717
 
3718
The program can also be used to create a SVG image of the version dependency
3719
tree. This does not work on Linux; only Windows with 'dot' installed.
3720
 
3721
=cut
3722