Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6887 dpurdie 1
########################################################################
2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
3
#
4
# Module name   : rmMerge_migrate_package.pl
5
# Module type   : JATS Utility
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Migrate a package-version from the Pulse RM to the one-true RM (old)
10
#
11
# Usage         : See POD at the end of this file
12
#
13
#......................................................................#
14
 
15
require 5.008_002;
16
use strict;
17
use warnings;
18
 
19
use Pod::Usage;
20
use Getopt::Long;
21
use JatsError;
22
use JatsRmApi;
23
use JatsSystem;
24
use FileUtils;
25
use ConfigurationFile;
26
use JatsProperties;
27
use File::Copy;
28
use DBI;
29
my $RM_DB;
30
 
31
 
32
my $oldRMWrite = ['OLD', 'jdbc:oracle:thin:@auawsards001:1521:RELEASEM', 'RELEASE_MANAGER', 'ske2k0se'];
33
my $oldRMCred  = ['OLD', 'jdbc:oracle:thin:@auawsards001:1521:RELEASEM', 'RM_READONLY', 'RM_READONLY'];
34
my $newRMCred  = ['NEW', 'jdbc:oracle:thin:@auawsards002:1521:RELEASEM', 'RM_READONLY', 'Tp8WmmDKMq2Z'];
35
 
36
my $newDpkg = '/net/auawsaarc002/export/devl/dpkg_archive';
37
my $oldDpkg = '/export/devl/dpkg_archive';
38
 
39
my $VERSION = 1.0;
40
my $opt_help=0;
41
my $opt_verbose=0;
42
my $opt_debug=0;
43
my $opt_reuseRm;
44
my $opt_justRm;
45
my $opt_test = 0;
46
my $opt_history;
47
my $opt_nohistory;
48
my $opt_relabel;
49
my $opt_placekeeper;
50
my $opt_autoPlacekeeper;
51
my $opt_base=0;
52
my $opt_after;
53
 
54
my $pname;
55
my $pversion;
56
 
57
my $SplitPvid = 1150630;
58
my $newData;
59
my $oldData;
60
my $mergeData;
61
my $newView;
62
my $tipView;
63
my $newRmData;
64
my %newSvnData;
65
my %oldSvnData;
66
my $isaNewPackage;
67
my $afterPvid;
68
 
69
my $dirOut = 'migrate';
70
my $dirWork = catdir( $dirOut, 'work');
71
my $dirLog  = catdir ($dirOut, 'log');
72
 
73
mkdir ($dirOut);
74
mkdir ($dirWork);
75
mkdir ($dirLog);
76
 
77
 
78
#-------------------------------------------------------------------------------
79
# Function        : Mainline Entry Point
80
#
81
# Description     :
82
#
83
# Inputs          :
84
#
85
my $result = GetOptions (
86
                "help:+"        => \$opt_help,
87
                "manual:3"      => \$opt_help,
88
                "verbose:+"     => \$opt_verbose,
89
                "debug:+"       => \$opt_debug,
90
                "reuseRm!"      => \$opt_reuseRm,
91
                "justRm!"       => \$opt_justRm,
92
                "test:+"        => \$opt_test,
93
                "historyTo:s"   => \$opt_history,
94
                "nohistory"     => \$opt_nohistory,
95
                "relabel!"      => \$opt_relabel,
96
                "placekeeper!"  => \$opt_placekeeper,
97
                "autoplacekeeper!"  => \$opt_autoPlacekeeper,
98
                "base:+"         => \$opt_base,
99
                "after:s"       => \$opt_after,
100
                );
101
 
102
                #
103
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
104
                #
105
 
106
#
107
#   Process help and manual options
108
#
109
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
110
pod2usage(-verbose => 1) if ( $opt_help == 2 );
111
pod2usage(-verbose => 2) if ( $opt_help > 2 );
112
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV != 1 );
113
 
114
#
115
#   Configure the error reporting rmMerge_process now that we have the user options
116
#
117
ErrorConfig( 'name'    =>'MIGRATE',
118
             'verbose' => $opt_verbose,
119
             'debug' => $opt_debug,
120
             'on_exit' => \&cleanup
121
            );
122
 
123
$pname = $ARGV[0];  
124
$pversion = $ARGV[1];
125
InitFileUtils();
126
testPath($newDpkg);
127
testPath($oldDpkg);
128
Message ("Migrating: $pname $pversion");
129
MassageVersion();
130
 
131
#
132
#   Lots of sanity testing
133
#       Make sure it has not already been transferred
134
#       Locate the marked package merge point
135
#
136
$newData = checkVersion($newRMCred);
137
Error ("Package does not exist in Pulse RM ") unless $newData;
138
 
139
$oldData = checkVersion($oldRMCred);
140
Error ("Package already exist in Old RM ") if $oldData && !$opt_reuseRm;
141
Error ("Package does not exist in Old RM ") if ! $oldData && $opt_reuseRm;
142
 
143
if ($opt_after) {
144
    if ($opt_after eq $pversion) {
145
        Warning("Attempt to insert after myself - after option ignored");
146
        $opt_after = undef;
147
    } else {
148
        $afterPvid = checkVersionExists($oldRMCred, $pname, $opt_after);
149
        Error ("Named (after) version does not exist: $opt_after") unless $afterPvid;
150
    }
151
}
152
 
