Commit 9f545abc authored by Benjamin Winger's avatar Benjamin Winger

Reimplemented ModInfo in a somewhat nicer manner

Rather than modifying frames, we abuse the import system to allow importing from portmod.pybuild.modinfo to return values relevant to the pybuild
Note that this requires deleting the cached version of the module to work, as otherwise the values will always be the same as the first time it was imported
parent 7b6b5a04
Pipeline #60830189 passed with stages
in 4 minutes and 3 seconds
# Copyright 2019 Portmod Authors # Copyright 2019 Portmod Authors
# Distributed under the terms of the GNU General Public License v3 # Distributed under the terms of the GNU General Public License v3
from .pybuild import ( # noqa # pylint: disable=unused-import from .pybuild import Pybuild1, InstallDir, File # noqa # pylint: disable=unused-import
Pybuild1,
InstallDir,
File,
ModInfo,
)
from .util import tr_patcher # noqa # pylint: disable=unused-import from .util import tr_patcher # noqa # pylint: disable=unused-import
from portmod.log import warn # noqa # pylint: disable=unused-import from portmod.log import warn # noqa # pylint: disable=unused-import
from portmod.globals import DOWNLOAD_DIR # noqa # pylint: disable=unused-import from portmod.globals import DOWNLOAD_DIR # noqa # pylint: disable=unused-import
......
import inspect
import os
from pathlib import Path
from portmod.repo.atom import Atom
"""
Importing values from this module fills them with information about the file
from which they were imported
This module should be removed from sys.modules prior to importing to ensure that
the cached version is not used instead.
"""
__FILENAME = None
for i in inspect.stack(0):
if i.filename.endswith(".pybuild"):
__FILENAME = i.filename
assert __FILENAME is not None
CATEGORY = Path(__FILENAME).resolve().parent.parent.name
__ATOM = Atom(
"{}/{}".format(CATEGORY, os.path.basename(__FILENAME)[: -len(".pybuild")])
)
M = __ATOM.M
MF = __ATOM.MF
MN = __ATOM.MN
MV = __ATOM.MV
MR = __ATOM.MR
MVR = __ATOM.MV
if __ATOM.MR is not None:
MVR += "-" + __ATOM.MR
...@@ -20,27 +20,6 @@ from portmod.log import err ...@@ -20,27 +20,6 @@ from portmod.log import err
from portmod.colour import colour from portmod.colour import colour
from colorama import Fore from colorama import Fore
from ..pybuild_interface import Pybuild from ..pybuild_interface import Pybuild
import inspect
# This is a terrible hack function, but it does the job. This should only be used
# within pybuild files. macropy may provide a more robust way of implementing this
def ModInfo():
frame = inspect.currentframe()
try:
g = frame.f_back.f_globals
filename = g["__file__"]
category = Path(filename).resolve().parent.parent.name
ATOM = Atom(
"{}/{}".format(category, os.path.basename(filename)[: -len(".pybuild")])
)
g["M"] = ATOM.M
g["MN"] = ATOM.MN
g["MV"] = ATOM.MV
g["MR"] = ATOM.MR
g["CM"] = ATOM.CM
finally:
del frame
class InstallDir: class InstallDir:
......
# Copyright 2019 Portmod Authors # Copyright 2019 Portmod Authors
# Distributed under the terms of the GNU General Public License v3 # Distributed under the terms of the GNU General Public License v3
import sys
import importlib.machinery import importlib.machinery
import glob import glob
import os import os
...@@ -21,6 +22,10 @@ __mods = {} ...@@ -21,6 +22,10 @@ __mods = {}
def load_file(file): def load_file(file):
# Ensure that we never use the cached version of modinfo
if "portmod.pybuild.modinfo" in sys.modules:
del sys.modules["portmod.pybuild.modinfo"]
filename, _ = os.path.splitext(os.path.basename(file)) filename, _ = os.path.splitext(os.path.basename(file))
loader = importlib.machinery.SourceFileLoader(filename, file) loader = importlib.machinery.SourceFileLoader(filename, file)
try: try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment