Class: Metabuild::LogHtml

Inherits:
Log
  • Object
show all
Defined in:
lib/metabuild.rb

Overview

Html log. For reporting in continuous integration systems, for example.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Log

#script

Constructor Details

#initialize(name, file) ⇒ LogHtml

Returns a new instance of LogHtml.



341
342
343
344
345
346
347
348
349
# File 'lib/metabuild.rb', line 341

def initialize(name, file)
  super(name)
  @file = file
  @fail, @success, @skip = [], [], []
  @subscripts = []
  @report_done = false
  @html = ""
  @has_bash = system("bash --version > /dev/null")
end

Instance Attribute Details

#failObject (readonly)

Returns the value of attribute fail.



339
340
341
# File 'lib/metabuild.rb', line 339

def fail
  @fail
end

#htmlObject (readonly)

Returns the value of attribute html.



339
340
341
# File 'lib/metabuild.rb', line 339

def html
  @html
end

#nameObject (readonly)

Returns the value of attribute name.



339
340
341
# File 'lib/metabuild.rb', line 339

def name
  @name
end

#successObject (readonly)

Returns the value of attribute success.



339
340
341
# File 'lib/metabuild.rb', line 339

def success
  @success
end

Instance Method Details

#add_fail(cmd, msg, output) ⇒ Object



351
352
353
# File 'lib/metabuild.rb', line 351

def add_fail(cmd, msg, output)
  @fail.push [cmd, msg, output]
end

#add_skip(cmd) ⇒ Object



359
360
361
# File 'lib/metabuild.rb', line 359

def add_skip(cmd)
  @skip.push "SKIPPED : #{cmd}"
end

#add_success(cmd, msg, output) ⇒ Object



355
356
357
# File 'lib/metabuild.rb', line 355

def add_success(cmd, msg, output)
  @success.push [cmd, msg, output]
end

#env_spawn(cmd, env, fail_msg = "", success_msg = "") ⇒ Object



637
638
639
640
641
642
# File 'lib/metabuild.rb', line 637

def env_spawn(cmd, env, fail_msg = "", success_msg = "")
  script, file, shell = setup_script(cmd, env)
  puts "@@@@@@@@@@@ Running #{cmd} in background @@@@@@@@@@@@@"
  pid = spawn(shell + script.path)
  return [pid, file, script, cmd, fail_msg, success_msg]
end

#env_system(cmd, env) ⇒ Object



627
628
629
630
631
632
633
634
635
# File 'lib/metabuild.rb', line 627

def env_system(cmd, env)
  script, file, shell = setup_script(cmd, env)
  puts "@@@@@@@@@@@ Running #{cmd} @@@@@@@@@@@@@"
  success = system(shell + script.path)
  output = file.read.strip
  script.close
  file.close
  return success, output
end

#failure(msg, cmd = "", output = "") ⇒ Object

Stop script, adding a message to the log



704
705
706
707
# File 'lib/metabuild.rb', line 704

def failure(msg, cmd = "", output = "")
  add_fail cmd, msg, output
  stop
end

#fill_htmlObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/metabuild.rb', line 374

