Skip to content

Commit

Permalink
apio init: --top-module update the current apio.ini file if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Feb 20, 2024
1 parent 80d5836 commit 95c9b00
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
3 changes: 3 additions & 0 deletions apio/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ def cli(ctx, board, top_module, scons, project_dir, sayyes):
Project().create_sconstruct(project_dir, "ice40", sayyes)
elif board:
Project().create_ini(board, top_module, project_dir, sayyes)
elif top_module:
print("INIT TOP-MODULE!!")
Project().update_ini(top_module)
else:
click.secho(ctx.get_help())
37 changes: 34 additions & 3 deletions apio/managers/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def outer(*args):
return outer


# R0912: Too many branches (14/12)
# pylint: disable=R0912
@debug_params
def process_arguments(
config_ini: dict, resources: type[Resources]
Expand Down Expand Up @@ -135,6 +137,11 @@ def process_arguments(
# -- * None: No apio.ini file
# -- * "name": Board name (str)

# -- proj.top_module:
# -- * None: Not specified in apio.ini file
# -- * "name": name of the top-module to use
# -- (if not overriden by arguments)

# -- DEBUG: Print both: project board and configuration board
debug_config_item(config, BOARD, proj.board)

Expand Down Expand Up @@ -201,9 +208,33 @@ def process_arguments(
raise ValueError(f"Missing FPGA {item.upper()}")

# -- Process the top-module
# -- If it has not set by arguments, give it the default value: main
if config[TOP_MODULE] is None:
config[TOP_MODULE] = "main"
# -- Priority 1: Given by arguments in the command line
# -- If it has not been set by arguments...
if not config[TOP_MODULE]:

# -- Priority 2: Use the top module in the apio.ini file
# -- if it exists...
if proj.top_module:
config[TOP_MODULE] = proj.top_module

# -- NO top-module specified!! Warn the user
else:
click.secho(
"No top module given\n",
fg="red",
)
click.secho(
"Option 1: Pass it as a parameter\n"
" `--top-module <top module name>`\n\n"
"Option 2: Insert in the ini file\n"
" `apio init --top-module <top-module>`\n",
fg="yellow",
)

# -- "main" is used as a default top-level
config[TOP_MODULE] = "main"

click.secho("Using the default top-module: `main`", fg="blue")

# -- Debug: Print current configuration
print_configuration(config)
Expand Down
42 changes: 36 additions & 6 deletions apio/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@ def create_ini(self, board, top_module, project_dir="", sayyes=False):
# -- Create the apio.ini from scratch
self._create_ini_file(board, top_module, ini_path, PROJECT_FILENAME)

def update_ini(self, top_module):
"""Update the current init file with the given top-module"""

# -- Build the filename
ini_path = PROJECT_FILENAME

# -- Check if the apio.ini file exists
if not isfile(ini_path):
click.secho(
"No apio.ini file. You should first create it:\n"
" apio init --board <boardname>\n",
fg="red",
)
return

# -- Read the current apio.ini file
config = configparser.ConfigParser()
config.read(PROJECT_FILENAME)

# -- Set the new top-mddule
self.top_module = top_module
config.set("env", "top-module", top_module)

# -- Write the apio ini file
with open(PROJECT_FILENAME, "w", encoding="utf-8") as inifile:
config.write(inifile)

click.secho(
f"File '{PROJECT_FILENAME}' has been successfully updated!",
fg="green",
)

@staticmethod
def _create_ini_file(board, top_module, ini_path, ini_name):
click.secho(f"Creating {ini_name} file ...")
Expand All @@ -113,7 +145,7 @@ def _create_ini_file(board, top_module, ini_path, ini_name):
config.add_section("env")
config.set("env", "board", board)

# -- Work in progress
# -- Set the top module
config.set("env", "top-module", top_module)

# -- Write the apio ini file
Expand Down Expand Up @@ -167,11 +199,10 @@ def read(self):
# -- Update top-module
self.top_module = top_module

# -- Warn the user the top module has not been set in the apio.ini
# -- file
if not top_module:
print("-------> NO TOP MODLE!!!")

# -- TODO: Check if top_module has a value or not....
# -- (backward compatibility)
click.secho("Warning! No TOP-MODULE in apio.ini", fg="yellow")

@staticmethod
def _read_board() -> str:
Expand All @@ -195,7 +226,6 @@ def _read_top_module() -> str:

config = configparser.ConfigParser()
config.read(PROJECT_FILENAME)
print("VAAAAAMOS A LEER TOP-MODULE!!!")

try:
top_module = config.get("env", "top-module")
Expand Down

0 comments on commit 95c9b00

Please sign in to comment.