Skip to content

Commit 296c59e

Browse files
committed
move global autorun code under if __name__ == "__main__": to make build_platform.py importable
1 parent a505386 commit 296c59e

File tree

1 file changed

+83
-71
lines changed

1 file changed

+83
-71
lines changed

build_platform.py

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@
6363
CHECK = u'\N{check mark}'
6464

6565

66-
BSP_URLS = "https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json,https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json,https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json,https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json"
66+
BSP_URLS = (
67+
"https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,"
68+
"http://arduino.esp8266.com/stable/package_esp8266com_index.json,"
69+
"https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json,"
70+
"https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json,"
71+
"https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json"
72+
)
6773

6874
class ColorPrint:
6975

@@ -156,16 +162,6 @@ def run_or_die(cmd, error):
156162
ColorPrint.print_fail(error)
157163
exit(-1)
158164

159-
################################ Install Arduino IDE
160-
print()
161-
ColorPrint.print_info('#'*40)
162-
print("INSTALLING ARDUINO BOARDS")
163-
ColorPrint.print_info('#'*40)
164-
165-
run_or_die("arduino-cli core update-index --additional-urls "+BSP_URLS+
166-
" > /dev/null", "FAILED to update core indices")
167-
168-
print()
169165

170166
def is_library_installed(lib_name):
171167
try:
@@ -175,43 +171,54 @@ def is_library_installed(lib_name):
175171
print("Error checking installed libraries:", e)
176172
return False
177173

178-
################################ Install dependencies
179-
our_name=None
180-
try:
181-
if IS_LEARNING_SYS:
182-
libprop = open(BUILD_DIR+'/library.deps')
183-
else:
184-
libprop = open(BUILD_DIR+'/library.properties')
185-
for line in libprop:
186-
if line.startswith("name="):
187-
our_name = line.replace("name=", "").strip()
188-
if line.startswith("depends="):
189-
deps = line.replace("depends=", "").split(",")
190-
for dep in deps:
191-
dep = dep.strip()
192-
if not is_library_installed(dep):
193-
print("Installing "+dep)
194-
run_or_die('arduino-cli lib install "'+dep+'" > /dev/null',
195-
"FAILED to install dependency "+dep)
196-
else:
197-
print("Skipping already installed lib: "+dep)
198-
except OSError:
199-
print("No library dep or properties found!")
200-
pass # no library properties
201174

202-
# Delete the existing library if we somehow downloaded
203-
# due to dependencies
204-
if our_name:
205-
run_or_die("arduino-cli lib uninstall \""+our_name+"\"", "Could not uninstall")
175+
def install_library_deps():
176+
print()
177+
ColorPrint.print_info('#'*40)
178+
print("INSTALLING ARDUINO LIBRARIES")
179+
ColorPrint.print_info('#'*40)
206180

207-
print("Libraries installed: ", glob.glob(os.environ['HOME']+'/Arduino/libraries/*'))
181+
run_or_die("arduino-cli core update-index --additional-urls "+BSP_URLS+
182+
" > /dev/null", "FAILED to update core indices")
183+
print()
208184