def fill_html

  top =  "<div class='raised'>\n" +
         "<b class='b1'></b><b class='b2'></b><b class='b3'></b><b class='b4'></b><div class='boxcontent'>\n"
  bottom = "</div><b class='b4b'></b><b class='b3b'></b><b class='b2b'></b><b class='b1b'></b></div>\n"
  red = "<img src='data:image/png;base64,
            iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAIAAABL1vtsAAAAAXNSR0IArs4c6QAAAWdJREFUOMul
            VD1zgkAUfF48nDmTIZPEZM6CxgZrCitqKwsaK3+elY2NjY2NFhZQyy8AE5jMkMSLQgIpjjn8jKBb
            Hrf79i3vXsk0TbgO5cOjUhRVXFdyHMl1se8DQIJxSGlI6YbSqFY7I4EYk+fz+8mk4jjScok9j0ts
            KA3r9e9G473dZs3mTsntRspB8DgaPYzHZLE46jkm5KPV8judQNePuCgHwdNw+DwY8MpHgRiTZzPe
            nVBB2bfp9H++SIrY9ku/L5wiflq17Tx8obJ9HwHADWO3lnWq/1Md3VkWse1UAjFWLcIXKpyVuuB6
            hSBYaRY5U9hLJMviSiA+fIdjexaChQDglxCmqkUlBAvxsV3tjn0eCFbq4kvTWBGVmJBPTctcJBiv
            VPWt282ZyN59JFQDXc+jkmDMVPW11xOus5f6I8u+YSQYX/7YuYpnGCGll6+cw8UneR4AxJJUYPHx
            mmtFWStKzr/zB1QX2NNv3dXcAAAAAElFTkSuQmCC'>"
  green = "<img src='data:image/png;base64,
            iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAIAAABL1vtsAAAAAXNSR0IArs4c6QAAAWhJREFUOMul
            lMGKwjAQhsdUUlq2UCIs23oVb148eNUH8AHWJ9wHWB+gexXx0lvx2mQRHIQslgaFPaRkS+tqiv8x
            zf/1ZzIzve12C8+p3z6STiFc5BQFRekUABBcvUixWLGoZMHVe4DgFJMw3QV7TlG4NUTJYsWmcrQ4
            TWLF/kVkfr4ebJIw5RQbuaSfZ36e+fm3i8vjbHwe3kBkfv7x+pWEqf7zTXGKn4ONdIrVYW4oxHzT
            /7/jN4mSMF0PNiYpMac2/jrF3CcAIFzU9bN/SE5xF+yFixWCU+zkNxTtIgAgKGpeJwkXhUFIp7Cs
            QvOlTS2eFNHN127bhzIuAgCRYlHJuiKikkWKVYhYsUbb28i4iOZN5agTRY+czl7VYnGaLE4Ty4o0
            7hNDXR5nNhTtXx5nJvXfpI7Pw9VhHly99rDX82v/7WHXlPfL/K1knVZOr707zeLjFH/6BQC8XLzY
            fvFVPXMe1qPe1y/ZDt7tn1jpQgAAAABJRU5ErkJggg=='>" 
  yellow = "<img src='data:image/png;base64,
            iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAIAAABL1vtsAAAAAXNSR0IArs4c6QAAAg1JREFUOMul
            lEFP20AQhWdtb8xuGttSUGK7WKVSEpVje0OiQlz7b7n02gM3iJpyKwSprQL1YpoqWcu7iR07PRjF
            VkCJK+Zo6X2eeTvzUL/fh5eV9vRTkiRSSiGEECJJEgDAGFNKKaWEEIzxFoQQgjGGsoHb+tp1f9Up
            B4BIGH8mb34HH5bKe9u2KaVlCSoPwjmP+Nle6/OefUN2ZA3HqpoCQJqqcVKTM3LLOrfBp7rx0TCM
            Z7rgnE//ful4p539qxqOy/9R1ZSokuzIV/VvmipvRguAkxVFW/Uf8bNn9eWq4bizfwVw+tPXNO0k
            n0jJ/WOMUf1is75MofoFYyw3WwEAKSXKBm5ruFW/oritIcoGUspHhBCC6KOmNa6+C01rTPSREKKM
            CCxjUh1hGROiBwUiSRIEj+9XsVQ1RRAXXrywlHx/xcwKo0Z1WRg1xMzKl10BAErpLG5PuVkdMeXm
            LG4Xe0EpnYQHI9+rjhj53iQ8KBCEEMN02UPPD5wqej9w2EPPMF1CSOGFbdvz7PD88mgrxQ+c88uj
            eXZo23bhRT6L43RDebyZkutDeew43dXJrx+77w+j8Kbd/N57e+20/UY9zP33753rH7378bt6o+M4
            3fKxo7XgyyOHT++W2ThLp8vlHAAQ0hXVRErTMF8/jZz11KKUep4nd3fz4FssFgCgadp/BF/uLsa4
            3Orm+gdpYQabBkXtNgAAAABJRU5ErkJggg=='>"


  unless @subscript
    @html << "<html>\n"
    @html << "<head>\n"
    @html << "<script type='text/javascript'>\n"
    @html << "function toggleMe(a){\n"
    @html << "var e=document.getElementById(a);\n"
    @html << "if(!e)return true;\n"
    @html << "if(e.style.display=='none'){\n"
    @html << "e.style.display='block'\n"
    @html << "} else {\n"
    @html << "e.style.display='none'\n"
    @html << "}\n"
    @html << "return true;\n"
    @html << "}\n"
    @html << "</script>\n"
    @html << "<style type='text/css'>\n"
    @html << "A"
    @html << "{"
    @html << "  color:white;"
    @html << "}"
    @html << "A:link {color:#000000}"
    @html << "A:visited {color:#ffffff}"
    @html << "A:active {color:#ffffff}"
    @html << "A:hover {background-color: black; color: white}"
    @html << "div.header"
    @html << "{"
    @html << "  text-align: center;"
    @html << "  margin-left: 100px;"
    @html << "  margin-right: 100px;"
    @html << "  margin-bottom: 50px;"
    @html << "  float: center;"
    @html << "}"
    @html << "div.content"
    @html << "{"
    @html << "  float:center;"
    @html << "  margin-left:100px;"
    @html << "  margin-right:100px;"
    @html << "}"
    @html << "div.output"
    @html << "{"
    @html << "  float:center;"
    @html << "  margin-left:100px;"
    @html << "  margin-right:100px;"
    @html << "  border-style: dashed;" 
    @html << "}"
    @html << ".raised {"
    @html << "  background:transparent; "
    @html << "  width:100%;"
    @html << "}"
    @html << ".raised h1, .raised p {"
    @html << "  margin:0 10px;"
    @html << "  }"
    @html << ".raised h1 {"
    @html << "  font-size:2em; "
    @html << "  }"
    @html << ".raised p {"
    @html << "  padding-bottom:0.5em;"
    @html << "  }"
    @html << ".raised .b1, .raised .b2, .raised .b3, .raised .b4, .raised .b1b, .raised .b2b, .raised .b3b, .raised .b4b {"
    @html << "  display:block; "
    @html << "  overflow:hidden;"
    @html << "  font-size:1px;"
    @html << "  }"
    @html << ".raised .b1, .raised .b2, .raised .b3, .raised .b1b, .raised .b2b, .raised .b3b {"
    @html << "  height:1px;"
    @html << "  }"
    @html << ".raised .b2 {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #fff; "
    @html << "  border-right:1px solid #eee;"
    @html << "  }"
    @html << ".raised .b3 {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #fff; "
    @html << "  border-right:1px solid #ddd;"
    @html << "  }"
    @html << ".raised .b4 {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #fff; "
    @html << "  border-right:1px solid #aaa;"
    @html << "  }"
    @html << ".raised .b4b {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #eee; "
    @html << "  border-right:1px solid #999;"
    @html << "  }"
    @html << ".raised .b3b {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #ddd; "
    @html << "  border-right:1px solid #999;"
    @html << "  }"
    @html << ".raised .b2b {"
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #aaa; "
    @html << "  border-right:1px solid #999;"
    @html << "  }"
    @html << ".raised .b1 {"
    @html << "  margin:0 5px; "
    @html << "  background:#fff;"
    @html << "  }"
    @html << ".raised .b2, .raised .b2b {"
    @html << "  margin:0 3px; "
    @html << "  border-width:0 2px;"
    @html << "  }"
    @html << ".raised .b3, .raised .b3b {"
    @html << "  margin:0 2px;"
    @html << "  }"
    @html << ".raised .b4, .raised .b4b {"
    @html << "  height:2px; margin:0 1px;"
    @html << "  }"
    @html << ".raised .b1b {"
    @html << "  margin:0 5px; background:#999;"
    @html << "  }"
    @html << ".raised .boxcontent {"
    @html << "  display:block;  "
    @html << "  background:#ccc; "
    @html << "  border-left:1px solid #fff; "
    @html << "  border-right:1px solid #999;"
    @html << "  }"
    @html << "</style>\n"
    @html << "</head><body bgcolor='grey'>\n"
  end
  
  @html << "<div class='header'>\n"
  @html << top
  @html << "<h1>#{@name}  "
  if status
    @html << green + "</h1><br>\n"
  else
    @html << red + "</h1><br>\n"
  end
  @html << bottom + "</div>"

  @html << "<div class='content'>\n"
  @html << top
  @html << "<p><h2>Subscript run :</h2><br>\n"
  id = (rand() * 500).to_i # FIXME
  @subscripts.each do |s|
    name, status, html = s[0], s[1], s[2]
    stat = status ? green : red
    @html << "<div id='para#{id}' style='display:none'>\n"
    @html << "<div class='output'>\n"
    @html << html
    @html << "</div>"
    @html << "</div>\n"
    @html << "<br>#{stat} #{name}<input type='button' class='button' onclick='return toggleMe(\"para#{id}\")' value='Show Html log'>\n"
    id += 1
  end
  @html << "<br></p>\n"

  @html << "<p><h2>Tests :</h2><br>\n"

  @fail.each do |s|
    cmd, msg, output = s[0], s[1], s[2]
    @html << "<div id='para#{id}' style='display:none'>\n"
    @html << "<div class='output'>\n"
    @html << "<pre>" + output + "</pre>"
    @html << "</div>"
    @html << "</div>\n"
    @html << "<br>#{red} #{msg}\n"
    @html << "<input type='button' class='button' onclick='return toggleMe(\"para#{id}\")' value='Show output'>\n"
    id += 1
  end

  @success.each do |s|
    cmd, msg, output = s[0], s[1], s[2]
    @html << "<div id='para#{id}' style='display:none'>\n"
    @html << "<div class='output'>\n"
    @html << "<pre>" + output + "</pre>"
    @html << "</div>"
    @html << "</div>\n"
    @html << "<br>#{green} #{msg}\n"
    @html << "<input type='button' class='button' onclick='return toggleMe(\"para#{id}\")' value='Show output'>\n"
    id += 1
  end

  @skip.each do |cmd|
    @html << "<br>#{yellow} #{cmd}\n"
  end

  @html << "<br></p>\n"

  @html << bottom + "</div>"

  unless @subscript
    @html << "</body></html>\n"
  end

