Subversion Repositories DevTools

Rev

Rev 4318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
866 mhunt 1
package com.erggroup.buildtool.abt;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.FileWriter;
7
import java.io.IOException;
8
 
9
import java.net.InetAddress;
10
import java.net.UnknownHostException;
11
 
12
import java.util.Iterator;
13
import java.util.Vector;
872 dpurdie 14
import java.util.Properties;
866 mhunt 15
 
16
import org.apache.log4j.Logger;
17
import org.apache.tools.ant.Project;
18
import org.apache.tools.ant.Target;
19
import org.apache.tools.ant.Task;
20
import org.apache.tools.ant.taskdefs.Execute;
21
import org.apache.tools.ant.taskdefs.Redirector;
22
import org.apache.tools.ant.BuildException;
868 mhunt 23
 
866 mhunt 24
import com.erggroup.buildtool.smtp.Smtpsend;
25
 
26
public class ABT extends Task
27
{
28
  // static fields
29
  // public
30
  // protected
31
  // private
32
 
33
  // fields
34
  // public
35
  // protected
36
  // private
37
  // System
38
  private String fs = System.getProperty( "file.separator" );
39
  private String ls = System.getProperty( "line.separator" );
40
  private String gbeMachtype = System.getenv("GBE_MACHTYPE");
41
  private String dpkg_archive = System.getenv("GBE_DPKG");
4280 dpurdie 42
  private String gatherMetricsOnly = System.getenv("GBE_GATHER_METRICS");
43
 
866 mhunt 44
  // ant built in
45
  private Project project;
46
  private String basedir;
47
  // global
4280 dpurdie 48
  private boolean master = false;
866 mhunt 49
  private String gbebuildfilter;
50
  private String mailServer;
51
  private String mailSender;
52
  // project related
53
  private String rtagId;
54
  private String daemon;
55
  private String release;
56
  private String buildtoolVersion;
57
  private String family;
58
  private String oldExtension;
59
  private String newExtension;
60
  // target related
61
  private Target target;
62
  private String packageVersionID;
63
  private String packageAlias;
64
  private String packageName;
65
  private String packageVersion;
4280 dpurdie 66
  private String packageFullVersion;
866 mhunt 67
  private String packageExtension;
68
  private String packageLoc;
924 dpurdie 69
  private String packageVcsTag;
866 mhunt 70
  private String directChange;
71
  private String doesNotRequireSourceControlInteraction;
908 mhunt 72
  private String testBuildInstruction;
866 mhunt 73
  private String generic;
74
  private String loc;
75
  private String unittests;
76
  private Vector<Depend> dependCollection = new Vector<Depend>();
77
  private Vector<Platform> platformCollection = new Vector<Platform>();
78
  private Vector<Jats> jatsCollection = new Vector<Jats>();
79
  private Vector<Ant> antCollection = new Vector<Ant>();
80
  private String owners;
4280 dpurdie 81
  private Machine mMachine;
82
 
866 mhunt 83
  // set up in execute
84
  private BuildStandards buildStandard = BuildStandards.JATS;
85
  private JavaVersions javaVersion = JavaVersions.NONE;
86
  private CompileTargets compileTarget = CompileTargets.NONE;
87
  // abt task result property
88
  private String propertyValue = "0";
89
  // output redirector
90
  private Redirector re;
91
  private File output;
92
  // exec thread
93
  private String[] command = new String[100];
94
  private int commandIndex = 0;
95
  private String cwd;
96
  private Execute thread;
97
  private File wd;
924 dpurdie 98
  private String newVcsTag;
866 mhunt 99
  private static final Logger mLogger = Logger.getLogger(ABT.class);
100
  private String fullyPublished = "no";
101
  private String hostname = new String("unknown");
102
 
103
  private void Log(String message1, String message2)
104
  {
105
    Log( message1 + message2, info );
106
  }
107
 
108
  private static final char debug = 0;
109
  private static final char info = 1;
110
  private static final char warn = 2;
111
  private static final char error = 3;
112
  private static final char fatal = 4;
113
 
114
  private void Log(String message, char facility)
115
  {
116
    if ( daemon != null )
117
    {
118
	    if ( facility == debug)
119
	    {
120
	      mLogger.debug(message);
121
	    }
122
	    else
123
	    if ( facility == info)
124
	    {
125
	      mLogger.info(message);
126
	    }
127
	    else
128
	    if ( facility == warn)
129
	    {
130
	      mLogger.warn(message);
131
	    }
132
	    else
133
	    if ( facility == error)
134
	    {
135
	      mLogger.error(message);
136
	    }
137
	    else
138
	    {
139
	      mLogger.fatal(message);
140
	    }
141
    }
142
 
143
    log( message );
144
  }
145
 
146
  private void initCommand()
147
  {
148
    for( int i = 0; i < 100; i++ )
149
    {
150
      command[ i ] = null;
151
    }
152
    commandIndex = 0;
153
  }
154
 
155
  private void addCommand( String c )
156
  {
157
    command[ commandIndex ] = c;
158
    commandIndex++;
159
  }
160
 
161
  private String printCommand( String addendum, boolean log )
162
  {
163
    String commandToPrint = "";
164
 
165
    for( int i = 0; i < 100; i++ )
166
    {
167
      if ( command[ i ] != null )
168
      {
169
        commandToPrint += command[ i ] + " ";
170
      }
171
      else
172
      {
173
        break;
174
      }
175
    }
176
 
177
    if ( addendum != null )
178
    {
179
      commandToPrint += addendum;
180
    }
181
 
182
    if ( log )
183
    {
184
      Log( commandToPrint, warn );
185
    }
186
 
187
    return commandToPrint;
188
  }
189
 
190
  private int runCommand( String message ) throws Exception
191
  {
192
    String cmd[] = new String[commandIndex];
193
    for ( int i = 0; i < commandIndex; i++ )
194
    {
195
      cmd[i] = command[i];
196
    }
197
    thread.setCommandline(cmd);
198
    printCommand( null, true );
199
    int retVal = thread.execute();
200
 
201
    if ( retVal == Execute.INVALID )
202
    {
203
      reportErrorException( "Failure to execute thread, internal ABT problem", 256 );
204
    }
205
    else
206
    {
207
      printCommand( " returned " + String.valueOf( retVal ), true );
208
 
209
      if ( message != null && retVal != 0 )
210
      {
211
        reportErrorException( message + " : " + printCommand( " returned " + String.valueOf( retVal ), true ), retVal );
212
      }
213
    }
214
 
215
    return retVal;
216
  }
217
 
4280 dpurdie 218
  /**
219
   * Check that one marker file has been published into dpkg_archive
220
   * 
221
   * @param archive     - Archive to test
222
   * @param tag         - Marker suffix
223
   * 
224
   * @return "yes" or "no" as text 
225
   */
226
  private String checkPublished(String archive, String tag) throws Exception
866 mhunt 227
  {
228
    String retVal = "no";
229
 
230
    if (archive == null)
231
    {
924 dpurdie 232
      reportErrorException( "GBE_DPKG environment variable not set", 256 );
866 mhunt 233
    }
234
 
4280 dpurdie 235
    String destination = archive + fs + packageName + fs + packageFullVersion;
866 mhunt 236
 
4280 dpurdie 237
    String filename = "built." + tag;
866 mhunt 238
 
2541 dpurdie 239
    //
240
    //  Due to NFS caching of small file requests the file may take
241
    //  a few seconds to appear. Try several times
4041 dpurdie 242
    //    5 seconds does not appear to be enough
4231 dpurdie 243
    //    Bumped to 60 seconds: 23-Sep-2013, except for escrow where its not used
2541 dpurdie 244
    //
245
    boolean found = false;
4231 dpurdie 246
    int   maxwait = (daemon == null) ? 1 : (60*2);
247
    for ( int ii = 0; ii < maxwait && !found; ii++)
866 mhunt 248
    {
4041 dpurdie 249
        File flag = new File( destination, filename );
2541 dpurdie 250
        found = flag.exists();
251
        if ( !found )
252
        {
4123 dpurdie 253
              try
254
              {
255
                String files;
256
                File folder = new File(destination);
257
                File[] listOfFiles = folder.listFiles();
258
                if ( listOfFiles == null )
259
                {
260
                  Log( "published. Destination not a dir: " + destination, fatal );
261
                }
262
                else
263
                {
264
 
265
                  for (int i = 0; i < listOfFiles.length; i++) 
266
                  {
267
                    if (listOfFiles[i].isFile())
268
                    {
269
                      files = listOfFiles[i].getName();
270
                      Log("published. DirContent:" + files , warn);
271
                    }
272
                  }
273
                }
274
              }
275
              catch( Exception e )
276
              {
277
                Log( "published. Display files caught Exception:" + e.getMessage(), fatal );
278
              }
279
 
280
 
281
           Thread.sleep(500);
4041 dpurdie 282
           Log("published. Wait for " + filename + ".(" + ii +")" , warn);
2541 dpurdie 283
        }
284
    }
285
 
286
    if ( found )
287
    {
866 mhunt 288
      retVal = "yes";
4041 dpurdie 289
      Log("published " + filename + " exists", warn);
866 mhunt 290
    }
291
    else
292
    {
4041 dpurdie 293
      Log("published " + filename + " does not exist", warn);
866 mhunt 294
    }
295
 
296
    return retVal;
297
  }
298
 
4280 dpurdie 299
  private void printPackage( FileWriter file, String packageName, String packageFullVersion ) throws IOException
866 mhunt 300
  {
301
    String line = packageName;
4280 dpurdie 302
    if ( packageFullVersion != null )
866 mhunt 303
    {
4280 dpurdie 304
      line += " " + packageFullVersion;
866 mhunt 305
    }
306
 
307
    line += ls;
308
    file.write( line );
309
  }
310
 
311
  private void copyFile( File source, File copy )
312
  {
313
    if ( source.exists() )
314
    {
315
      try
316
      {
317
        FileInputStream fis = new FileInputStream(source);
318
        FileOutputStream fos = new FileOutputStream(copy);
319
        byte[] buf = new byte[1024];
320
        int i = 0;
321
        while( (i=fis.read(buf)) != -1)
322
        {
323
          fos.write(buf, 0, i);
324
        }
325
        fis.close();
326
        fos.close();
327
      }
328
      catch( Exception e )
329
      {
330
        Log( "copyFile caught Exception", error );
331
      }
332
    }
333
  }
1351 dpurdie 334
 
335
  //
336
  //  Save the log file
337
  //    used on error build
338
  //    used on complete build
339
  //
340
  private boolean saveLogs()
866 mhunt 341
  {
342
    if ( daemon == null )
343
    {
344
      // do nothing more in escrow
1351 dpurdie 345
      return false;
866 mhunt 346
    }
1351 dpurdie 347
 
866 mhunt 348
    // persist the failure log to GBE_LOG/hostname/rtagid/timestamp
349
    String log = System.getenv("GBE_LOG");
350
 
351
    if ( log == null )
352
    {
353
      // cannot happen, GBE_LOG is checked in BuildDaemon at startup
1351 dpurdie 354
      return false;
866 mhunt 355
    }
356
 
357
    File destinationLog = new File( log );
358
    File destinationHostname = new File( destinationLog, hostname );
359
    File destinationRtagId = new File( destinationHostname, rtagId );
360
    // create directories
361
    new File( destinationRtagId, daemon).mkdirs();
362
 
363
    File destinationTimestamp = new File( destinationRtagId, daemon );
364
 
365
    if ( destinationTimestamp.exists() )
366
    {
367
      File buildsource = new File(rtagId + "build.xml");
368
      File logsource = new File(rtagId + ".log");
369
      File buildcopy = new File( destinationTimestamp, packageAlias + ".xml");
370
      copyFile( buildsource, buildcopy );
371
      File logcopy = new File( destinationTimestamp, packageAlias + ".log");
372
      copyFile( logsource, logcopy );
373
      Log("reportError persisting " + logcopy.getName(), warn);
374
    }
1351 dpurdie 375
 
376
    return true;
377
  }
378
 
379
  private void reportError(String error, int retVal)
380
  {
381
    propertyValue = String.valueOf( retVal );
382
 
383
    Log( error, ABT.error );
384
    Log( "reportError propertyValue = " + propertyValue.toString() + ", ", error);
385
 
386
    //  Save the logs
387
    //      Will return false in situations were we can't proceed
388
    if ( ! saveLogs() )
389
    {
390
        return;
391
    }
866 mhunt 392
 
4280 dpurdie 393
    if ( !ABTData.ownerCollection.isEmpty() || mailServer == null || mailSender == null )
866 mhunt 394
    {
395
      String unc = System.getenv("GBE_UNC");
396
 
397
      if ( unc == null )
398
      {
399
        // cannot happen, GBE_UNC is checked in BuildDaemon at startup
400
        return;
401
      }
402
 
922 dpurdie 403
      String href = hostname + "/" + rtagId + "/" + daemon + "/" + packageAlias + ".log";
404
      unc += "/" + href;
405
 
866 mhunt 406
      String body = new String();
407
      body = "Release: " + release + "<p>" +
408
             "Package: " + packageName + "<p>" + 
409
             "Build machine: " + hostname + "<p>" +
410
             "GBE_MACHTYPE: " + gbeMachtype + "<p>" +
922 dpurdie 411
             "Error: " + error + "<p>" +
412
             "Log File: <a href=\"" + unc + "\">" + href + "</a> ... URL expires in 5 working days time<p><hr>";
866 mhunt 413
 
414
      try
415
      {
922 dpurdie 416
        Smtpsend.send( mailServer,  // mailServer
417
                       mailSender,  // source
418
                       owners,      // target
419
                       null,        // cc
420
                       null,        // bcc
866 mhunt 421
                       "BUILD FAILURE on package " + packageName, // subject
922 dpurdie 422
                       body,        // body
423
                       null         // attachment
866 mhunt 424
                      );
425
      }
426
      catch( Exception e )
427
      {
428
        // not fatal
429
        Log( "reportError caught Exception", warn);
430
      }
431
    }
432
 
433
  }
434
 
435
  private void reportErrorException(String error, int retVal) throws Exception
436
  {
437
    reportError( error, retVal );
438
    throw new Exception( hostname );
439
  }
440
 
4280 dpurdie 441
  private void genbuild() throws Exception
866 mhunt 442
  {
443
    if ( master )
444
    {
445
      // gather metrics
886 mhunt 446
      // jats -locatefile=.jats.packageroot etool jats_metrics -mode=init -rootdir=.
447
      // -locate=.jats.packageroot causes jats to cd to the folder containing the file .jats.packageroot
448
      // this file is created in the -path folder by jats release -extract
449
      // DEVI 51370 it is essential for metrics branch calculation the metrics utility is started within a static view
866 mhunt 450
      initCommand();
451
      addCommand( "jats" );
886 mhunt 452
      addCommand( "-locatefile=.jats.packageroot" );
866 mhunt 453
      addCommand( "etool" );
454
      addCommand( "jats_metrics" );
455
      addCommand( "-mode=init" );
456
      addCommand( "-rootdir=." );
457
 
458
      runCommand( "Failure to collect metrics" );
459
    }
460
 
868 mhunt 461
    if ( gatherMetricsOnly != null )
462
    {
463
      return;
464
    }
872 dpurdie 465
 
466
    //
467
    //  Create build files with updated dependency information
468
    //  In escrow mode : No new dependency information
894 mhunt 469
    //  In daemon mode :
470
    //      Ripple     : New dependency information
471
    //      Verify     : Existing dependency information
872 dpurdie 472
    //
894 mhunt 473
    //  In all modes  : ANT builds need an auto.xml
872 dpurdie 474
    //
894 mhunt 475
    //  In order to create auto.xml/auto.pl with package dependencies need to
872 dpurdie 476
    //  create file with dependency data (auto.cfg) only because its
477
    //  needed by "jats_rewrite"
478
    //
479
    File cfg = null;
480
    boolean  create_file = false;
894 mhunt 481
    if ( daemon != null )
866 mhunt 482
    {
872 dpurdie 483
      create_file = true;
484
 
894 mhunt 485
      // always generate auto.cfg
872 dpurdie 486
      cfg = new File( cwd + fs + "auto.cfg" );
866 mhunt 487
      cfg.createNewFile();
488
 
489
      if ( !cfg.exists() || !cfg.canWrite() )
490
      {
491
        reportErrorException( "Failed to open auto.cfg", 263 );
492
      }
493
 
494
      try
495
      {
496
        FileWriter cfgfw = new FileWriter(cfg);
4280 dpurdie 497
        printPackage( cfgfw, packageName, packageFullVersion );
866 mhunt 498
 
499
        // provide releasemanager.projectname=<name[0]> and releasemanager.releasename=<name[1]>
500
        String[] name = release.split( " > ", 2 );
501
 
502
        if ( name.length == 2 )
503
        {
4280 dpurdie 504
          printPackage( cfgfw, "releasemanager.projectname=" + name[0], null );
505
          printPackage( cfgfw, "releasemanager.releasename=" + name[1], null );
866 mhunt 506
        }
507
 
508
        for (Iterator<Depend> it=dependCollection.iterator(); it.hasNext(); )
509
        {
510
          Depend depend = it.next();
4280 dpurdie 511
          printPackage( cfgfw, depend.getPackage_Alias(), null );
866 mhunt 512
        }
513
 
514
        cfgfw.close();
515
      }
516
      catch( IOException e )
517
      {
518
        reportErrorException( "Failure to write to auto.cfg", 263 );
519
      }
872 dpurdie 520
    }
521
    else
522
    if ( buildStandard == BuildStandards.ANT )
523
    {
524
      // Have not created an auto.xml with updated dependencies
525
      // Ant build still would like an auto.xml file
526
      //
527
      // ie packages with a line <import file="auto.xml" optional="true"/> are fine
528
      //    but in escrow and rebuild we still need an auto.xml to be kind
529
      //
530
      create_file = true;
531
    }
532
 
533
    //
534
    //  Create the auto.pl/auto.xml file if required
535
    //
536
    if ( create_file )
537
    {
866 mhunt 538
      initCommand();
539
      addCommand( "jats" );
872 dpurdie 540
 
866 mhunt 541
      if ( buildStandard == BuildStandards.ANT )
542
      {
872 dpurdie 543
        addCommand( "-locatefile=" + packageName + ".xml" );
866 mhunt 544
        addCommand( "etool" );
545
        addCommand( "jats_rewrite" );
546
        addCommand( "-infile=" + packageName + "depends.xml" );
547
        addCommand( "-outfile=auto.xml" );
548
      }
549
      else
550
      {
551
        addCommand( "-locatepkg=" + packageLoc );
552
        addCommand( "etool" );
553
        addCommand( "jats_rewrite" );
554
        addCommand( "-infile=build.pl" );
555
        addCommand( "-outfile=auto.pl" );
556
      }
872 dpurdie 557
 
558
      if ( cfg != null )
559
      {
560
        addCommand( "-config=" + cfg.getAbsolutePath() );
894 mhunt 561
 
908 mhunt 562
        if ( doesNotRequireSourceControlInteraction != null && testBuildInstruction.compareTo("0") == 0 )
894 mhunt 563
        {
564
            // Have a config file, but we are not rippling the package
565
            // just rebuilding it. Validate the versions in Release Manager
566
            // against those in the build files
567
            addCommand( "-validate");
568
        }
872 dpurdie 569
      }
570
      else
571
      {
572
        addCommand( "-noconfig");
573
      }
866 mhunt 574
 
575
      if ( oldExtension != null )
576
      {
577
        addCommand( "-oldproject=" + oldExtension );
578
        addCommand( "-newproject=" + newExtension );
579
      }
580
 
581
      runCommand( "Failure to generate auto.pl potentially caused by missing dependencies in the Release Manager database" );
582
    }
583
  }
584
 
4280 dpurdie 585
  private void jatsBuildPackage() throws Exception
866 mhunt 586
  {
872 dpurdie 587
    //--------------------------------------------------------------------------
588
    //  Build Phase
589
    //  Create build environment
590
 
866 mhunt 591
    initCommand();
872 dpurdie 592
    addCommand( "jats" );
866 mhunt 593
 
872 dpurdie 594
    if ( gbebuildfilter != null )
595
    {
596
      addCommand( "-buildfilter" );
597
      addCommand( gbebuildfilter);
598
    }
599
 
866 mhunt 600
    if ( buildStandard == BuildStandards.ANT )
601
    {
872 dpurdie 602
      addCommand( "-locatefile=" + packageName + ".xml" );
603
      addCommand( "abt" );
604
      addCommand( "-java=" + javaVersion.getJavaVersion() );
605
      addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 606
      addCommand( "build" );
607
    }
608
    else
609
    {
610
      addCommand( "-locatepkg=" + packageLoc );
611
      addCommand( "build" );
872 dpurdie 612
      addCommand( "--package" );
866 mhunt 613
 
614
      if ( family.compareTo( "windows" ) == 0 )
615
      {
616
        addCommand( "--cache" );
617
      }
618
    }
619
 
620
    runCommand( "Failure to set up sandbox" );
621
 
872 dpurdie 622
    //--------------------------------------------------------------------------
623
    //  Make Package phase
624
    //  Create the package (make)
625
 
866 mhunt 626
    initCommand();
872 dpurdie 627
    addCommand( "jats" );
628
 
629
    if ( gbebuildfilter != null )
630
    {
631
      addCommand( "-buildfilter" );
632
      addCommand( gbebuildfilter);
633
    }
634
 
866 mhunt 635
    if ( buildStandard == BuildStandards.ANT )
636
    {
872 dpurdie 637
      addCommand( "-locatefile=" + packageName + ".xml" );
638
      addCommand( "abt" );
639
      addCommand( "-java=" + javaVersion.getJavaVersion() );
640
      addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 641
      addCommand( "make_package" );
642
    }
643
    else
644
    {
645
      addCommand( "-locatepkg=" + packageLoc );
646
      addCommand( "make" );
872 dpurdie 647
      addCommand( "NODEPEND=1" );
648
 
866 mhunt 649
      if ( compileTarget == CompileTargets.PROD )
650
      {
651
        addCommand( "prod" );
652
        addCommand( "package_prod" );
653
      }
872 dpurdie 654
      else if ( compileTarget == CompileTargets.DEBUG )
866 mhunt 655
      {
656
        addCommand( "debug" );
657
        addCommand( "package_debug" );
658
      }
659
      else
660
      {
661
        addCommand( "all" );
662
      }
663
    }
872 dpurdie 664
 
665
    runCommand( "Failure to generate derived files" );
866 mhunt 666
 
667
 
872 dpurdie 668
    //--------------------------------------------------------------------------
669
    //  Unit tests phase
670
    //  Run autonimous unit tests
671
 
866 mhunt 672
    if ( unittests != null )
673
    {
674
      initCommand();
872 dpurdie 675
      addCommand( "jats" );
676
 
677
      if ( gbebuildfilter != null )
678
      {
679
        addCommand( "-buildfilter" );
680
        addCommand( gbebuildfilter);
681
      }
682
 
866 mhunt 683
      if ( buildStandard == BuildStandards.ANT )
684
      {
872 dpurdie 685
        addCommand( "-locatefile=" + packageName + ".xml" );
686
        addCommand( "abt" );
687
        addCommand( "-java=" + javaVersion.getJavaVersion() );
688
        addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 689
        addCommand( "run_tests" );
690
      }
691
      else
692
      {
693
        addCommand( "-locatepkg=" + packageLoc );
872 dpurdie 694
        addCommand( "make" );
866 mhunt 695
        if ( compileTarget == CompileTargets.PROD )
696
        {
697
          addCommand( "run_unit_tests_prod" );
698
        }
872 dpurdie 699
        else if ( compileTarget == CompileTargets.DEBUG )
866 mhunt 700
        {
701
          addCommand( "run_unit_tests_debug" );
702
        }
703
        else
704
        {
705
          addCommand( "run_unit_tests" );
706
        }
707
      }
872 dpurdie 708
 
866 mhunt 709
      runCommand( "Failure to run unit tests" );
710
    }
872 dpurdie 711
 
712
    //--------------------------------------------------------------------------
713
    //  create_dpkg phase
714
    //  Publish the package to dpkg_archive
715
    //
716
 
866 mhunt 717
    initCommand();
718
    addCommand( "jats" );
719
    if ( buildStandard == BuildStandards.ANT )
720
    {
872 dpurdie 721
      addCommand( "-locatefile=" + packageName + ".xml" );
866 mhunt 722
    }
723
    else
724
    {
725
      addCommand( "-locatepkg=" + packageLoc );
726
    }
727
    addCommand( "create_dpkg" );
728
    addCommand( "-o" );
729
    addCommand( "-m" );
730
    addCommand( "-pname" );
731
    addCommand( packageName );
732
    addCommand( "-pversion" );
4280 dpurdie 733
    addCommand( packageFullVersion );
866 mhunt 734
 
735
    if ( generic != null )
736
    {
737
      addCommand( "-generic" );
738
    }
739
 
740
    runCommand( "Failure to publish to archive" );
741
  }
4280 dpurdie 742
 
743
  /**   Check if the package has been Published on all the required machines
744
   *    If its a 'generic' package then a tag of built.generic will have been placed in the package
745
   *    Otherwise each build machine will place a built.<MachName> file in the package
746
   *  
747
   *    All we know is which MachTypes we build for. This needs to be expanded into
748
   *    a list of MachineNames. Thus a build-set with multiple machines of the same MachType
749
   *    can indicate build failures.
750
   */
751
  private void publish() throws Exception
752
  {  
866 mhunt 753
 
4280 dpurdie 754
    //  Metrics gathering (only) does not create anything in dpkg_archive
755
    if ( gatherMetricsOnly != null )
866 mhunt 756
    {
4280 dpurdie 757
        fullyPublished = "yes";
758
        return;
866 mhunt 759
    }
4280 dpurdie 760
 
761
    if ( generic != null )
762
    {
763
        //  Generic - only one maker file
764
        fullyPublished = checkPublished( dpkg_archive, "generic" );
765
    }
766
    else
767
    {
768
        //  Multi machine package
769
        //  Scan required target machines artifacts
770
        //  Expand from MachType to one or more MachName's
4319 dpurdie 771
        scanBuilds:
4280 dpurdie 772
        for (Iterator<Platform> ip=platformCollection.iterator(); ip.hasNext(); )
773
        {
774
          Platform platform = ip.next();
775
          for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
776
          {
777
            Machine ma = it.next();
778
            if (ma.getMachtype().compareTo(platform.getGbe_Machtype()) == 0 )
779
            {
780
                fullyPublished = checkPublished( dpkg_archive, ma.getName() );
781
                if ( fullyPublished.compareTo("no") == 0 )
782
                {
4319 dpurdie 783
                  //  Break out of both loops
784
                  //  Once we have seen an error we need to retain it.
785
                  break scanBuilds;
4280 dpurdie 786
                }
787
            }
788
          }
789
        }
790
    }
866 mhunt 791
 
4280 dpurdie 792
    //  Detect problem with zero length descpkg file
793
    //      May be caused by write clash from multiple machines
794
    //      Don't know
795
    //
796
    if ( fullyPublished.compareTo("yes") == 0 )
868 mhunt 797
    {
4280 dpurdie 798
        if (dpkg_archive == null)
799
        {
800
          reportErrorException( "GBE_DPKG environment variable not set", 256 );
801
        }
802
 
803
        String destination = dpkg_archive + fs + packageName + fs + packageFullVersion;
804
        String filename = "descpkg";
805
 
806
        File flag = new File( destination, filename );
807
 
808
        //  File Must
809
        //      Exist
810
        //      Be a File
811
        //      Have a length. Problem appers to be zero length file
812
        //
813
        boolean found = false;
4285 dpurdie 814
        Log( "published. Descpkg Sanity Test: " + flag.getPath(), info );
4280 dpurdie 815
        if (!flag.exists() ) {
816
            Log( "published. Descpkg Sanity Test: File not found", fatal );
817
 
818
        } else if ( !flag.isFile()) {
819
            Log( "published. Descpkg Sanity Test: Is not a file", fatal );
820
 
821
        } else if ( flag.length() <= 0 ) {
822
            Log( "published. Descpkg Sanity Test: Zero length File", fatal );
823
 
824
        } else {
825
            Log( "published. Descpkg Sanity Test: Passed", info );
826
            found = true;
827
        }
828
 
829
        if (!found) {
830
            fullyPublished = "no";
831
        }
868 mhunt 832
    }
866 mhunt 833
 
834
    if ( fullyPublished.compareTo("yes") == 0 && daemon != null )
835
    {
836
      if ( doesNotRequireSourceControlInteraction == null )
837
      {
872 dpurdie 838
        //
839
        //  Save modified build files
840
        //  Label the resultant files
841
        //  Create a command of the form:
924 dpurdie 842
        //      jats etool jats_vcssave_build
843
        //          -infile     auto.xml/auto.pl
844
        //          -outfile    xxxdepends.xml/build.pl
845
        //          -pname      package_name
846
        //          -pversion   package_version
847
        //          -infofile   path_to_info_file
848
        //          -wiplabel   Existing WIP label (optional)
849
        //          -baselabel  View label
850
 
872 dpurdie 851
        Log("publish save build files", warn);
852
 
853
        File saveinfo = new File(basedir + fs + rtagId + "abtinfo.txt");
854
        if ( saveinfo.exists() )
866 mhunt 855
        {
872 dpurdie 856
          saveinfo.delete();
866 mhunt 857
        }
858
 
859
        initCommand();
860
        addCommand( "jats" );
872 dpurdie 861
        if ( buildStandard == BuildStandards.ANT )
866 mhunt 862
        {
872 dpurdie 863
          addCommand( "-locatefile=" + packageName + ".xml" );
864
        }
865
        else
866
        {
866 mhunt 867
          addCommand( "-locatepkg=" + packageLoc );
868
        }
872 dpurdie 869
 
866 mhunt 870
        addCommand( "etool" );
924 dpurdie 871
        addCommand( "jats_vcssave_build" );
872 dpurdie 872
 
866 mhunt 873
        if ( buildStandard == BuildStandards.ANT )
874
        {
875
          addCommand( "-infile" );
876
          addCommand( "auto.xml" );
877
          addCommand( "-outfile" );
878
          addCommand( packageName + "depends.xml" );
879
        }
872 dpurdie 880
        else
881
        {
882
          addCommand( "-infile" );
883
          addCommand( "auto.pl" );
884
          addCommand( "-outfile" );
885
          addCommand( "build.pl" );
886
        }
866 mhunt 887
 
872 dpurdie 888
        addCommand( "-pname" );
889
        addCommand( packageName );
890
 
891
        addCommand( "-pversion" );
4280 dpurdie 892
        addCommand( packageFullVersion );
872 dpurdie 893
 
894
        if ( directChange != null )
895
        {
926 dpurdie 896
          addCommand( "-isawip" );
872 dpurdie 897
        }
926 dpurdie 898
 
899
        addCommand( "-infofile" );
900
        addCommand( saveinfo.getAbsolutePath() );
872 dpurdie 901
 
926 dpurdie 902
        addCommand( "-baselabel" );
903
        addCommand( packageVcsTag );
924 dpurdie 904
 
905
 
866 mhunt 906
        if ( oldExtension != null && newExtension != null && packageExtension.compareTo( newExtension ) == 0 )
907
        {
908
          // only branch build.pl where its extension is being migrated
909
          addCommand( "-branch" );
910
          addCommand( "project_migration_branch" );
911
          addCommand( "-newbranch" );
912
        }
872 dpurdie 913
 
914
        runCommand( "Failure to save build file changes into source control" );
915
 
916
        //
898 mhunt 917
        //  The command creates an info file(saveinfo) to pass information back
872 dpurdie 918
        //  to the caller (me). This is a 'properties' file
919
        //  Need to extract the following information
924 dpurdie 920
        //    newVcsTag - which is held as VCS.tag
872 dpurdie 921
        //
898 mhunt 922
        Log("publish read info file", info);
872 dpurdie 923
 
924
        Properties properties = new Properties();
925
        try
866 mhunt 926
        {
872 dpurdie 927
          properties.load(new FileInputStream(saveinfo));
866 mhunt 928
        }
872 dpurdie 929
        catch (IOException e)
866 mhunt 930
        {
872 dpurdie 931
          reportErrorException( "Failed to open save_build info file", 262 );
866 mhunt 932
        }
872 dpurdie 933
 
924 dpurdie 934
        newVcsTag = properties.getProperty("VCS.tag" );
935
        if ( newVcsTag == null )
872 dpurdie 936
        {
924 dpurdie 937
          reportErrorException( "Save_build info. Missing property:VCS.tag", 262 );
872 dpurdie 938
        }
924 dpurdie 939
        Log("publish read info file. Label:" + newVcsTag , info);
872 dpurdie 940
      }
866 mhunt 941
    }
942
  }
943
 
4280 dpurdie 944
/** determineBuildStandard
945
 *  Process the current target and set up
946
 *      buildStandard
947
 *      compileTarget
948
 *      javaVersion
949
 */
950
  private void determineBuildStandard() throws Exception
951
  {
952
      // Scan the platform collection looking for an entry that matches the current machine
953
      // Remember the index - as it used to locate a matching entry in the 
954
      // jatsCollection or jatsCollection
955
      // 
956
      // Just wanting to set up
957
      //    buildStandard and one of
958
      //        compileTarget
959
      //    OR  javaVersion
960
      //
961
      int platformIndex = 0;
962
      for ( Iterator<Platform> it = platformCollection.iterator(); it.hasNext(); )
963
      {
964
        Platform platform = it.next();
965
        platformIndex++;
966
 
967
        if ( gbeMachtype.compareTo( platform.getGbe_Machtype() ) == 0 )
968
        {
969
          break;
970
        }
971
      }
972
 
973
      int index = 0;
974
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
975
      {
976
        Jats j = it.next();
977
        index++;
978
 
979
        if ( platformIndex == index )
980
        {
981
          String target = j.getTarget();
982
          if ( target.compareTo( "none" ) == 0 )
983
          {
984
            compileTarget = CompileTargets.NONE;
985
          }
986
          else
987
          if ( target.compareTo( "production" ) == 0 )
988
          {
989
            compileTarget = CompileTargets.PROD;
990
          }
991
          else
992
          if ( target.compareTo( "debug") == 0 )
993
          {
994
            compileTarget = CompileTargets.DEBUG;
995
          }
996
          else
997
          if ( target.compareTo( "all" ) == 0 )
998
          {
999
            compileTarget = CompileTargets.ALL;
1000
          }
1001
          else
1002
          {
1003
            reportErrorException( "Failure, unsupported build environment " + target + ", internal ABT problem", 256 );
1004
          }
1005
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1006
          Log( "execute compileTarget: ", compileTarget.getCompileTarget() );
1007
          break;
1008
        }
1009
      }
1010
 
1011
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1012
      {
1013
        Ant a = it.next();
1014
        index++;
1015
 
1016
        if ( platformIndex == index )
1017
        {
1018
          buildStandard = BuildStandards.ANT;
1019
          String version = a.getJava();
1020
          if ( version.compareTo( "none" ) == 0 )
1021
          {
1022
            javaVersion = JavaVersions.NONE;
1023
          }
1024
          else
1025
          if ( version.compareTo( "1.4" ) == 0 )
1026
          {
1027
            javaVersion = JavaVersions.FOUR;
1028
          }
1029
          else
1030
          if ( version.compareTo( "1.5" ) == 0 )
1031
          {
1032
            javaVersion = JavaVersions.FIVE;
1033
          }
1034
          else
1035
          if ( version.compareTo( "1.6" ) == 0 )
1036
          {
1037
            javaVersion = JavaVersions.SIX;
1038
          }
1039
          else
1040
          if ( version.compareTo( "1.7" ) == 0 )
1041
          {
1042
            javaVersion = JavaVersions.SEVEN;
1043
          }
1044
          else
1045
          {
1046
            reportErrorException( "Failure, unsupported build environment " + version + ", internal ABT problem", 256 );
1047
          }
1048
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1049
          Log( "execute javaVersion: ", javaVersion.getJavaVersion() );
1050
          break;
1051
        }
1052
      }
1053
  }
1054
 
1055
  /** execute
1056
   *  The Aant Task entry point. This is invoked to perform the
1057
   *  targets action
1058
   */
866 mhunt 1059
  public void execute()
1060
  {
1061
    boolean errorReported = false;
1062
    boolean benign = false;
4280 dpurdie 1063
 
1064
 
866 mhunt 1065
    try
1066
    {
1067
      re = new Redirector(this);
1068
 
1069
      // early ant built in
1070
      project = getProject();
1071
      // required by the Execute API on non win32 platforms, don't ask because I don't know
1072
      project.setProperty("ant.home", System.getenv("ANT_HOME"));
4280 dpurdie 1073
 
866 mhunt 1074
      daemon = project.getProperty("abt_daemon");
1075
      if ( daemon != null )
1076
      {
1077
        Log("execute daemon: yes", info);
1078
      }
4280 dpurdie 1079
 
1080
      // Get the current hostname
1081
      // Perhaps this should be injected by the calling task
1082
      // Daemon Mode:
1083
      //    When sending emails
1084
      //    Identify the target machine parameters
1085
      // Escrow Mode:
1086
      //    Deterime escrow build components    
1087
      try
1088
      {
1089
        InetAddress local = InetAddress.getLocalHost();
1090
        hostname = local.getHostName();
1091
      }
1092
      catch( UnknownHostException e )
1093
      {
1094
        // do nothing, shouldn't happen, but hostname will be "unknown" if it does
1095
      }
1096
 
866 mhunt 1097
      // early target related
1098
      target = getOwningTarget();
1099
      packageAlias = target.getName();
924 dpurdie 1100
      packageVcsTag = project.getProperty(packageAlias + "packagevcstag");
1101
      Log("execute packageVcsTag: ", packageVcsTag);
866 mhunt 1102
 
924 dpurdie 1103
      if ( packageVcsTag == null )
866 mhunt 1104
      {
1105
        // this abt task has no properties, by design
1106
        // future use to estimate build completion time
898 mhunt 1107
        Log("execute packageAlias: " + packageAlias, info);
866 mhunt 1108
        return;
1109
      }
1110
 
898 mhunt 1111
      Log("execute packageAlias: " + packageAlias, warn);
1112
 
866 mhunt 1113
      // ant built in
1114
      basedir = project.getProperty("basedir");
1115
      Log("execute basedir: ", basedir);
1116
 
1117
      // global
4280 dpurdie 1118
 
1119
      if ( gbeMachtype == null ) {
1120
        reportErrorException( "GBE_MACHTYPE environment variable not set", 256 );
866 mhunt 1121
      }
4280 dpurdie 1122
 
866 mhunt 1123
      mailServer = project.getProperty("abt_mail_server");
1124
      if ( mailServer == null )
1125
      {
4280 dpurdie 1126
          reportErrorException( "Missing ANT property: abt_mail_server", 256 );
866 mhunt 1127
      }
1128
      Log("execute mailServer: ", mailServer);
1129
 
1130
      mailSender = project.getProperty( "abt_mail_sender" );
1131
      if ( mailSender == null )
1132
      {
4280 dpurdie 1133
          reportErrorException( "Missing ANT property: abt_mail_sender", 256 );
866 mhunt 1134
      }
1135
      Log("execute mailSender: ", mailSender);
4280 dpurdie 1136
 
1137
      for (Iterator<Owner> it=ABTData.ownerCollection.iterator(); it.hasNext(); )
1138
      {
1139
        Owner owner = it.next();
1140
 
1141
        if ( owners == null )
1142
        {
1143
          owners = new String();
1144
        }
1145
        else
1146
        {
1147
          owners += ",";
1148
        }
1149
        owners += owner.getEmail();
1150
        Log("execute owner: ", owner.getEmail());
1151
      }
1152
      Log("execute owners: ", owners);
1153
 
866 mhunt 1154
 
1155
      // project related
1156
      rtagId = project.getProperty("abt_rtag_id");
1157
      Log("execute rtagId: ", rtagId);
1158
 
1159
      release = project.getProperty("abt_release");
1160
      Log("execute release: ", release);
4212 dpurdie 1161
 
866 mhunt 1162
      buildtoolVersion = project.getProperty("abt_buildtool_version");
1163
      Log("execute buildtoolVersion: ", buildtoolVersion);
4212 dpurdie 1164
 
866 mhunt 1165
      family = project.getProperty("abt_family");
1166
      Log("execute family: ", family);
4212 dpurdie 1167
 
866 mhunt 1168
      oldExtension = project.getProperty("abt_old_extension");
1169
      Log("execute oldExtension: ", oldExtension);
4212 dpurdie 1170
 
866 mhunt 1171
      newExtension = project.getProperty("abt_new_extension");
1172
      Log("execute newExtension: ", newExtension);
4212 dpurdie 1173
 
908 mhunt 1174
      testBuildInstruction = project.getProperty("abt_test_build_instruction");
1175
      Log("execute testBuildInstruction: ", testBuildInstruction);
4280 dpurdie 1176
 
1177
      // Locate Machine Information for this machine
1178
      //    It contains the build filter for the current machine
1179
      //    Only present in daemon builds
1180
      //
1181
      for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
1182
      {
1183
        Machine ma = it.next();
1184
        if (ma.getName().compareTo(hostname) == 0 )
1185
        {
1186
            mMachine = ma;
1187
            Log("execute machine: ", mMachine.getName() + ", Type:" + mMachine.getMachtype() + ", Class:" + mMachine.getMachclass() + ", Master:" + mMachine.getMaster() ); 
1188
            break;
1189
        }
1190
      }
1191
 
1192
      if ( (daemon != null) && (mMachine == null) ) {
1193
          reportErrorException( "Cannot find Machine entry for: " + hostname, 256 );
1194
      }
908 mhunt 1195
 
4280 dpurdie 1196
      //  Setup machine specific information
1197
      //    Build Filter
1198
      //    Master Mode
1199
      if (mMachine != null) 
1200
      {
1201
          if (mMachine.getMaster() != null)
1202
          {
1203
            master = true;
1204
            Log("execute master: ", "true");
1205
          }
1206
 
1207
          gbebuildfilter = mMachine.getBuildfilter(); 
1208
          if ( gbebuildfilter != null )
1209
          {
1210
            //
1211
            // Ensure the Execute task passes gbebuildfilter as one parameter...
1212
            // Replace spaces with commas
1213
            //
1214
            gbebuildfilter = gbebuildfilter.replace( " ", ",");
1215
            Log("execute gbebuildfilter: ", gbebuildfilter);
1216
          }
1217
      }
1218
 
908 mhunt 1219
      if ( testBuildInstruction == null )
1220
      {
1221
        // use a meaningful default
1222
        testBuildInstruction = "0";
1223
      }
866 mhunt 1224
 
1225
      // redirect all thread (therefore jats) output to the output file in the basedir
1226
      if ( daemon != null )
1227
      {
1228
        // thread logging is done to <rtagId>thread.log and is continually re-used every build
1229
        // full thread and non thread (ie abt logging) is done to the DefaultLogger set up by the build daemons, <rtagId>.log
1230
        // this file is copied on a build failure to cwd (if it exists) and is continually re-used every build
1231
        output = new File( rtagId + "thread.log" );
1232
        // capture full logging
1233
        re.setAlwaysLog(true);
1234
      }
1235
      else
1236
      {
1237
        output = new File( packageAlias + ".log" );
1238
      }
1239
 
1240
      re.setOutput( output );
1241
      thread = new Execute(re.createHandler());
1242
      thread.setAntRun( project );
1243
      thread.setVMLauncher(false);
1244
 
1245
      // must set up package name and owners before reportError
1246
      packageName = project.getProperty(packageAlias + "packagename");
1247
      Log("execute packageName: ", packageName);
1248
 
4280 dpurdie 1249
      //-----------------------------------------------------------------------
1250
      //    AbtSetUp
1251
      //
866 mhunt 1252
      if ( packageAlias.compareTo("AbtSetUp") == 0 )
1253
      {
924 dpurdie 1254
        // create rtag_id directory from scratch
1255
        File rId = new File( rtagId );
1256
        deleteDirectory( rId );
1257
        rId.mkdirs();
866 mhunt 1258
 
924 dpurdie 1259
        // jats jats_vcsrelease -extract -label=pkgVcsTag -path=src_path -root=rtag_id/timestamp -tag=timestamp -noprefix
1260
        initCommand();
1261
        addCommand( "jats" );
1262
        addCommand( "jats_vcsrelease" );
1263
        addCommand( "-extract" );
1264
        addCommand( "-label=" + packageVcsTag );
1265
        addCommand( "-root=" + rtagId );
1266
        addCommand( "-tag=" + daemon );
1267
        addCommand( "-noprefix" );
1268
        addCommand( "-verbose=2" );
866 mhunt 1269
 
924 dpurdie 1270
        runCommand( "Failure to extract source code from source control" );
1271
        return;
866 mhunt 1272
      }
1273
 
4280 dpurdie 1274
      //-----------------------------------------------------------------------
1275
      //    AbtTearDown
1276
      //
866 mhunt 1277
      if ( packageAlias.compareTo("AbtTearDown") == 0 )
1278
      {
924 dpurdie 1279
        // tear the build view down, regardless of build error
866 mhunt 1280
        wd = new File( basedir );
1281
 
1282
        // only now change the thread working directory back
1283
        thread.setWorkingDirectory( wd );
1284
 
868 mhunt 1285
        File buildDirectory = new File(wd + getProject().getProperty("abt_package_location"));
866 mhunt 1286
 
1287
        if ( buildDirectory.exists() )
1288
        {
4212 dpurdie 1289
          // jats jats_vcsrelease -label pkgTag -root=rtag_id/timestamp -tag=timestamp -noprefix -delete=2
866 mhunt 1290
          initCommand();
1291
          addCommand( "jats" );
924 dpurdie 1292
          addCommand( "jats_vcsrelease" );
1293
          addCommand( "-label=" + packageVcsTag );
878 mhunt 1294
          addCommand( "-root=" + rtagId );
866 mhunt 1295
          addCommand( "-tag=" + daemon );
1296
          addCommand( "-noprefix" );
924 dpurdie 1297
          addCommand( "-delete=2" );
1298
          addCommand( "-verbose=2" );
1299
 
866 mhunt 1300
          runCommand( "Failure to remove source code extraction" );
1301
        }
1302
 
878 mhunt 1303
        // delete the rtag directory
1304
        deleteDirectory( new File( rtagId ) );
1351 dpurdie 1305
 
1306
        // Always save the log files
1307
        saveLogs();
1308
 
866 mhunt 1309
        return;      
1310
      }
1311
 
4280 dpurdie 1312
      // target related    
1313
      packageVersion = project.getProperty(packageAlias + "packageversion");
1314
      Log("execute packageVersion: ", packageVersion);
1315
 
1316
      packageExtension = project.getProperty(packageAlias + "packageextension");
1317
      Log("execute packageExtension: ", packageExtension);
1318
 
1319
      packageFullVersion = packageVersion + packageExtension;
1320
      Log("execute packageFullVersion: ", packageFullVersion);
1321
 
1322
      packageLoc = packageName + packageExtension;
1323
      Log("execute packageLoc: ", packageLoc);
1324
 
1325
      generic = project.getProperty(packageAlias + "generic");
1326
      if ( generic != null ) {
1327
        Log("execute generic: yes", info);  
1328
      }
1329
 
1330
      // Extract the build standard information from the abt element
1331
      determineBuildStandard();
1332
 
866 mhunt 1333
      directChange = project.getProperty(packageAlias + "directchange");
1334
      if ( directChange != null )
1335
      {
1336
        Log("execute directChange: yes", info);  
1337
      }
1338
 
1339
      doesNotRequireSourceControlInteraction = project.getProperty(packageAlias + "doesnotrequiresourcecontrolinteraction");
1340
      if ( doesNotRequireSourceControlInteraction != null )
1341
      {
1342
        Log("execute doesNotRequireSourceControlInteraction: true", info);  
1343
      }
1344
 
4280 dpurdie 1345
      //    Display details of the Platform Jats and Ant elements within the
1346
      //    current ABT element
1347
      //
866 mhunt 1348
      for (Iterator<Platform> it=platformCollection.iterator(); it.hasNext(); )
1349
      {
1350
        Platform platform = it.next();
1351
        Log("execute platform: ", platform.getGbe_Machtype());
1352
      }
1353
 
1354
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
1355
      {
1356
        Jats jats = it.next();
1357
        Log("execute jats: ", jats.getTarget());
1358
      }
1359
 
1360
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1361
      {
1362
        Ant ant = it.next();
1363
        Log("execute ant: ", ant.getJava());
1364
      }
924 dpurdie 1365
 
1366
      // Set newVsTag to the existing VcsTag
1367
      // Will be used when rebuilding a package that already exists
1368
      newVcsTag = packageVcsTag;
1369
 
866 mhunt 1370
      loc = project.getProperty(packageAlias + "loc");
1371
      Log("execute loc: ", loc);
4280 dpurdie 1372
 
866 mhunt 1373
      cwd = basedir + loc;
1374
      Log("execute cwd: ", cwd);
4280 dpurdie 1375
 
866 mhunt 1376
      wd = new File( cwd );
1377
      if ( !wd.exists() )
1378
      {
1379
        reportErrorException( "Failure " + cwd + " does not exist", 265 );
1380
      }
1381
 
1382
      // only now change the thread working directory
1383
      thread.setWorkingDirectory( wd );
1384
 
4280 dpurdie 1385
      //-----------------------------------------------------------------------
1386
      //    AbtPublish
1387
      //    Note: Have changed directory to the working directory
1388
      //
866 mhunt 1389
      if ( packageAlias.compareTo("AbtPublish") == 0 )
1390
      {
4280 dpurdie 1391
        publish();
866 mhunt 1392
        if ( daemon != null )
1393
        {
1394
          // set properties to drive BuildThread attributes
1395
          // these are used to determine what ant did
1396
          getProject().setProperty("abt_fully_published", fullyPublished);
924 dpurdie 1397
          getProject().setProperty("abt_new_vcstag", newVcsTag);
866 mhunt 1398
        }
1399
 
1400
        return;
1401
      }
1402
 
4280 dpurdie 1403
      //-----------------------------------------------------------------------
1404
      //    Build Target
1405
      //    Note: Have changed directory to the working directory
1406
      //
866 mhunt 1407
      packageVersionID = project.getProperty(packageAlias + "pv_id");
1408
      Log("execute packageVersionID: ", packageVersionID);
1409
      unittests = project.getProperty(packageAlias + "unittests");
1410
 
1411
      if ( unittests != null )
1412
      {
1413
        Log("execute unittests: yes", info);
1414
      }
1415
 
1416
      for (Iterator<Depend> it=dependCollection.iterator(); it.hasNext(); )
1417
      {
1418
        Depend depend = it.next();
1419
        Log("execute depend: ", depend.getPackage_Alias());
1420
      }
1421
 
1422
      if ( daemon == null )
1423
      {
1424
        // escrow centric
1425
        // recognise an escrow will be run multiple times on a build stage
1426
        // do not force continuous rebuilding each time
1427
        // has this package version already been published on this platform
4280 dpurdie 1428
        String published = checkPublished( dpkg_archive, ( generic != null ) ? "generic" : hostname );
1429
 
866 mhunt 1430
        if ( published.compareTo("no") != 0 )
1431
        {
1432
          Log( "execute package has been published on this platform - skipping", info);
1433
          propertyValue = "257";
1434
          throw new Exception();
1435
        }
1436
      }
910 mhunt 1437
      else
866 mhunt 1438
      {
910 mhunt 1439
        // DEVI 56714 daemon centric check
1440
        String majorV = this.getClass().getPackage().getSpecificationVersion();
1441
 
1442
        if ( buildtoolVersion.compareTo( majorV ) != 0 )
1443
        {
1444
          reportErrorException( "Failure, incompatible build.xml not generated with version " + majorV, 256 );
1445
        }
866 mhunt 1446
      }
1447
 
1448
      if ( ( buildStandard == BuildStandards.ANT && javaVersion == JavaVersions.NONE ) ||
1449
           ( buildStandard == BuildStandards.JATS && compileTarget == CompileTargets.NONE ) )
1450
      {
4280 dpurdie 1451
        //  Flag a benign build is good
1452
        //  A benign is a no-operation. The build is not rquired on the current machine
1453
        //  Issue: We have already extracted the source and will need to tear it down
1454
        //
866 mhunt 1455
        benign = true;
1456
      }
1457
 
4280 dpurdie 1458
      // Pre Build processing
1459
      //    Init metrics gathering
1460
      //    Generate auto.pl or <PackageName>depends.xml file
1461
      //
1462
      genbuild();
866 mhunt 1463
 
4280 dpurdie 1464
      if ( !benign && gatherMetricsOnly == null )
866 mhunt 1465
      {
4280 dpurdie 1466
          //
1467
          //    Build the target package
1468
          //
1469
          jatsBuildPackage();
866 mhunt 1470
      }
1471
 
1472
      if ( master )
1473
      {
1474
        File metrics = new File(basedir + fs + rtagId + "abtmetrics.txt");
1475
        if ( metrics.exists() )
1476
        {
1477
          metrics.delete();
1478
        }
1479
 
1480
        // gather metrics
886 mhunt 1481
        // jats -locatefile=.jats.packageroot etool jats_metrics -mode=finish -out=abtmetrics.txt
866 mhunt 1482
        initCommand();
1483
        addCommand( "jats" );
886 mhunt 1484
        addCommand( "-locatefile=.jats.packageroot" );
866 mhunt 1485
        addCommand( "etool" );
1486
        addCommand( "jats_metrics" );
1487
        addCommand( "-mode=finish" );
1488
        addCommand( "-out=" + metrics.getAbsolutePath() );
1489
 
1490
        runCommand( "Failure to collect metrics" );
1491
      }
1492
    }
1493
    catch( SecurityException e)
1494
    {
1495
      errorReported = true;
1496
      reportError( "Failure caught SecurityException", 256 );
1497
    }
1498
    catch( IOException e)
1499
    {
1500
      errorReported = true;
1501
      reportError( "Failure caught IOException", 256 );
1502
    }
1503
    catch( Exception e)
1504
    {
1505
      // assume this condition has been reported on
1506
      errorReported = true;
1507
 
1508
      if ( e.getMessage() != null )
1509
      {
898 mhunt 1510
        Log( "execute caught Exception " + e.getMessage(), warn );
866 mhunt 1511
      }
1512
      else
1513
      {
1514
        Log( "execute caught Exception", warn );
1515
      }
1516
    }
1517
    finally
1518
    {
1519
      try
1520
      {
1521
        if ( daemon != null )
1522
        {
1523
          // do not write to publish.log
1524
          throw new IOException();
1525
        }
1526
 
1527
        FileWriter publish = new FileWriter( basedir + fs + "publish.log", true );
1528
        if ( propertyValue.compareTo( "0" ) == 0 )
1529
        {
1530
          if ( benign )
1531
          {
4280 dpurdie 1532
            publish.write( "successfully published " + packageName + " at " + packageFullVersion + ", no action required on this platform" );
866 mhunt 1533
          }
1534
          else
1535
          {
4280 dpurdie 1536
            publish.write( "successfully published " + packageName + " at " + packageFullVersion );
866 mhunt 1537
          }
1538
 
1539
          publish.write( ls );
1540
        }
1541
        else
1542
        if ( propertyValue.compareTo( "256" ) == 0 )
1543
        {
4280 dpurdie 1544
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion +  ", internal abt issue" + ls );
866 mhunt 1545
        }
1546
        else
1547
        if ( propertyValue.compareTo( "257" ) == 0 )
1548
        {
1549
          errorReported = false;
4280 dpurdie 1550
          publish.write( "previously published " + packageName + " at " + packageFullVersion + ls );
866 mhunt 1551
        }
1552
        else
872 dpurdie 1553
        if ( propertyValue.compareTo( "262" ) == 0 )
1554
        {
4280 dpurdie 1555
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't process save_build info" + ls );
872 dpurdie 1556
        }
1557
        else
866 mhunt 1558
        if ( propertyValue.compareTo( "263" ) == 0 )
1559
        {
4280 dpurdie 1560
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't write to auto.cfg" + ls );
866 mhunt 1561
        }
1562
        else
1563
        if ( propertyValue.compareTo( "265" ) == 0 )
1564
        {
4280 dpurdie 1565
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", " + cwd + " does not exist" + ls );
866 mhunt 1566
        }
1567
        else
1568
        {
1569
          // nb jats or ant can presumably return a value 1 to 255
4280 dpurdie 1570
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", jats or ant failed" + ls );
866 mhunt 1571
        }
1572
 
1573
        publish.close();
1574
      }
