Files
ahriman/toxfile.py

71 lines
2.1 KiB
Python

#
# Copyright (c) 2021-2025 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import importlib
import sys
from tox.config.sets import EnvConfigSet
from tox.plugin import impl
from tox.session.state import State
from tox.tox_env.api import ToxEnv
@impl
def tox_add_env_config(env_conf: EnvConfigSet, state: State) -> None:
"""
append plugin options to the configuration keys
Args:
env_conf(EnvConfigSet): the core configuration object
state(State): the global tox state object
"""
del state
env_conf.add_config(
keys=["dynamic_version"],
of_type=str,
default="",
desc="import path for the version variable",
)
@impl
def tox_before_run_commands(tox_env: ToxEnv) -> None:
"""
extract version dynamically and set VERSION environment variable
Args:
tox_env(ToxEnv): the tox environment being executed
"""
import_path = tox_env.conf["dynamic_version"]
if not import_path:
return # global switch
set_env = tox_env.conf["set_env"]
if "PYTHONPATH" in set_env:
# package is not installed here yet in env, so we need to import it from root
sys.path.append(set_env.load("PYTHONPATH"))
module_name, variable_name = import_path.rsplit(".", maxsplit=1)
module = importlib.import_module(module_name)
version = getattr(module, variable_name)
set_env.update({"VERSION": version})