diff options
Diffstat (limited to 'src/python')
| -rw-r--r-- | src/python/common.py | 63 | ||||
| -rw-r--r-- | src/python/module.py | 8 |
2 files changed, 68 insertions, 3 deletions
diff --git a/src/python/common.py b/src/python/common.py new file mode 100644 index 000000000..f4cd97bbd --- /dev/null +++ b/src/python/common.py @@ -0,0 +1,63 @@ +# +# InspIRCd -- Internet Relay Chat Daemon +# +# Copyright (C) 2026 Sadie Powell <sadie@witchery.services> +# +# This file is part of InspIRCd. InspIRCd 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, version 2. +# +# 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 os +import pathlib +import re +import subprocess + +ROOT = pathlib.Path(__file__).resolve().parents[2] + + +# Executes a command and returns its stdout or None if it failed. +def command(*args): + result = subprocess.run( + args, + stderr=subprocess.DEVNULL, + stdout=subprocess.PIPE, + text=True, + ) + return result.stdout.strip() if result.returncode == 0 else None + + +# Retrieves the InspIRCd version from Git or the version file. +def version(): + segments = {"MAJOR": "0", "MINOR": "0", "PATCH": "0", "LABEL": None} + + # Attempt to retrieve version information from src/cmake/version.cmake + with open(ROOT / "src" / "cmake" / "version.cmake", "r") as fh: + for type, version in re.findall(r"\bset\(VERSION_([A-Z]+)\s+\"?(.+?)\"?\)", fh.read()): + segments[type] = version + + # Attempt to retrieve missing version information from Git + git_version = command(os.getenv("GIT", "git"), "describe", "--tags") + if git_version: + if match := re.fullmatch(r"v([0-9]+)\.([0-9]+)\.([0-9]+)(?:[a-z]+\d+)?(?:-\d+-g([0-9a-f]+))?", git_version): + segments["MAJOR"] = match.group(1) + segments["MINOR"] = match.group(2) + segments["PATCH"] = match.group(3) + if match.group(4): + segments["LABEL"] = match.group(4) + + # Build the full version string + segments["FULL"] = ".".join(str(segments[k]) for k in ["MAJOR", "MINOR", "PATCH"]) + if segments["LABEL"]: + segments["FULL"] += "-" + segments["LABEL"] + + return {k: int(v) if v.isnumeric() else v for k, v in segments.items()} diff --git a/src/python/module.py b/src/python/module.py index f04c60c68..65c54f4bb 100644 --- a/src/python/module.py +++ b/src/python/module.py @@ -19,9 +19,11 @@ import pathlib -DIR = pathlib.Path(__file__).resolve().parents[2] / "modules" -CONTRIB_DIR = DIR / "contrib" -EXTRA_DIR = DIR / "extra" +from . import common + +DIR = common.ROOT / "modules" +CONTRIB_DIR = DIR / "contrib" +EXTRA_DIR = DIR / "extra" # Converts a module name to a file name. |