1575
      catch( IOException e )
1576
      {
1577
        if ( daemon == null )
1578
        {
1579
          reportError( "Failure to write to publish.log", 266 );
1580
        }
1581
      }
1582
      catch( Exception e )
1583
      {
1584
        if ( daemon == null )
1585
        {
1586
          reportError( "Failure to write to publish.log", 266 );
1587
        }
1588
      }
1589
 
1590
      getProject().setProperty(packageAlias + ".res", propertyValue);
1591
 
1592
      if ( errorReported )
1593
      {
1594
        // designed to prevent subsequent build activity on this build file
1595
        // supply the build failure log file in message with / as file sep
1596
        // takes the form hostname/rtag_id/daemon/logfile
1597
        if ( daemon != null )
1598
        {
1599
          String message = hostname + "/" + rtagId + "/" + daemon + "/" + packageAlias + ".log";
1600
          throw new BuildException( message );
1601
        }
1602
        else
1603
        {
1604
          throw new BuildException();
1605
        }
1606
      }
1607
 
1608
    }
1609
  }
1610
 
1611
  private void deleteDirectory(File directory)
1612
  {
1613
    Log("deleteDirectory " + directory.getName(), debug);
1614
    try
1615
    {
1616
      if ( directory.exists() )
1617
      {
878 mhunt 1618
        File[] children = directory.listFiles();
866 mhunt 1619
 
1620
        if ( children != null )
1621
        {
1622
          for ( int child=0; child < children.length; child++ )
1623
          {
1624
            Log("deleteDirectory deleting " + children[ child ].getName(), warn);
1625
 
1626
            if ( children[ child ].isDirectory() )
1627
            {
1628
              deleteDirectory( children[ child ] );
1629
            }
1630
            else
1631
            {
1632
              children[ child ].delete();
1633
            }
1634
          }
1635
        }
1636
        directory.delete();
1637
      }
1638
    }
1639
    catch( SecurityException e )
1640
    {
1641
      // this can be thrown by exists and delete
1642
       Log("deleteDirectory caught SecurityException", warn);
1643
    }
1644
  }
1645
 
4280 dpurdie 1646
  //---------------------------------------------------------------------------
1647
  //    Extend the <abt> task with several new elements
1648
  // //
1649
  //        <depend package_alias="${debian_dpkg.cots}"/>
1650
  //        <platform gbe_machtype="solaris10_x86"/>
1651
  //        <jats target="all"/>
1652
  //        <ant java="java 1.6"/>
1653
  //
866 mhunt 1654
  public Depend createDepend()
1655
  {
1656
    Depend depend = new Depend();
1657
    dependCollection.add(depend);
1658
    return depend;
1659
  }
1660
 
1661
  public Platform createPlatform()
1662
  {
1663
    Platform platform = new Platform();
1664
    platformCollection.add(platform);
1665
    return platform;
1666
  }
1667
 
1668
  public Jats createJats()
1669
  {
1670
    Jats jats = new Jats();
1671
    jatsCollection.add(jats);
1672
    return jats;
1673
  }
1674
 
1675
  public Ant createAnt()
1676
  {
1677
    Ant ant = new Ant();
1678
    antCollection.add(ant);
1679
    return ant;
1680
  }
1681
}