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 <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-06-19 06:15:22 -04:00
parent ebaac290e7
commit 39a532cf87
1 changed files with 25 additions and 15 deletions

View File

@ -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