153
checkDependentPackages() unless $opt_placekeeper;
154
findMergePoint();
155
Error("Just testing the dependencies and insertion point") if ($opt_test == 1);
156
 
157
unless ($opt_reuseRm) {
158
    # Serialse the package data
159
    $newRmData = catfile( $dirLog, join('.', $pname, $pversion, 'rminfo', 'txt'));
160
    unlink $newRmData;
161
    Message ("Caputure Rm Data: $newRmData") ;
162
    my $rv = JatsCmd('eprog', 'rmMerge_suck.pl', $pname, $pversion, '-outfile', $newRmData);
163
    Error ("rmMerge_suck - failed") if $rv;
164
    Error ("Extracted data not found - $newRmData") unless -f $newRmData;
165
 
166
    # Insert new package into the old Release Manager
167
    Message("Create package in Release Manager");
168
    my @args;
169
    unless ($isaNewPackage) {
170
        Error("Internal: Merge Version not found") unless ( $mergeData->[3] );
171
        push @args, '-previous', $mergeData->[3];
172
    } else {
173
        push @args, '-newPackage';
174
    }
175
    push (@args ,'-placekeeper') if $opt_placekeeper;
176
    push (@args ,'-placekeeper') if $opt_placekeeper;
177
    push (@args, '-toHistory', $opt_history) if $opt_history;
178
    push (@args, $opt_nohistory ? '-noHistory' : '-history');
179
    $rv = JatsCmd('eprog', 'rmMerge_spit.pl', $pname, $pversion, '-infile', $newRmData, @args );
180
    Error("Package data not inserting into Release Manager") if $rv;
181
 
182
    # Add the package to a 'dummy' Release
183
    addToRelease();
184
}
185
Error("Stopped after RM import") if $opt_justRm;
186
 
187
 
188
# Now migrate the package into Subversion
189
#   Extract the new version - may already have been done
190
svnInsertNewPackage();
191
 
192
# Transfer the dpkg_archive objects
193
#
194
dpkgTransfer();
195
 
196
cleanup();
197
 
198
#-------------------------------------------------------------------------------
199
# Function        : MassageVersion 
200
#
201
# Description     : Massage the provided version number. MAy set it to the base ripple
202
#
203
# Inputs          : 
204
#
205
# Returns         : 
206
#
207
sub MassageVersion
208
{
209
    return unless $opt_base;
210
    if ($pversion =~ m~(.*)(\.\d+)(\d{3})(\.[\w]+)~) {
211
        my ($p1,$p2,$p3,$p4) = ($1,$2,$3,$4);
212
        $p3 = '000';
213
        $p2 = '.0' if $opt_base > 1;
214
        $pversion = $p1 . $p2 . '000' . $p4;
215
        Message ("Base Non Ripple: $pname $pversion");
216
 
217
    } else {
218
        Error ("Cannot massage: $pversion");
219
    }
220
}
221
 
222
#-------------------------------------------------------------------------------
223
# Function        : addToRelease 
224
#
225
# Description     : Add the package to a 'dummy' release
226
#                   The rtagId is 38670 - temp and will be deleted one day
227
#
228
# Inputs          : 
229
#
230
# Returns         : 
231
#
232
sub addToRelease 
233
{
234
    my $user = 3768;            # buildadm
235
    my $rtagId = 38670;         # Known Release 
236
 
237
    #
238
    #   Determine the PVID of the new package
239
    #
240
    $oldData = checkVersion($oldRMCred);
241
    Error("Internal: Can't find added package in Release Manager") unless $oldData;
242
 
243
    Message ("addToRelease");
244
    my $m_sqlstr =  "BEGIN RELEASE_MANAGER.PK_RELEASE.ADD_PACKAGE ( $oldData->[0], 1723, $rtagId, $user );   END;";
245
    getDataFromRm('addToRelease', $m_sqlstr, $oldRMWrite);
246
 
247
    #   Force the Release Icons to be rebuilt
248
    $m_sqlstr =  "BEGIN RELEASE_MANAGER.TOUCH_RELEASE ( $rtagId ); END;";
249
    getDataFromRm('addToRelease', $m_sqlstr, $oldRMWrite);
250
}
251
 
