From 9107474d315629abeb286cde8e3af848dc38ebb9 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Fri, 23 Jun 2023 21:30:31 -0400 Subject: [PATCH] Add version info from git tags --- Makefile | 3 +-- mail4one/server.py | 12 ++++++++---- mail4one/version.py | 2 ++ scripts/get_version.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 mail4one/version.py create mode 100755 scripts/get_version.sh diff --git a/Makefile b/Makefile index 525f4f8..9aff346 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,3 @@ - - shell: MYPYPATH=`pipenv --venv`/lib/python3.11/site-packages pipenv shell @@ -18,6 +16,7 @@ requirements.txt: Pipfile.lock build: clean requirements.txt python3 -m pip install -r requirements.txt --target build cp -r mail4one/ build/ + sed -i "s/DEVELOMENT/$(shell scripts/get_version.sh)/" build/mail4one/version.py python3 -m compileall build/mail4one -f rm -rf build/*.dist-info python3 -m zipapp \ diff --git a/mail4one/server.py b/mail4one/server.py index efdac58..fbc9294 100644 --- a/mail4one/server.py +++ b/mail4one/server.py @@ -9,6 +9,7 @@ from getpass import getpass from .smtp import create_smtp_server_starttls, create_smtp_server from .pop3 import create_pop_server +from .version import VERSION from . import config from . import pwhash @@ -25,15 +26,17 @@ def setup_logging(cfg: config.LogCfg): if cfg.logfile == "STDOUT": logging.basicConfig(level=cfg.level, format=logging_format) else: - logging.basicConfig(filename=cfg.logfile, level=cfg.level, format=logging_format) - + logging.basicConfig(filename=cfg.logfile, + level=cfg.level, + format=logging_format) async def a_main(cfg: config.Config) -> None: default_tls_context: ssl.SSLContext | None = None if tls := cfg.default_tls: - logging.info(f"Initializing default tls {tls.certfile=}, {tls.keyfile=}") + logging.info( + f"Initializing default tls {tls.certfile=}, {tls.keyfile=}") default_tls_context = create_tls_context(tls.certfile, tls.keyfile) def get_tls_context(tls: config.TLSCfg | str): @@ -102,6 +105,7 @@ def main() -> None: description="Personal Mail Server", epilog="See https://gitea.balki.me/balki/mail4one for more info", ) + parser.add_argument("-v", "--version", action="version", version=VERSION) parser.add_argument( "-e", "--echo_password", @@ -150,7 +154,7 @@ def main() -> None: else: cfg = config.Config(args.config.read_text()) setup_logging(config.LogCfg(cfg.logging)) - logging.info(f"Starting mail4one {args.config=!s}") + logging.info(f"Starting mail4one {VERSION} {args.config=!s}") asyncio.run(a_main(cfg)) diff --git a/mail4one/version.py b/mail4one/version.py new file mode 100644 index 0000000..799ec46 --- /dev/null +++ b/mail4one/version.py @@ -0,0 +1,2 @@ + +VERSION = "DEVELOMENT" diff --git a/scripts/get_version.sh b/scripts/get_version.sh new file mode 100755 index 0000000..336560f --- /dev/null +++ b/scripts/get_version.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +commit=$(git rev-parse --short HEAD) + +# This is true if there is a tag on current HEAD +if git describe --exact-match > /dev/null 2>&1 +then + tag_val=$(git describe --dirty=DIRTY --exact-match) + case "$tag_val" in + *DIRTY) + echo "git=$commit-changes" + exit + ;; + v*) # Only consider tags starting with v + echo "$tag_val" + ;; + *) + echo "git-$commit" + esac +else + tag_val=$(git describe --dirty=DIRTY) + case "$tag_val" in + *DIRTY) + echo "git-$commit-changes" + ;; + *) + echo "git-$commit" + esac +fi +