Subversion Repositories DevTools

Rev

Rev 4285 | Go to most recent revision | 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
771
        for (Iterator<Platform> ip=platformCollection.iterator(); ip.hasNext(); )
772
        {
773
          Platform platform = ip.next();
774
          for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
775
          {
776
            Machine ma = it.next();
777
            if (ma.getMachtype().compareTo(platform.getGbe_Machtype()) == 0 )
778
            {
779
                fullyPublished = checkPublished( dpkg_archive, ma.getName() );
780
                if ( fullyPublished.compareTo("no") == 0 )
781
                {
782
                  break;
783
                }
784
            }
785
          }
786
        }
787
    }
866 mhunt 788
 
4280 dpurdie 789
    //  Detect problem with zero length descpkg file
790
    //      May be caused by write clash from multiple machines
791
    //      Don't know
792
    //
793
    if ( fullyPublished.compareTo("yes") == 0 )
868 mhunt 794
    {
4280 dpurdie 795
        if (dpkg_archive == null)
796
        {
797
          reportErrorException( "GBE_DPKG environment variable not set", 256 );
798
        }
799
 
800
        String destination = dpkg_archive + fs + packageName + fs + packageFullVersion;
801
        String filename = "descpkg";
802
 
803
        File flag = new File( destination, filename );
804
 
805
        //  File Must
806
        //      Exist
807
        //      Be a File
808
        //      Have a length. Problem appers to be zero length file
809
        //
810
        boolean found = false;
4285 dpurdie 811
        Log( "published. Descpkg Sanity Test: " + flag.getPath(), info );
4280 dpurdie 812
        if (!flag.exists() ) {
813
            Log( "published. Descpkg Sanity Test: File not found", fatal );
814
 
815
        } else if ( !flag.isFile()) {
816
            Log( "published. Descpkg Sanity Test: Is not a file", fatal );
817
 
818
        } else if ( flag.length() <= 0 ) {
819
            Log( "published. Descpkg Sanity Test: Zero length File", fatal );
820
 
821
        } else {
822
            Log( "published. Descpkg Sanity Test: Passed", info );
823
            found = true;
824
        }
825
 
826
        if (!found) {
827
            fullyPublished = "no";
828
        }
868 mhunt 829
    }
866 mhunt 830
 
831
    if ( fullyPublished.compareTo("yes") == 0 && daemon != null )
