From fdfe9756fb1e26cfd06239351f99b10a21851416 Mon Sep 17 00:00:00 2001 From: Zapta Date: Sun, 17 Nov 2024 17:54:38 -0800 Subject: [PATCH] Formalized the concept of apio env options with a new file env_options.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'. --- apio/env_options.py | 60 ++++++++++++++++++++++++++++++++++ apio/managers/old_installer.py | 4 +++ apio/resources.py | 11 ++++++- apio/util.py | 40 +++-------------------- 4 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 apio/env_options.py diff --git a/apio/env_options.py b/apio/env_options.py new file mode 100644 index 00000000..3133a3f1 --- /dev/null +++ b/apio/env_options.py @@ -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 +# ---- 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 diff --git a/apio/managers/old_installer.py b/apio/managers/old_installer.py index b7ff9a40..1a69c506 100644 --- a/apio/managers/old_installer.py +++ b/apio/managers/old_installer.py @@ -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... diff --git a/apio/resources.py b/apio/resources.py index 022f81a6..bbbc2488 100644 --- a/apio/resources.py +++ b/apio/resources.py @@ -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 @@ -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) diff --git a/apio/util.py b/apio/util.py index fe19752c..811b385c 100644 --- a/apio/util.py +++ b/apio/util.py @@ -22,6 +22,7 @@ import click from serial.tools.list_ports import comports import requests +from apio import env_options # ---------------------------------------- # -- Constants @@ -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 @@ -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 @@ -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