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
@@ -369,6 +370,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
369
370
370
371
"""
371
372
results = {}
373
+ test_json = {"builds" :{}}
372
374
valid_examples = set (examples )
373
375
print ("\n Compiling example repos....\n " )
374
376
for example in config ['examples' ]:
@@ -380,31 +382,60 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
380
382
successes = []
381
383
compiled = True
382
384
pass_status = True
385
+ if example .has_key ('test' ) and example .has_key ('baud_rate' ) and example .has_key ('compare_log' ):
386
+ test_example = True
387
+ else :
388
+ test_example = False
383
389
if example ['compile' ]:
384
390
for repo_info in get_repo_list (example ):
385
391
name = basename (repo_info ['repo' ])
386
392
os .chdir (name )
387
-
388
393
# Check that the target, toolchain and features combinations are valid and return a
389
394
# list of valid combinations to work through
390
395
for target , toolchain in target_cross_toolchain (valid_choices (example ['targets' ], targets ),
391
396
valid_choices (example ['toolchains' ], toolchains ),
392
397
example ['features' ]):
393
- print ( "Compiling %s for %s, %s" % ( name , target , toolchain ))
398
+
394
399
build_command = ["mbed-cli" , "compile" , "-t" , toolchain , "-m" , target ] + (['-v' ] if verbose else [])
395
-
396
400
if profile :
397
401
build_command .append ("--profile" )
398
402
build_command .append (profile )
399
-
400
- proc = subprocess .Popen (build_command )
401
-
402
- proc .wait ()
403
+
404
+ print ("Compiling [%s] for [%s] with toolchain [%s]\n \n > %s" % (name , target , toolchain , " " .join (build_command )))
405
+
406
+ proc = subprocess .Popen (build_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
407
+
408
+ std_out , std_err = proc .communicate ()
409
+ print ("\n #### STDOUT ####\n %s\n #### STDERR ####\n %s\n #### End of STDOUT/STDERR ####\n " % (std_out ,std_err ))
410
+
411
+ if test_example :
412
+ log = example ['compare_log' ].pop (0 )
413
+ image = fetch_output_image (std_out )
414
+ if image :
415
+ image_info = [{"binary_type" : "bootable" ,"path" : normpath (join (name ,image )),"compare_log" :log }]
416
+ else :
417
+ print ("Warning: could not found built image for example %s" % name )
418
+
403
419
example_summary = "{} {} {}" .format (name , target , toolchain )
404
420
if proc .returncode :
405
421
failures .append (example_summary )
406
422
else :
407
423
successes .append (example_summary )
424
+ if test_example :
425
+ test_group = "{}-{}-{}" .format (target , toolchain , example ['baud_rate' ])
426
+ if example ['test' ] and image :
427
+ if not test_json ['builds' ].has_key (test_group ):
428
+ test_json ['builds' ][test_group ] = {
429
+ "platform" :target ,
430
+ "toolchain" : toolchain ,
431
+ "base_path" : os .getcwd () ,
432
+ "baud_rate" : int (example ['baud_rate' ]),
433
+ "tests" :{} }
434
+ test_json ['builds' ][test_group ]['tests' ][name ]= {"binaries" :image_info }
435
+ else :
436
+ print ("Warning: Test for %s will not be generated." % name )
437
+ print ("One or more of 'test' 'baud_rate' and 'compare_log' keys are missing from the json file\n " )
438
+
408
439
os .chdir (".." )
409
440
410
441
# If there are any compilation failures for the example 'set' then the overall status is fail.
@@ -415,6 +446,7 @@ def compile_repos(config, toolchains, targets, profile, verbose, examples):
415
446
416
447
results [example ['name' ]] = [compiled , pass_status , successes , failures ]
417
448
449
+ save_test_spec (test_json )
418
450
return results
419
451
420
452
@@ -444,3 +476,18 @@ def update_mbedos_version(config, tag, examples):
444
476
445
477
return 0
446
478
479
+ def save_test_spec (json_spec , name = "test_spec.json" ):
480
+ """save the given json data to test_spec.json"""
481
+ print ("Dumping json test_specs file {}" .format (name ))
482
+ with open (name , 'w' ) as outfile :
483
+ json .dump (json_spec , outfile , indent = 4 )
484
+
485
+ def fetch_output_image (output ):
486
+ """find the mbed build image from thet last 5 lines of a given log """
487
+ lines = output .splitlines ()
488
+ for index in range (- 1 ,- 6 ,- 1 ):
489
+ if lines [index ].startswith ("Image:" ):
490
+ image = lines [index ][7 :]
491
+ if os .path .isfile (image ):
492
+ return image
493
+ return False
0 commit comments