832
    {
833
      if ( doesNotRequireSourceControlInteraction == null )
834
      {
872 dpurdie 835
        //
836
        //  Save modified build files
837
        //  Label the resultant files
838
        //  Create a command of the form:
924 dpurdie 839
        //      jats etool jats_vcssave_build
840
        //          -infile     auto.xml/auto.pl
841
        //          -outfile    xxxdepends.xml/build.pl
842
        //          -pname      package_name
843
        //          -pversion   package_version
844
        //          -infofile   path_to_info_file
845
        //          -wiplabel   Existing WIP label (optional)
846
        //          -baselabel  View label
847
 
872 dpurdie 848
        Log("publish save build files", warn);
849
 
850
        File saveinfo = new File(basedir + fs + rtagId + "abtinfo.txt");
851
        if ( saveinfo.exists() )
866 mhunt 852
        {
872 dpurdie 853
          saveinfo.delete();
866 mhunt 854
        }
855
 
856
        initCommand();
857
        addCommand( "jats" );
872 dpurdie 858
        if ( buildStandard == BuildStandards.ANT )
866 mhunt 859
        {
872 dpurdie 860
          addCommand( "-locatefile=" + packageName + ".xml" );
861
        }
862
        else
863
        {
866 mhunt 864
          addCommand( "-locatepkg=" + packageLoc );
865
        }
872 dpurdie 866
 
866 mhunt 867
        addCommand( "etool" );
924 dpurdie 868
        addCommand( "jats_vcssave_build" );
872 dpurdie 869
 
866 mhunt 870
        if ( buildStandard == BuildStandards.ANT )
871
        {
872
          addCommand( "-infile" );
873
          addCommand( "auto.xml" );
874
          addCommand( "-outfile" );
875
          addCommand( packageName + "depends.xml" );
876
        }
872 dpurdie 877
        else
878
        {
879
          addCommand( "-infile" );
880
          addCommand( "auto.pl" );
881
          addCommand( "-outfile" );
882
          addCommand( "build.pl" );
883
        }
866 mhunt 884
 
872 dpurdie 885
        addCommand( "-pname" );
886
        addCommand( packageName );
887
 
888
        addCommand( "-pversion" );
4280 dpurdie 889
        addCommand( packageFullVersion );
872 dpurdie 890
 
891
        if ( directChange != null )
892
        {
926 dpurdie 893
          addCommand( "-isawip" );
872 dpurdie 894
        }
926 dpurdie 895
 
896
        addCommand( "-infofile" );
897
        addCommand( saveinfo.getAbsolutePath() );
872 dpurdie 898
 
926 dpurdie 899
        addCommand( "-baselabel" );
900
        addCommand( packageVcsTag );
924 dpurdie 901
 
902
 
866 mhunt 903
        if ( oldExtension != null && newExtension != null && packageExtension.compareTo( newExtension ) == 0 )
904
        {
905
          // only branch build.pl where its extension is being migrated
906
          addCommand( "-branch" );
907
          addCommand( "project_migration_branch" );
908
          addCommand( "-newbranch" );
909
        }
872 dpurdie 910
 
911
        runCommand( "Failure to save build file changes into source control" );
912
 
913
        //
898 mhunt 914
        //  The command creates an info file(saveinfo) to pass information back
872 dpurdie 915
        //  to the caller (me). This is a 'properties' file
916
        //  Need to extract the following information
924 dpurdie 917
        //    newVcsTag - which is held as VCS.tag
872 dpurdie 918
        //
898 mhunt 919
        Log("publish read info file", info);
872 dpurdie 920
 
921
        Properties properties = new Properties();
922
        try
866 mhunt 923
        {
872 dpurdie 924
          properties.load(new FileInputStream(saveinfo));
866 mhunt 925
        }
872 dpurdie 926
        catch (IOException e)
866 mhunt 927
        {
872 dpurdie 928
          reportErrorException( "Failed to open save_build info file", 262 );
866 mhunt 929
        }
872 dpurdie 930
 
924 dpurdie 931
        newVcsTag = properties.getProperty("VCS.tag" );
932
        if ( newVcsTag == null )
872 dpurdie 933
        {
924 dpurdie 934
          reportErrorException( "Save_build info. Missing property:VCS.tag", 262 );
872 dpurdie 935
        }
924 dpurdie 936
        Log("publish read info file. Label:" + newVcsTag , info);
872 dpurdie 937
      }
866 mhunt 938
    }
939
  }
940
 
4280 dpurdie 941
/** determineBuildStandard
942
 *  Process the current target and set up
943
 *      buildStandard
944
 *      compileTarget
945
 *      javaVersion
946
 */
947
  private void determineBuildStandard() throws Exception
948
  {
949
      // Scan the platform collection looking for an entry that matches the current machine
950
      // Remember the index - as it used to locate a matching entry in the 
951
      // jatsCollection or jatsCollection
952
      // 
953
      // Just wanting to set up
954
      //    buildStandard and one of
955
      //        compileTarget
956
      //    OR  javaVersion
957
      //
958
      int platformIndex = 0;
959
      for ( Iterator<Platform> it = platformCollection.iterator(); it.hasNext(); )
960
      {
961
        Platform platform = it.next();
962
        platformIndex++;
963
 
964
        if ( gbeMachtype.compareTo( platform.getGbe_Machtype() ) == 0 )
965
        {
966
          break;
967
        }
968
      }
969
 
970
      int index = 0;
971
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
972
      {
973
        Jats j = it.next();
974
        index++;
975
 
976
        if ( platformIndex == index )
977
        {
978
          String target = j.getTarget();
979
          if ( target.compareTo( "none" ) == 0 )
980
          {
981
            compileTarget = CompileTargets.NONE;
982
          }
983
          else
984
          if ( target.compareTo( "production" ) == 0 )
985
          {
986
            compileTarget = CompileTargets.PROD;
987
          }
988
          else
989
          if ( target.compareTo( "debug") == 0 )
990
          {
991
            compileTarget = CompileTargets.DEBUG;
992
          }
993
          else
994
          if ( target.compareTo( "all" ) == 0 )
995
          {
996
            compileTarget = CompileTargets.ALL;
997
          }
998
          else
999
          {
1000
            reportErrorException( "Failure, unsupported build environment " + target + ", internal ABT problem", 256 );
1001
          }
1002
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1003
          Log( "execute compileTarget: ", compileTarget.getCompileTarget() );
1004
          break;
1005
        }
1006
      }
1007
 
1008
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1009
      {
1010
        Ant a = it.next();
1011
        index++;
1012
 
1013
        if ( platformIndex == index )
1014
        {
1015
          buildStandard = BuildStandards.ANT;
1016
          String version = a.getJava();
1017
          if ( version.compareTo( "none" ) == 0 )
1018
          {
1019
            javaVersion = JavaVersions.NONE;
1020
          }
1021
          else
1022
          if ( version.compareTo( "1.4" ) == 0 )
1023
          {
1024
            javaVersion = JavaVersions.FOUR;
1025
          }
1026
          else
1027
          if ( version.compareTo( "1.5" ) == 0 )
1028
          {
1029
            javaVersion = JavaVersions.FIVE;
1030
          }
1031
          else
1032
          if ( version.compareTo( "1.6" ) == 0 )
1033
          {
1034
            javaVersion = JavaVersions.SIX;
1035
          }
1036
          else
1037
          if ( version.compareTo( "1.7" ) == 0 )
1038
          {
1039
            javaVersion = JavaVersions.SEVEN;
1040
          }
1041
          else
1042
          {
1043
            reportErrorException( "Failure, unsupported build environment " + version + ", internal ABT problem", 256 );
1044
          }
1045
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1046
          Log( "execute javaVersion: ", javaVersion.getJavaVersion() );
1047
          break;
1048
        }
1049
      }
1050
  }
