16
16
limitations
17
17
"""
18
18
import os
19
- from os .path import dirname , abspath , basename
19
+ from os .path import dirname , abspath , basename , join , normpath
20
20
import os .path
21
21
import sys
22
22
import subprocess
23
23
from shutil import rmtree
24
+ import json
24
25
25
26
""" Import and bulid a bunch of example programs
26
27
37
38
from tools .export import EXPORTERS
38
39
from tools .project import EXPORTER_ALIASES
39
40
from tools .toolchains import TOOLCHAINS
41
+ from tools .utils import write_json_to_file
40
42
41
43
SUPPORTED_TOOLCHAINS = list (TOOLCHAINS - set (u'uARM' ))
42
44
SUPPORTED_IDES = [exp for exp in EXPORTERS .keys () + EXPORTER_ALIASES .keys ()
@@ -369,7 +371,9 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
369
371
370
372
"""
371
373
results = {}
374
+ test_json = {"builds" :{}}
372
375
valid_examples = set (examples )
376
+ base_path = os .getcwd ()
373
377
print ("\n Compiling example repos....\n " )
374
378
for example in config ['examples' ]:
375
379
example_names = [basename (x ['repo' ]) for x in get_repo_list (example )]
@@ -380,31 +384,66 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
380
384
successes = []
381
385
compiled = True
382
386
pass_status = True
387
+ if example .has_key ('test' ) and example ['test' ] and example .has_key ('baud_rate' ) and example .has_key ('compare_log' ):
388
+ test_example = True
389
+ else :
390
+ test_example = False
383
391
if example ['compile' ]:
384
392
for repo_info in get_repo_list (example ):
385
393
name = basename (repo_info ['repo' ])
386
394
os .chdir (name )
387
-
388
395
# Check that the target, toolchain and features combinations are valid and return a
389
396
# list of valid combinations to work through
390
397
for target , toolchain in target_cross_toolchain (valid_choices (example ['targets' ], targets ),
391
398
valid_choices (example ['toolchains' ], toolchains ),
392
399
example ['features' ]):
393
- print ( "Compiling %s for %s, %s" % ( name , target , toolchain ))
400
+
394
401
build_command = ["mbed-cli" , "compile" , "-t" , toolchain , "-m" , target ] + (['-v' ] if verbose else [])
395
-
396
402
if profile :
397
403
build_command .append ("--profile" )
398
404
build_command .append (profile )
399
-
400
- proc = subprocess .Popen (build_command )
401
-
402
- proc .wait ()
405
+
406
+ print ("Compiling [%s] for [%s] with toolchain [%s]\n \n > %s" % (name , target , toolchain , " " .join (build_command )))
407
+
408
+ proc = subprocess .Popen (build_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
409
+
410
+ std_out , std_err = proc .communicate ()
411
+ print ("\n #### STDOUT ####\n %s\n #### STDERR ####\n %s\n #### End of STDOUT/STDERR ####\n " % (std_out ,std_err ))
412
+
413
+ if test_example :
414
+ log = example ['compare_log' ].pop (0 )
415
+ # example['compare_log'] is a list of log file/files, which matches each examples/sub-examples from same repo.
416
+ # pop the log file out of list regardless the compilation for each example pass of fail
417
+ image = fetch_output_image (std_out )
418
+ if image :
419
+ image_info = [{"binary_type" : "bootable" ,"path" : normpath (join (name ,image )),"compare_log" :log }]
420
+ else :
421
+ print ("Warning: could not find built image for example %s" % name )
422
+
403
423
example_summary = "{} {} {}" .format (name , target , toolchain )
404
424
if proc .returncode :
405
425
failures .append (example_summary )
406
426
else :
407
- successes .append (example_summary )
427
+ if test_example :
428
+ test_group = "{}-{}-{}" .format (target , toolchain , example ['baud_rate' ])
429
+ if image :
430
+ if not test_json ['builds' ].has_key (test_group ):
431
+ test_json ['builds' ][test_group ] = {
432
+ "platform" :target ,
433
+ "toolchain" : toolchain ,
434
+ "base_path" : base_path ,
435
+ "baud_rate" : int (example ['baud_rate' ]),
436
+ "tests" :{} }
437
+ test_json ['builds' ][test_group ]['tests' ][name ]= {"binaries" :image_info }
438
+ test_status = "TEST_ON"
439
+ else :
440
+ test_status = "NO_IMAGE"
441
+ else :
442
+ print ("Warning: Test for %s will not be generated." % name )
443
+ print ("One or more of 'test', 'baud_rate', and 'compare_log' keys are missing from the example config json file\n " )
444
+ test_status = "TEST_OFF"
445
+ successes .append (example_summary + " " + test_status )
446
+
408
447
os .chdir (".." )
409
448
410
449
# If there are any compilation failures for the example 'set' then the overall status is fail.
@@ -415,6 +454,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
415
454
416
455
results [example ['name' ]] = [compiled , pass_status , successes , failures ]
417
456
457
+ write_json_to_file (test_json , "test_spec.json" )
418
458
return results
419
459
420
460
@@ -443,4 +483,14 @@ def update_mbedos_version(config, tag, examples):
443
483
return result
444
484
445
485
return 0
446
-
486
+
487
+ def fetch_output_image (output ):
488
+ """Find the build image from the last 5 lines of a given log"""
489
+ lines = output .splitlines ()
490
+ last_index = - 6 if len (lines )> 4 else (- 1 - len (lines ))
491
+ for index in range (- 1 ,last_index ,- 1 ):
492
+ if lines [index ].startswith ("Image:" ):
493
+ image = lines [index ][7 :]
494
+ if os .path .isfile (image ):
495
+ return image
496
+ return False
0 commit comments