MSS icon indicating copy to clipboard operation
MSS copied to clipboard

review / refactor tests/constants.py

Open ReimarBauer opened this issue 1 year ago • 4 comments

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}")

ReimarBauer avatar Sep 19 '24 08:09 ReimarBauer

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

MF-Vroom avatar Sep 20 '24 15:09 MF-Vroom

Can I work on this issue?

YashaswiniTB avatar Sep 29 '24 12:09 YashaswiniTB

Welcome @YashaswiniTB I assigned it to you.

ReimarBauer avatar Sep 30 '24 07:09 ReimarBauer

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)

imakhan80 avatar Oct 11 '24 10:10 imakhan80