Subversion Repositories DevTools

Rev

Rev 7047 | Rev 7049 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import java.io.File;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
7048 dpurdie 6
import java.util.Comparator;
6914 dpurdie 7
import java.util.HashMap;
8
import java.util.Iterator;
9
import java.util.LinkedHashSet;
10
import java.util.Vector;
11
 
7033 dpurdie 12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
6914 dpurdie 14
 
15
import com.erggroup.buildtool.ripple.ReleaseManager.BuildReason;
16
import com.erggroup.buildtool.smtp.CreateUrls;
17
import com.erggroup.buildtool.smtp.Smtpsend;
18
import com.erggroup.buildtool.utilities.MutableInt;
19
import com.erggroup.buildtool.utilities.XmlBuilder;
20
import com.erggroup.buildtool.utilities.utilities;
21
 
22
public class Package
23
{
24
    /**
25
     * name of package, must not contain spaces
26
     * 
27
     * @attribute
28
     */
29
    String mName = new String();
30
 
31
    /**
32
     * package scope
33
     * 
34
     * @attribute
35
     */
36
    String mExtension = new String();
37
 
38
    /**
39
     * instance identifier
40
     * 
41
     * @attribute
42
     */
43
    public String mVersion = new String();
7048 dpurdie 44
 
45
    /**
46
     * If this package is a candidate for building, then this value will be calculated
47
     * and will  be used if the package is selected for building
48
     */
49
    public String mNextVersion = null;
6914 dpurdie 50
 
51
    /**
52
     * Version string as specified by the user. Used with a ripple type of 'F'
53
     */
54
    String mFixedVersion = new String();
55
 
56
    /**
57
     * unique identifier 
58
     *      daemon builds = mName + mExtension
59
     *      escrow builds = mName + mVersion + mExtension
60
     * 
61
     * @attribute
62
     */
63
    public String mAlias = new String();
64
 
65
    /**
66
     * Version Control System Tag
67
     * 
68
     * @attribute
69
     */
70
    String mVcsTag = new String();
71
 
72
    /**
73
     * build standards
74
     * 
75
     * @attribute
76
     */
77
    public Vector<BuildStandard> mBuildStandardCollection = new Vector<BuildStandard>();
78
 
79
    /**
80
     * GBE_MACHTYPE used to build generic packages for this baseline only has
81
     * meaning in the daemon build, not the escrow build accessed by
82
     * BuildStandard::getPlatform, getBuildStandard
83
     * 
84
     * @attribute
85
     */
86
    public static String mGenericMachtype = System.getenv("GBE_MACHTYPE");
87
 
88
    /**
89
     * build dependencies by package alias
90
     * 
91
     * @attribute
92
     */
93
    public Vector<String> mDependencyCollection = new Vector<String>();
94
 
95
    /**
96
     * primary package version key pv_id in database
97
     * 
98
     * @attribute
99
     */
100
    public int mId;
101
 
102
    /**
103
     * indication of the nature of change
104
     * 
105
     * @attribute
106
     */
107
    Package.VersionNumberingStandard mChangeType = new VersionNumberingStandard();
108
 
109
    /**
110
     * determines what field is rippled on a package version whose dependencies
111
     * have changed
112
     * 
113
     * @attribute
114
     */
115
    Package.VersionNumberingStandard mRippleField = new VersionNumberingStandard();
116
 
117
    /**
118
     * interested owners
119
     * 
120
     * @attribute
121
     */
122
    private Vector<String> mBuildFailureEmailCollection = new Vector<String>();
123
 
124
    /**
125
     * when true will trigger unit tests as part of the package build phase in
126
     * daemon mode
127
     * 
128
     * @attribute
129
     */
130
    public boolean mHasAutomatedUnitTests = false;
131
 
132
    /**
133
     * when true, do not ripple this package through packages which are
134
     * dependent upon it in daemon mode
135
     * 
136
     * @attribute
137
     */
138
    public boolean mAdvisoryRipple = false;
139
 
140
    /**
141
     * Pegged packages will:
142
     *   - Not be rippled
143
     *   - Not be test buildable
144
     *   - Dependencies will not be considered
145
     *   Daemon Mode:
146
     *      Are not built in this release
147
     *      Are imported with out consideration as to their dependencies
148
     *      Must exist in dpkg_archive - will not be built if not present
149
     *   Escrow Mode:
150
     *      Have no special consideration
151
     *      The package and its dependencies will be built
152
     *
153
     * Packages imported into the release from an SDK will be treated as if they were
154
     * pegged by the built system.
155
     * 
156
     */
157
    public boolean mIsPegged = false;
158
    public boolean mIsSdk = false;
159
 
160
    /**
161
     * Unbuildable packages will not be candidates for a ripple or a rebuild
162
     * If they do not exist in dpkg_archive, then then will cause an error
163
     */
164
    public boolean mIsBuildable = true;
165
 
166
    /**
167
     * determines the build file the package is built in, or not
7048 dpurdie 168
     * <br>   1 Post Plan: build file - Result of planning
169
     * <br>   2 Post Plan: Future build requirement
170
     * <br>   3 Post Plan: Package has no build requirement
171
     * <br>   0 not yet processed (initial value) 
172
     * <br>  -1 not reproducible
173
     * <br>  -2 escrow: not reproducible on the build platforms configured for this release
174
     * <br>  -3 daemon: do not ripple
175
     * <br>  -4 directly dependent on package versions not in the baseline
176
     * <br>  -5 indirectly dependent on package versions which are not reproducible due to detected fault
177
     * <br>  -6 circular dependency
178
     * <br>  -7 Pegged or SDK Imported package not in dpkg_archive
179
     * <br>  -8 Pegged or SDK Imported package
180
     * <br>  -9 Rejected Daemon Instruction
181
     * <br>  -10 UnBuildable Package not in dpkg_archive
182
     * <br>  -11 Marked as RippleStop
6914 dpurdie 183
     * 
184
     */
185
    int mBuildFile = 0;
186
 
187
    /** Retained reason the package will not be built
188
     *  Save negative values as mBuildFile
189
     * @attribute
190
     */
191
    int mNoBuildReason = 0;
192
 
193
    /**
194
     * build dependencies by package
195
     * Calculated from information in mDependencyCollection
196
     * 
197
     * @attribute
198
     */
199
    Vector<Package> mPackageDependencyCollection = new Vector<Package>();
200
 
201
    /**
202
     * used for escrow build purposes set true when a package has been processed
203
     * 
204
     * @attribute
205
     */
206
    boolean mProcessed = false;
207
 
208
    /**
209
     * Reason that a package is being built
210
     */
211
    public BuildReason mBuildReason = null;
212
 
213
    /**
214
     * set true for WIP package versions only used in daemon mode
215
     * 
216
     * @attribute
217
     */
218
    public boolean mDirectlyPlanned = false;
219
 
220
    /**
221
     * set true when it is determined to be ripple built
222
     * 
223
     * @attribute
224
     */
225
    boolean mIndirectlyPlanned = false;
226
 
227
    /**
228
     * non zero instruction number when it is determined to be ripple built by force
229
     * 
230
     * @attribute
231
     */
7048 dpurdie 232
    public int mForcedRippleInstruction = 0;
6914 dpurdie 233
 
234
    /**
235
     * test build - non zero instruction number when it is determined to be test built
236
     * 
237
     * @attribute
238
     */
7048 dpurdie 239
    public int mTestBuildInstruction = 0;
6914 dpurdie 240
 
241
    /**
242
     * build dependencies by pv_id (-1 or not used for planned dependencies)
243
     * 
244
     * @attribute
245
     */
246
    public Vector<Integer> mDependencyIDCollection = new Vector<Integer>();
247
 
248
    /**
249
     * unique pkg_id in the database used for querying package version existence
250
     * in the database in daemon mode
251
     * 
252
     * @attribute
253
     */
254
    public int mPid;
255
 
256
    /**
257
     * maximum major number supported for determining ripple number
258
     * 
259
     * @attribute
260
     */
261
    int mMajorLimit;
262
 
263
    /**
264
     * maximum minor number supported for determining ripple number
265
     * 
266
     * @attribute
267
     */
268
    int mMinorLimit;
269
 
270
    /**
271
     * maximum patch number supported for determining ripple number
272
     * 
273
     * @attribute
274
     */
275
    int mPatchLimit;
276
 
277
    /**
278
     * maximum build number number supported for determining ripple number
279
     * 
280
     * @attribute
281
     */
282
    int mBuildLimit;
283
 
284
    /**
285
     * Logger
286
     * 
287
     * @attribute
288
     */
7033 dpurdie 289
    private static final Logger mLogger = LoggerFactory.getLogger(Package.class);
6914 dpurdie 290
 
291
    /**
292
     * dpkg archive location - The writable dpkg_archive
293
     * 
294
     * @attribute
295
     */
296
    public static String mGbeDpkg = System.getenv("GBE_DPKG");
297
 
298
    /**
299
     *  A physically close replica of dpkg_archive
300
     *  Is not writable 
301
     */
302
    public static final String mGbeDpkgReplica = System.getenv("GBE_DPKG_REPLICA");
303
 
304
    /**
305
     * Exception message used upon detection an archive does not exist Seems
306
     * this is a rare but transient and recoverable scenario
307
     * 
308
     * @attribute
309
     */
310
    public static final String mRecoverable = "dpkg_archive does not exist, recovery will be attempted";
311
 
312
    /**
313
     * true if the package exists in the package archive (dpkg_archive)
314
     * 
315
     * @attribute
316
     */
317
    private boolean mArchivalExistence = true;
318
 
319
    /**
320
     * when true will trigger source control interaction eg labelling
321
     * 
322
     * @attribute
323
     */
324
    public boolean mRequiresSourceControlInteraction = true;
325
 
326
    /**
327
     * when true has been checked for circular dependency
328
     * 
329
     * @attribute
330
     */
331
    boolean mCheckedCircularDependency = false;
332
 
333
    /**
334
     * when true has circular dependency, or in the process of detecting a circular dependency
335
     * 
336
     * @attribute
337
     */
338
    boolean mHasCircularDependency = false;
339
 
340
    /**
341
     * Bread crumb used to detect circular dependencies
342
     * 0 - Normal state
343
     * 1 - Crumb has been set
344
     * 2 - Crumb is a part of a circular dependency
345
     * 3 - Crumb is the start/end of the loop
346
     */
347
    int mBreadCrumb = 0;
348
 
349
    /**
350
     * Planned packages (WIPS) need calculate a new version number
351
     * This should be based on the Version of the package that the WIP was based upon
352
     */
353
	public String mPrevVersion;
354
 
7048 dpurdie 355
    /** Used to preserve the basic ordering of the package list(s)
356
     *  Only valid for the duration of the comparison sorts
357
     */
358
    int mSeqId;	
359
 
6914 dpurdie 360
	/** Indicates that the package not be ripple built at this time
361
	 *  Sequence is:
362
	 *     User sets to 's'
363
	 *     This buildtool will set to 'w' when a ripple has been detected
364
	 *     User resets to NULL when the ripple can be resumed. This is stored as an 'n'
365
	 * 
366
	 */
7048 dpurdie 367
 
6914 dpurdie 368
    public char mRippleStop;
7048 dpurdie 369
 
370
    /**
371
     *  The calculated ripple build plan for this package
372
     *  Will not be calculated for all packages, just those of interest
373
     *  All packages that can (not must) be built when this package is built
374
     *  The list will not include packages that cannot be built at the moment
375
     *  
376
     *  Does not include this package
377
     */
378
    public ArrayList<Package> mRipplePlan = null;
379
 
380
    /** The duration (seconds) of the last build of this package
381
     *  Used to estimate the cost of building the package
382
     *  A WIP packages gets the buildTime of the package its based upon
383
     */
384
    public int mBuildTime;
6914 dpurdie 385
 
7048 dpurdie 386
    /** Calculated time ( seconds) that is may take to build this package and then ripple packages that depend on it
387
     *  both directly and indirectly. Based on mRipplePlan. 
388
     */
389
    public int mRippleTime = 0;
390
 
391
    /** Set to indicate that the package is NOT a part of the full release set
392
     *  The package is a WIP, TestBuild or a ForcedRipple
393
     *  
394
     *   Used to simplify detection of such packages and to limit processing of these packages
395
     *   May be cleared if the package is added to the full set
396
     */
397
    public boolean mIsNotReleased = false;
398
 
6914 dpurdie 399
    /**
400
     * Constructor
401
     * Used to create planned packages. In these packages the change_type is significant
402
     * 
403
     * @param pkg_id	   Package Name Identifier
404
     * @param pv_id        Package Version Id
405
     * @param pkg_name     Package Name
406
     * @param pkg_version  Package Version
407
     * @param v_ext        Package Suffix
408
     * @param alias        Package Alias
409
     * @param pkg_vcs_tag  Vcs Tag
410
     * @param ripple_field Ripple Field
411
     * @param change_type  Change Type
412
     */
413
    public Package(int pkg_id, int pv_id, String pkg_name, String pkg_version, String v_ext, String alias, String pkg_vcs_tag,
414
            char ripple_field, char change_type)
415
    {
416
        mLogger.debug("Package 1: pv_id " + pv_id + " pkg_name " + pkg_name + " v_ext " + v_ext + " alias " + alias
417
                + " pkg_vcs_tag " + pkg_vcs_tag + " change_type " + change_type);
418
        mId = pv_id;
419
        mName = pkg_name;
420
        mPid = pkg_id;
421
        mVersion = "0.0.0000";
422
        mExtension = v_ext;
423
        mAlias = alias;
424
        mVcsTag = pkg_vcs_tag;
425
 
426
        // Remove the package suffix from package_version to create the fixed version number
427
        mFixedVersion = pkg_version;
428
        mFixedVersion = mFixedVersion.substring(0, mFixedVersion.length() - mExtension.length());
429
 
430
        // a ripple_field of 'L' indicates this package has limited version numbering
431
        if (change_type == 'M') {
432
            mChangeType.setMajor(ripple_field == 'L' ? true : false);
433
 
434
        } else if (change_type == 'N') {
435
            mChangeType.setMinor(ripple_field == 'L' ? true : false);
436
 
437
        } else if (change_type == 'P') {
438
            mChangeType.setPatch(ripple_field == 'L' ? true : false);
439
 
440
        } else if (change_type == 'F') {
441
            mChangeType.setFixed();
442
 
443
        } else {
444
            mChangeType.setUnknown();
445
        }
446
    }
447
 
448
    /**
449
     * Constructor
450
     * Used to create existing packages, in these packages the ripple_field is significant
451
     * 
452
     * @param pkg_id	   Package Name Identifier
453
     * @param pv_id        Package Version Id
454
     * @param pkg_name     Package Name
455
     * @param pkg_version  Package Version
456
     * @param v_ext        Package Suffix
457
     * @param alias        Package Alias
458
     * @param pkg_vcs_tag  Vcs Tag
459
     * @param ripple_field Ripple Field
460
     */
461
    public Package(int pkg_id,int pv_id, String pkg_name, String pkg_version, String v_ext, String alias, String pkg_vcs_tag,
462
            char ripple_field)
463
    {
464
        mLogger.debug("Package 2: pv_id " + pv_id + " pkg_name " + pkg_name + " pkg_version " + pkg_version + " v_ext "
465
                + v_ext + " alias " + alias + " pkg_vcs_tag " + pkg_vcs_tag + " ripple_field " + ripple_field);
466
        mId = pv_id;
467
        mName = pkg_name;
468
        mPid = pkg_id;
469
        mVersion = pkg_version;
470
        int endindex = mVersion.length() - v_ext.length();
471
 
472
        if (endindex > 0)
473
        {
474
            mVersion = mVersion.substring(0, endindex);
475
        }
476
 
477
        mExtension = v_ext;
478
        mAlias = alias;
479
        mVcsTag = pkg_vcs_tag;
480
 
481
        // setBuild is the default
482
        if (ripple_field == 'M') {
483
            mRippleField.setMajor();
484
 
485
        } else if (ripple_field == 'm') {
486
            mRippleField.setMinor();
487
 
488
        } else if (ripple_field == 'p') {
489
            mRippleField.setPatch();
490
 
491
        } else if (ripple_field == 'L') {
492
            mRippleField.setLimit();
493
        }
494
    }
495
 
496
    /**
497
     * constructor
498
     */
499
    Package()
500
    {
501
        mLogger.debug("Package 3");
502
        mId = 0;
503
        mName = "null";
504
        mExtension = "null";
505
        mAlias = "null";
506
        mVcsTag = "null";
507
    }
508
 
509
    /**
510
     * constructor for unit test purposes
511
     */
512
    public Package(ReleaseManager rm, String version, int majorLimit, int minorLimit, int patchLimit, int buildNumberLimit)
513
    {
514
        mId = -1;
515
        mRippleField.setLimit();
516
        mVersion = version;
517
        mMajorLimit = majorLimit;
518
        mMinorLimit = minorLimit;
519
        mPatchLimit = patchLimit;
520
        mBuildLimit = buildNumberLimit;
521
 
522
        if (version.endsWith(".cots"))
523
        {
524
            mExtension = ".cots";
525
            mVersion = version.substring(0, version.length() - 5);
526
            mChangeType.setMajor(false);
527
            mChangeType.setMinor(false);
528
            mChangeType.setPatch(true);
529
            mRippleField.setBuild();
530
        }
531
 
532
        try
533
        {
534
            mId = applyPV(rm, 0);
535
        } catch (Exception e)
536
        {
537
        }
538
    }
539
 
540
    /**
541
     * accessor for unit test purposes
542
     */
543
    public int getId()
544
    {
545
        return mId;
546
    }
547
 
548
    /**
549
     * accessor for unit test purposes
550
     */
551
    public String getVersion()
552
    {
553
        return mVersion;
554
    }
555
 
556
 
557
    /**
558
     * returns true if mBuildStandardCollection is not empty
559
     */
560
    boolean isReproducible()
561
    {
7048 dpurdie 562
        mLogger.debug("isReproducible on Package {}", mName);
6914 dpurdie 563
        boolean retVal = ! mBuildStandardCollection.isEmpty();
7048 dpurdie 564
        mLogger.info("isReproducible returned {}", retVal);
6914 dpurdie 565
        return retVal;
566
    }
567
 
568
 
569
    /**
570
     * returns true if at least one of its BuildStandards has mGeneric true
571
     */
572
    boolean isGeneric()
573
    {
7048 dpurdie 574
        mLogger.debug("isGeneric on Package {}", mName);
6914 dpurdie 575
        boolean retVal = false;
576
        for (Iterator<BuildStandard> it = mBuildStandardCollection.iterator(); it.hasNext();)
577
        {
578
            BuildStandard buildStandard = it.next();
579
 
580
            if (buildStandard.isGeneric())
581
            {
582
                retVal = true;
583
                break;
584
            }
585
        }
586
 
7048 dpurdie 587
        mLogger.info("isGeneric returned {}", retVal);
6914 dpurdie 588
        return retVal;
589
    }
590
 
591
    /**
592
     * Returns true if at least one of the packages build standards can be 
593
     * built in the named machine class. 
594
     *  
595
     * Used to determine if the package can be built with the current buildset 
596
     * by iteration over all machine classes in the buildset. 
597
     */
598
    boolean canBeBuildby(String machineClass)
599
    {
600
        mLogger.debug("canBeBuildby on Package " + mName);
601
        boolean retVal = false;
602
        for (Iterator<BuildStandard> it = mBuildStandardCollection.iterator(); it.hasNext();)
603
        {
604
            BuildStandard buildStandard = it.next();
605
 
606
            if (buildStandard.isGeneric())
607
            {
608
                retVal = true;
609
                break;
610
            }
611
 
612
            if (buildStandard.mMachClass.equals(machineClass))
613
            {
614
                retVal = true;
615
                break;
616
            }
617
        }
618
 
619
        mLogger.info("canBeBuildby returned " + retVal);
620
        return retVal;
621
    }
622
 
623
    /**
624
     * Compare the build standards of two packages 
625
     * Used only in escrow mode 
626
     * Used to compare a package and its dependents (one by one) 
627
     *  
628
     * Returns true if the parent (this) package  can be built in the same escrow 
629
     * build iteration as the dependent package. 
630
     *  
631
     * This is a complex decision and has a few built in assumptions. 
632
     *  
633
     * If the dependent package is 'generic' then the parent package can be built 
634
     *  
635
     * If the parent package is generic then is can only be built if the dependent is 
636
     * also generic. Otherwise we must assume that the parent package is an agregator 
637
     * package and requires artifacts from the dependent package built by all of its 
638
     * required build machines. 
639
     *  
640
     * If both packages are not generic, then if the build standards of the parent and the 
641
     * dependent are identical then 
642
     *  
643
     *      If we have one build standard, we can build the parent in this iteration as
644
     *      the dependent package has been completely built.
645
     *  
646
     *      If we have more than one build standard ( but they are identical ) then we
647
     *      ASSUME that we can build the parent in this iteration. The assumption is that
648
     *      there is no mixing between build machines. ie: Windows consumer users Windows
649
     *      artifacts and the Linux consumer uses Linux artifacts ...
650
     *  
651
     * If both packages are not generic and the build standards are not identical, then 
652
     * things get hard. The safest solution is to assume the parent cannot be built in this 
653
     * iteration. This is not a bad assumption. 
654
     *  
655
     */
656
    boolean haveSameBuildStandards(Package d)
657
    {
658
        HashMap<String, Boolean> standardSet = new HashMap<String, Boolean>();
659
        boolean isGeneric = false;
660
        boolean isGeneric_d = false;
661
        boolean isIdentical = true;
662
 
663
        // Scan the build standards of the parent package and create a hash map for each machine
664
        // class required by the parent. Also determine if the parent is generic.
665
        for (Iterator<BuildStandard> it = mBuildStandardCollection.iterator(); it.hasNext();)
666
        {
667
            BuildStandard bs = it.next();
668
            standardSet.put(bs.mMachClass, false);
669
 
670
            if (bs.isGeneric())
671
            {
672
                isGeneric = true;
673
                break;
674
            }
675
        }
676
 
677
        // Scan the build standards in the dependent package and remove items from the map
678
        // If it was not in the map then the dependent package builds for platforms that the
679
        // parent package does not - thus the build standards cannot be identical
680
        // Also determine if the dependent is generic.
681
        for (Iterator<BuildStandard> it = d.mBuildStandardCollection.iterator(); it.hasNext();)
682
        {
683
            BuildStandard bs = it.next();
684
             if (bs.isGeneric())
685
             {
686
             isGeneric_d = true;
687
             break;
688
             }
689
 
690
            Boolean value = standardSet.remove(bs.mMachClass);
691
            if (value == null)
692
            {
693
                isIdentical = false;
694
                break;
695
            }
696
        }
697
 
698
        //
699
        // If there are any items left in the map then the parent package builds on machines
700
        // that the dependent does not. The two are not identical.
701
 
702
        if (!standardSet.isEmpty())
703
        {
704
            isIdentical = false;
705
        }
706
 
707
        // If dependent is generic, then it will be build on the first platform in this iteration
708
        // All is good
709
 
710
        if( isGeneric_d)
711
            return true;
712
 
713
        //  If I am generic and the dependent is generic, then I can be built at this time
714
        // 
715
        //  Will not reach here as we have already said that if dependenbt is generic, then all is good
716
        //  if (isGeneric_d && isGeneric)
717
        //  {
718
        //      return true;
719
        //  }
720
 
721
 
722
        //  If I am generic then I must wait for ALL dependent to be built on all platforms
723
        //  Thus I can't be built this round
724
        //
725
        if (isGeneric)
726
            return false;
727
 
728
        //
729
        //  If the two sets of build standards don't have generic, BUT are identical
730
        //  the we can assume that there is no cross breading. and that windows bits build from windows bits
731
        //  and solaris bits build from solaris
732
        //
733
        if (isIdentical)
734
            return true;
735
 
736
        //  The two sets of build standards are not an exact match
737
        //  Cheap solution: Assume that I can't be built this round
738
        //  Possible solution: If I am a subset of the dependent - then I can be built
739
        //                     If I am a superset of the dependent - then ???
740
        return false;
741
 
742
    }
743
 
744
    /**
7048 dpurdie 745
     * Applies the required version number change. Will calculate mNextVersion
6914 dpurdie 746
     * 
747
     * @param releaseManager    Release Manager instance to work against
748
     * @param rtag_id           Release Identifier
749
     * 
7048 dpurdie 750
     * @return 0 : success
751
     *         1 : cannot work with non standard versioning
752
     *         2 : ripple field limitations prevent a ripple build
753
     *         3 : Invalid Change Type
6914 dpurdie 754
     * @exception Exception
755
     */
756
    int applyPV(ReleaseManager releaseManager, int rtag_id) throws Exception
757
    {
758
        String logInfo = "applyPV," + mName;
759
        mLogger.debug("applyPV on Package " + mName);
7048 dpurdie 760
 
6914 dpurdie 761
        //
7048 dpurdie 762
        //  This method used to actually perform the version change
763
        //  Now it just calculates the potential value
764
        //  Must not alter mVersion as it will be used if the package is not selected to be built,
765
        //  but some of the code assumes that it can be.
766
        //
767
        String originalVersion = mVersion;
768
 
769
        //
6914 dpurdie 770
        // Four scenarios, only applyPV for 3 of them
771
        // mDirectlyPlanned mIndirectlyPlanned mArchivalExistence mForcedRipple
772
        // Action
773
        // WIP/test build exists: true true don't care don't care applyPV
774
        // Package version is out of date: false true true don't care applyPV
775
        // Forced ripple: false true don't care > 0 applyPV
776
        // Package version does not exist: false true false = 0 do not applyPV
777
        //
778
        if (!mDirectlyPlanned && mIndirectlyPlanned && !mArchivalExistence && mForcedRippleInstruction == 0)
779
        {
780
            // the package has an mIndirectlyPlanned flag set true in daemon
781
            // mode because the package does not exist in an archive
782
            // do not apply a different package version
7048 dpurdie 783
            mLogger.info("applyPV. Rebuild Package {}", mName);
6914 dpurdie 784
            mLogger.info("applyPv returned 0");
785
            return 0;
786
        }
787
 
788
        // override - no longer doing a rebuild - version number change from this point on
789
        if (mTestBuildInstruction == 0)
790
        {
791
            mRequiresSourceControlInteraction = true;
792
        }
793
 
794
        //	Force test builds to use a sensible version number
795
        if (mTestBuildInstruction > 0 )
796
        {
797
        	mChangeType.resetData();		// Resolve conflict via build numbers
798
        	mRippleField.setBuild();
799
        	mVersion = "99.99.98999";		// Such that rippling build number will goto 99.99.99000
800
        }
801
 
802
        //
803
        // Detect invalid change type
804
        // Flagged when package instance is created
805
        //
806
        if (mChangeType.mUnknown)
807
        {
7048 dpurdie 808
            mLogger.info("Package Version specified on Package {} New Version: {}", mName, mVersion);
6914 dpurdie 809
            mLogger.info("applyPv returned 3");
810
            return 3;
811
        }
812
 
813
        // If we are not calculating the new package version because the user
814
        // has fixed the version of the package. We are given the new package version.
815
        if (mChangeType.mFixed)
816
        {
817
            // mVersion is already setup
818
 
7048 dpurdie 819
            mNextVersion = mFixedVersion;
820
            mLogger.info("Package Version specified on Package {} New Version: {}", mName,  mVersion);
6914 dpurdie 821
            mLogger.info("applyPv returned 0");
822
            return 0;
823
        }
824
 
825
        // We need to calculate the new version number
826
        //
827
        MutableInt major = new MutableInt(0);
828
        MutableInt minor = new MutableInt(0);
829
        MutableInt patch = new MutableInt(1000);
830
 
831
        // Planned packages have a previous version number to be used as the bases for the
832
        // calculation. Ripples won't.
833
        //
834
        if (mPrevVersion != null)
835
        {
836
        	mVersion = mPrevVersion;
837
        }
838
        String[] field = mVersion.split("\\D");
839
        String nonStandardCotsVersion = "";
840
        logInfo += ", Prev:" + mVersion;
841
 
842
        if (field.length == 3)
843
        {
844
            major.value = Integer.parseInt(field[0]);
845
            minor.value = Integer.parseInt(field[1]);
846
            patch.value = Integer.parseInt(field[2]);
847
        } 
848
        else
849
        {
850
            //
851
            // Can ripple a .cots package under very controlled conditions
852
            // Its ends with a .patchBuild field
853
            // Package is marked as ripple via build number
854
            // Change type of Major and Minor are not allowed
855
            //
7048 dpurdie 856
            if (!mChangeType.mMajor && !mChangeType.mMinor && mRippleField.mBuild && mExtension.compareTo(".cots") == 0 && field.length > 0)
6914 dpurdie 857
            {
858
                // allow and work with (ripple build) versions a.b.c.d....xxxx
859
                // where xxxx.length > 3
860
                String patchStr = field[field.length - 1];
861
                int patchLen = patchStr.length();
862
 
863
                // check patchStr is the last (at least 4) digits
864
                if (patchLen > 3
865
                        && mVersion.substring(mVersion.length() - patchLen, mVersion.length()).compareTo(patchStr) == 0)
866
                {
867
                    patch.value = Integer.parseInt(patchStr);
868
                    nonStandardCotsVersion = mVersion.substring(0, mVersion.length() - patchLen);
869
                }
870
            }
871
 
872
            if (nonStandardCotsVersion.length() == 0)
873
            {
874
                // cannot work with non standard versioning
875
                mLogger.error("applyPV cannot work with non standard versioning");
876
                mLogger.info("applyPv returned 1");
877
                return 1;
878
            }
879
        }
880
 
881
        if (nonStandardCotsVersion.length() == 0 && patch.value < 1000 && field[2].substring(0, 1).compareTo("0") != 0)
882
        {
7048 dpurdie 883
            mLogger.info("applyPV accomodate old style Version of the form 1.0.1");
6914 dpurdie 884
            patch.value = patch.value * 1000;
885
        }
886
 
887
        // mChangeType overrides mRippleField
888
        do
889
        {
890
            if (mChangeType.mMajor)
891
            {
892
                logInfo += ",CT Major";
893
                if (!incrementFieldsAccordingToLimits(4, major, minor, patch))
894
                {
895
                    mLogger.info("applyPv returned 2");
7048 dpurdie 896
                    mVersion = originalVersion;
6914 dpurdie 897
                    return 2;
898
                }
899
            } else if (mChangeType.mMinor)
900
            {
901
                logInfo += ",CT Minor";
902
                if (!incrementFieldsAccordingToLimits(3, major, minor, patch))
903
                {
904
                    mLogger.info("applyPv returned 2");
7048 dpurdie 905
                    mVersion = originalVersion;
6914 dpurdie 906
                    return 2;
907
                }
908
            } else if (mChangeType.mPatch)
909
            {
910
                logInfo += ",CT Patch";
911
                if (!incrementFieldsAccordingToLimits(2, major, minor, patch))
912
                {
913
                    mLogger.info("applyPv returned 2");
7048 dpurdie 914
                    mVersion = originalVersion;
6914 dpurdie 915
                    return 2;
916
                }
917
            } else
918
            {
919
                if (mRippleField.mMajor)
920
                {
921
                    logInfo += ",R Major";
922
                    major.value++;
7048 dpurdie 923
                    mLogger.info("applyPV mRippleField.mMajor {}", major.value);
6914 dpurdie 924
                    minor.value = 0;
925
                    patch.value = 0;
926
                } else if (mRippleField.mMinor)
927
                {
928
                    logInfo += ",R Minor";
929
                    minor.value++;
7048 dpurdie 930
                    mLogger.info("applyPV mRippleField.mMinor {}", minor.value);
6914 dpurdie 931
                    patch.value = 0;
932
                } else if (mRippleField.mPatch)
933
                {
934
                    logInfo += ",R Patch";
935
                    patch.value = ((patch.value / 1000) + 1) * 1000; 
7048 dpurdie 936
                    mLogger.info("applyPV mRippleField.mPatch {}", patch.value);
6914 dpurdie 937
                } else if (mRippleField.mBuild)
938
                {
939
                    logInfo += ", R Build";
940
                    patch.value++;
7048 dpurdie 941
                    mLogger.info("applyPV mRippleField.mBuild {}", patch.value);
6914 dpurdie 942
                } else
943
                {
944
                    if (!incrementFieldsAccordingToLimits(1, major, minor, patch))
945
                    {
946
                        mLogger.info("applyPv returned 2");
7048 dpurdie 947
                        mVersion = originalVersion;
6914 dpurdie 948
                        return 2;
949
                    }
950
                }
951
            }
952
 
953
            if (nonStandardCotsVersion.length() == 0)
954
            {
955
                mVersion = String.valueOf(major.value) + "." + String.valueOf(minor.value) + ".";
956
            } else
957
            {
958
                mVersion = nonStandardCotsVersion;
959
            }
960
 
961
            if (patch.value < 10)
962
            {
963
                mVersion += "000";
964
            } else if (patch.value < 100)
965
            {
966
                mVersion += "00";
967
            } else if (patch.value < 1000)
968
            {
969
                mVersion += "0";
970
            }
971
 
972
            mVersion += String.valueOf(patch.value);
973
        } while (exists(releaseManager, rtag_id));
974
 
7048 dpurdie 975
        logInfo += ", Next Version:" + mVersion;
7033 dpurdie 976
        mLogger.error(logInfo);
6914 dpurdie 977
        mLogger.info("applyPv returned 0");
7048 dpurdie 978
        mNextVersion = mVersion;
979
        mVersion = originalVersion;
6914 dpurdie 980
        return 0;
981
    }
982
 
983
    /**
984
     * increments fields according to mRippleField.mLimit if necessary will
985
     * apply it to the field passed as follows 1 = build 2 = patch 3 = minor
986
     * other = major returns true on success false on ripple field limitations
987
     * prevent a ripple build
988
     */
989
    private boolean incrementFieldsAccordingToLimits(int field, MutableInt major, MutableInt minor, MutableInt patch)
990
    {
991
        boolean retVal = true;
992
 
993
        if (!mChangeType.mLimit && !mRippleField.mLimit)
994
        {
995
            // simple case
996
            // no need to take field limits into consideration
997
            switch (field)
998
            {
999
            case 1:
1000
                // unreachable
1001
                // the only scenario involving build number manipulation
1002
                // involves the mRippleField.mLimit being set
1003
                retVal = false;
1004
                break;
1005
            case 2:
1006
                do
1007
                {
1008
                    patch.value++;
1009
                } while ((patch.value / 1000) * 1000 != patch.value);
1010
                mLogger.info("incrementFieldsAccordingToLimits patch " + patch.value);
1011
                break;
1012
            case 3:
1013
                minor.value++;
1014
                mLogger.info("incrementFieldsAccordingToLimits minor " + minor.value);
1015
                patch.value = 0;
1016
                break;
1017
            default:
1018
                major.value++;
1019
                mLogger.info("incrementFieldsAccordingToLimits major " + major.value);
1020
                minor.value = 0;
1021
                patch.value = 0;
1022
            }
1023
        } else
1024
        {
1025
            // take field limits into consideration
1026
            boolean changeOccurred = false;
1027
            boolean incrementField = true;
1028
 
1029
            switch (field)
1030
            {
1031
            case 1:
1032
                if (mBuildLimit != 0)
1033
                {
1034
                    // increment or reset the patch build number
1035
                    int buildNumber = patch.value - (patch.value / 1000) * 1000;
1036
 
1037
                    if (buildNumber < mBuildLimit)
1038
                    {
1039
                        // can increment the patch build number
1040
                        patch.value++;
1041
                        mLogger.info("incrementFieldsAccordingToLimits mRippleField.mLimit build number " + patch.value);
1042
                        changeOccurred = true;
1043
                        incrementField = false;
1044
                    } else
1045
                    {
1046
                        if (mPatchLimit == 0)
1047
                        {
1048
                            // reset the patch number and patch build number
1049
                            patch.value = 0;
1050
                        }
1051
                    }
1052
                }
1053
                // no break by design
1054
            case 2:
1055
                if (mPatchLimit != 0 && incrementField)
1056
                {
1057
                    // increment or reset the patch number
1058
                    if ((patch.value / 1000) < mPatchLimit)
1059
                    {
1060
                        do
1061
                        {
1062
                            patch.value++;
1063
                        } while ((patch.value / 1000) * 1000 != patch.value);
1064
 
1065
                        mLogger.info("incrementFieldsAccordingToLimits mRippleField.mLimit patch " + patch.value);
1066
                        changeOccurred = true;
1067
                        incrementField = false;
1068
                    } else
1069
                    {
1070
                        // reset the patch number and patch build number
1071
                        patch.value = 0;
1072
                    }
1073
                }
1074
                // no break by design
1075
            case 3:
1076
                if (mMinorLimit != 0 && incrementField)
1077
                {
1078
                    // increment or reset the minor number
1079
                    if (minor.value < mMinorLimit)
1080
                    {
1081
                        minor.value++;
1082
                        patch.value = 0;
1083
                        mLogger.info("incrementFieldsAccordingToLimits mRippleField.mLimit minor " + minor.value);
1084
                        changeOccurred = true;
1085
                        incrementField = false;
1086
                    } else
1087
                    {
1088
                        // reset the minor number
1089
                        minor.value = 0;
1090
                    }
1091
                }
1092
                // no break by design
1093
            default:
1094
                if (mMajorLimit != 0 && incrementField)
1095
                {
1096
                    // increment or reset the major number
1097
                    if (major.value < mMajorLimit)
1098
                    {
1099
                        // increment the major number
1100
                        changeOccurred = true;
1101
                        major.value++;
1102
                        minor.value = 0;
1103
                        patch.value = 0;
1104
                        mLogger.info("incrementFieldsAccordingToLimits mRippleField.mLimit major " + major.value);
1105
                    }
1106
                }
1107
            }
1108
 
1109
            if (!changeOccurred)
1110
            {
1111
                // unable to increment a field due to field limitations
1112
                mLogger.error("incrementFieldsAccordingToLimits ripple field limitations prevent a ripple build");
1113
                mLogger.info("incrementFieldsAccordingToLimits returned false");
1114
                retVal = false;
1115
            }
1116
        }
1117
 
1118
        return retVal;
1119
    }
1120
 
1121
    /**
7048 dpurdie 1122
     * Check if a specified Version of the package exists in dpkg_archive or the Release Manager Database
6914 dpurdie 1123
     * 
1124
     * @param releaseManager Release Manager Instance
1125
     * @param rtag_id        Release Tag Identifier
1126
     * 
1127
     * @return True if the Package Version exists within the Release Manager Database
1128
     * @exception Exception
1129
     */
1130
    private boolean exists(ReleaseManager releaseManager, int rtag_id) throws Exception
1131
    {
1132
        mLogger.debug("exists on Package " + mName + " version " + mVersion + " extension " + mExtension);
1133
        boolean retVal = false;
1134
 
7048 dpurdie 1135
        if (!releaseManager.mUseDatabase)
6914 dpurdie 1136
        {
1137
            mLogger.info("exists !releaseManager.mUseDatabase");
1138
        }
1139
        else
1140
        {
1141
            //  Check Package Archive
1142
            retVal = existsInDpkgArchive();
1143
            if (!retVal)
1144
            {
1145
                //  Check Release Manager Database
1146
                retVal = releaseManager.queryPackageVersions(mPid, mVersion + mExtension);
1147
            }
1148
        }
1149
 
1150
        mLogger.info("exists returned " + retVal);
1151
        return retVal;
1152
    }
1153
 
1154
 
1155
    /**
1156
     * Check to see if a package exists in dpkg_archive
1157
     * 
1158
     * @return true if the version exists in dpkg_archive
1159
     * @exception Exception Thrown if dpkg_archive does not exist. The 'cause' of 'mRecoverable' is special and
1160
     *                      will be trapped later to determine if this is a recoverable exception.
1161
     */
1162
    boolean existsInDpkgArchive() throws Exception
1163
    {
1164
        mLogger.debug("existsInDpkgArchive on " + mName);
1165
        boolean retVal = false;
1166
        String name = utilities.catDir(mName, mVersion + mExtension );
1167
 
1168
        //  If a replica exists, then check it first
1169
        //  If we are configured with a replica its because access to the main archive is slow
1170
        //  and we want this check to be fast
1171
        //
1172
        if (mGbeDpkgReplica != null && mGbeDpkgReplica.length() > 0)
1173
        {
1174
            File dpkg = new File(mGbeDpkgReplica);
1175
            if (!dpkg.exists())
1176
            {
7033 dpurdie 1177
                mLogger.error("existsInDpkgArchive. mGbeDpkgReplica not accessable. " + mRecoverable);
6914 dpurdie 1178
                throw new Exception(mRecoverable);
1179
            }
1180
 
1181
            if( utilities.freshFileExists(utilities.catDir(mGbeDpkgReplica, name) ) )
1182
            {
1183
                mLogger.info("existsInDpkgArchive mGbeDpkgReplica");
1184
                retVal = true;
1185
            }
1186
        }
1187
 
1188
        //  Check (possibly remote) dpkg_archive for files existence if it was not found locally
1189
        //
1190
        if ( !retVal )
1191
        {
1192
 
1193
            //  If the package archive does not exist at the moment, then we have a network issue
1194
            //  This is a recoverable error
1195
 
1196
            File dpkg = new File(mGbeDpkg);
1197
            if (!dpkg.exists())
1198
            {
7033 dpurdie 1199
                mLogger.error("existsInDpkgArchive. mGbeDpkg not accessable. " + mRecoverable);
6914 dpurdie 1200
                throw new Exception(mRecoverable);
1201
            }
1202
 
1203
            if( utilities.freshFileExists(utilities.catDir(mGbeDpkg, name) ) )
1204
            {
1205
                mLogger.info("existsInDpkgArchive mGbeDpkg");
1206
                retVal = true;
1207
            }
1208
        }
1209
 
1210
        mArchivalExistence = retVal;
1211
        mLogger.info("existsInDpkgArchive returned " + retVal);
1212
        return retVal;
1213
    }
1214
 
1215
    /**
1216
     * returns true if the required package archives (dpkg_archive) exist
1217
     * attempt to recover from their transient loss
1218
     */
1219
    public static boolean recover()
1220
    {
1221
        mLogger.debug("recover");
1222
        boolean retVal = false;
1223
 
1224
        String Release = mGbeDpkg;
1225
        if (Release != null)
1226
        {
1227
            if ( utilities.freshFileExists(mGbeDpkg) )
1228
            {
1229
                retVal = true;
7033 dpurdie 1230
                mLogger.error("recover: dpkg_archive access has been restored");
6914 dpurdie 1231
            }
1232
        }
1233
 
1234
        mLogger.info("recover returned " + retVal);
1235
        return retVal;
1236
    }
1237
 
1238
    /**
1239
     * Returns a data structure of unique email addresses
1240
     * Uses the Global Email Collection and the packages own failure email collection
1241
     */
1242
    private LinkedHashSet<String>buildEmailList(RippleEngine rippleEngine)
1243
    {
1244
        //  Create a single list of email targets ensuring only one instance of each email address
1245
        //  ie: Remove duplicates, null and empty strings
1246
        LinkedHashSet<String> hs = new LinkedHashSet<String>();
1247
 
1248
        // Global and Project Wide emails
1249
        for (Iterator<String> it = rippleEngine.mMailGlobalCollection.iterator(); it.hasNext();)
1250
        {
1251
            String item = it.next();
1252
            if (item != null && item.length() > 0) 
1253
            {
1254
                hs.add(item);
1255
            }
1256
        }
1257
 
1258
        // Package specific collection
1259
        for (Iterator<String> it = mBuildFailureEmailCollection.iterator(); it.hasNext();)
1260
        {
1261
            String item = it.next();
1262
            if (item != null && item.length() > 0) 
1263
            {
1264
                hs.add(item);
1265
            }
1266
        }
1267
 
1268
        return hs;
1269
    }
1270
 
1271
    /**
1272
     * Add email information in a form suitable for creating an Ant file
1273
     * @param   rippleEngine    - Ripple Engine Instance
1274
     * @param   xml             - An XmlBuilder element to extend
1275
     */
1276
    void emailInfo(RippleEngine rippleEngine, XmlBuilder xml)
1277
    {
1278
 
1279
        //  Create a single list of email targets ensuring only one instance of each email address
1280
        //  ie: Remove duplicates
1281
        LinkedHashSet<String> hs = buildEmailList(rippleEngine);
1282
 
1283
        for (Iterator<String> it = hs.iterator(); it.hasNext();)
1284
        {
1285
            String email = it.next();
1286
            XmlBuilder entry = xml.addNewElement("owner");
1287
            entry.addAttribute("email", email);
1288
        }
1289
    }
1290
 
1291
    /**
1292
     * Returns email information in a form suitable for direct use
1293
     * @param   rippleEngine Current Release Manager context
1294
     * @return  A comma separated list of user names. May return a 'null' String
1295
     */
1296
    String emailInfoNonAntTask(RippleEngine rippleEngine)
1297
    {
1298
        //  Create a single list of email targets ensuring only one instance of each email address
1299
        //  ie: Remove duplicates
1300
        LinkedHashSet<String> hs = buildEmailList(rippleEngine);
1301
 
1302
        String retVal = null;
1303
        for (Iterator<String> it = hs.iterator(); it.hasNext();)
1304
        {
1305
            String email = it.next();
1306
 
1307
            if (retVal == null)
1308
            {
1309
                retVal = new String();
1310
            } else
1311
            {
1312
                retVal += ",";
1313
            }
1314
            retVal += email;
1315
        }
1316
 
1317
        return retVal;
1318
    }
1319
 
1320
    /**
1321
     * Adds email to mBuildFailureEmailCollection.
1322
     * Do not worry about multiple entries. These will be handled when the data is extracted
7048 dpurdie 1323
     * @param   email   - Email address. Null is allowed and will not be added
6914 dpurdie 1324
     */
1325
    public void addEmail(String email)
1326
    {
7048 dpurdie 1327
        if (email != null) 
6914 dpurdie 1328
        {
7048 dpurdie 1329
            mBuildFailureEmailCollection.add(email);
6914 dpurdie 1330
        }
1331
    }
1332
 
1333
    /**
1334
     * End of Processing for a Test Build
1335
     *     - sends email notification
1336
     *     - marks the instruction complete in the database
1337
     */
1338
    public void completeTestBuild(RippleEngine rippleEngine, boolean success) throws SQLException, Exception
1339
    {
1340
        mLogger.debug("completeTestBuild");
1341
 
1342
        if (mTestBuildInstruction == 0)
1343
        {
1344
            mLogger.info("completeTestBuild. Not Build Instruction");
1345
            return;
1346
        }
1347
 
1348
        //  Email Subject
1349
        String subject = (success == true ? "TEST BUILD COMPLETED SUCCESSFULLY" : "TEST BUILD FAILED") 
1350
                + " on package "
1351
                + mAlias;
1352
 
1353
        // Email Body
1354
        String mailBody = "";
1355
        if ( success != true)
1356
        {
1357
            mailBody += "Test build issues are identified in preceding build failure email.<p>"; 
1358
        }
1359
        mailBody += "Release: " + rippleEngine.mBaselineName + "<br>" 
1360
                  + "Package: " + mAlias + "<br>" 
1361
                  + "Rm Ref: " + CreateUrls.generateRmUrl(rippleEngine.getRtagId(), mId) + "<br>" 
1362
                  + "VcsTag: " + mVcsTag + "<br>"
1363
                  + "Build dependencies:<br>";
1364
 
1365
        String indentString = "&nbsp;&nbsp;&nbsp;&nbsp;";
1366
 
1367
        for (Iterator<Package> it3 = mPackageDependencyCollection.iterator(); it3.hasNext();)
1368
        {
1369
            Package depend = it3.next();
1370
 
1371
            String dependsExtension = depend.mExtension;
1372
            String dependsVersion = depend.mVersion;
1373
 
1374
            if (dependsExtension.length() > 0)
1375
            {
1376
                dependsVersion += dependsExtension;
1377
            }
1378
            mailBody += indentString + RippleEngine.quoteString(depend.mName, dependsVersion) + "<br>";
1379
        }
1380
 
1381
        mailBody += "<br>Build standards:<br>";
1382
 
1383
        for (Iterator<BuildStandard> it = mBuildStandardCollection.iterator(); it.hasNext();)
1384
        {
1385
            BuildStandard bs = it.next();
1386
 
1387
            String bsText = bs.getBuildStandardText();
1388
            if (bsText.length() > 0)
1389
            {
1390
                mailBody += indentString + bsText + "<br>";
1391
            }
1392
        }
1393
 
1394
        mailBody += "<p><hr>";
1395
        try
1396
        {
1397
            String target = emailInfoNonAntTask(rippleEngine);
7033 dpurdie 1398
            mLogger.error("completeTestBuildEmail Server: " + rippleEngine.getMailServer());
1399
            mLogger.error("completeTestBuildEmail Sender: " + rippleEngine.getMailSender());
1400
            mLogger.error("completeTestBuildEmail Target: " + target);
6914 dpurdie 1401
 
1402
            Smtpsend.send(rippleEngine.getMailServer(), // mailServer
1403
                    rippleEngine.getMailSender(),       // source
1404
                    target,                         // target
1405
                    rippleEngine.getMailSender(),       // cc
1406
                    null,                           // bcc
1407
                    subject,                        // subject
1408
                    mailBody,                       // body
1409
                    null                            // attachment
1410
            );
1411
        } catch (Exception e)
1412
        {
1413
            mLogger.warn("Email Failure: completeTestBuild:" + e.getMessage());
1414
        }
1415
 
1416
        // Update the Release Manager Database
1417
        rippleEngine.mReleaseManager.markDaemonInstCompleted(mTestBuildInstruction);
7033 dpurdie 1418
        mLogger.error("completeTest. Returning");
6914 dpurdie 1419
    }
1420
 
1421
 
1422
    /**
1423
     * Returns true if the package is a part of a circular dependency
1424
     * 
1425
     * If the package depends on a package with a circular dependency then the function
1426
     * will return false.
1427
     */
1428
    public boolean hasCircularDependency(RippleEngine ripEng)
1429
    {
7048 dpurdie 1430
        mLogger.debug("hasCircularDependency: {}", mAlias);
6914 dpurdie 1431
        boolean retVal = detectCircularDependency(mAlias, ripEng, null);
7048 dpurdie 1432
        mLogger.info("hasCircularDependency returned {} ", retVal);
6914 dpurdie 1433
        return retVal;
1434
    }
1435
 
1436
    /**
1437
     * Returns true is a part of a circular dependency
1438
     * Will examine all the packages sub dependencies and mark those that do have a
1439
     * circular dependency.
1440
     * 
1441
     * This process works by descending the dependency tree and dropping a bread crumb
1442
     * If the bread crumb is seen during the decent, then a circle has been detected and
1443
     * the package (with the bread crumb) will be marked as having a circular dependency
1444
     * 
1445
     *  Assumes that the caller will walk ALL packages and flag those with a circular
1446
     *  dependence AND those that depend on that package.
1447
     * 
1448
     */
1449
    private boolean detectCircularDependency(String alias, RippleEngine ripEng, Package parent)
1450
    {
1451
        mLogger.debug("detectCircularDependency");
1452
        boolean retVal = false;
1453
 
1454
        // if this package has yet to be checked for circular dependency
1455
        if (!mCheckedCircularDependency)
1456
        {
1457
            // Will be set as we drill down through dependencies
1458
            // If we see this marker (bread crumb) then we have a loop
1459
            if (mBreadCrumb != 0)
1460
            {
1461
                mBreadCrumb = 3;
1462
 
1463
                mHasCircularDependency = true;
1464
                if(parent != null && parent.mBreadCrumb != 3 )
1465
                {
1466
                    parent.mBreadCrumb = 2;
1467
                }
1468
            }
1469
            else
1470
            {
1471
                // Mark this package as potentially having a circular dependency
1472
                // Will now drill down and see if we hit a marker
1473
                mBreadCrumb = 1;
1474
 
1475
                // Recurse down the dependencies and sub dependencies
1476
                for (Iterator<String> it2 = mDependencyCollection.iterator(); it2.hasNext();)
1477
                {
1478
                    String dependencyAlias = it2.next();
1479
                    Package dependency = ripEng.findPackage(dependencyAlias);
1480
                    dependency.detectCircularDependency(alias, ripEng, this);
1481
                }
1482
 
1483
                if (mBreadCrumb == 2)
1484
                {
1485
                    mHasCircularDependency = true;
1486
                    if(parent != null && parent.mBreadCrumb != 3 )
1487
                    {
1488
                        parent.mBreadCrumb = 2;
1489
                    }
1490
                }
1491
                mBreadCrumb = 0;
1492
            }
1493
 
1494
            // Flag package as having been examined
1495
            mCheckedCircularDependency = true;
1496
        } 
1497
 
1498
        // return the persisted circular dependency outcome
1499
        retVal = mHasCircularDependency;
7048 dpurdie 1500
        mLogger.info("detectCircularDependency 2 returned {}", retVal);
6914 dpurdie 1501
        return retVal;
1502
    }
7048 dpurdie 1503
 
1504
    /** Set the sequence of all packages in the list
1505
     *  Used so that the Unit Tests function can preserve the basic ordering of the list 
1506
     */
1507
    public static void setSequence(ArrayList<Package> al)
1508
    {
1509
        int seq = 1;
1510
        for (Iterator<Package> it = al.iterator(); it.hasNext(); )
1511
        {
1512
            Package p = it.next();
1513
            p.mSeqId = seq++;
1514
        }
1515
    }
6914 dpurdie 1516
 
7048 dpurdie 1517
 
1518
    /** Comparator for sorting package collections by mSeqId
1519
     *  Used to preserve order for unit testing
1520
     */
1521
    public static final Comparator<Package> SeqComparator = new Comparator<Package>() {
1522
 
1523
        /**
1524
         * Returns -ve: p1 is less than p2
1525
         *           0: p1 = p2
1526
         *         +ve: p1 > p2
1527
         */
1528
        public int compare (Package p1, Package p2) {
1529
                return p1.mSeqId - p2.mSeqId;
1530
        }
1531
    };    
1532
 
6914 dpurdie 1533
    /**
1534
     * entity class supporting the ERG version numbering standard:
1535
     * <major>.<minor>.<patch/build> patch/build is at least a 4 digit number
1536
     * whose last 3 digits represent the build
1537
     */
1538
    public class VersionNumberingStandard
1539
    {
1540
        /**
1541
         * in terms of the mChangeType Package field, when true indicates the
1542
         * contract of the package has changed in a non backwardly compatible
1543
         * manner in terms of the mRippleField Package field, when true indicates
1544
         * the major version number will be incremented
1545
         * 
1546
         * @attribute
1547
         */
1548
        private boolean mMajor = false;
1549
 
1550
        /**
1551
         * in terms of the mChangeType Package field, when true indicates the
1552
         * contract of the package has changed in a backwardly compatible manner
1553
         * in terms of the mRippleField Package field, when true indicates the
1554
         * minor version number will be incremented
1555
         * 
1556
         * @attribute
1557
         */
1558
        private boolean mMinor = false;
1559
 
1560
        /**
1561
         * in terms of the mChangeType Package field, when true indicates the
1562
         * contract of the package has not changed, but the package has changed
1563
         * internally in terms of the mRippleField Package field, when true
1564
         * indicates the minor version number will be incremented
1565
         * 
1566
         * @attribute
1567
         */
1568
        private boolean mPatch = false;
1569
 
1570
        /**
1571
         * in terms of the mChangeType Package field, when true indicates the
1572
         * package has not changed, its dependencies potentially have in terms
1573
         * of the mRippleField Package field, when true indicates the build
1574
         * number will be incremented
1575
         * 
1576
         * @attribute
1577
         */
1578
        private boolean mBuild = true;
1579
 
1580
        /**
1581
         * in terms of the mChangeType Package field, when true indicates the
1582
         * major, minor, and patch number will be incremented according to field
1583
         * limits in terms of the mRippleField Package field, when true indicates
1584
         * the major, minor, patch and build number will be incremented
1585
         * according to field limits
1586
         * 
1587
         * @attribute
1588
         */
1589
        private boolean mLimit = false;
1590
 
1591
        /**
1592
         * in terms of the mChangeType Package field, when true indicates the
1593
         * package version number will not be rippled. The user will have fixed
1594
         * the version number. This is only application to WIP packages
1595
         * 
1596
         * @attribute
1597
         */
1598
        private boolean mFixed = false;
1599
 
1600
        /**
1601
         * in terms of the mChangeType Package field, when true indicates the
1602
         * method of rippling a package version number is not known.
1603
         * 
1604
         * @attribute
1605
         */
1606
        private boolean mUnknown = false;
1607
 
1608
        /**
1609
         * constructor
1610
         */
1611
        private VersionNumberingStandard()
1612
        {
1613
            mLogger.debug("VersionNumberingStandard");
1614
        }
1615
 
1616
        /**
1617
         * Reset all values to a known state
1618
         * 
1619
         */
1620
        void resetData()
1621
        {
1622
            mBuild = false;
1623
            mMajor = false;
1624
            mMinor = false;
1625
            mPatch = false;
1626
            mLimit = false;
1627
            mFixed = false;
1628
            mUnknown = false;
1629
        }
1630
 
1631
        /**
1632
         * sets mBuild true, mMajor false, mMinor false, mPatch false, mLimit
1633
         * false
1634
         */
1635
        void setBuild()
1636
        {
1637
            mLogger.debug("setBuild");
1638
            resetData();
1639
            mBuild = true;
1640
        }
1641
 
1642
        /**
1643
         * sets mBuild false, mMajor true, mMinor false, mPatch false, mLimit
1644
         * false
1645
         */
1646
        void setMajor()
1647
        {
1648
            mLogger.debug("setMajor");
1649
            resetData();
1650
            mMajor = true;
1651
        }
1652
 
1653
        /**
1654
         * sets mBuild false, mMajor true, mMinor false, mPatch false, mLimit
1655
         * limit
1656
         */
1657
        void setMajor(boolean limit)
1658
        {
1659
            mLogger.debug("setMajor " + limit);
1660
            resetData();
1661
            mMajor = true;
1662
            mLimit = limit;
1663
        }
1664
 
1665
        /**
1666
         * sets mBuild false, mMajor false, mMinor true, mPatch false, mLimit
1667
         * false
1668
         */
1669
        void setMinor()
1670
        {
1671
            mLogger.debug("setMinor");
1672
            resetData();
1673
            mMinor = true;
1674
        }
1675
 
1676
        /**
1677
         * sets mBuild false, mMajor false, mMinor true, mPatch false, mLimit
1678
         * limit
1679
         */
1680
        void setMinor(boolean limit)
1681
        {
1682
            mLogger.debug("setMinor " + limit);
1683
            resetData();
1684
            mMinor = true;
1685
            mLimit = limit;
1686
        }
1687
 
1688
        /**
1689
         * sets mBuild false, mMajor false, mMinor false, mPatch true, mLimit
1690
         * false
1691
         */
1692
        void setPatch()
1693
        {
1694
            mLogger.debug("setPatch");
1695
            resetData();
1696
            mPatch = true;
1697
        }
1698
 
1699
        /**
1700
         * sets mBuild false, mMajor false, mMinor false, mPatch true, mLimit
1701
         * limit
1702
         */
1703
        void setPatch(boolean limit)
1704
        {
1705
            mLogger.debug("setPatch");
1706
            resetData();
1707
            mPatch = true;
1708
            mLimit = limit;
1709
        }
1710
 
1711
        /**
1712
         * sets mBuild false, mMajor false, mMinor false, mPatch false, mLimit
1713
         * true
1714
         */
1715
        void setLimit()
1716
        {
1717
            mLogger.debug("setPatch");
1718
            resetData();
1719
            mLimit = true;
1720
        }
1721
 
1722
        /**
1723
         * sets parameters to indicate that the change type is Fixed. The
1724
         * version number is set by the user and a ripple will not be calculated
1725
         */
1726
        void setFixed()
1727
        {
1728
            mLogger.debug("setFixed");
1729
            resetData();
1730
            mFixed = true;
1731
        }
1732
 
1733
        /**
1734
         * Sets parameters to indicate that the change type is not known
1735
         * 
1736
         */
1737
        void setUnknown()
1738
        {
1739
            resetData();
1740
            mUnknown = true;
1741
        }
1742
 
1743
    }
1744
 
1745
}