1051
 
1052
  /** execute
1053
   *  The Aant Task entry point. This is invoked to perform the
1054
   *  targets action
1055
   */
866 mhunt 1056
  public void execute()
1057
  {
1058
    boolean errorReported = false;
1059
    boolean benign = false;
4280 dpurdie 1060
 
1061
 
866 mhunt 1062
    try
1063
    {
1064
      re = new Redirector(this);
1065
 
1066
      // early ant built in
1067
      project = getProject();
1068
      // required by the Execute API on non win32 platforms, don't ask because I don't know
1069
      project.setProperty("ant.home", System.getenv("ANT_HOME"));
4280 dpurdie 1070
 
866 mhunt 1071
      daemon = project.getProperty("abt_daemon");
1072
      if ( daemon != null )
1073
      {
1074
        Log("execute daemon: yes", info);
1075
      }
4280 dpurdie 1076
 
1077
      // Get the current hostname
1078
      // Perhaps this should be injected by the calling task
1079
      // Daemon Mode:
1080
      //    When sending emails
1081
      //    Identify the target machine parameters
1082
      // Escrow Mode:
1083
      //    Deterime escrow build components    
1084
      try
1085
      {
1086
        InetAddress local = InetAddress.getLocalHost();
1087
        hostname = local.getHostName();
1088
      }
1089
      catch( UnknownHostException e )
1090
      {
1091
        // do nothing, shouldn't happen, but hostname will be "unknown" if it does
1092
      }
1093
 
866 mhunt 1094
      // early target related
1095
      target = getOwningTarget();
1096
      packageAlias = target.getName();
924 dpurdie 1097
      packageVcsTag = project.getProperty(packageAlias + "packagevcstag");
1098
      Log("execute packageVcsTag: ", packageVcsTag);
866 mhunt 1099
 
924 dpurdie 1100
      if ( packageVcsTag == null )
866 mhunt 1101
      {
1102
        // this abt task has no properties, by design
1103
        // future use to estimate build completion time
898 mhunt 1104
        Log("execute packageAlias: " + packageAlias, info);
866 mhunt 1105
        return;
1106
      }
1107
 
898 mhunt 1108
      Log("execute packageAlias: " + packageAlias, warn);
1109
 
866 mhunt 1110
      // ant built in
1111
      basedir = project.getProperty("basedir");
1112
      Log("execute basedir: ", basedir);
1113
 
1114
      // global
4280 dpurdie 1115
 
1116
      if ( gbeMachtype == null ) {
1117
        reportErrorException( "GBE_MACHTYPE environment variable not set", 256 );
866 mhunt 1118
      }
4280 dpurdie 1119
 
866 mhunt 1120
      mailServer = project.getProperty("abt_mail_server");
1121
      if ( mailServer == null )
1122
      {
4280 dpurdie 1123
          reportErrorException( "Missing ANT property: abt_mail_server", 256 );
866 mhunt 1124
      }
1125
      Log("execute mailServer: ", mailServer);
1126
 
1127
      mailSender = project.getProperty( "abt_mail_sender" );
1128
      if ( mailSender == null )
1129
      {
4280 dpurdie 1130
          reportErrorException( "Missing ANT property: abt_mail_sender", 256 );
866 mhunt 1131
      }
1132
      Log("execute mailSender: ", mailSender);
4280 dpurdie 1133
 
1134
      for (Iterator<Owner> it=ABTData.ownerCollection.iterator(); it.hasNext(); )
1135
      {
1136
        Owner owner = it.next();
1137
 
1138
        if ( owners == null )
1139
        {
1140
          owners = new String();
1141
        }
1142
        else
1143
        {
1144
          owners += ",";
1145
        }
1146
        owners += owner.getEmail();
1147
        Log("execute owner: ", owner.getEmail());
1148
      }
1149
      Log("execute owners: ", owners);
1150
 
866 mhunt 1151
 
1152
      // project related
1153
      rtagId = project.getProperty("abt_rtag_id");
1154
      Log("execute rtagId: ", rtagId);
1155
 
1156
      release = project.getProperty("abt_release");
1157
      Log("execute release: ", release);
4212 dpurdie 1158
 
866 mhunt 1159
      buildtoolVersion = project.getProperty("abt_buildtool_version");
1160
      Log("execute buildtoolVersion: ", buildtoolVersion);
4212 dpurdie 1161
 
866 mhunt 1162
      family = project.getProperty("abt_family");
1163
      Log("execute family: ", family);
4212 dpurdie 1164
 
866 mhunt 1165
      oldExtension = project.getProperty("abt_old_extension");
1166
      Log("execute oldExtension: ", oldExtension);
4212 dpurdie 1167
 
866 mhunt 1168
      newExtension = project.getProperty("abt_new_extension");
1169
      Log("execute newExtension: ", newExtension);
4212 dpurdie 1170
 
908 mhunt 1171
      testBuildInstruction = project.getProperty("abt_test_build_instruction");
1172
      Log("execute testBuildInstruction: ", testBuildInstruction);
4280 dpurdie 1173
 
1174
      // Locate Machine Information for this machine
1175
      //    It contains the build filter for the current machine
1176
      //    Only present in daemon builds
1177
      //
1178
      for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
1179
      {
1180
        Machine ma = it.next();
1181
        if (ma.getName().compareTo(hostname) == 0 )
1182
        {
1183
            mMachine = ma;
1184
            Log("execute machine: ", mMachine.getName() + ", Type:" + mMachine.getMachtype() + ", Class:" + mMachine.getMachclass() + ", Master:" + mMachine.getMaster() ); 
1185
            break;
1186
        }
1187
      }
1188
 
1189
      if ( (daemon != null) && (mMachine == null) ) {
1190
          reportErrorException( "Cannot find Machine entry for: " + hostname, 256 );
1191
      }
908 mhunt 1192
 
4280 dpurdie 1193
      //  Setup machine specific information
1194
      //    Build Filter
1195
      //    Master Mode
1196
      if (mMachine != null) 
1197
      {
1198
          if (mMachine.getMaster() != null)
1199
          {
1200
            master = true;
1201
            Log("execute master: ", "true");
1202
          }
1203
 
1204
          gbebuildfilter = mMachine.getBuildfilter(); 
1205
          if ( gbebuildfilter != null )
1206
          {
1207
            //
1208
            // Ensure the Execute task passes gbebuildfilter as one parameter...
1209
            // Replace spaces with commas
1210
            //
1211
            gbebuildfilter = gbebuildfilter.replace( " ", ",");
1212
            Log("execute gbebuildfilter: ", gbebuildfilter);
1213
          }
1214
      }
1215
 
908 mhunt 1216
      if ( testBuildInstruction == null )
1217
      {
1218
        // use a meaningful default
1219
        testBuildInstruction = "0";
1220
      }
866 mhunt 1221
 
1222
      // redirect all thread (therefore jats) output to the output file in the basedir
1223
      if ( daemon != null )
1224
      {
1225
        // thread logging is done to <rtagId>thread.log and is continually re-used every build
1226
        // full thread and non thread (ie abt logging) is done to the DefaultLogger set up by the build daemons, <rtagId>.log
1227
        // this file is copied on a build failure to cwd (if it exists) and is continually re-used every build
1228
        output = new File( rtagId + "thread.log" );
1229
        // capture full logging
1230
        re.setAlwaysLog(true);
1231
      }
1232
      else
1233
      {
1234
        output = new File( packageAlias + ".log" );
1235
      }
1236
 
1237
      re.setOutput( output );
1238
      thread = new Execute(re.createHandler());
1239
      thread.setAntRun( project );
1240
      thread.setVMLauncher(false);
1241
 
1242
      // must set up package name and owners before reportError
1243
      packageName = project.getProperty(packageAlias + "packagename");
1244
      Log("execute packageName: ", packageName);
1245
 
4280 dpurdie 1246
      //-----------------------------------------------------------------------
1247
      //    AbtSetUp
1248
      //
866 mhunt 1249
      if ( packageAlias.compareTo("AbtSetUp") == 0 )
1250
      {
924 dpurdie 1251
        // create rtag_id directory from scratch
1252
        File rId = new File( rtagId );
1253
        deleteDirectory( rId );
1254
        rId.mkdirs();
866 mhunt 1255
 
924 dpurdie 1256
        // jats jats_vcsrelease -extract -label=pkgVcsTag -path=src_path -root=rtag_id/timestamp -tag=timestamp -noprefix
1257
        initCommand();
1258
        addCommand( "jats" );
1259
        addCommand( "jats_vcsrelease" );
1260
        addCommand( "-extract" );
1261
        addCommand( "-label=" + packageVcsTag );
1262
        addCommand( "-root=" + rtagId );
1263
        addCommand( "-tag=" + daemon );
1264
        addCommand( "-noprefix" );
1265
        addCommand( "-verbose=2" );
866 mhunt 1266
 
924 dpurdie 1267
        runCommand( "Failure to extract source code from source control" );
1268
        return;
866 mhunt 1269
      }
1270
 
4280 dpurdie 1271
      //-----------------------------------------------------------------------
1272
      //    AbtTearDown
1273
      //
866 mhunt 1274
      if ( packageAlias.compareTo("AbtTearDown") == 0 )
1275
      {
924 dpurdie 1276
        // tear the build view down, regardless of build error
866 mhunt 1277
        wd = new File( basedir );
1278
 
1279
        // only now change the thread working directory back
1280
        thread.setWorkingDirectory( wd );
1281
 
868 mhunt 1282
        File buildDirectory = new File(wd + getProject().getProperty("abt_package_location"));
866 mhunt 1283
 
1284
        if ( buildDirectory.exists() )
1285
        {
4212 dpurdie 1286
          // jats jats_vcsrelease -label pkgTag -root=rtag_id/timestamp -tag=timestamp -noprefix -delete=2
866 mhunt 1287
          initCommand();
1288
          addCommand( "jats" );
924 dpurdie 1289
          addCommand( "jats_vcsrelease" );
1290
          addCommand( "-label=" + packageVcsTag );
878 mhunt 1291
          addCommand( "-root=" + rtagId );
866 mhunt 1292
          addCommand( "-tag=" + daemon );
1293
          addCommand( "-noprefix" );
924 dpurdie 1294
          addCommand( "-delete=2" );
1295
          addCommand( "-verbose=2" );
1296
 
866 mhunt 1297
          runCommand( "Failure to remove source code extraction" );
1298
        }
1299
 
878 mhunt 1300
        // delete the rtag directory
1301
        deleteDirectory( new File( rtagId ) );
1351 dpurdie 1302
 
1303
        // Always save the log files
1304
        saveLogs();
1305
 
866 mhunt 1306
        return;      
1307
      }
1308
 
4280 dpurdie 1309
      // target related    
1310
      packageVersion = project.getProperty(packageAlias + "packageversion");
1311
      Log("execute packageVersion: ", packageVersion);
1312
 
1313
      packageExtension = project.getProperty(packageAlias + "packageextension");
1314
      Log("execute packageExtension: ", packageExtension);
1315
 
1316
      packageFullVersion = packageVersion + packageExtension;
1317
      Log("execute packageFullVersion: ", packageFullVersion);
1318
 
1319
      packageLoc = packageName + packageExtension;
1320
      Log("execute packageLoc: ", packageLoc);
1321
 
1322
      generic = project.getProperty(packageAlias + "generic");
1323
      if ( generic != null ) {
1324
        Log("execute generic: yes", info);  
1325
      }
1326
 
1327
      // Extract the build standard information from the abt element
1328
      determineBuildStandard();
1329
 
866 mhunt 1330
      directChange = project.getProperty(packageAlias + "directchange");
1331
      if ( directChange != null )
1332
      {
1333
        Log("execute directChange: yes", info);  
1334
      }
1335
 
1336
      doesNotRequireSourceControlInteraction = project.getProperty(packageAlias + "doesnotrequiresourcecontrolinteraction");
1337
      if ( doesNotRequireSourceControlInteraction != null )
1338
      {
1339
        Log("execute doesNotRequireSourceControlInteraction: true", info);  
1340
      }
1341
 
4280 dpurdie 1342
      //    Display details of the Platform Jats and Ant elements within the
1343
      //    current ABT element
1344
      //
866 mhunt 1345
      for (Iterator<Platform> it=platformCollection.iterator(); it.hasNext(); )
1346
      {
1347
        Platform platform = it.next();
1348
        Log("execute platform: ", platform.getGbe_Machtype());
1349
      }
1350
 
1351
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
1352
      {
1353
        Jats jats = it.next();
1354
        Log("execute jats: ", jats.getTarget());
1355
      }
1356
 
1357
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1358
      {
1359
        Ant ant = it.next();
1360
        Log("execute ant: ", ant.getJava());
1361
      }
924 dpurdie 1362
 
1363
      // Set newVsTag to the existing VcsTag
1364
      // Will be used when rebuilding a package that already exists
1365
      newVcsTag = packageVcsTag;
1366
 
866 mhunt 1367
      loc = project.getProperty(packageAlias + "loc");
1368
      Log("execute loc: ", loc);
4280 dpurdie 1369
 
866 mhunt 1370
      cwd = basedir + loc;
1371
      Log("execute cwd: ", cwd);
4280 dpurdie 1372
 
866 mhunt 1373
      wd = new File( cwd );
1374
      if ( !wd.exists() )
1375
      {
1376
        reportErrorException( "Failure " + cwd + " does not exist", 265 );
1377
      }
1378
 
1379
      // only now change the thread working directory
1380
      thread.setWorkingDirectory( wd );
1381
 
4280 dpurdie 1382
      //-----------------------------------------------------------------------
1383
      //    AbtPublish
1384
      //    Note: Have changed directory to the working directory
1385
      //
866 mhunt 1386
      if ( packageAlias.compareTo("AbtPublish") == 0 )
1387
      {
4280 dpurdie 1388
        publish();
866 mhunt 1389
        if ( daemon != null )
1390
        {
1391
          // set properties to drive BuildThread attributes
1392
          // these are used to determine what ant did
1393
          getProject().setProperty("abt_fully_published", fullyPublished);
924 dpurdie 1394
          getProject().setProperty("abt_new_vcstag", newVcsTag);
866 mhunt 1395
        }
1396
 
1397
        return;
1398
      }
1399
 
4280 dpurdie 1400
      //-----------------------------------------------------------------------
1401
      //    Build Target
1402
      //    Note: Have changed directory to the working directory
1403
      //
866 mhunt 1404
      packageVersionID = project.getProperty(packageAlias + "pv_id");
1405
      Log("execute packageVersionID: ", packageVersionID);
1406
      unittests = project.getProperty(packageAlias + "unittests");
1407
 
1408
      if ( unittests != null )
1409
      {
1410
        Log("execute unittests: yes", info);
1411
      }
1412
 
1413
      for (Iterator<Depend> it=dependCollection.iterator(); it.hasNext(); )
1414
      {
1415
        Depend depend = it.next();
1416
        Log("execute depend: ", depend.getPackage_Alias());
1417
      }
1418
 
1419
      if ( daemon == null )
1420
      {
1421
        // escrow centric
1422
        // recognise an escrow will be run multiple times on a build stage
1423
        // do not force continuous rebuilding each time
1424
        // has this package version already been published on this platform
4280 dpurdie 1425
        String published = checkPublished( dpkg_archive, ( generic != null ) ? "generic" : hostname );
1426
 
866 mhunt 1427
        if ( published.compareTo("no") != 0 )
1428
        {
1429
          Log( "execute package has been published on this platform - skipping", info);
1430
          propertyValue = "257";
1431
          throw new Exception();
1432
        }
1433
      }
910 mhunt 1434
      else
866 mhunt 1435
      {
910 mhunt 1436
        // DEVI 56714 daemon centric check
1437
        String majorV = this.getClass().getPackage().getSpecificationVersion();
1438
 
1439
        if ( buildtoolVersion.compareTo( majorV ) != 0 )
1440
        {
1441
          reportErrorException( "Failure, incompatible build.xml not generated with version " + majorV, 256 );
1442
        }
866 mhunt 1443
      }
1444
 
1445
      if ( ( buildStandard == BuildStandards.ANT && javaVersion == JavaVersions.NONE ) ||
1446
           ( buildStandard == BuildStandards.JATS && compileTarget == CompileTargets.NONE ) )
1447
      {
4280 dpurdie 1448
        //  Flag a benign build is good
1449
        //  A benign is a no-operation. The build is not rquired on the current machine
1450
        //  Issue: We have already extracted the source and will need to tear it down
1451
        //
866 mhunt 1452
        benign = true;
1453
      }
1454
 
4280 dpurdie 1455
      // Pre Build processing
1456
      //    Init metrics gathering
1457
      //    Generate auto.pl or <PackageName>depends.xml file
1458
      //
1459
      genbuild();
866 mhunt 1460
 
4280 dpurdie 1461
      if ( !benign && gatherMetricsOnly == null )
866 mhunt 1462
      {
4280 dpurdie 1463
          //
1464
          //    Build the target package
1465
          //
1466
          jatsBuildPackage();
866 mhunt 1467
      }
1468
 
1469
      if ( master )
1470
      {
1471
        File metrics = new File(basedir + fs + rtagId + "abtmetrics.txt");
1472
        if ( metrics.exists() )
1473
        {
1474
          metrics.delete();
1475
        }
1476
 
1477
        // gather metrics
886 mhunt 1478
        // jats -locatefile=.jats.packageroot etool jats_metrics -mode=finish -out=abtmetrics.txt
866 mhunt 1479
        initCommand();
1480
        addCommand( "jats" );
886 mhunt 1481
        addCommand( "-locatefile=.jats.packageroot" );
866 mhunt 1482
        addCommand( "etool" );
1483
        addCommand( "jats_metrics" );
1484
        addCommand( "-mode=finish" );
1485
        addCommand( "-out=" + metrics.getAbsolutePath() );
1486
 
1487
        runCommand( "Failure to collect metrics" );
1488
      }
1489
    }
1490
    catch( SecurityException e)
1491
    {
1492
      errorReported = true;
1493
      reportError( "Failure caught SecurityException", 256 );
1494
    }
1495
    catch( IOException e)
1496
    {
1497
      errorReported = true;
1498
      reportError( "Failure caught IOException", 256 );
1499
    }
1500
    catch( Exception e)
1501
    {
1502
      // assume this condition has been reported on
1503
      errorReported = true;
1504
 
1505
      if ( e.getMessage() != null )
1506
      {
898 mhunt 1507
        Log( "execute caught Exception " + e.getMessage(), warn );
866 mhunt 1508
      }
1509
      else
1510
      {
1511
        Log( "execute caught Exception", warn );
1512
      }
1513
    }
1514
    finally
1515
    {
1516
      try
1517
      {
1518
        if ( daemon != null )
1519
        {
1520
          // do not write to publish.log
1521
          throw new IOException();
1522
        }
1523
 
1524
        FileWriter publish = new FileWriter( basedir + fs + "publish.log", true );
1525
        if ( propertyValue.compareTo( "0" ) == 0 )
1526
        {
1527
          if ( benign )
1528
          {
4280 dpurdie 1529
            publish.write( "successfully published " + packageName + " at " + packageFullVersion + ", no action required on this platform" );
866 mhunt 1530
          }
1531
          else
1532
          {
4280 dpurdie 1533
            publish.write( "successfully published " + packageName + " at " + packageFullVersion );
866 mhunt 1534
          }
1535
 
1536
          publish.write( ls );
1537
        }
1538
        else
1539
        if ( propertyValue.compareTo( "256" ) == 0 )
1540
        {
4280 dpurdie 1541
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion +  ", internal abt issue" + ls );
866 mhunt 1542
        }
1543
        else
1544
        if ( propertyValue.compareTo( "257" ) == 0 )
1545
        {
1546
          errorReported = false;
4280 dpurdie 1547
          publish.write( "previously published " + packageName + " at " + packageFullVersion + ls );
866 mhunt 1548
        }
1549
        else
872 dpurdie 1550
        if ( propertyValue.compareTo( "262" ) == 0 )
1551
        {
4280 dpurdie 1552
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't process save_build info" + ls );
872 dpurdie 1553
        }
1554
        else
866 mhunt 1555
        if ( propertyValue.compareTo( "263" ) == 0 )
1556
        {
4280 dpurdie 1557
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't write to auto.cfg" + ls );
866 mhunt 1558
        }
1559
        else
1560
        if ( propertyValue.compareTo( "265" ) == 0 )
1561
        {
4280 dpurdie 1562
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", " + cwd + " does not exist" + ls );
866 mhunt 1563
        }
1564
        else
1565
        {
1566
          // nb jats or ant can presumably return a value 1 to 255
4280 dpurdie 1567
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", jats or ant failed" + ls );
866 mhunt 1568
        }
