|
| 1 | +import os |
| 2 | +import glob |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import time |
| 6 | + |
| 7 | +travis = False |
| 8 | +if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true": |
| 9 | + travis = True |
| 10 | + |
| 11 | +all_warnings = False |
| 12 | +if "ALL_WARNINGS" in os.environ and os.environ["ALL_WARNINGS"] == "true": |
| 13 | + all_warnings = True |
| 14 | + |
| 15 | +ENV_VARIABLE_NAME = 'VARIANT' |
| 16 | + |
| 17 | + |
| 18 | +exit_status = 0 |
| 19 | +success_count = 0 |
| 20 | +fail_count = 0 |
| 21 | + |
| 22 | +build_format = '| {:20} | {:30} | {:9} ' |
| 23 | +build_separator = '-' * 78 |
| 24 | + |
| 25 | +variants_dict = { |
| 26 | + 'feather52840': 'Feather nRF52840 Express', |
| 27 | + 'cplaynrf52840': 'Circuit Playground Bluefruit Express', |
| 28 | + 'itsybitsy52840': 'ItsyBitsy nRF52840 Express', |
| 29 | + 'cluenrf52840': 'Clue nRF52840', |
| 30 | + 'feather52832': 'Feather nRF52832' |
| 31 | +} |
| 32 | + |
| 33 | +# STDERR receives output that starts with the following text, none of which should be considered a warning or error... |
| 34 | +output_to_ignore = ( |
| 35 | + 'Picked up JAVA_TOOL_OPTIONS:', |
| 36 | + 'Loading configuration...', |
| 37 | + 'Initializing packages...', |
| 38 | + 'Preparing boards...', |
| 39 | + 'Verifying...', |
| 40 | +) |
| 41 | + |
| 42 | +def errorOutputFilter(line): |
| 43 | + if len(line) == 0: |
| 44 | + return False |
| 45 | + if line.isspace(): # Note: empty string does not match here! |
| 46 | + return False |
| 47 | + if line.startswith(output_to_ignore): # alternatively, can trim() each line, but that would create lots of short-lived strings... |
| 48 | + return False |
| 49 | + # TODO: additional items to remove? |
| 50 | + return True |
| 51 | + |
| 52 | + |
| 53 | +def build_examples(variant): |
| 54 | + global exit_status, success_count, fail_count, build_format, build_separator |
| 55 | + |
| 56 | + print('\n') |
| 57 | + print(build_separator) |
| 58 | + print('| {:^74} |'.format(variants_dict[variant])) |
| 59 | + print(build_separator) |
| 60 | + print((build_format + '| {:6} |').format('Library', 'Example', 'Result', 'Time')) |
| 61 | + print(build_separator) |
| 62 | + subprocess.run("arduino --board adafruit:nrf52:{}:softdevice={},debug=l0 --save-prefs".format(variant, 's140v6' if variant != 'feather52832' else 's132v6'), shell=True, |
| 63 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 64 | + |
| 65 | + if all_warnings: |
| 66 | + subprocess.run("arduino --pref 'compiler.warning_level=all' --save-prefs", shell=True, |
| 67 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 68 | + |
| 69 | + for sketch in glob.iglob('libraries/**/*.ino', recursive=True): |
| 70 | + start_time = time.monotonic() |
| 71 | + |
| 72 | + # skip if example contains: ".skip" or ".skip.variant" |
| 73 | + # however ".build.variant" file can overwrite ".skip", used to build a specific variant only |
| 74 | + sketchdir = os.path.dirname(sketch) |
| 75 | + if ( (os.path.exists(sketchdir + '/.skip') or os.path.exists(sketchdir + '/.skip.' + variant)) and |
| 76 | + not os.path.exists(sketchdir + '/.build.' + variant)): |
| 77 | + success = "skipped" |
| 78 | + else: |
| 79 | + # TODO - preferably, would have STDERR show up in **both** STDOUT and STDERR. |
| 80 | + # preferably, would use Python logging handler to get both distinct outputs and one merged output |
| 81 | + # for now, split STDERR when building with all warnings enabled, so can detect warning/error output. |
| 82 | + if all_warnings: |
| 83 | + build_result = subprocess.run("arduino --verify {}".format(sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 84 | + else: |
| 85 | + build_result = subprocess.run("arduino --verify {}".format(sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 86 | + |
| 87 | + # get stderr into a form where len(warningLines) indicates a true warning was output to stderr |
| 88 | + warningLines = []; |
| 89 | + if all_warnings and build_result.stderr: |
| 90 | + tmpWarningLines = build_result.stderr.decode("utf-8").splitlines() |
| 91 | + warningLines = list(filter(errorOutputFilter, (tmpWarningLines))) |
| 92 | + |
| 93 | + if build_result.returncode != 0: |
| 94 | + exit_status = build_result.returncode |
| 95 | + success = "\033[31mfailed\033[0m " |
| 96 | + fail_count += 1 |
| 97 | + elif len(warningLines) != 0: |
| 98 | + exit_status = -1 |
| 99 | + success = "\033[31mwarnings\033[0m " |
| 100 | + fail_count += 1 |
| 101 | + else: |
| 102 | + success = "\033[32msucceeded\033[0m" |
| 103 | + success_count += 1 |
| 104 | + |
| 105 | + build_duration = time.monotonic() - start_time |
| 106 | + |
| 107 | + if travis: |
| 108 | + print('travis_fold:start:build-{}\\r'.format(sketch)) |
| 109 | + |
| 110 | + print((build_format + '| {:5.2f}s |').format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, build_duration)) |
| 111 | + |
| 112 | + if success != "skipped": |
| 113 | + if build_result.returncode != 0: |
| 114 | + print(build_result.stdout.decode("utf-8")) |
| 115 | + if (build_result.stderr): |
| 116 | + print(build_result.stderr.decode("utf-8")) |
| 117 | + if len(warningLines) != 0: |
| 118 | + for line in warningLines: |
| 119 | + print(line) |
| 120 | + |
| 121 | + if travis: |
| 122 | + print('travis_fold:end:build-{}\\r'.format(sketch)) |
| 123 | + |
| 124 | + |
| 125 | +build_time = time.monotonic() |
| 126 | + |
| 127 | + |
| 128 | +# build only one variant if the environment variable is specified |
| 129 | +if (ENV_VARIABLE_NAME in os.environ): |
| 130 | + variant = os.environ.get(ENV_VARIABLE_NAME) |
| 131 | + # only use the environment variable if the variant exists in the dictionary |
| 132 | + if (variant in variants_dict): |
| 133 | + build_examples(variant) |
| 134 | + else: |
| 135 | + print('\033[31INTERNAL ERR\033[0m - invalid variant name "{}"'.format(variant)) |
| 136 | + fail_count += 1 |
| 137 | + exit_status = -1 |
| 138 | + |
| 139 | +else: # no environment variable specified, so build all variants |
| 140 | + for var in variants_dict: |
| 141 | + build_examples(var) |
| 142 | + |
| 143 | + |
| 144 | +print(build_separator) |
| 145 | +build_time = time.monotonic() - build_time |
| 146 | +print("Build Summary: {} \033[32msucceeded\033[0m, {} \033[31mfailed\033[0m and took {:.2f}s".format(success_count, fail_count, build_time)) |
| 147 | +print(build_separator) |
| 148 | + |
| 149 | +sys.exit(exit_status) |
0 commit comments