mpbuild 0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mpbuild/__init__.py +21 -0
- mpbuild/__main__.py +9 -0
- mpbuild/board_database.py +187 -0
- mpbuild/build.py +155 -0
- mpbuild/check_images.py +87 -0
- mpbuild/cli.py +119 -0
- mpbuild/completions.py +17 -0
- mpbuild/find_boards.py +26 -0
- mpbuild/list_boards.py +41 -0
- mpbuild-0.5.dist-info/METADATA +141 -0
- mpbuild-0.5.dist-info/RECORD +14 -0
- mpbuild-0.5.dist-info/WHEEL +4 -0
- mpbuild-0.5.dist-info/entry_points.txt +2 -0
- mpbuild-0.5.dist-info/licenses/LICENSE +21 -0
mpbuild/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
__app_name__ = "mpbuild"
|
|
2
|
+
__version__ = "0.1.0"
|
|
3
|
+
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from functools import cache
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from .find_boards import find_mpy_root
|
|
8
|
+
from .board_database import Database
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@cache
|
|
12
|
+
def board_database(mpy_dir: Path | None = None, port: str | None = None) -> Database:
|
|
13
|
+
mpy_dir, auto_port = find_mpy_root(mpy_dir)
|
|
14
|
+
port = port or auto_port
|
|
15
|
+
# assert port
|
|
16
|
+
return Database(mpy_dir, port)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class OutputFormat(str, Enum):
|
|
20
|
+
rich = "rich"
|
|
21
|
+
text = "text"
|
mpbuild/__main__.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The micropython git repo contains many 'board.json' files.
|
|
3
|
+
|
|
4
|
+
This is an example:
|
|
5
|
+
ports/stm32/boards/PYBV11/board.json
|
|
6
|
+
|
|
7
|
+
{
|
|
8
|
+
"deploy": [
|
|
9
|
+
"../PYBV10/deploy.md"
|
|
10
|
+
],
|
|
11
|
+
"docs": "",
|
|
12
|
+
"features": [],
|
|
13
|
+
"images": [
|
|
14
|
+
"PYBv1_1.jpg",
|
|
15
|
+
"PYBv1_1-C.jpg",
|
|
16
|
+
"PYBv1_1-E.jpg"
|
|
17
|
+
],
|
|
18
|
+
"mcu": "stm32f4",
|
|
19
|
+
"product": "Pyboard v1.1",
|
|
20
|
+
"thumbnail": "",
|
|
21
|
+
"url": "https://store.micropython.org/product/PYBv1.1",
|
|
22
|
+
"variants": {
|
|
23
|
+
"DP": "Double-precision float",
|
|
24
|
+
"DP_THREAD": "Double precision float + Threads",
|
|
25
|
+
"NETWORK": "Wiznet 5200 Driver",
|
|
26
|
+
"THREAD": "Threading"
|
|
27
|
+
},
|
|
28
|
+
"vendor": "George Robotics"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
This module implements `class Database` which reads all 'board.json' files and
|
|
32
|
+
provides a way to browse it's data.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
import json
|
|
39
|
+
from dataclasses import dataclass, field
|
|
40
|
+
from glob import glob
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(order=True)
|
|
44
|
+
class Variant:
|
|
45
|
+
name: str
|
|
46
|
+
"""
|
|
47
|
+
Example: "DP_THREAD"
|
|
48
|
+
"""
|
|
49
|
+
text: str
|
|
50
|
+
"""
|
|
51
|
+
Example: "Double precision float + Threads"
|
|
52
|
+
"""
|
|
53
|
+
board: Board = field(repr=False)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(order=True)
|
|
57
|
+
class Board:
|
|
58
|
+
name: str
|
|
59
|
+
"""
|
|
60
|
+
Example: "PYBV11"
|
|
61
|
+
"""
|
|
62
|
+
variants: list[Variant]
|
|
63
|
+
"""
|
|
64
|
+
List of variants available for this board.
|
|
65
|
+
Variants are sorted. May be an empty list if no variants are available.
|
|
66
|
+
Example key: "DP_THREAD"
|
|
67
|
+
"""
|
|
68
|
+
url: str
|
|
69
|
+
"""
|
|
70
|
+
Primary URL to link to this board.
|
|
71
|
+
"""
|
|
72
|
+
mcu: str
|
|
73
|
+
"""
|
|
74
|
+
Example: "stm32f4"
|
|
75
|
+
"""
|
|
76
|
+
product: str
|
|
77
|
+
"""
|
|
78
|
+
Example: "Pyboard v1.1"
|
|
79
|
+
"""
|
|
80
|
+
vendor: str
|
|
81
|
+
"""
|
|
82
|
+
Example: "George Robotics"
|
|
83
|
+
"""
|
|
84
|
+
images: list[str]
|
|
85
|
+
"""
|
|
86
|
+
Images of this board, stored in the micropython-media repository.
|
|
87
|
+
Example: ["PYBv1_1.jpg", "PYBv1_1-C.jpg", "PYBv1_1-E.jpg"]
|
|
88
|
+
"""
|
|
89
|
+
deploy: list[str]
|
|
90
|
+
"""
|
|
91
|
+
Files that explain how to deploy for this board:
|
|
92
|
+
Example: ["../PYBV10/deploy.md"]
|
|
93
|
+
"""
|
|
94
|
+
port: Port | None= field(default=None, compare=False)
|
|
95
|
+
|
|
96
|
+
@staticmethod
|
|
97
|
+
def factory(filename_json: Path) -> Board:
|
|
98
|
+
with filename_json.open() as f:
|
|
99
|
+
board_json = json.load(f)
|
|
100
|
+
|
|
101
|
+
board = Board(
|
|
102
|
+
name=filename_json.parent.name,
|
|
103
|
+
variants=[],
|
|
104
|
+
url=board_json["url"],
|
|
105
|
+
mcu=board_json["mcu"],
|
|
106
|
+
product=board_json["product"],
|
|
107
|
+
vendor=board_json["vendor"],
|
|
108
|
+
images=board_json["images"],
|
|
109
|
+
deploy=board_json["deploy"],
|
|
110
|
+
)
|
|
111
|
+
board.variants.extend(
|
|
112
|
+
sorted([Variant(*v, board) for v in board_json.get("variants", {}).items()])
|
|
113
|
+
)
|
|
114
|
+
return board
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@dataclass(order=True)
|
|
118
|
+
class Port:
|
|
119
|
+
name: str
|
|
120
|
+
"""
|
|
121
|
+
Example: "stm32"
|
|
122
|
+
"""
|
|
123
|
+
boards: dict[str, Board] = field(default_factory=dict, repr=False)
|
|
124
|
+
"""
|
|
125
|
+
Example key: "PYBV11"
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class Database:
|
|
131
|
+
"""
|
|
132
|
+
This database contains all information retrieved from all 'board.json' files.
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
mpy_root_directory: Path = field(repr=False)
|
|
136
|
+
port_filter: str = field(default="", repr=False)
|
|
137
|
+
|
|
138
|
+
ports: dict[str, Port] = field(default_factory=dict)
|
|
139
|
+
boards: dict[str, Board] = field(default_factory=dict)
|
|
140
|
+
|
|
141
|
+
def __post_init__(self) -> None:
|
|
142
|
+
mpy_dir = self.mpy_root_directory
|
|
143
|
+
# Take care to avoid using Path.glob! Performance was 15x slower.
|
|
144
|
+
for p in glob(f"{mpy_dir}/ports/**/boards/**/board.json"):
|
|
145
|
+
filename_json = Path(p)
|
|
146
|
+
port_name = filename_json.parent.parent.parent.name
|
|
147
|
+
if self.port_filter and self.port_filter != port_name:
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
# Create a port
|
|
151
|
+
port = self.ports.get(port_name, None)
|
|
152
|
+
if port is None:
|
|
153
|
+
port = Port(port_name)
|
|
154
|
+
self.ports[port_name] = port
|
|
155
|
+
|
|
156
|
+
# Load board.json and attach it to the board
|
|
157
|
+
board = Board.factory(filename_json)
|
|
158
|
+
board.port = port
|
|
159
|
+
|
|
160
|
+
port.boards[board.name] = board
|
|
161
|
+
self.boards[board.name] = board
|
|
162
|
+
|
|
163
|
+
# Add 'special' ports, that don't have boards
|
|
164
|
+
# TODO(mst) Tidy up later (variant descriptions etc)
|
|
165
|
+
for special_port_name in ["unix", "webassembly", "windows"]:
|
|
166
|
+
if self.port_filter and self.port_filter != special_port_name:
|
|
167
|
+
continue
|
|
168
|
+
path = Path(mpy_dir, "ports", special_port_name)
|
|
169
|
+
variant_names = [
|
|
170
|
+
var.name for var in path.glob("variants/*") if var.is_dir()
|
|
171
|
+
]
|
|
172
|
+
board = Board(
|
|
173
|
+
special_port_name,
|
|
174
|
+
[],
|
|
175
|
+
f"https://github.com/micropython/micropython/blob/master/ports/{special_port_name}/README.md",
|
|
176
|
+
"",
|
|
177
|
+
"",
|
|
178
|
+
"",
|
|
179
|
+
[],
|
|
180
|
+
[],
|
|
181
|
+
)
|
|
182
|
+
board.variants = [Variant(v, "", board) for v in variant_names]
|
|
183
|
+
port = Port(special_port_name, {special_port_name: board})
|
|
184
|
+
board.port = port
|
|
185
|
+
|
|
186
|
+
self.ports[special_port_name] = port
|
|
187
|
+
self.boards[board.name] = board
|
mpbuild/build.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import Optional, List
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import multiprocessing
|
|
6
|
+
import subprocess
|
|
7
|
+
|
|
8
|
+
from rich import print
|
|
9
|
+
from rich.panel import Panel
|
|
10
|
+
from rich.markdown import Markdown
|
|
11
|
+
|
|
12
|
+
from . import board_database, find_mpy_root
|
|
13
|
+
|
|
14
|
+
ARM_BUILD_CONTAINER = "micropython/build-micropython-arm"
|
|
15
|
+
BUILD_CONTAINERS = {
|
|
16
|
+
"stm32": ARM_BUILD_CONTAINER,
|
|
17
|
+
"rp2": ARM_BUILD_CONTAINER,
|
|
18
|
+
"nrf": ARM_BUILD_CONTAINER,
|
|
19
|
+
"mimxrt": ARM_BUILD_CONTAINER,
|
|
20
|
+
"renesas-ra": ARM_BUILD_CONTAINER,
|
|
21
|
+
"samd": ARM_BUILD_CONTAINER,
|
|
22
|
+
"esp32": "espressif/idf",
|
|
23
|
+
"unix": "gcc:12-bookworm", # Special, doesn't have boards
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
IDF_DEFAULT = "v5.2.2"
|
|
27
|
+
|
|
28
|
+
nprocs = multiprocessing.cpu_count()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def build_board(
|
|
32
|
+
board: str,
|
|
33
|
+
variant: Optional[str] = None,
|
|
34
|
+
extra_args: List[str] = [],
|
|
35
|
+
build_container_override: Optional[str] = None,
|
|
36
|
+
idf: Optional[str] = IDF_DEFAULT,
|
|
37
|
+
mpy_dir: str|Path|None = None,
|
|
38
|
+
) -> None:
|
|
39
|
+
# mpy_dir = mpy_dir or Path.cwd()
|
|
40
|
+
# mpy_dir = Path(mpy_dir)
|
|
41
|
+
mpy_dir, _ = find_mpy_root(mpy_dir)
|
|
42
|
+
db = board_database(mpy_dir)
|
|
43
|
+
|
|
44
|
+
if board not in db.boards.keys():
|
|
45
|
+
print("Invalid board")
|
|
46
|
+
raise SystemExit()
|
|
47
|
+
|
|
48
|
+
_board = db.boards[board]
|
|
49
|
+
port = _board.port.name
|
|
50
|
+
|
|
51
|
+
if variant and variant not in [v.name for v in _board.variants]:
|
|
52
|
+
print("Invalid variant")
|
|
53
|
+
raise SystemExit()
|
|
54
|
+
|
|
55
|
+
if port not in BUILD_CONTAINERS.keys():
|
|
56
|
+
print(f"Sorry, builds are not supported for the {port} port at this time")
|
|
57
|
+
raise SystemExit()
|
|
58
|
+
|
|
59
|
+
if port != "esp32" and idf != IDF_DEFAULT:
|
|
60
|
+
print("An IDF version can only be specified for ESP32 builds")
|
|
61
|
+
raise SystemExit()
|
|
62
|
+
|
|
63
|
+
build_container = (
|
|
64
|
+
build_container_override if build_container_override else BUILD_CONTAINERS[port]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if port == "esp32" and not build_container_override:
|
|
68
|
+
if not idf:
|
|
69
|
+
idf = IDF_DEFAULT
|
|
70
|
+
build_container += f":{idf}"
|
|
71
|
+
|
|
72
|
+
variant_param = "VARIANT" if board == port else "BOARD_VARIANT"
|
|
73
|
+
variant_cmd = f" {variant_param}={variant}" if variant else ""
|
|
74
|
+
|
|
75
|
+
args = " " + " ".join(extra_args)
|
|
76
|
+
|
|
77
|
+
make_mpy_cross_cmd = "make -C mpy-cross && "
|
|
78
|
+
update_submodules_cmd = (
|
|
79
|
+
f"make -C ports/{port} submodules BOARD={board}{variant_cmd} && "
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
uid, gid = os.getuid(), os.getgid()
|
|
83
|
+
|
|
84
|
+
if extra_args and extra_args[0].strip() == "clean":
|
|
85
|
+
# When cleaning we run with full privs
|
|
86
|
+
uid, gid = 0, 0
|
|
87
|
+
# Don't need to build mpy_cross or update submodules
|
|
88
|
+
make_mpy_cross_cmd = ""
|
|
89
|
+
update_submodules_cmd = ""
|
|
90
|
+
|
|
91
|
+
home = os.environ["HOME"]
|
|
92
|
+
mpy_dir = db.mpy_root_directory
|
|
93
|
+
|
|
94
|
+
# fmt: off
|
|
95
|
+
build_cmd = (
|
|
96
|
+
f"docker run -it --rm "
|
|
97
|
+
f"-v /sys/bus:/sys/bus " # provides access to USB for deploy
|
|
98
|
+
f"-v /dev:/dev " # provides access to USB for deploy
|
|
99
|
+
f"--net=host --privileged " # provides access to USB for deploy
|
|
100
|
+
f"-v {mpy_dir}:{mpy_dir} -w {mpy_dir} " # mount micropython dir with same path so elf/map paths match host
|
|
101
|
+
f"--user {uid}:{gid} " # match running user id so generated files aren't owned by root
|
|
102
|
+
f"-v {home}:{home} -e HOME={home} " # when changing user id to one not present in container this ensures home is writable
|
|
103
|
+
f"{build_container} "
|
|
104
|
+
f'bash -c "'
|
|
105
|
+
f"git config --global --add safe.directory '*' 2> /dev/null;"
|
|
106
|
+
f'{make_mpy_cross_cmd}'
|
|
107
|
+
f'{update_submodules_cmd}'
|
|
108
|
+
f'make -j {nprocs} -C ports/{port} BOARD={board}{variant_cmd}{args}"'
|
|
109
|
+
)
|
|
110
|
+
# fmt: on
|
|
111
|
+
|
|
112
|
+
title = "Build" if "clean" not in extra_args else "Clean"
|
|
113
|
+
title += f" {port}/{board}" + (f" ({variant})" if variant else "")
|
|
114
|
+
print(Panel(build_cmd, title=title, title_align="left", padding=1))
|
|
115
|
+
|
|
116
|
+
subprocess.run(build_cmd, shell=True)
|
|
117
|
+
|
|
118
|
+
# Display deployment markdown
|
|
119
|
+
# Note: Only displaying the first deploy file.
|
|
120
|
+
# Q: Are there cases where there's >1? A: Currently, no.
|
|
121
|
+
# >>> sum([len(b.deploy) for b in db.boards.values()])
|
|
122
|
+
# 166
|
|
123
|
+
# >>> len(db.boards())
|
|
124
|
+
# 169 # 3x boards are the 'special' boards without deployment instructions.
|
|
125
|
+
if _board.deploy and "clean" not in extra_args:
|
|
126
|
+
deploy_filename = Path(
|
|
127
|
+
"/".join(
|
|
128
|
+
[
|
|
129
|
+
str(mpy_dir),
|
|
130
|
+
"ports",
|
|
131
|
+
_board.port.name,
|
|
132
|
+
"boards",
|
|
133
|
+
_board.name,
|
|
134
|
+
_board.deploy[0],
|
|
135
|
+
]
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
if deploy_filename.is_file():
|
|
139
|
+
with open(deploy_filename) as deployfile:
|
|
140
|
+
print(Panel(Markdown(deployfile.read())))
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def clean_board(
|
|
144
|
+
board: str,
|
|
145
|
+
variant: Optional[str] = None,
|
|
146
|
+
idf: Optional[str] = IDF_DEFAULT,
|
|
147
|
+
mpy_dir: Optional[str] = None,
|
|
148
|
+
) -> None:
|
|
149
|
+
build_board(
|
|
150
|
+
board=board,
|
|
151
|
+
variant=variant,
|
|
152
|
+
mpy_dir=mpy_dir,
|
|
153
|
+
idf=idf,
|
|
154
|
+
extra_args=["clean"],
|
|
155
|
+
)
|
mpbuild/check_images.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from . import board_database
|
|
2
|
+
|
|
3
|
+
from urllib.request import Request, urlopen
|
|
4
|
+
from urllib.error import HTTPError
|
|
5
|
+
|
|
6
|
+
from rich.progress import Progress
|
|
7
|
+
from rich import print
|
|
8
|
+
from rich.panel import Panel
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def check_images(verbose: bool = False, mpy_dir: str = None) -> None:
|
|
13
|
+
db = board_database(mpy_dir)
|
|
14
|
+
# TODO(mst) A minor improvement: Should count the number of images in all
|
|
15
|
+
# the boards for each port (this assumes one per board).
|
|
16
|
+
num_boards = len(db.boards)
|
|
17
|
+
|
|
18
|
+
# no_images = [("stm32", "fooobar"), ("rp2", "PICO")] # []
|
|
19
|
+
# image_not_found = [("stm32", "fooobar", "https://raw.githubusercontent.com/micropython/micropython-media/main/boards/VK_RA6M5/VK-RA6M5.jpg"),
|
|
20
|
+
# ("stm32", "fooobar", "https://raw.githubusercontent.com/micropython/micropython-media/main/boards/VK_RA6M5/VK-RA6M5.jpg"),
|
|
21
|
+
# ("rp2", "PICO", "https://raw.githubusercontent.com/micropython/micropython-media/main/boards/VK_RA6M5/VK-RA6M5.jpg")] # []
|
|
22
|
+
# image_too_large = [("esp32", "GENERIC", "https://raw.githubusercontent.com/micropython/micropython-media/main/boards/VK_RA6M5/VK-RA6M5.jpg", 500_000),
|
|
23
|
+
# ("rp2", "PICO", "https://raw.githubusercontent.com/micropython/micropython-media/main/boards/VK_RA6M5/VK-RA6M5.jpg", 400_000)] # []
|
|
24
|
+
no_images = []
|
|
25
|
+
image_not_found = []
|
|
26
|
+
image_too_large = []
|
|
27
|
+
|
|
28
|
+
base_url = (
|
|
29
|
+
r"https://raw.githubusercontent.com/micropython/micropython-media/main/boards"
|
|
30
|
+
)
|
|
31
|
+
with Progress(transient=True) as progress:
|
|
32
|
+
task1 = progress.add_task("[cyan]Checking images...", total=num_boards)
|
|
33
|
+
for _board in db.boards.values():
|
|
34
|
+
image_list = _board.images
|
|
35
|
+
if len(image_list) == 0:
|
|
36
|
+
# No images specified in build.json (should be at least one)
|
|
37
|
+
no_images.append((_board.port.name, _board.name))
|
|
38
|
+
for image in image_list:
|
|
39
|
+
# Check each image listed in board.json
|
|
40
|
+
image_url = f"{base_url}/{_board.name}/{image}"
|
|
41
|
+
req = Request(image_url, method="HEAD")
|
|
42
|
+
try:
|
|
43
|
+
f = urlopen(req)
|
|
44
|
+
except HTTPError:
|
|
45
|
+
image_not_found.append((_board.port.name, _board.name, image_url))
|
|
46
|
+
if f.status == 200:
|
|
47
|
+
# Check size < ~500KB
|
|
48
|
+
image_size = int(f.headers["Content-Length"])
|
|
49
|
+
if image_size > 500_000:
|
|
50
|
+
image_too_large.append(
|
|
51
|
+
(_board.port.name, _board.name, image_url, image_size)
|
|
52
|
+
)
|
|
53
|
+
progress.update(task1, advance=1)
|
|
54
|
+
|
|
55
|
+
# Display output
|
|
56
|
+
grid = Table.grid(expand=True)
|
|
57
|
+
grid.add_column()
|
|
58
|
+
grid.add_column()
|
|
59
|
+
grid.add_column()
|
|
60
|
+
grid.add_row(
|
|
61
|
+
Panel(
|
|
62
|
+
"\n".join([f"{p}/[bright_white]{b}[/]" for p, b in no_images]),
|
|
63
|
+
title="No images",
|
|
64
|
+
subtitle="No image in board.json",
|
|
65
|
+
),
|
|
66
|
+
Panel(
|
|
67
|
+
"\n".join(
|
|
68
|
+
[
|
|
69
|
+
f"[link={url}]{p}/[bright_white]{b}[/][/link]"
|
|
70
|
+
for p, b, url in image_not_found
|
|
71
|
+
]
|
|
72
|
+
),
|
|
73
|
+
title="Not found",
|
|
74
|
+
subtitle="Image not in micropython-media",
|
|
75
|
+
),
|
|
76
|
+
Panel(
|
|
77
|
+
"\n".join(
|
|
78
|
+
[
|
|
79
|
+
f"[link={url}]{p}/[bright_white]{b}[/][/link]"
|
|
80
|
+
for p, b, url, s in image_too_large
|
|
81
|
+
]
|
|
82
|
+
),
|
|
83
|
+
title="Too large",
|
|
84
|
+
subtitle="Image > 500KB",
|
|
85
|
+
),
|
|
86
|
+
)
|
|
87
|
+
print(grid)
|
mpbuild/cli.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from typing import Optional, List
|
|
2
|
+
from typing_extensions import Annotated
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
|
|
6
|
+
from . import __app_name__, __version__, OutputFormat
|
|
7
|
+
from .build import build_board, clean_board, IDF_DEFAULT
|
|
8
|
+
from .list_boards import print_boards
|
|
9
|
+
from .check_images import check_images
|
|
10
|
+
from .completions import list_ports, list_boards, list_variants_for_board
|
|
11
|
+
|
|
12
|
+
app = typer.Typer(chain=True, context_settings={"help_option_names": ["-h", "--help"]})
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _complete(words: list[str], incomplete: str):
|
|
16
|
+
completion = []
|
|
17
|
+
for name in words:
|
|
18
|
+
if name.startswith(incomplete):
|
|
19
|
+
completion.append(name)
|
|
20
|
+
return completion
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _complete_board(incomplete: str):
|
|
24
|
+
return _complete(list_boards(), incomplete)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _complete_variant(ctx: typer.Context, incomplete: str):
|
|
28
|
+
board = ctx.params.get("board") or []
|
|
29
|
+
return _complete(list_variants_for_board(board), incomplete)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _complete_port(incomplete: str):
|
|
33
|
+
return _complete(list_ports(), incomplete)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@app.command()
|
|
37
|
+
def build(
|
|
38
|
+
board: Annotated[
|
|
39
|
+
str, typer.Argument(help="Board name", autocompletion=_complete_board)
|
|
40
|
+
],
|
|
41
|
+
variant: Annotated[
|
|
42
|
+
Optional[str],
|
|
43
|
+
typer.Argument(help="Board variant", autocompletion=_complete_variant),
|
|
44
|
+
] = None,
|
|
45
|
+
idf: Annotated[
|
|
46
|
+
Optional[str],
|
|
47
|
+
typer.Option(help="esp32 port only: select IDF version to build with"),
|
|
48
|
+
] = IDF_DEFAULT,
|
|
49
|
+
extra_args: Annotated[
|
|
50
|
+
Optional[List[str]], typer.Argument(help="additional arguments to pass to make")
|
|
51
|
+
] = None,
|
|
52
|
+
build_container: Annotated[
|
|
53
|
+
Optional[str],
|
|
54
|
+
typer.Option(help="Override the default build container"),
|
|
55
|
+
] = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""
|
|
58
|
+
Build a MicroPython board.
|
|
59
|
+
"""
|
|
60
|
+
build_board(board, variant, extra_args or [], build_container, idf)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@app.command()
|
|
64
|
+
def clean(
|
|
65
|
+
board: str, variant: Annotated[Optional[str], typer.Argument()] = None
|
|
66
|
+
) -> None:
|
|
67
|
+
"""
|
|
68
|
+
Clean a MicroPython board.
|
|
69
|
+
"""
|
|
70
|
+
clean_board(board, variant)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@app.command("list")
|
|
74
|
+
def list_boards_and_variants(
|
|
75
|
+
port: Annotated[
|
|
76
|
+
Optional[str], typer.Argument(help="Port name", autocompletion=_complete_port)
|
|
77
|
+
] = None,
|
|
78
|
+
fmt: Annotated[
|
|
79
|
+
OutputFormat,
|
|
80
|
+
typer.Option(
|
|
81
|
+
"--format", case_sensitive=False, help="Configure the output format"
|
|
82
|
+
),
|
|
83
|
+
] = OutputFormat.rich,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""
|
|
86
|
+
List available boards.
|
|
87
|
+
"""
|
|
88
|
+
print_boards(port, fmt)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@app.command("check_images")
|
|
92
|
+
def image_check(
|
|
93
|
+
verbose: Annotated[bool, typer.Option(help="More verbose output")] = False,
|
|
94
|
+
) -> None:
|
|
95
|
+
"""
|
|
96
|
+
Check images
|
|
97
|
+
"""
|
|
98
|
+
check_images(verbose)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _version_callback(value: bool) -> None:
|
|
102
|
+
if value:
|
|
103
|
+
typer.echo(f"{__app_name__} v{__version__}")
|
|
104
|
+
|
|
105
|
+
raise typer.Exit()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@app.callback()
|
|
109
|
+
def main(
|
|
110
|
+
version: Optional[bool] = typer.Option(
|
|
111
|
+
None,
|
|
112
|
+
"--version",
|
|
113
|
+
"-v",
|
|
114
|
+
help="Show the application's version and exit.",
|
|
115
|
+
callback=_version_callback,
|
|
116
|
+
is_eager=True,
|
|
117
|
+
),
|
|
118
|
+
) -> None:
|
|
119
|
+
return
|
mpbuild/completions.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from . import board_database
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def list_ports() -> list[str]:
|
|
5
|
+
db = board_database()
|
|
6
|
+
return [p.name for p in sorted(db.ports.values())]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def list_boards() -> list[str]:
|
|
10
|
+
db = board_database()
|
|
11
|
+
return [b.name for b in sorted(db.boards.values())]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def list_variants_for_board(board: str) -> list[str]:
|
|
15
|
+
db = board_database()
|
|
16
|
+
variants = db.boards[board].variants
|
|
17
|
+
return [v.name for v in variants if v]
|
mpbuild/find_boards.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from functools import cache
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@cache
|
|
7
|
+
def find_mpy_root(root: str| Path | None = None):
|
|
8
|
+
if root is None:
|
|
9
|
+
root = Path(os.environ.get("MICROPY_DIR", ".")).resolve()
|
|
10
|
+
else:
|
|
11
|
+
root = Path(root)
|
|
12
|
+
|
|
13
|
+
port = None
|
|
14
|
+
while True:
|
|
15
|
+
# If run from a port folder, store that for use in filters
|
|
16
|
+
if root.parent.name == "ports":
|
|
17
|
+
port = root.name
|
|
18
|
+
|
|
19
|
+
if (root / "ports").exists() and (root / "mpy-cross").exists():
|
|
20
|
+
return root, port
|
|
21
|
+
|
|
22
|
+
if root.parent == root:
|
|
23
|
+
raise SystemExit(
|
|
24
|
+
"Please run from MicroPython source tree or specify with env: MICROPY_DIR"
|
|
25
|
+
)
|
|
26
|
+
root = root.parent
|
mpbuild/list_boards.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from . import board_database, OutputFormat
|
|
3
|
+
from rich.tree import Tree
|
|
4
|
+
from rich import print
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def print_boards(
|
|
8
|
+
port: Optional[str] = None,
|
|
9
|
+
fmt: OutputFormat = OutputFormat.rich,
|
|
10
|
+
mpy_dir: Optional[str] = None,
|
|
11
|
+
) -> None:
|
|
12
|
+
db = board_database(mpy_dir, port)
|
|
13
|
+
|
|
14
|
+
if port and port not in db.ports.keys():
|
|
15
|
+
raise ValueError("Invalid port")
|
|
16
|
+
|
|
17
|
+
if fmt == OutputFormat.rich:
|
|
18
|
+
tree = Tree(":snake: [bright_white]MicroPython Boards[/]")
|
|
19
|
+
tree.add
|
|
20
|
+
for p in sorted(db.ports.values()):
|
|
21
|
+
if not port or (port and port == p.name):
|
|
22
|
+
treep = tree.add(f"{p.name} [bright_black]{len(p.boards)}[/]")
|
|
23
|
+
for b in sorted(p.boards.values()):
|
|
24
|
+
variants = ", ".join([v.name for v in b.variants])
|
|
25
|
+
variants = f" [bright_black]{variants}[/]" if variants else ""
|
|
26
|
+
treep.add(
|
|
27
|
+
f"[bright_white][link={b.url}]{b.name}[/link][/] {variants}"
|
|
28
|
+
)
|
|
29
|
+
print(tree)
|
|
30
|
+
|
|
31
|
+
if fmt == OutputFormat.text:
|
|
32
|
+
""" Output a space-separated list of boards. Doesn't display variants."""
|
|
33
|
+
print(
|
|
34
|
+
" ".join(
|
|
35
|
+
[
|
|
36
|
+
b.name
|
|
37
|
+
for b in sorted(db.boards.values())
|
|
38
|
+
if not port or (port and port == b.port.name)
|
|
39
|
+
]
|
|
40
|
+
)
|
|
41
|
+
)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mpbuild
|
|
3
|
+
Version: 0.5
|
|
4
|
+
Summary: Build MicroPython firmware with ease!
|
|
5
|
+
Author-email: Matt Trentini <matt.trentini@gmail.com>
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Requires-Dist: rich>=13.7.1
|
|
8
|
+
Requires-Dist: typer>=0.12.3
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
|
|
13
|
+
# mpbuild
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
Build MicroPython firmware with ease!
|
|
18
|
+
|
|
19
|
+
**mpbuild** builds MicroPython firmware in containers so you don't need to install any compiler toolchains or development tools. It knows which containers to use for each board so the appropriate build tools are used.
|
|
20
|
+
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
## Table of Contents
|
|
24
|
+
|
|
25
|
+
- [Usage](#usage)
|
|
26
|
+
- [Advanced Usage](#advanced-usage)
|
|
27
|
+
- [Use as a Module](#use-as-a-module)
|
|
28
|
+
- [Installation](#installation)
|
|
29
|
+
- [Prerequisites](#prerequisites)
|
|
30
|
+
- [Examples](#examples)
|
|
31
|
+
- [Related links](#related-links)
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
**mpbuild** is intended to be executed in the root of a MicroPython repository. Help text (accessed with adding `--help`) is extensive and documents advanced options.
|
|
36
|
+
|
|
37
|
+
> [!NOTE]
|
|
38
|
+
> Note that there are some _special_ builds. `unix`, `webassembly` and `windows` can all be specified as `BOARD`s but their target isn't a microcontroller. See the [MicroPython documentation](https://github.com/micropython/micropython/) for details.
|
|
39
|
+
|
|
40
|
+
> [!WARNING]
|
|
41
|
+
> Currently **mpbuild** is tested on Linux (specifically Ubuntu 24.04 on WSL on Windows 11) but it's intended to work on any platform that supports Docker.
|
|
42
|
+
|
|
43
|
+
Build a board, with optional variant:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
mpbuild build BOARD [VARIANT]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Remove build artifacts:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
mpbuild clean BOARD [VARIANT]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
List the available boards, optionally filter by the port name.
|
|
56
|
+
|
|
57
|
+
Displays the board names (as a clickable link), variants and number of boards per port:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
mpbuild list [PORT]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Advanced Usage
|
|
64
|
+
|
|
65
|
+
Validate the state of all images referenced in board definitions:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
mpbuild check_images
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Use as a Module
|
|
72
|
+
|
|
73
|
+
> [!CAUTION]
|
|
74
|
+
> This is _very much_ a work-in-progress and the API is subject to change.
|
|
75
|
+
|
|
76
|
+
**mpbuild** can also be used from Python as a module. This allows it to easily be integrated into other tools.
|
|
77
|
+
|
|
78
|
+
Example:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
import mpbuild
|
|
82
|
+
|
|
83
|
+
mpbuild.build("RPI_PICO")
|
|
84
|
+
mpbuild.list()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Installation
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
uv tool install mpbuild
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Or use pipx, pip etc.
|
|
94
|
+
|
|
95
|
+
After installation, you may want to also install command-line tab completion for your shell (bash, zsh, fish and PowerShell are supported). Tab completion includes the various mpbuild commands as well as the board names and variants:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
mpbuild --install-completion
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Prerequisites
|
|
102
|
+
|
|
103
|
+
A clone of MicroPython (or a fork):
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
git clone git@github.com:micropython/micropython.git
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
[Docker](https://www.docker.com/) is currently necessary for managing containers and must be installed and available on your system path.
|
|
110
|
+
|
|
111
|
+
## Examples
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
$ mpbuild build RPI_PICO
|
|
115
|
+
# Downloads appropriate containers and builds firmware for the Raspberry Pi Pico
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
$ mpbuild list rp2
|
|
120
|
+
🐍 MicroPython Boards
|
|
121
|
+
└── rp2 19
|
|
122
|
+
├── ADAFRUIT_FEATHER_RP2040
|
|
123
|
+
├── ADAFRUIT_ITSYBITSY_RP2040
|
|
124
|
+
├── ADAFRUIT_QTPY_RP2040
|
|
125
|
+
├── ARDUINO_NANO_RP2040_CONNECT
|
|
126
|
+
├── GARATRONIC_PYBSTICK26_RP2040
|
|
127
|
+
├── NULLBITS_BIT_C_PRO
|
|
128
|
+
├── PIMORONI_PICOLIPO FLASH_16M
|
|
129
|
+
├── PIMORONI_TINY2040 FLASH_8M
|
|
130
|
+
├── POLOLU_3PI_2040_ROBOT
|
|
131
|
+
├── POLOLU_ZUMO_2040_ROBOT
|
|
132
|
+
├── RPI_PICO
|
|
133
|
+
├── RPI_PICO2 RISCV
|
|
134
|
+
├── RPI_PICO_W
|
|
135
|
+
├── SIL_RP2040_SHIM
|
|
136
|
+
├── SPARKFUN_PROMICRO
|
|
137
|
+
├── SPARKFUN_THINGPLUS
|
|
138
|
+
├── W5100S_EVB_PICO
|
|
139
|
+
├── W5500_EVB_PICO
|
|
140
|
+
└── WEACTSTUDIO FLASH_2M, FLASH_4M, FLASH_8M
|
|
141
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
mpbuild/__init__.py,sha256=t8sltJNYGgfeWpdU6bWpgqwIDLDgLfcca3EJpzAotMs,493
|
|
2
|
+
mpbuild/__main__.py,sha256=AJMqg360n7QBDIIoZ8c5fwN2bkD-ns0M2LusLlZG8WI,122
|
|
3
|
+
mpbuild/board_database.py,sha256=gsW8nk_AMmsEQuDvDmdy3iIV-WP0dATPEjA80lRLoZk,5081
|
|
4
|
+
mpbuild/build.py,sha256=gGJoDVbhn0XnN-BlxwiOi1rLikp-poemmuAuGVA1QEo,4979
|
|
5
|
+
mpbuild/check_images.py,sha256=6tZP3YBi8n9tFDzmLOURlK-ga8cvLrMB0SbMyEJI4kU,3617
|
|
6
|
+
mpbuild/cli.py,sha256=OGHq6MzbQMiLAQp4eB4meT-x1GfJVbZvdl_MA7NtvDo,3031
|
|
7
|
+
mpbuild/completions.py,sha256=_2bH6ma3wWWutnufFb36GMODtWXy6ILM3mQ8Zd3-pHY,425
|
|
8
|
+
mpbuild/find_boards.py,sha256=j00SG1m2dmIDxH_1sgIvj_i9JCMQzMStS5ziGPkCtEw,712
|
|
9
|
+
mpbuild/list_boards.py,sha256=4E7QajCMB_fjL1n-mM9kxnwlg_FXlDRu33jgXMBIi_g,1411
|
|
10
|
+
mpbuild-0.5.dist-info/METADATA,sha256=6F6rGamH14Ogc-zJFBBRfdtRo4AxOQXGoq_E_8U5voM,3768
|
|
11
|
+
mpbuild-0.5.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
12
|
+
mpbuild-0.5.dist-info/entry_points.txt,sha256=EKygfktCuagf1MQuqksfmd_I71kxEz5gZYqIXgxhktI,50
|
|
13
|
+
mpbuild-0.5.dist-info/licenses/LICENSE,sha256=qKKKpea4-qzJm3mtfO7X2XXn58IA4ecnoO5IYZWWSwQ,1070
|
|
14
|
+
mpbuild-0.5.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Matt Trentini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|