Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/mpbuild/board_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Board:
"""
Example: "PYBV11"
"""
directory: Path
"""
The directory of the source code.
Example: "<repo>/ports/esp32/boards/[BOARD]"
"""
variants: list[Variant]
"""
List of variants available for this board.
Expand Down Expand Up @@ -91,7 +96,7 @@ class Board:
Files that explain how to deploy for this board:
Example: ["../PYBV10/deploy.md"]
"""
port: Port | None= field(default=None, compare=False)
port: Port | None = field(default=None, compare=False)

@staticmethod
def factory(filename_json: Path) -> Board:
Expand All @@ -100,6 +105,7 @@ def factory(filename_json: Path) -> Board:

board = Board(
name=filename_json.parent.name,
directory=filename_json.parent,
variants=[],
url=board_json["url"],
mcu=board_json["mcu"],
Expand All @@ -120,6 +126,11 @@ class Port:
"""
Example: "stm32"
"""
directory: Path
"""
The directory of the source code.
Example: "ports/stm32"
"""
boards: dict[str, Board] = field(default_factory=dict, repr=False)
"""
Example key: "PYBV11"
Expand All @@ -143,14 +154,15 @@ def __post_init__(self) -> None:
# Take care to avoid using Path.glob! Performance was 15x slower.
for p in glob(f"{mpy_dir}/ports/**/boards/**/board.json"):
filename_json = Path(p)
port_name = filename_json.parent.parent.parent.name
port_directory = filename_json.parent.parent.parent
port_name = port_directory.name
if self.port_filter and self.port_filter != port_name:
continue

# Create a port
port = self.ports.get(port_name, None)
if port is None:
port = Port(port_name)
port = Port(port_name, port_directory)
self.ports[port_name] = port

# Load board.json and attach it to the board
Expand All @@ -171,6 +183,7 @@ def __post_init__(self) -> None:
]
board = Board(
special_port_name,
path,
[],
f"https://github.com/micropython/micropython/blob/master/ports/{special_port_name}/README.md",
"",
Expand Down
2 changes: 1 addition & 1 deletion src/mpbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def build_board(
extra_args: List[str] = [],
build_container_override: Optional[str] = None,
idf: Optional[str] = IDF_DEFAULT,
mpy_dir: str|Path|None = None,
mpy_dir: str | Path | None = None,
) -> None:
# mpy_dir = mpy_dir or Path.cwd()
# mpy_dir = Path(mpy_dir)
Expand Down
46 changes: 46 additions & 0 deletions src/mpbuild/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from .check_images import check_images
from .completions import list_ports, list_boards, list_variants_for_board

import shutil
from . import board_database

app = typer.Typer(chain=True, context_settings={"help_option_names": ["-h", "--help"]})


Expand Down Expand Up @@ -98,6 +101,49 @@ def image_check(
check_images(verbose)


@app.command("copy_board")
def copy_board(
src_board: Annotated[
str,
typer.Argument(help="Source board (copy from)", autocompletion=_complete_board),
],
new_board: Annotated[
Optional[str], typer.Argument(help="Name of the new board (copy to)")
] = None,
) -> None:
"""
Copy a board definition (to start a new board)
"""
# Check for uppercase (allow with -f?)
if any(c for c in new_board if c.islower()):
print("The new board must not contain lowercase letters")
raise SystemExit()

db = board_database(None)

if new_board in db.boards.keys():
print(
f"The new board must have a unique name:\n {db.boards[new_board].directory} exists"
)
raise SystemExit()

if src_board not in db.boards.keys():
print("Invalid board")
raise SystemExit()

board = db.boards[src_board]

dest_path = board.port.directory / "boards" / new_board

# Check if the destination board name already exists
if dest_path.exists():
print("Invalid: Destination board name already exists")
raise SystemExit()

print(f"Copying {board.directory} to {dest_path}")
shutil.copytree(board.directory, dest_path)


def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} v{__version__}")
Expand Down
2 changes: 1 addition & 1 deletion src/mpbuild/find_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@cache
def find_mpy_root(root: str| Path | None = None):
def find_mpy_root(root: str | Path | None = None):
if root is None:
root = Path(os.environ.get("MICROPY_DIR", ".")).resolve()
else:
Expand Down