From 3ced078798e3ac3c0aeded37fc4cf9ac5a5ae3a2 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 24 Apr 2026 00:58:00 +0100 Subject: Add the common Python module to deduplicate some code. --- src/python/common.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/python/common.py (limited to 'src/python/common.py') 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 +# +# 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 . +# + + +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()} -- cgit v1.3.1-10-gc9f91