utils: add method to validate packaged applications

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-06-29 20:20:53 -04:00
parent 0527904c48
commit 7c56a8fa74
1 changed files with 21 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import sys
import subprocess import subprocess
import asyncio import asyncio
import hashlib import hashlib
import json
from queue import SimpleQueue as Queue from queue import SimpleQueue as Queue
# Annotation imports # Annotation imports
@ -25,6 +26,8 @@ from typing import (
Any, Any,
) )
MOONRAKER_PATH = os.path.join(os.path.dirname(__file__), '..')
class ServerError(Exception): class ServerError(Exception):
def __init__(self, message: str, status_code: int = 400) -> None: def __init__(self, message: str, status_code: int = 400) -> None:
Exception.__init__(self, message) Exception.__init__(self, message)
@ -89,15 +92,13 @@ def retreive_git_version(source_path: str) -> str:
raise Exception(f"Failed to retreive git version: {err.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(
os.path.dirname(__file__), '..')
version = "?" version = "?"
try: try:
version = retreive_git_version(moonraker_path) version = retreive_git_version(MOONRAKER_PATH)
except Exception: except Exception:
vfile = pathlib.Path(os.path.join( vfile = pathlib.Path(os.path.join(
moonraker_path, "moonraker/.version")) MOONRAKER_PATH, "moonraker/.version"))
if vfile.exists(): if vfile.exists():
try: try:
version = vfile.read_text().strip() version = vfile.read_text().strip()
@ -118,6 +119,8 @@ def setup_logging(app_args: Dict[str, Any]
stdout_fmt = logging.Formatter( stdout_fmt = logging.Formatter(
'[%(filename)s:%(funcName)s()] - %(message)s') '[%(filename)s:%(funcName)s()] - %(message)s')
stdout_hdlr.setFormatter(stdout_fmt) stdout_hdlr.setFormatter(stdout_fmt)
for name, val in app_args.items():
logging.info(f"{name}: {val}")
file_hdlr = None file_hdlr = None
if app_args.get('log_file', ""): if app_args.get('log_file', ""):
file_hdlr = MoonrakerLoggingHandler( file_hdlr = MoonrakerLoggingHandler(
@ -157,3 +160,17 @@ def hash_directory(dir_path: str,
except Exception: except Exception:
pass pass
return checksum.hexdigest() return checksum.hexdigest()
def verify_source(path: str = MOONRAKER_PATH) -> Optional[Tuple[str, bool]]:
rfile = pathlib.Path(os.path.join(path, ".release_info"))
if not rfile.exists():
return None
try:
rinfo = json.loads(rfile.read_text())
except Exception:
return None
orig_chksum = rinfo['source_checksum']
ign_dirs = rinfo['ignored_dirs']
ign_exts = rinfo['ignored_exts']
checksum = hash_directory(path, ign_exts, ign_dirs)
return checksum, checksum == orig_chksum