252
#-------------------------------------------------------------------------------
253
# Function        : dpkgTransfer 
254
#
255
# Description     : Transfer the generated package
256
#                   It may not exist 
257
#
258
# Inputs          : 
259
#
260
# Returns         : 
261
#
262
sub dpkgTransfer
263
{
264
    #
265
    #   Ensure the package exists in the 'pulse' dpkg_archive
266
    #
267
    my $srcDir = catdir($newDpkg, $pname, $pversion);
268
    unless (-d $srcDir) {
269
        Warning("Package not found in dpkg_archive", $srcDir);
270
        return;
271
    }
272
 
273
    my $dstDir = catdir ($oldDpkg, $pname);
274
    my $dstPkg = catdir ($oldDpkg, $pname, $pversion);
275
    Message("Source: $srcDir") ;
276
    Message("Target: $dstDir");
277
    Error ("Package already exists: $dstPkg ") if -x $dstPkg;
278
    if ( ! -d $dstDir) {
279
        Message ("Create Package directory: $pname, $dstDir");
280
        mkdir $dstDir;
281
    }
282
    testPath( $dstDir );
283
    System ('--Show','cp', '-a', $srcDir, $dstDir);
284
    Error("Package not copied") unless -d $dstPkg;
285
 
286
    #   Need to set the facl stuff - its not done by the 'cp' command 
287
    System('setfacl', '-R', '-x', 'user:releasem',$dstPkg);
288
    System('setfacl', '-R', '-x', 'user:buildadm',$dstPkg);
289
    System('setfacl', '-R', '-x', 'group:ccperdev',$dstPkg);
290
    System('setfacl', '-R', '-m', 'default:user:buildadm:rwx','-m','default:user::rwx','-m','mask::r-x',$dstPkg);
291
    System('setfacl', '-R', '-m', 'user::r-X,group::r-X,other::r-X',$dstPkg);
292
}
293
 
294
#-------------------------------------------------------------------------------
295
# Function        : testPath 
296
#
297
# Description     : Test that a directory exists
298
#
299
# Inputs          : $path
300
#
301
# Returns         : Returns PATH
302
#                   Will not return on error
303
#
304
sub testPath
305
{
306
    my ($path) = @_;
307
    Debug("Testing: $path");
308
    return $path if -d $path;
309
    Error ("Path not found: $path");
310
}
311
 
312
#-------------------------------------------------------------------------------
313
# Function        : svnInsertNewPackage 
314
#
315
# Description     : Insert the new package version into subversion
316
#
317
# Inputs          : 
318
#
319
# Returns         : 
320
#
321
sub svnInsertNewPackage
322
{
323
    my $rv;
324
    #
325
    #   Get data about the new version of the package
326
    #   The information has already been inserting into the RM database
327
    #
328
    getReleaseData(\%newSvnData, $newRMCred);
329
    getReleaseData(\%oldSvnData, $oldRMCred);
330
#    DebugDumpData("newSvnData", \%newSvnData);
331
#    DebugDumpData("oldSvnData", \%oldSvnData);
332
 
333
    #
334
    #   Extract the 'new' package files into a known area
335
    #   Don't need the .svn info - just the files
336
    #   This may have been done in previous step
337
    #       Locate is stored in $newView
338
    #
339
    checkoutNewPackage();
340
    Error ("Internal. Source package not found: $newView") unless ( $newView  && -d $newView) ;
341
 
342
    #
343
    #   Extract the 'old' package files into a known area
344
    #   Need svn info
345
    #   Need to massage the newVcs to extract the tip of the package in the old system
346
    #       Assume its in the same location
347
    #       Assume we are simply appending the new version to the tip of the old version
348
    #       
349
    my $tipName = join('_', $pname, $pversion, 'tip');
350
    my $tipVcs = $isaNewPackage ? $oldSvnData{VCS_TAG}: $mergeData->[4];
351
    $tipVcs =~ s~::AUPERASVN0X~::AUPERASVN01~;
352
    $tipVcs =~ s~\@.*~~;
353
 
354
    my $svnUrl = $tipVcs;
355
    $svnUrl =~ m~^SVN::(.*)::~;
356
    $svnUrl = $1;
357
 
358
    my $pkgBase = $svnUrl;
359
    $pkgBase =~ s~/[^/]*$~~;
360
    $pkgBase =~ s~/branches$~~;
361
 
362
    $tipView = catdir($dirWork,$tipName );
363
 
364
    #
365
    #   Determine if we need to branch this view
366
    #   Do this before we create the view
367
    #       If branching - then do an extact extract
368
    #       Else - do a tip extract
369
    #
370
    my $mustBranch = 0;   
371
    my $branchname;
372
    my $pkgext = $mergeData->[2];
373
    if ($pkgext =~ m~PulseImport.Branch.(.*)~) {
374
        $branchname = $1;
375
Debug0("Import to branch: $branchname");
376
        my $coBranch = 'trunk';
377
        if ($mergeData->[4] =~ m~/branches::(.*)~ ){
378
            $coBranch = $1;
379
            $coBranch =~ s~\@.*~~;
380
        }
381
Debug0("View is on: $coBranch");
382
       $mustBranch = ($branchname ne $coBranch);
383
    }
384
 
385
    RmDirTree($tipView);
386
    if ($isaNewPackage) {
387
        JatsToolPrint ( 'jats_svn', 'create', $pkgBase );
388
    }
389
    Message("Extract Tip: $tipVcs");
390
    JatsCmdPrint('-logfile', catfile($dirLog, $tipName.'.log'),'jats_vcsrelease.pl', '-extract', '-noprefix', 
391
                    '-root',$dirWork, 
392
                    '-view', $tipName ,
393
                    '-label', $tipVcs, 
394
                    $mustBranch ? '-devMode=Work' : '-devMode=Tip' );
395
 
396
    #
397
    #   Need to branch this view
398
    #       Check for branch existence - error if it doesnt exist
399
    #
400
    if ($mustBranch) {
401
        SystemConfig ('ExitOnError' => 0);
402
        $rv = JatsToolPrint ( 'jats_svnlabel', '-check', '-packagebase', $pkgBase, '-branch', $branchname );
403
        if ($rv) {
404
            Message ("Create Branch: $branchname");
405
            $rv  = JatsToolPrint ( 'jats_svn', 'branch', $branchname, '-path', AbsPath($tipView) );
406
            Error("Cannot create branch: $branchname") if $rv;
407
        } else {
408
            Message ("Switch to branch: $branchname");
409
            $rv  = JatsToolPrint ( 'jats_svn', 'switch', $branchname, '-path', AbsPath($tipView) );
410
            Error("Cannot switch to branch: $branchname") if $rv;
411
        }
412
        SystemConfig ('ExitOnError' => 1);
413
    }
414
 
415
    #
416
    #   Use the 'jats svn import' utility
417
    #       It will do most of the hard work as it was developed to support the ClearCase to Subversion migration
418
    #
419
    my $dataFile = catfile($dirLog, "${pname}_${pversion}.dat");
420
 
421
    my @args;
422
    #push (@args, '-replace') if $opt_replace;
423
 
424
    if ($oldSvnData{COMMENTS}) {
425
        push @args, '-log', $oldSvnData{COMMENTS}
426
    }
427
 
428
    if ($oldSvnData{MODIFIER_NAME}) {
429
        push @args, '-author', $oldSvnData{MODIFIER_NAME}
430
    }
431
 
432
    my $created = $oldSvnData{MODIFIED_STAMP};
433
    if ( $created )
434
    {
435
        $created =~ s~ ~T~;
436
        $created .= '00000Z';
437
        push @args, '-date', $created;
438
    }
439
 
440
    if ($opt_relabel) {
441
        push @args, '-replace';
442
    }
443
    Message("Subverion Merge Package");
444
    $rv = JatsCmdPrint ('-logfile', catfile($dirLog, "${pname}_${pversion}.import.log"), 'jats_svn', 'import', $pkgBase, '-reuse', '-workspace', $tipView,
445
                   '-dir' , $newView, '-datafile' ,  $dataFile, '-label', "${pname}_${pversion}", @args);
446
    Error ("Could not import package from OLD RM") if $rv;
447
 
448
    #
449
    #   Extract the Release Manager tag
450
    #   Needs to be inserted into the database
451
    #   
452
    my $data;
453
    if ( -f $dataFile  )
454
    {
455
        my $rmData = JatsProperties::New($dataFile);
456
        if ( $rmData->getProperty('subversion.tag') ) {
457
            $data->{rmRef} = 'SVN::' . $rmData->getProperty('subversion.tag');
458
        } else {
459
            Warning ("Property files has no subversion.tag");
460
        }
461
        $data->{fileCount}    = $rmData->getProperty('files.base', 0);
462
        $data->{filesRemoved} = $rmData->getProperty('files.removed',0);
463
        $data->{filesAdded}   = $rmData->getProperty('files.added',0);
464
    }
465
 
466
    unless ( $data->{rmRef}  )
467
    {
468
        $data->{errStr} = 'Failed to determine Rm Reference';
469
        Error ($data->{errStr});
470
    }
471
    intoReleaseManager( $oldSvnData{PV_ID}, "${pname}_${pversion}", $data->{rmRef}, $oldRMCred);
472
}
473
 
