Skip to content

Commit a6e7d70

Browse files
committed
- def main() to prevent global variable platform/platforms/fqbn
- remove obsolete test_examples_in_learningrepo()
1 parent 3ecccaf commit a6e7d70

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

build_platform.py

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
CROSS = u'\N{cross mark}'
6363
CHECK = u'\N{check mark}'
6464

65-
6665
BSP_URLS = (
6766
"https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,"
6867
"http://arduino.esp8266.com/stable/package_esp8266com_index.json,"
@@ -71,6 +70,9 @@
7170
"https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json"
7271
)
7372

73+
# global exit code
74+
success = 0
75+
7476
class ColorPrint:
7577

7678
@staticmethod
@@ -249,10 +251,12 @@ def download_uf2_utils():
249251
return False
250252
return True
251253

252-
def generate_uf2(example_path):
254+
255+
def generate_uf2(platform, fqbn, example_path):
253256
"""Generates a .uf2 file from a .bin or .hex file.
257+
:param str platform: The platform name.
258+
:param str fqbn: The fully qualified board name.
254259
:param str example_path: A path to the compiled .bin or .hex file.
255-
256260
"""
257261
if not download_uf2_utils():
258262
return None
@@ -314,12 +318,13 @@ def group_output(title):
314318
sys.stdout.flush()
315319

316320

317-
def test_examples_in_folder(folderpath):
321+
def test_examples_in_folder(platform, folderpath):
318322
global success
323+
fqbn = ALL_PLATFORMS[platform][0]
319324
for example in sorted(os.listdir(folderpath)):
320325
examplepath = folderpath+"/"+example
321326
if os.path.isdir(examplepath):
322-
test_examples_in_folder(examplepath)
327+
test_examples_in_folder(platform, examplepath)
323328
continue
324329
if not examplepath.endswith(".ino"):
325330
continue
@@ -374,11 +379,11 @@ def test_examples_in_folder(folderpath):
374379
with group_output(f"{example} {fqbn} build output"):
375380
ColorPrint.print_fail(err.decode("utf-8"))
376381
if os.path.exists(gen_file_name):
377-
if ALL_PLATFORMS[platform][1] == None:
382+
if ALL_PLATFORMS[platform][1] is None:
378383
ColorPrint.print_info("Platform does not support UF2 files, skipping...")
379384
else:
380385
ColorPrint.print_info("Generating UF2...")
381-
filename = generate_uf2(folderpath)
386+
filename = generate_uf2(platform, fqbn, folderpath)
382387
if filename is None:
383388
success = 1 # failure
384389
if IS_LEARNING_SYS:
@@ -394,52 +399,14 @@ def test_examples_in_folder(folderpath):
394399
ColorPrint.print_fail(err.decode("utf-8"))
395400
success = 1
396401

397-
def test_examples_in_learningrepo(folderpath):
398-
global success
399-
for project in os.listdir(folderpath):
400-
projectpath = folderpath+"/"+project
401-
if os.path.isdir(learningrepo):
402-
test_examples_in_learningrepo(projectpath)
403-
continue
404-
if not projectpath.endswith(".ino"):
405-
continue
406-
# found an INO!
407-
print('\t'+projectpath, end=' ', flush=True)
408-
# check if we should SKIP
409-
skipfilename = folderpath+"/."+platform+".test.skip"
410-
onlyfilename = folderpath+"/."+platform+".test.only"
411-
if os.path.exists(skipfilename):
412-
ColorPrint.print_warn("skipping")
413-
continue
414-
elif glob.glob(folderpath+"/.*.test.only") and not os.path.exists(onlyfilename):
415-
ColorPrint.print_warn("skipping")
416-
continue
417402

418-
cmd = ['arduino-cli', 'compile', '--warnings', 'all', '--fqbn', fqbn, projectpath]
419-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
420-
stderr=subprocess.PIPE)
421-
r = proc.wait()
422-
out = proc.stdout.read()
423-
err = proc.stderr.read()
424-
if r == 0:
425-
ColorPrint.print_pass(CHECK)
426-
if err:
427-
# also print out warning message
428-
ColorPrint.print_fail(err.decode("utf-8"))
429-
else:
430-
ColorPrint.print_fail(CROSS)
431-
ColorPrint.print_fail(out.decode("utf-8"))
432-
ColorPrint.print_fail(err.decode("utf-8"))
433-
success = 1
434-
435-
436-
if __name__ == "__main__":
403+
def main():
437404
# Test platforms
438405
platforms = []
439-
success = 0
440406

441407
# expand groups:
442408
for arg in sys.argv[1:]:
409+
443410
platform = ALL_PLATFORMS.get(arg, None)
444411
if isinstance(platform, list):
445412
platforms.append(arg)
@@ -460,7 +427,11 @@ def test_examples_in_learningrepo(folderpath):
460427
install_platform(":".join(fqbn.split(':', 2)[0:2]), ALL_PLATFORMS[platform]) # take only first two elements
461428
print('#'*80)
462429
if not IS_LEARNING_SYS:
463-
test_examples_in_folder(BUILD_DIR+"/examples")
430+
test_examples_in_folder(platform, BUILD_DIR+"/examples")
464431
else:
465-
test_examples_in_folder(BUILD_DIR)
432+
test_examples_in_folder(platform, BUILD_DIR)
433+
434+
435+
if __name__ == "__main__":
436+
main()
466437
exit(success)

0 commit comments

Comments
 (0)