changed the way on how to get the version to a json file, added helper to libreftit

This commit is contained in:
2025-10-16 19:51:16 +02:00
parent 6cbb352efb
commit 82e6856439
3 changed files with 38 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
import os
import sys
import json
from .refit_logger import logger
@@ -247,3 +248,30 @@ def create_parallel_directories(input_path: str, target_depth: int, width: int,
current_depth=0,
name=name,
)
def get_version_from_file(input_path) -> str:
"""Returns the version of the program.
The function accepts an path to a version file as a string and it
returns it version number formatted.
Arg:
input_path (str): The absolute or relative path to the
version file.
Example:
>>>get_version_from_file("/path/to/file.json")
'0.2.1'"""
# Expands the input path
expanded_path = os.path.expanduser(input_path)
# Opening the file and reading its contents, saving as as an dict.
file_path = open(expanded_path)
prog_version = json.load(file_path)
# Formatting the output before returning the string again
pretty_version = (
f"{prog_version['major']}.{prog_version['minor']}.{prog_version['patch']}"
)
return pretty_version

View File

@@ -1,43 +1,14 @@
import argparse
import sys
import os
import configparser
from modules.refit_logger import logger
from modules.refit_create import Refit_Create
from modules.librefit import get_version_from_file
CONFIG_FILE = "version.cfg"
def read_version_config():
# logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}")
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
if not os.path.exists(CONFIG_FILE):
# logger.error(
# f"FUNC: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'"
# )
return "x.x.x"
if "VERSION" not in config:
# logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'")
return "x.x.x"
try:
v = config["VERSION"]
major = v.get("major", "0")
minor = v.get("minor", "0")
patch = v.get("patch", "0")
# logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}")
return f"{major}.{minor}.{patch}"
except Exception:
# logger.warning("Couldn not read version from config file")
return "x.x.x"
REFIT_VERSION = f"Refit Beta {read_version_config()}"
# NOTE: The final version file needs a dedicated place to live in
# so the version number is always readable, independent from where it
# is executed
REFIT_VERSION = get_version_from_file("~/Documents/git/python/refit/src/version.json")
# ---------------------------ARGPARSE START---------------------------

5
refit/src/version.json Normal file
View File

@@ -0,0 +1,5 @@
{
"minor": 3,
"major": 0,
"patch": 8
}