474
#-------------------------------------------------------------------------------
475
# Function        : JatsToolPrint
476
#
477
# Description     : Print and Execuate a JatsTool command
478
#
479
# Inputs          : 
480
#
481
# Returns         : 
482
#
483
 
484
sub JatsToolPrint
485
{
486
    Information ("Command: @_");
487
    return JatsTool @_;
488
}
489
sub JatsCmdPrint
490
{
491
    Information ("Command: @_");
492
    return JatsCmd @_;
493
}
494
 
495
#-------------------------------------------------------------------------------
496
# Function        : intoReleaseManager
497
#
498
# Description     : Update VCS tags in RM
499
#
500
# Inputs          : $pvid           - PVId
501
#                   $pvname         - Package Version (text)
502
#                   $tag            - New Tag
503
#
504
# Returns         : 
505
#
506
sub intoReleaseManager
507
{
508
    my ($pvid, $pvname, $new_tag, $rmRef ) = @_;
509
    my $user = 3768;            # buildadm
510
 
511
    Message ("Update SVN tag: $pvid, $pvname  - $new_tag");
512
    my $m_sqlstr =  "begin release_manager.PK_RMAPI.update_vcs_details($pvid, '$new_tag', $user); end;";
513
    getDataFromRm('intoReleaseManager', $m_sqlstr, $rmRef);
514
}
515
 