209-
# link our library folder to the arduino libraries folder
210-
if not IS_LEARNING_SYS:
185+
# Install dependencies
186+
our_name=None
211187
try:
212-
os.symlink(BUILD_DIR, os.environ['HOME']+'/Arduino/libraries/' + os.path.basename(BUILD_DIR))
213-
except FileExistsError:
214-
pass
188+
if IS_LEARNING_SYS:
189+
libprop = open(BUILD_DIR+'/library.deps')
190+
else:
191+
libprop = open(BUILD_DIR+'/library.properties')
192+
for line in libprop:
193+
if line.startswith("name="):
194+
our_name = line.replace("name=", "").strip()
195+
if line.startswith("depends="):
196+
deps = line.replace("depends=", "").split(",")
197+
for dep in deps:
198+
dep = dep.strip()
199+
if not is_library_installed(dep):
200+
print("Installing "+dep)
201+
run_or_die('arduino-cli lib install "'+dep+'" > /dev/null',
202+
"FAILED to install dependency "+dep)
203+
else:
204+
print("Skipping already installed lib: "+dep)
205+
except OSError:
206+
print("No library dep or properties found!")
207+
pass # no library properties
208+
209+
# Delete the existing library if we somehow downloaded
210+
# due to dependencies
211+
if our_name:
212+
run_or_die("arduino-cli lib uninstall \""+our_name+"\"", "Could not uninstall")
213+
214+
print("Libraries installed: ", glob.glob(os.environ['HOME']+'/Arduino/libraries/*'))
215+
216+
# link our library folder to the arduino libraries folder
217+
if not IS_LEARNING_SYS:
218+
try:
219+
os.symlink(BUILD_DIR, os.environ['HOME']+'/Arduino/libraries/' + os.path.basename(BUILD_DIR))
220+
except FileExistsError:
221+
pass
215222

216223
################################ UF2 Utils.
217224

@@ -292,21 +299,6 @@ def generate_uf2(example_path):
292299
return None
293300
return output_file
294301

295-
################################ Test platforms
296-
platforms = []
297-
success = 0
298-
299-
# expand groups:
300-
for arg in sys.argv[1:]:
301-
platform = ALL_PLATFORMS.get(arg, None)
302-
if isinstance(platform, list):
303-
platforms.append(arg)
304-
elif isinstance(platform, tuple):
305-
for p in platform:
306-
platforms.append(p)
307-
else:
308-
print("Unknown platform: ", arg)
309-
exit(-1)
310302

311303
@contextmanager
312304
def group_output(title):
@@ -411,7 +403,7 @@ def test_examples_in_learningrepo(folderpath):
411403
continue
412404
if not projectpath.endswith(".ino"):
413405
continue
414-
# found an INO!
406+
# found an INO!
415407
print('\t'+projectpath, end=' ', flush=True)
416408
# check if we should SKIP
417409
skipfilename = folderpath+"/."+platform+".test.skip"
@@ -441,14 +433,34 @@ def test_examples_in_learningrepo(folderpath):
441433
success = 1
442434

443435

444-
for platform in platforms:
445-
fqbn = ALL_PLATFORMS[platform][0]
446-
print('#'*80)
447-
ColorPrint.print_info("SWITCHING TO "+fqbn)
448-
install_platform(":".join(fqbn.split(':', 2)[0:2]), ALL_PLATFORMS[platform]) # take only first two elements
449-
print('#'*80)
450-
if not IS_LEARNING_SYS:
451-
test_examples_in_folder(BUILD_DIR+"/examples")
452-
else:
453-
test_examples_in_folder(BUILD_DIR)
454-
exit(success)
436+
if __name__ == "__main__":
437+
# Test platforms
438+
platforms = []
439+
success = 0
440+
441+
# expand groups:
442+
for arg in sys.argv[1:]:
443+
platform = ALL_PLATFORMS.get(arg, None)
444+
if isinstance(platform, list):
445+
platforms.append(arg)
446+
elif isinstance(platform, tuple):
447+
for p in platform:
448+
platforms.append(p)
449+
else:
450+
print("Unknown platform: ", arg)
451+
exit(-1)
452+
453+
# Install libraries deps
454+
install_library_deps()
455+
456+
for platform in platforms:
457+
fqbn = ALL_PLATFORMS[platform][0]
458+
print('#'*80)
459+
ColorPrint.print_info("SWITCHING TO "+fqbn)
460+
install_platform(":".join(fqbn.split(':', 2)[0:2]), ALL_PLATFORMS[platform]) # take only first two elements
461+
print('#'*80)
462+
if not IS_LEARNING_SYS:
463+
test_examples_in_folder(BUILD_DIR+"/examples")
464+
else:
465+
test_examples_in_folder(BUILD_DIR)
466+
exit(success)

0 commit comments

Comments
 (0)