Open-MSS / MSS

A QT application, a OGC web map server, a collaboration server to plan atmospheric research flights.
https://open-mss.github.io
Apache License 2.0
61 stars 77 forks source link

review / refactor tests/constants.py #2536

Open ReimarBauer opened 1 month ago

ReimarBauer commented 1 month ago

This looks to me currently a bit overengeenered. On github also each worker creates anyway a new tmp dir. Is there any case on local development where we need a second information about the state of the git repository?

try:
    import git
except ImportError:
    SHA = ""
else:
    path = os.path.dirname(os.path.realpath(__file__))
    repo = git.Repo(path, search_parent_directories=True)
    logging.debug(path)
    try:
        # this is for local development important to get a fresh named tmpdir
        SHA = repo.head.object.hexsha[0:10]
    except (ValueError, BrokenPipeError) as ex:
        logging.debug("Unknown Problem: %s", ex)
        # mounted dir in docker container and owner is not root
        SHA = ""

ROOT_FS = TempFS(identifier=f"msui{SHA}")
MF-Vroom commented 1 month ago

Catch specific exceptions instead of general ValueError and BrokenPipeError. Consider git.exc.InvalidGitRepositoryError

YashaswiniTB commented 3 weeks ago

Can I work on this issue?

ReimarBauer commented 3 weeks ago

Welcome @YashaswiniTB I assigned it to you.

imakhan80 commented 2 weeks ago

This looks to me currently a bit overengeenered. On github also each worker creates anyway a new tmp dir. Is there any case on local development where we need a second information about the state of the git repository?

try:
    import git
except ImportError:
    SHA = ""
else:
    path = os.path.dirname(os.path.realpath(__file__))
    repo = git.Repo(path, search_parent_directories=True)
    logging.debug(path)
    try:
        # this is for local development important to get a fresh named tmpdir
        SHA = repo.head.object.hexsha[0:10]
    except (ValueError, BrokenPipeError) as ex:
        logging.debug("Unknown Problem: %s", ex)
        # mounted dir in docker container and owner is not root
        SHA = ""

ROOT_FS = TempFS(identifier=f"msui{SHA}")

The SHA is being used as an identifier for the TempFS, presumably to create unique and consistent temporary filesystems during local development. However, as you pointed out, in many environments (like GitHub Actions), a new temporary directory is created for each run anyway, making this extra identifier potentially unnecessary. For local development, having the SHA in the identifier could potentially help in tracking changes across different commits or branches, but this benefit might be minimal in most cases. The current implementation has error handling for cases where the git repository information can't be retrieved, falling back to an empty string for the SHA. import os import tempfile import logging

def get_git_sha(): try: from git import Repo except ImportError: logging.debug("GitPython not installed, skipping SHA retrieval") return ""

try:
    path = os.path.dirname(os.path.realpath(__file__))
    repo = Repo(path, search_parent_directories=True)
    sha = repo.head.object.hexsha[:10]
    logging.debug(f"Git SHA: {sha}")
    return sha
except Exception as ex:
    logging.debug(f"Failed to get Git SHA: {ex}")
    return ""

Use a unique identifier for the temporary filesystem

unique_id = f"msui{get_git_sha()}" ROOT_FS = tempfile.mkdtemp(prefix=unique_id)