516
#-------------------------------------------------------------------------------
517
# Function        : getReleaseData 
518
#
519
# Description     : Get some essential data
520
#
521
# Inputs          : dataRef
522
#                   RmCredentails 
523
#
524
# Returns         : 
525
#
526
sub getReleaseData
527
{
528
    my ($dataRef, $rmRef) = @_;
529
 
530
    my $m_sqlstr = <<"SQL_END";
531
        SELECT
532
            pv.pv_id,
533
            pv.src_path,
534
            pv.pkg_label,
535
            release_manager.PK_RMAPI.return_vcs_tag(PV_ID) as vcsTag,
536
            pv.comments,
537
            u.user_name,
538
            pv.modified_stamp
539
        FROM
540
            package_versions pv,
541
            packages p,
542
            users u
543
        WHERE
544
            p.pkg_id = pv.pkg_id
545
            AND p.pkg_name = ':pname'
546
            AND pv.pkg_version = ':pversion'
547
            AND pv.modifier_id = u.user_id
548
SQL_END
549
    $m_sqlstr =~ s~:pname~$pname~g;
550
    $m_sqlstr =~ s~:pversion~$pversion~g;
551
    my $data = getDataFromRm('getReleaseData', $m_sqlstr, $rmRef, {oneRow => 1, error =>  "getReleaseData: No data found"  });
552
 
553
    $dataRef->{PV_ID}           = $data->[0]; 
554
    $dataRef->{SRC_PATH}        = $data->[1]; 
555
    $dataRef->{PKG_LABEL}       = $data->[2]; 
556
    $dataRef->{VCS_TAG}         = $data->[3]; 
557
    $dataRef->{COMMENTS}        = $data->[4]; 
558
    $dataRef->{MODIFIER_NAME}   = $data->[5]; 
559
    $dataRef->{MODIFIED_STAMP}  = $data->[6]; 
560
}
561
 
562
#-------------------------------------------------------------------------------
563
# Function        : checkDependentPackages 
564
#
565
# Description     : Check that ALL dependent packages of the 'new' version exist
566
#                   in the old Release Manager 
567
#
568
# Inputs          : 
569
#
570
# Returns         : Will not return if dependencies are missing 
571
#
572
sub checkDependentPackages
573
{
574
    #
575
    #   Determine the package dependencies
576
    #
577
    my $m_sqlstr = <<"END_SQL";
578
SELECT
579
    p2.pkg_name,
580
    pv2.pkg_version
581
FROM
582
    package_versions pv,
583
    package_versions pv2,
584
    packages p,
585
    packages p2,
586
    package_dependencies pd
587
WHERE
588
    p.pkg_id = pv.pkg_id
589
    AND p.pkg_name = '$pname'
590
    AND pv.pkg_version = '$pversion'
591
    and pv.pv_id = pd.pv_id
592
    and pd.dpkg_id = p2.pkg_id
593
    and pd.dpv_id = pv2.pv_id
594
END_SQL
595
    my $data = getDataFromRm('checkDependentPackages', $m_sqlstr, $newRMCred);
596
    return unless $data;
597
 
598
    #
599
    #   Examine each dependency
600
    #
601
    foreach my $entry ( @{$data}) {
602
        Verbose("Check for: $entry->[0], $entry->[1]");
603
        my $pvid = checkVersionExists($oldRMCred, $entry->[0], $entry->[1]);
604
        unless ($pvid) {
605
            if ($opt_autoPlacekeeper) {
606
                Warning("Required Dependency not found: $entry->[0], $entry->[1]", "Others not tested");
607
                $opt_placekeeper = 1;
608
                last;
609
            } else {
610
                ReportError("Required Dependency not found: $entry->[0], $entry->[1]");
611
            }
612
        }
613
    }
614
    ErrorDoExit();
615
 
616
}
617
 
618
#-------------------------------------------------------------------------------
619
# Function        : cleanup 
620
#
621
# Description     : Cleanup created stuff
622
#
623
# Inputs          : 
624
#
625
# Returns         : 
626
#
627
sub cleanup
628
{
629
    Message("Cleanup");
630
    #Debug0("Leaving: newView", $newView);
631
    #Debug0("Leaving: tipView", $tipView);
632
    RmDirTree($newView);
633
    RmDirTree($tipView);
634
}
635
 
