From 39a532cf87370873f2c222dd7cc4dbf095c25637 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sat, 19 Jun 2021 06:15:22 -0400 Subject: [PATCH] utils: move git version parsing into its own method This creates a generic utility for retreiving the git version. Moonraker will now attempt to parse basic version info from a ".version" file if git describe fails. Signed-off-by: Eric Callahan --- moonraker/utils.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/moonraker/utils.py b/moonraker/utils.py index cc1073d..67e7524 100644 --- a/moonraker/utils.py +++ b/moonraker/utils.py @@ -8,6 +8,7 @@ from __future__ import annotations import logging import logging.handlers import os +import pathlib import sys import subprocess import asyncio @@ -76,26 +77,35 @@ class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler): # Parse the git version from the command line. This code # is borrowed from Klipper. +def retreive_git_version(source_path: str) -> str: + # Obtain version info from "git" program + prog = ('git', '-C', source_path, 'describe', '--always', + '--tags', '--long', '--dirty') + process = subprocess.Popen(prog, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + ver, err = process.communicate() + retcode = process.wait() + if retcode == 0: + return ver.strip().decode() + raise Exception(f"Failed to retreive git version: {err.decode()}") + def get_software_version() -> str: moonraker_path = os.path.join( os.path.dirname(__file__), '..') + version = "?" - # Obtain version info from "git" program - prog = ('git', '-C', moonraker_path, 'describe', '--always', - '--tags', '--long', '--dirty') try: - process = subprocess.Popen(prog, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - ver, err = process.communicate() - retcode = process.wait() - if retcode == 0: - return ver.strip().decode() - else: - logging.debug(f"Error getting git version: {err.decode()}") - except OSError: - logging.exception("Error runing git describe") - - return "?" + version = retreive_git_version(moonraker_path) + except Exception: + vfile = pathlib.Path(os.path.join( + moonraker_path, "moonraker/.version")) + if vfile.exists(): + try: + version = vfile.read_text().strip() + except Exception: + logging.exception("Unable to extract version from file") + version = "?" + return version def setup_logging(log_file: str, software_version: str