Skip to content

Commit

Permalink
Merge pull request #485 from zapta/develop
Browse files Browse the repository at this point in the history
Switched to new board id format while supporting the old names for backward compatibility.
  • Loading branch information
Obijuan authored Dec 5, 2024
2 parents e5d834b + 1571b80 commit 0c45646
Show file tree
Hide file tree
Showing 213 changed files with 647 additions and 953 deletions.
40 changes: 20 additions & 20 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"program": "${workspaceFolder}/apio_run.py",
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio boards",
Expand All @@ -21,7 +21,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio build",
Expand All @@ -33,7 +33,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio clean",
Expand All @@ -45,7 +45,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio create",
Expand All @@ -62,7 +62,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio drivers",
Expand All @@ -75,7 +75,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio examples",
Expand All @@ -88,7 +88,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio graph",
Expand All @@ -101,7 +101,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio install",
Expand All @@ -116,7 +116,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio lint",
Expand All @@ -128,7 +128,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio modify",
Expand All @@ -144,7 +144,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio packages",
Expand All @@ -158,7 +158,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio raw",
Expand All @@ -171,7 +171,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio report",
Expand All @@ -183,7 +183,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio system",
Expand All @@ -196,7 +196,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio time",
Expand All @@ -208,7 +208,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio uninstall",
Expand All @@ -221,7 +221,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio upload [alhambra-ii]",
Expand All @@ -233,7 +233,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Apio upload [TinyFPGA-BX]",
Expand Down Expand Up @@ -281,7 +281,7 @@
],
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${workspaceFolder}/test-examples/Alhambra-II/02-jumping-LED"
"cwd": "${workspaceFolder}/test-examples/alhambra-ii/02-jumping-LED"
},
{
"name": "Attach remote",
Expand All @@ -300,7 +300,7 @@
"module": "pytest",
"args": [
"-s",
"test/commands/test_boards.py"
"test/commands/test_build.py::test_errors_without_apio_ini_1"
],
"console": "integratedTerminal",
"justMyCode": false,
Expand Down
86 changes: 74 additions & 12 deletions apio/apio_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import click
from apio import util, env_options
from apio.profile import Profile
from apio.managers.project import Project
from apio.managers.project import (
Project,
ProjectResolver,
load_project_from_file,
)


# ---------- RESOURCES
Expand Down Expand Up @@ -156,12 +160,60 @@ def __init__(
sorted(self.fpgas.items(), key=lambda t: t[0])
)

# -- If requested, load the project's apio.ini
# -- Save the load_project request, mostly for debugging.
self.project_loading_requested = load_project

# -- If requested, try to load the project's apio.ini. If apio.ini
# -- does not exist, the loading returns None.
self._project: Project = None
if load_project:
self._project = Project(self.project_dir)
self._project.read()
resolver = _ProjectResolverImpl(self)
self._project = load_project_from_file(self.project_dir, resolver)

def lookup_board_id(
self, board: str, *, warn: bool = True, strict: bool = True
) -> str:
"""Lookup and return the board's canonical board id which is its key
in boards.json(). 'board' can be the canonical id itself or a
legacy id of the board as defined in boards.json. The method prints
a warning if 'board' is a legacy board id that is mapped to its
canonical name and 'warn' is True. If the board is not found, the
method returns None if 'strict' is False or exit the program with a
message if 'strict' is True."""
# -- If this fails, it's a programming error.
assert board is not None

# -- The result. The board's key in boards.json.
canonical_id = None

if board in self.boards:
# -- Here when board is already the canonical id.
canonical_id = board
else:
self._project = None
# -- Look up for a board with 'board' as its legacy id.
for board_key, board_val in self.boards.items():
if board == board_val.get("legacy_id", None):
canonical_id = board_key
break

# -- Fatal error if unknown board.
if strict and canonical_id is None:
click.secho(f"Error: no such board '{board}'", fg="red")
click.secho(
"\nRun 'apio boards' for the list of board ids.", fg="yellow"
)
sys.exit(1)

# -- Warning if caller used a legacy board id.
if warn and canonical_id and board != canonical_id:
click.secho(
f"Warning: '{board}' board name was changed. "
f"Please use '{canonical_id}' instead.",
fg="yellow",
)

# -- Return the canonical board id.
return canonical_id

@staticmethod
def _check_no_spaces_in_dir(dir_path: Path, subject: str):
Expand All @@ -186,13 +238,10 @@ def has_project_loaded(self):
return self._project is not None

@property
def project(self):
"""Property to return the project after verification that it was
loaded. It's a programming error to call this method on a context
that was initialized with load_project = False."""
assert (
self.has_project_loaded
), "ApioContext.project() called with no project loaded."
def project(self) -> Project:
"""Return the project. It's None if project loading not requested or
project doesn't have apio.ini.
."""
return self._project

def _load_resource(self, name: str, allow_custom: bool = False) -> dict:
Expand Down Expand Up @@ -649,3 +698,16 @@ def _get_packages_dir(home_dir: Path) -> Path:

# -- All done.
return packages_dir


# pylint: disable=too-few-public-methods
class _ProjectResolverImpl(ProjectResolver):
def __init__(self, apio_context: ApioContext):
"""When ApioContext instanciates this object, ApioContext is fully
constructed, except for the project field."""
self._apio_context = apio_context

# @override
def lookup_board_id(self, board: str) -> str:
"""Implementation of lookup_board_id."""
return self._apio_context.lookup_board_id(board)
2 changes: 1 addition & 1 deletion apio/commands/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def list_boards(apio_ctx: ApioContext):
if config.terminal_mode():
# -- Board name with a bullet point and color
board_str = click.style(board, fg="cyan")
item_board = f"{board_str}"
item_board = f"{board_str}"

# -- Item in one line
one_line_item = f"{item_board} {item_fpga}"
Expand Down
16 changes: 11 additions & 5 deletions apio/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import sys
from pathlib import Path
import click
from apio.managers.project import Project, DEFAULT_TOP_MODULE, PROJECT_FILENAME
from apio.managers.project import (
DEFAULT_TOP_MODULE,
APIO_INI,
create_project_file,
)
from apio import util
from apio import cmd_util
from apio.commands import options
Expand All @@ -36,7 +40,7 @@
the command asks for permision to delete it. If --sayyes is specified,
the file is deleted automatically.
[Note] this command creates just the '{PROJECT_FILENAME}' file
[Note] this command creates just the '{APIO_INI}' file
rather than a full buildable project.
Some users use instead the examples command to copy a working
project for their board, and then modify it with with their design.
Expand Down Expand Up @@ -80,12 +84,14 @@ def cli(

project_dir = util.get_project_dir(project_dir)

# -- Map to canonical board id. This fails if the board is unknown.
board_id = apio_ctx.lookup_board_id(board)

# Create the apio.ini file
ok = Project.create_ini_file(
ok = create_project_file(
apio_ctx.project_dir,
board,
board_id,
top_module,
apio_ctx.boards,
sayyes,
)

Expand Down
2 changes: 1 addition & 1 deletion apio/commands/fpgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def list_fpgas(apio_ctx: ApioContext):
if config.terminal_mode():
# -- For terminal, print the FPGA name in color.
fpga_str = click.style(f"{fpga:32}", fg="cyan")
item = f"{fpga_str} {data_str}"
item = f"{fpga_str} {data_str}"
click.secho(item)
else:
# -- For pipe, no colors and no bullet point.
Expand Down
4 changes: 2 additions & 2 deletions apio/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _list_packages(apio_ctx: ApioContext, installed=True, notinstalled=True):
version = package["version"]
description = package["description"]

click.secho(f"{name} {version}")
click.secho(f"{name} {version}")
click.secho(f" {description}")

click.secho(dline, fg="green")
Expand All @@ -94,7 +94,7 @@ def _list_packages(apio_ctx: ApioContext, installed=True, notinstalled=True):
for package in notinstalled_packages:

click.secho(line)
name = click.style(f"{package['name']}", fg="red")
name = click.style(f"{package['name']}", fg="red")
description = package["description"]
click.secho(f"{name} {description}")

Expand Down
10 changes: 7 additions & 3 deletions apio/commands/modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path
from varname import nameof
import click
from apio.managers.project import Project
from apio.managers.project import modify_project_file
from apio import cmd_util
from apio.commands import options
from apio.apio_context import ApioContext
Expand Down Expand Up @@ -53,12 +53,16 @@ def cli(
# -- Create the apio context.
apio_ctx = ApioContext(project_dir=project_dir, load_project=False)

# -- If board is not empty, map it to canonical board id. This
# -- fails if the board is unknown.
if board:
board = apio_ctx.lookup_board_id(board)

# Create the apio.ini file
ok = Project.modify_ini_file(
ok = modify_project_file(
apio_ctx.project_dir,
board,
top_module,
apio_ctx.boards,
)

exit_code = 0 if ok else 1
Expand Down
Loading

0 comments on commit 0c45646

Please sign in to comment.