636
#-------------------------------------------------------------------------------
637
# Function        : findMergePoint 
638
#
639
# Description     : Determine the point at which the package will be inserted into the old RM
640
#                   This may have been marked in the OLD rm database
641
#
642
# Inputs          : 
643
#
644
# Returns         : 
645
# 
646
sub findMergePoint
647
{
648
    my $m_sqlstr;
649
 
650
    #
651
    #   Use merge point specifed by 'after'
652
    #   
653
    if ($opt_after) {
654
        $m_sqlstr = <<"END_SQL";
655
    SELECT
656
        pv.pv_id,
657
        pv.pkg_id,
658
        pv.pkg_idext,
659
        pv.pkg_version,
660
        release_manager.PK_RMAPI.return_vcs_tag(PV_ID)
661
    FROM
662
        release_manager.package_versions pv,
663
        release_manager.packages p
664
    WHERE
665
      pv.pv_id = $afterPvid
666
END_SQL
667
        my $data = getDataFromRm('findMergePoint', $m_sqlstr, $oldRMCred, {oneRow => 1, error => "findMergePoint-After. No RMdata", sql => 0});
668
        $mergeData = $data;
669
        Message("Merge Point specified by user - after $opt_after");
670
        return;
671
    }
672
 
673
 
674
    #
675
    # Determine if a branchpoint has been marked on the package
676
    #
677
    $m_sqlstr = <<"END_SQL";
678
    SELECT * FROM (
679
        SELECT
680
            pv.pv_id,
681
            pv.pkg_id,
682
            pv.pkg_idext,
683
            pv.pkg_version,
684
            release_manager.PK_RMAPI.return_vcs_tag(PV_ID)
685
        FROM
686
            release_manager.package_versions pv,
687
            release_manager.packages p
688
        WHERE
689
            p.pkg_id = pv.pkg_id
690
            AND p.pkg_name = '$pname'
691
            AND pv.pkg_idext IS NOT NULL
692
        ORDER by pv.pv_id desc  )
693
    WHERE ROWNUM = 1
694
END_SQL
695
 
696
    my $data = getDataFromRm('findMergePoint', $m_sqlstr, $oldRMCred, {oneRow => 1, data => 0});
697
    if ($data) {
698
        $mergeData = $data;
699
        Message("Merge Point specified in Release Manager");
700
    } else {
701
 
702
        #
703
        #   Merge point has not been marked
704
        #       If the package name does not exist, then its a new package
705
        $m_sqlstr = <<"END_SQL";
706
    SELECT
707
        count(*)
708
    FROM
709
        release_manager.packages p, package_versions pv
710
    WHERE
711
        p.pkg_name = '$pname'
712
        and pv.pkg_id = p.pkg_id
713
        and pv.v_ext = '$newData->[5]'
714
END_SQL
715
       $data = getDataFromRm('findMergePoint', $m_sqlstr, $oldRMCred, {oneRow => 1, sql => 0});
716
       unless ($data && $data->[0] > 0 ) {
717
           #
718
           #   Package does not exist at all in Release Manager
719
           #   Flag new package creation
720
           #
721
           $isaNewPackage = 1;
722
           Message("New Package: $pname");
723
           $mergeData->[2] = '';
724
           $mergeData->[4] = '';
725
           return;
726
       }
727
 
728
        #   See if we can guess it.
729
        #       Get the most recent (non-ripple) package-version
730
        #       See how much it differs from the new version
731
        #
732
        $m_sqlstr = <<"END_SQL";
733
    SELECT
734
        pv.pv_id,
735
        pv.pkg_id,
736
        pv.pkg_idext,
737
        pv.pkg_version,
738
        release_manager.PK_RMAPI.return_vcs_tag(PV_ID)
739
    FROM
740
        release_manager.package_versions pv,
741
        release_manager.packages p
742
    WHERE
743
        p.pkg_id = pv.pkg_id
744
        AND p.pkg_name = '$pname'
745
        and pv.build_type = 'A'
746
    ORDER by pv.pv_id desc
747
END_SQL
748
        my $data = getDataFromRm('findMergePoint', $m_sqlstr, $oldRMCred, {oneRow => 1, error => "findMergePoint. No RMdata", sql => 0});
749
 
750
        #
751
        #   If the 'most recent' version is before the split, then nothing has changed in the old system
752
        #   We can determine the merge point.
753
        #
754
        if ($data->[0] < $SplitPvid ) {
755
            Message("Most Recent version is before split");
756
            if ( my $baseVersion = checkForBaseVersion( $data->[0], $data->[3]) ) {
757
                $data = $baseVersion;
758
            }
759
            $mergeData = $data;
760
 
761
        } else {
762
 
763
           if ( my $baseVersion = checkForBaseVersion( $data->[0], $data->[3]) ) {
764
               if ($baseVersion && $baseVersion->[0] < $SplitPvid ) {
765
                   $mergeData = $baseVersion;
766
               }
767
           }
768
 
769
           unless ($mergeData) {
770
               checkoutNewPackage();
771
               checkoutTipPackage($data);
772
               my $rv = checkPkgDiffs($newView, $tipView);
773
               if ($rv eq 'd') {
774
                   $mergeData = $data;
775
                }
776
           }
777
       }
778
    }
779
    Error("Cannot determine package merge point") unless $mergeData;
780
    Message ("Insert Package after: $mergeData->[3], $mergeData->[2], $mergeData->[4]")
781
#DebugDumpData("mergeData", $mergeData);
782
}
783
 