end

#finishObject



715
716
717
718
# File 'lib/metabuild.rb', line 715

def finish
  report
  exit status
end

#integrate(log) ⇒ Object



695
696
697
698
699
700
701
# File 'lib/metabuild.rb', line 695

def integrate log
  if log.kind_of? LogHtml
    @subscripts.push [log.name, log.status, log.html]
  else 
    @subscripts.push [log.name, log.status, ""]
  end
end

#outputObject



601
602
603
604
605
# File 'lib/metabuild.rb', line 601

def output
  File.open(@file, "w") do |f|
    f.puts @html
  end
end

#parallel_wait(p) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/metabuild.rb', line 644

def parallel_wait(p)
  pid, file, script, cmd, fail_msg, success_msg= p[0], p[1], p[2], p[3], p[4], p[5]
  pid, stat = Process::waitpid2(pid)
  success = stat.success?
  output = File.new(file).read.strip
  File.delete script
  File.delete file
  if success
    add_success cmd, success_msg, output
  else
    add_fail cmd, fail_msg, output
  end
end

#reportObject



363
364
365
366
367
368
369
370
371
372
# File 'lib/metabuild.rb', line 363

def report
  return if @report_done # prevent reporting several times
  fill_html
  if @subscript
    $subscript_log = self
  else
    output
  end
  @report_done = true
