Skip to content

Commit

Permalink
Formalized the concept of apio env options with a new file env_option…
Browse files Browse the repository at this point in the history
…s.py.

Now also printing on program start a message with the names of the active env options,
for better log transparancy.

Currently there are two env options, APIO_HOME_DIR and APIO_PKG_DIR. Next will add one
for APIO_PLATFORM to override the platform id.

The modified values can be examined in the existing command 'apio system --info'.
  • Loading branch information
zapta committed Nov 18, 2024
1 parent f0aca51 commit fdfe975
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
60 changes: 60 additions & 0 deletions apio/env_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# -- This file is part of the Apio project
# -- (C) 2016-2018 FPGAwars
# -- Author Jesús Arroyo
# -- Licence GPLv2
# -- Derived from:
# ---- Platformio project
# ---- (C) 2014-2016 Ivan Kravets <[email protected]>
# ---- Licence Apache v2
"""Functions for reading the APIO env options. This are system env
variables that are used to modify the default behavior of APIO.
"""

import os
from typing import List

# -- Env variable to override the apio home dir ~/.apio
APIO_HOME_DIR = "APIO_HOME_DIR"

# -- Env variable to override the apio packages dir ~/.apio/packages.
APIO_PKG_DIR = "APIO_PKG_DIR"


_SUPPORTED_APIO_VARS = [
APIO_HOME_DIR,
APIO_PKG_DIR,
]


def get(var_name: str, default: str = None):
"""Return the given APIO config env value or default if not found.
var_name must start with 'APIO_' and match _API_ENV_NAME_REGEX.
"""

# -- Sanity check. To make sure we are aware of all the vars used.
assert (
var_name in _SUPPORTED_APIO_VARS
), f"Unknown apio env var '{var_name}'"

# -- Get the value, None if not defined.
var_value = os.getenv(var_name)

if var_value is None:
# -- Var is undefied. Use default
var_value = default
else:
# -- Var is defined. For windows benefit, remove optional quotes.
if var_value.startswith('"') and var_value.endswith('"'):
var_value = var_value[1:-1]

return var_value


def get_defined() -> List[str]:
"""Return the list of apio env options vars that are defined."""
result = []
for var in _SUPPORTED_APIO_VARS:
if get(var):
result.append(var)
return result
4 changes: 4 additions & 0 deletions apio/managers/old_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def __init__(
self.packages_dir = ""

# -- Folder name were the packages are stored
# --
# -- NOTE: we shouldn't assume the directory name since it can be
# -- overriden with APIO_PKG_DIR but since this old installer is
# -- going away, we leave this as is. (Nov 2024)
dirname = "packages"

# -- If the package is known...
Expand Down
11 changes: 10 additions & 1 deletion apio/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pathlib import Path
from typing import Optional, Dict
import click
from apio import util
from apio import util, env_options
from apio.profile import Profile


Expand Down Expand Up @@ -93,6 +93,15 @@ def __init__(
'apio packages' uses the global scope while commands such as
'apio build' use the project scope.
"""

# -- Inform as soon as possible about the list of apio env options
# -- that modify its default behavior.
defined_env_options = env_options.get_defined()
if defined_env_options:
click.secho(
f"Active env options {defined_env_options}.", fg="yellow"
)

# -- Maps the optional project_dir option to a path.
self.project_dir: Path = util.get_project_dir(project_dir)

Expand Down
40 changes: 5 additions & 35 deletions apio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import click
from serial.tools.list_ports import comports
import requests
from apio import env_options

# ----------------------------------------
# -- Constants
Expand Down Expand Up @@ -150,39 +151,6 @@ def get_path_in_apio_package(subpath: str) -> Path:
return path


def get_projconf_option_dir(name: str, default=None):
"""Return the project option with the given name
These options are place either on environment variables or
into the /etc/apio.json file in the case of debian distributions
All the APIO environment variables have the prefix "APIO_"
Project options:
* home_dir : APIO home directory
"""

# -- Get the full name of the environment variable
_env_name = f"APIO_{name.upper()}"

# -- Check if the environment variable
# -- is defined
if _env_name in os.environ:
# -- Read the value of the environmental variable
_env_value = os.getenv(_env_name)

# -- On window systems the environmental variables can
# -- include the quotes (""). But not in Linux
# -- If there are quotes, remove them
if _env_value.startswith('"') and _env_value.endswith('"'):
_env_value = _env_value[1:-1]

return _env_value

# -- Return the default home_dir
return default


def get_home_dir() -> Path:
"""Get the APIO Home dir. This is the apio folder where the profle is
located and the packages installed. The APIO Home dir can be set in the
Expand All @@ -195,7 +163,9 @@ def get_home_dir() -> Path:

# -- Get the APIO_HOME_DIR env variable
# -- It returns None if it was not defined
apio_home_dir_env = get_projconf_option_dir("home_dir")
apio_home_dir_env = env_options.get(
env_options.APIO_HOME_DIR, default=None
)

# -- Get the home dir. It is what the APIO_HOME_DIR env variable
# -- says, or the default folder if None
Expand Down Expand Up @@ -234,7 +204,7 @@ def get_packages_dir() -> Path:

# -- Get the APIO_PKG_DIR env variable
# -- It returns None if it was not defined
apio_pkg_dir_env = get_projconf_option_dir("pkg_dir")
apio_pkg_dir_env = env_options.get(env_options.APIO_PKG_DIR)

# -- Get the pkg base dir. It is what the APIO_PKG_DIR env variable
# -- says, or the default folder if None
Expand Down

0 comments on commit fdfe975

Please sign in to comment.