Skip to content

Commit 87e4f47

Browse files
committed
docs/shared_bindings_matrix.py: changes to allow usage in 'tools/build_board_info.py'; includes root dir finder, and output board name option.
1 parent 8abf8c2 commit 87e4f47

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

docs/shared_bindings_matrix.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,47 @@
2323

2424
import json
2525
import os
26+
import pathlib
2627
import re
2728
import subprocess
2829
import sys
2930

3031

3132
SUPPORTED_PORTS = ['atmel-samd', 'esp32s2', 'litex', 'mimxrt10xx', 'nrf', 'stm']
3233

34+
def get_circuitpython_root_dir():
35+
""" The path to the root './circuitpython' directory
36+
"""
37+
cwd = pathlib.Path('.').resolve()
38+
cwd_parts = cwd.parts
39+
40+
root_idx = len(cwd_parts)
41+
42+
# Search the path from tail to head, so that we capture the
43+
# deepest folder. This avoids overshooting in instances like:
44+
# '/home/user/circuitpython_v5/circuitpython'
45+
for idx, val in enumerate(cwd_parts[::-1]):
46+
if val.startswith("circuitpython"):
47+
root_idx = root_idx - idx
48+
break
49+
50+
root_dir = '/'.join(cwd_parts[:root_idx])
51+
52+
return pathlib.Path(root_dir).resolve()
53+
3354
def get_shared_bindings():
3455
""" Get a list of modules in shared-bindings based on folder names
3556
"""
36-
return [item for item in os.listdir("./shared-bindings")]
57+
shared_bindings_dir = get_circuitpython_root_dir() / "shared-bindings"
58+
return [item.name for item in shared_bindings_dir.iterdir()]
3759

3860

3961
def read_mpconfig():
4062
""" Open 'circuitpy_mpconfig.mk' and return the contents.
4163
"""
4264
configs = []
43-
with open("py/circuitpy_mpconfig.mk") as mpconfig:
65+
cpy_mpcfg = get_circuitpython_root_dir() / "py" / "circuitpy_mpconfig.mk"
66+
with open(cpy_mpcfg) as mpconfig:
4467
configs = mpconfig.read()
4568

4669
return configs
@@ -120,7 +143,7 @@ def lookup_setting(settings, key, default=''):
120143
key = value[2:-1]
121144
return value
122145

123-
def support_matrix_by_board():
146+
def support_matrix_by_board(use_branded_name=True):
124147
""" Compiles a list of the available core modules available for each
125148
board.
126149
"""
@@ -129,20 +152,22 @@ def support_matrix_by_board():
129152
boards = dict()
130153
for port in SUPPORTED_PORTS:
131154

132-
port_dir = "ports/{}/boards".format(port)
133-
for entry in os.scandir(port_dir):
155+
port_dir = get_circuitpython_root_dir() / "ports" / port
156+
for entry in (port_dir / "boards").iterdir():
134157
if not entry.is_dir():
135158
continue
136159
board_modules = []
160+
board_name = entry.name
137161

138-
settings = get_settings_from_makefile(f'ports/{port}', entry.name)
162+
settings = get_settings_from_makefile(str(port_dir), entry.name)
139163

140-
with open(os.path.join(entry.path, "mpconfigboard.h")) as get_name:
141-
board_contents = get_name.read()
142-
board_name_re = re.search("(?<=MICROPY_HW_BOARD_NAME)\s+(.+)",
143-
board_contents)
144-
if board_name_re:
145-
board_name = board_name_re.group(1).strip('"')
164+
if use_branded_name:
165+
with open(os.path.join(entry.path, "mpconfigboard.h")) as get_name:
166+
board_contents = get_name.read()
167+
board_name_re = re.search(r"(?<=MICROPY_HW_BOARD_NAME)\s+(.+)",
168+
board_contents)
169+
if board_name_re:
170+
board_name = board_name_re.group(1).strip('"')
146171

147172
board_modules = []
148173
for module in base:

0 commit comments

Comments
 (0)