end

#run(command, env, fail_msg, skip) ⇒ Object

Run a shell command, log exit status



660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/metabuild.rb', line 660

def run(command, env, fail_msg, skip)
  if skip
    add_skip command
  else
    success, output= env_system(command, env)
    if success
      add_success command, "#{command} success", output
    else
      add_fail command, fail_msg, output
      stop
    end
  end
end

#setup_script(cmd, env) ⇒ Object



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/metabuild.rb', line 611

def setup_script(cmd, env)
  file = Tempfile.new("output")
  script = Tempfile.new("script")
  if @has_bash
    script.puts "exec > >(tee #{file.path}) 2>&1" # Redirect output to tmpfile + tee
    shell = "bash "
  else
    script.puts "exec > #{file.path} 2>&1" # Redirect output to tmpfile
    shell = "sh "
  end
  env.each { |k, v| script.puts "export #{k}=#{v}"}
  script.puts cmd
  script.flush
  return script, file, shell
end

#silent(command, env) ⇒ Object

Run a shell command, no logging



675
676
677
678
# File 'lib/metabuild.rb', line 675

def silent(command, env)
  success, output = env_system(command, env)
  stop unless success
end

#statusObject



607
608
609
# File 'lib/metabuild.rb', line 607

def status
  @fail.empty?
end

#stopObject

Output log and exit with error exit status



710
711
712
713
# File 'lib/metabuild.rb', line 710

def stop
  report
  exit false
end

#valid(command, env, fail_msg, success_msg, skip) ⇒ Object

Run a shell command. Log success and failure, do not abort on failure



681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/metabuild.rb', line 681

def valid(command, env, fail_msg, success_msg, skip)
  if skip
    add_skip command
  else
    success, output = env_system(command, env)
    if success
      add_success command, success_msg, output
    else
      failure(fail_msg) if ENV.has_key? "METABUILD_PARALLEL"
      add_fail command, fail_msg, output
    end
  end
end