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
import logging.handlers import logging.handlers
import os import os
import pathlib
import sys import sys
import subprocess import subprocess
import asyncio import asyncio
@ -76,26 +77,35 @@ class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler):
# Parse the git version from the command line. This code # Parse the git version from the command line. This code
# is borrowed from Klipper. # 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: def get_software_version() -> str:
moonraker_path = os.path.join( moonraker_path = os.path.join(
os.path.dirname(__file__), '..') os.path.dirname(__file__), '..')
version = "?"
# Obtain version info from "git" program
prog = ('git', '-C', moonraker_path, 'describe', '--always',
'--tags', '--long', '--dirty')
try: try:
process = subprocess.Popen(prog, stdout=subprocess.PIPE, version = retreive_git_version(moonraker_path)
stderr=subprocess.PIPE) except Exception:
ver, err = process.communicate() vfile = pathlib.Path(os.path.join(
retcode = process.wait() moonraker_path, "moonraker/.version"))
if retcode == 0: if vfile.exists():
return ver.strip().decode() try:
else: version = vfile.read_text().strip()
logging.debug(f"Error getting git version: {err.decode()}") except Exception:
except OSError: logging.exception("Unable to extract version from file")
logging.exception("Error runing git describe") version = "?"
return version
return "?"
def setup_logging(log_file: str, def setup_logging(log_file: str,
software_version: str software_version: str