784
#-------------------------------------------------------------------------------
785
# Function        : checkForBaseVersion 
786
#
787
# Description     : Determine the previous non-ripple version of a package version
788
#                   Designed to backtrack to allow insertion at base of package
789
#
790
# Inputs          : $tpvid - pvid of test version
791
#                   $tversion   - Version to test
792
#
793
# Returns         : 
794
#
795
sub checkForBaseVersion
796
{
797
    my ($tpvid, $tversion) = @_;
798
 
799
    my $m_sqlstr = <<"END_SQL";
800
SELECT pv_id, pkg_version
801
 FROM ( SELECT * 
802
  FROM
803
    (SELECT pv.build_type,pv.last_pv_id AS raw_last_pvid ,pv_id,pv.pkg_version,DECODE(pv.pv_id, pv.last_pv_id, NULL, pv.last_pv_id) AS last_pv_id, comments, to_char(pv.modified_stamp, 'DD-MON-YYYY HH24:MM') as modified_stamp, u.user_name
804
    FROM release_manager.package_versions pv, users u
805
    WHERE u.user_id = pv.modifier_id and pv.PKG_ID IN (SELECT pkg_id FROM release_manager.package_versions pv WHERE pv.pv_id = $tpvid )
806
    )
807
    START WITH pv_id =  $tpvid
808
    CONNECT BY nocycle prior last_pv_id = pv_id
809
  ) WHERE build_type != 'Y'
810
END_SQL
811
       my $data = getDataFromRm('checkForBaseVersion', $m_sqlstr, $oldRMCred, {oneRow => 1, dump => 0});
812
       return undef unless $data;
813
 
814
       $m_sqlstr = <<"END_SQL";
815
       SELECT
816
           pv.pv_id,
817
           pv.pkg_id,
818
           pv.pkg_idext,
819
           pv.pkg_version,
820
           release_manager.PK_RMAPI.return_vcs_tag(PV_ID)
821
       FROM
822
           release_manager.package_versions pv
823
       WHERE
824
           pv.pv_id = $data->[0]
825
END_SQL
826
          $data = getDataFromRm('checkForBaseVersion', $m_sqlstr, $oldRMCred, {oneRow => 1, dump => 0});
827
          Message ("Base Version for $tversion is $data->[3]") if $data;
828
          return $data;
829
}
830
 
831
 
832
#-------------------------------------------------------------------------------
833
# Function        : checkPkgDiffs 
834
#
835
# Description     : Determine how different two package-versions are
836
#
837
# Inputs          : $newView
838
#                   $tipView    
839
#
840
# Returns         : d - Only build file diffs
841
#                   D - More than just build file diffs
842
#
843
sub checkPkgDiffs
844
{
845
    my $rCode;
846
    my ($newView, $tipView) = @_;
847
    my $rv = System('--Shell','diff', '-rq', '--exclude=build.pl', $newView, $tipView, '>/dev/null');
848
    if ($rv == 0) {
849
        $rCode = 'd';
850
    } else {
851
        $rCode = 'D';
852
    }
853
    print("TipBuildDiff: $rCode\n");
854
   return $rCode;
855
}
856
 
857
#-------------------------------------------------------------------------------
858
# Function        : checkoutNewPackage 
859
#
860
# Description     : Checkout the Pulse package 
861
#
862
# Inputs          : 
863
#
864
# Returns         : 
865
#
866
sub checkoutNewPackage
867
{
868
    return if $newView;
869
    my $newName = join('_', $pname, $pversion, 'new');
870
    my $newVcs = $newData->[4];
871
    $newView = catdir($dirWork,$newName );
872
 
873
    RmDirTree($newView);
874
 
875
    Message("Extract $pname $pversion, $newVcs, $newView");
876
 
877
    JatsCmd('-logfile', catfile($dirLog, $newName.'.log'),'jats_vcsrelease.pl', '-extractfiles', '-noprefix', '-root',$dirWork, '-view', $newName ,'-label', $newVcs );
878
}
879
 
880
#-------------------------------------------------------------------------------
881
# Function        : checkoutTipPackage 
882
#
883
# Description     : Checkout the Tip package from the old RM
884
#
885
# Inputs          : 
886
#
887
# Returns         : 
888
#
889
sub checkoutTipPackage
890
{
891
    my ($pkgData) = @_;
892
 
893
    my $tipName = join('_', $pname, $pversion, 'tip');
894
    my $tipVcs = $pkgData->[4];
895
    $tipView = catdir($dirWork,$tipName );
896
 
897
    RmDirTree($tipView);
898
 
899
    print("Extract $pname $pversion, $tipVcs, $tipView\n");
900
 
901
    JatsCmd('-logfile', catfile($dirLog, $tipName.'.log'),'jats_vcsrelease.pl', '-extractfiles', '-noprefix', '-root',$dirWork, '-view', $tipName ,'-label', $tipVcs, '-devMode=Tip' );
902
}
903
 
904
 
905
#-------------------------------------------------------------------------------
906
# Function        : checkVersion 
907
#
908
# Description     : Determine if the version exists in a RM instance 
909
#
910
# Inputs          : rmRef           - Ref to RM credentials 
911
#
912
# Returns         : Not found - undef
913
#                   Founf - nice data
914
#
915
sub checkVersion
916
{
917
    my ($rmRef) = @_;
918
 
919
    my $m_sqlstr = <<"END_SQL";
920
   SELECT
921
        pv.pv_id,
922
        pv.pkg_id,
923
        pv.pkg_idext,
924
        pv.pkg_version,
925
        release_manager.PK_RMAPI.return_vcs_tag(PV_ID),
926
        pv.v_ext
927
   FROM release_manager.package_versions pv, release_manager.packages p
928
        where p.pkg_id = pv.pkg_id
929
        and p.pkg_name = '$pname'
930
        and pv.pkg_version = '$pversion'
931
END_SQL
932
 
933
    my $data = getDataFromRm('checkVersion', $m_sqlstr, $rmRef, {oneRow => 1});
934
    return undef unless $data;
935
    return $data;
936
}
937
 
