From 31a169a2d65cc9ae0ed40dad96b1f4c8f4a83315 Mon Sep 17 00:00:00 2001
From: Stefan Hansson <newbyte@postmarketos.org>
Date: Fri, 18 Oct 2024 23:30:18 +0200
Subject: [PATCH] mrhlpr, mrtest: Add --version command
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This also effectively reverts 7d068e2031ef398a6c05b344deeda8a49c6868fb,
because now we actually use the version for something in the program. It
also avoids the problems reported in
https://gitlab.postmarketos.org/postmarketOS/mrhlpr/-/issues/15 to the
best of my knowledge — I tried reproducing the bug as written in there
and did not get any build failures.

The primary purpose of this version command is for making
experimentation with mypyc easier, but I also can imagine it might be
nice for users to be able to get their version of mrtest easily.
---
 mrhlpr/frontend.py |  4 ++++
 mrhlpr/version.py  | 14 ++++++++++++++
 mrtest/frontend.py |  4 ++++
 pyproject.toml     |  5 ++++-
 4 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 mrhlpr/version.py

diff --git a/mrhlpr/frontend.py b/mrhlpr/frontend.py
index 9562008..d34298b 100644
--- a/mrhlpr/frontend.py
+++ b/mrhlpr/frontend.py
@@ -16,6 +16,7 @@ from . import ci_labels  # type: ignore[attr-defined]
 from . import git
 from . import gitlab
 from . import mr
+from . import version
 
 
 def print_status(mr_id: int, no_cache: bool = False) -> None:
@@ -178,6 +179,9 @@ def parse_args() -> argparse.Namespace:
         action="store_true",
         help="display debug log: all git commands and" " locations of http cache files",
     )
+    parser.add_argument(
+        "-V", "--version", action="version", version=version.get_full_version_information()
+    )
     sub = parser.add_subparsers(title="action", dest="action")
     sub.required = True
 
diff --git a/mrhlpr/version.py b/mrhlpr/version.py
new file mode 100644
index 0000000..009db83
--- /dev/null
+++ b/mrhlpr/version.py
@@ -0,0 +1,14 @@
+from typing import Final
+
+VERSION: Final[str] = "1.5.0"
+
+
+def get_full_version_information() -> str:
+    """Provides a full version string, including an educated guess about whether the program was
+    compiled by mypyc."""
+    return f"{VERSION} (compiled: {'yes' if is_mypyc_compiled() else 'no'})"
+
+
+def is_mypyc_compiled() -> bool:
+    """Attempts to determine whether the program was compiled by mypyc."""
+    return not __file__.endswith(".py")
diff --git a/mrtest/frontend.py b/mrtest/frontend.py
index c5b748c..7a2b434 100644
--- a/mrtest/frontend.py
+++ b/mrtest/frontend.py
@@ -6,6 +6,7 @@ import argparse
 import logging
 import sys
 
+import mrhlpr.version
 import mrtest.add_packages
 import mrtest.origin
 import mrtest.upgrade_packages
@@ -57,6 +58,9 @@ def parse_args() -> argparse.Namespace:
         action="store_true",
         help="display debug log: all commands and locations of" " http cache files",
     )
+    parser.add_argument(
+        "-V", "--version", action="version", version=mrhlpr.version.get_full_version_information()
+    )
     sub = parser.add_subparsers(title="action", dest="action")
     sub.required = True
 
diff --git a/pyproject.toml b/pyproject.toml
index a5e631c..737c558 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "mrhlpr"
-version = "1.5.0"
+dynamic = ["version"]
 authors = [
   {name = "postmarketOS Developers", email="info@postmarketos.org"}
 ]
@@ -35,6 +35,9 @@ api = ["python-gitlab>=4.0.0"]
 [project.urls]
 Homepage = "https://www.postmarketos.org"
 
+[tool.setuptools.dynamic]
+version = {attr =  "mrhlpr.version.VERSION"}
+
 [tool.setuptools.packages.find]
 include = ["mrhlpr*", "mrtest*"]
 
-- 
GitLab