1569
 
1570
        publish.close();
1571
      }
1572
      catch( IOException e )
1573
      {
1574
        if ( daemon == null )
1575
        {
1576
          reportError( "Failure to write to publish.log", 266 );
1577
        }
1578
      }
1579
      catch( Exception e )
1580
      {
1581
        if ( daemon == null )
1582
        {
1583
          reportError( "Failure to write to publish.log", 266 );
1584
        }
1585
      }
1586
 
1587
      getProject().setProperty(packageAlias + ".res", propertyValue);
1588
 
1589
      if ( errorReported )
1590
      {
1591
        // designed to prevent subsequent build activity on this build file
1592
        // supply the build failure log file in message with / as file sep
1593
        // takes the form hostname/rtag_id/daemon/logfile
1594
        if ( daemon != null )
1595
        {
1596
          String message = hostname + "/" + rtagId + "/" + daemon + "/" + packageAlias + ".log";
1597
          throw new BuildException( message );
1598
        }
1599
        else
1600
        {
1601
          throw new BuildException();
1602
        }
1603
      }
1604
 
1605
    }
1606
  }
1607
 
1608
  private void deleteDirectory(File directory)
1609
  {
1610
    Log("deleteDirectory " + directory.getName(), debug);
1611
    try
1612
    {
1613
      if ( directory.exists() )
1614
      {
878 mhunt 1615
        File[] children = directory.listFiles();
866 mhunt 1616
 
1617
        if ( children != null )
1618
        {
1619
          for ( int child=0; child < children.length; child++ )
1620
          {
1621
            Log("deleteDirectory deleting " + children[ child ].getName(), warn);
1622
 
1623
            if ( children[ child ].isDirectory() )
1624
            {
1625
              deleteDirectory( children[ child ] );
1626
            }
1627
            else
1628
            {
1629
              children[ child ].delete();
1630
            }
1631
          }
1632
        }
1633
        directory.delete();
1634
      }
1635
    }
1636
    catch( SecurityException e )
1637
    {
1638
      // this can be thrown by exists and delete
1639
       Log("deleteDirectory caught SecurityException", warn);
1640
    }
1641
  }
1642
 
4280 dpurdie 1643
  //---------------------------------------------------------------------------
1644
  //    Extend the <abt> task with several new elements
1645
  // //
1646
  //        <depend package_alias="${debian_dpkg.cots}"/>
1647
  //        <platform gbe_machtype="solaris10_x86"/>
1648
  //        <jats target="all"/>
1649
  //        <ant java="java 1.6"/>
1650
  //
866 mhunt 1651
  public Depend createDepend()
1652
  {
1653
    Depend depend = new Depend();
1654
    dependCollection.add(depend);
1655
    return depend;
1656
  }
1657
 
1658
  public Platform createPlatform()
1659
  {
1660
    Platform platform = new Platform();
1661
    platformCollection.add(platform);
1662
    return platform;
1663
  }
1664
 
1665
  public Jats createJats()
1666
  {
1667
    Jats jats = new Jats();
1668
    jatsCollection.add(jats);
1669
    return jats;
1670
  }
1671
 
1672
  public Ant createAnt()
1673
  {
1674
    Ant ant = new Ant();
1675
    antCollection.add(ant);
1676
    return ant;
1677
  }
1678
}