938
#-------------------------------------------------------------------------------
939
# Function        : checkVersionExists 
940
#
941
# Description     : Determine if the version exists in a RM instance 
942
#
943
# Inputs          : rmRef           - Ref to RM credentials 
944
#                   $pname
945
#                   $pversion
946
#
947
# Returns         : Not found - undef
948
#                   Found - pvid
949
#
950
sub checkVersionExists
951
{
952
    my ($rmRef, $pname, $pversion) = @_;
953
 
954
    my $m_sqlstr = <<"END_SQL";
955
   SELECT
956
        pv.pv_id
957
   FROM release_manager.package_versions pv, release_manager.packages p
958
        where p.pkg_id = pv.pkg_id
959
        and p.pkg_name = '$pname'
960
        and pv.pkg_version = '$pversion'
961
END_SQL
962
 
963
    my $data = getDataFromRm('checkVersionExists', $m_sqlstr, $rmRef, {oneRow => 1});
964
    return undef unless $data;
965
    return $data->[0];
966
}
967
 
968
 
969
#-------------------------------------------------------------------------------
970
# Function        : getDataFromRm 
971
#
972
# Description     : Get an array of data from RM 
973
#
974
# Inputs          : $name           - Query Name
975
#                   $m_sqlstr       - Query
976
#                   $rmRef          - Ref to RM
977
#                   $options        - Ref to a hash of options
978
#                                       sql     - show sql
979
#                                       data    - show data
980
#                                       dump    - show results
981
#                                       oneRow  - Only feth one row
982
#                                       error   - Must find data
983
#                                       
984
# Returns         : 
985
#
986
sub getDataFromRm
987
{
988
    my ($name,$m_sqlstr, $rmRef, $options ) = @_;
989
    my @row;
990
    my $data;
991
 
992
    if (ref $options ne 'HASH') {
993
        $options = {}; 
994
    }
995
 
996
    $ENV{GBE_RM_LOCATION} = $rmRef->[1];
997
    $ENV{GBE_RM_USERNAME} = $rmRef->[2];
998
    $ENV{GBE_RM_PASSWORD} = $rmRef->[3];
999
    connectRM(\$RM_DB, $opt_verbose);
1000
 
1001
    if ($options->{sql}) {
1002
        Message("$name: $m_sqlstr")
1003
    }
1004
    my $sth = $RM_DB->prepare($m_sqlstr);
1005
    if ( defined($sth) )
1006
    {
1007
        if ( $sth->execute( ) ) {
1008
            if ( $sth->rows ) {
1009
                while ( @row = $sth->fetchrow_array ) {
1010
                    if ($options->{data}) {
1011
                        Message ("$name: @row");
1012
                    }
1013
                    #Debug0("$name: @row");
1014
                    push @{$data}, [@row];
1015
 
1016
                    last if $options->{oneRow};
1017
                }
1018
            }
1019
            $sth->finish();
1020
        } else {
1021
            Error("Execute failure:$name: $m_sqlstr", $sth->errstr() );
1022
        }
1023
    } else {
1024
        Error("Prepare failure:$name" );
1025
    }
1026
    disconnectRM(\$RM_DB);
1027
 
1028
    if (!$data && $options->{error}) {
1029
        Error( $options->{error} );
1030
    }
1031
 
1032
    if ($data && $options->{oneRow}) {
1033
        $data = $data->[0];
1034
    }
1035
 
1036
    if ($options->{dump}) {
1037
        DebugDumpData("$name", $data);
1038
    }
1039
    return $data;
1040
}
1041
 
1042
 
1043
#-------------------------------------------------------------------------------
1044
#   Documentation
1045
#
1046
 
1047
=pod
1048
 
1049
=for htmltoc    GENERAL::ClearCase::
1050
 
1051
=head1 NAME
1052
 
1053
rmMerge_migrate_package - Migrate a package between Release Manager instances
1054
 
1055
=head1 SYNOPSIS
1056
 
1057
jats rmMerge_migrate_package [options] PackageName PackageVersion
1058
 
1059
 Options:
1060
    -help              - brief help message
1061
    -help -help        - Detailed help message
1062
    -man               - Full documentation
1063
    -verbose[=n]       - Enable additional output
1064
    -[no]test          - Test Dependecies and insertion point
1065
    -[no]history       - Do not impoty history
1066
    -historyTo=txt     - Stop history import at named version
1067
    -relabel           - Allow SVN label to be replaced
1068
    -placekeeper       - Mark as a placekeeper
1069
    -autoPlaceKeeper   - Mark as a placekeeper if dependencies are missing
1070
    -base              - Use non-ripple version of package-version
1071
    -after=version     - Insert after namd version
1072
 
1073
=head1 OPTIONS
1074
 
1075
=over 8
1076
 
1077
=item B<-help>
1078
 
1079
Print a brief help message and exits.
1080
 
1081
=item B<-help -help>
1082
 
1083
Print a detailed help message with an explanation for each option.
1084
 
1085
=item B<-versbose[=n]>
1086
 
1087
Enable additional output
1088
 
1089
=back
1090
 
1091
=head1 EXAMPLE
1092
 
1093
jats eprog rmMerge_migrate_package PackageName PackageVersion
1094
 
1095
=cut
1096
 
1097
 
1098