IMPROVE: rename repo to archive and rewrite certain parts
This commit is contained in:
parent
c9d77ca119
commit
0d1c7b4768
|
@ -6,7 +6,7 @@ from typing import List, Optional
|
|||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from mia import parser, api, config
|
||||
from mia import parser, api, config, archiveUtils
|
||||
from mia.parser import Subparser
|
||||
from mia.utils import customTypes
|
||||
|
||||
|
@ -54,7 +54,7 @@ def doSetup(argv: List[str]) -> argparse.Namespace:
|
|||
_log.debug(f"Start mia with {argv}")
|
||||
|
||||
initSettingsConfig(args.config)
|
||||
verifyRepository()
|
||||
initArchive()
|
||||
|
||||
return args
|
||||
|
||||
|
@ -76,22 +76,26 @@ def initSettingsConfig(configPath: Optional[Path]) -> None:
|
|||
sys.exit(customTypes.Exit.errConfig)
|
||||
|
||||
|
||||
def verifyRepository() -> None:
|
||||
"""Performs several checks on the archive repository."""
|
||||
def initArchive() -> None:
|
||||
"""Initializes archive and performs several checks on it.
|
||||
|
||||
repoPath = api.settings.getValue(api.settings.General.archivePath)
|
||||
api.repository.init(repoPath)
|
||||
Initializes underlying repository and configures it. Exists on unrecoverable issues.
|
||||
"""
|
||||
|
||||
archivePath = api.settings.getValue(api.settings.General.archivePath)
|
||||
archiveUtils.archive.init(archivePath)
|
||||
archive = archiveUtils.archive.archive
|
||||
|
||||
# Check if mainBranch exists
|
||||
mainBranch = api.settings.getValue(api.settings.General.mainBranch)
|
||||
if not api.repository.repo.branchExists(mainBranch):
|
||||
if not archive.branchExists(mainBranch):
|
||||
# TODO: create main branch
|
||||
raise NotImplementedError("Create main branch")
|
||||
|
||||
# Check if initialCommit exists
|
||||
commit = api.settings.getValue(api.settings.General.initialCommit)
|
||||
if not api.repository.repo.treeIshExists(commit):
|
||||
_log.error(f"Sha {commit} does not exists.")
|
||||
initialCommit = api.settings.getValue(api.settings.General.initialCommit)
|
||||
if not archive.commitExistsOnBranch(initialCommit, mainBranch):
|
||||
_log.error(f"Commit {initialCommit} does not exists.")
|
||||
sys.exit(customTypes.Exit.errConfig)
|
||||
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
from mia.api import settings, repository
|
||||
from mia.api import settings
|
||||
|
|
|
@ -1,84 +0,0 @@
|
|||
""" Interact with the archive repository.
|
||||
|
||||
Module attributes:
|
||||
_archiveRepo: An instance of Repository representing the archive.
|
||||
"""
|
||||
|
||||
from __future__ import (
|
||||
annotations,
|
||||
) # For forward references, should not be necessary in py 3.11
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import git
|
||||
from git.repo import Repo
|
||||
from typing import Optional
|
||||
|
||||
from mia import utils
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
_repository: Optional[Repository] = None
|
||||
|
||||
|
||||
def init(path: Path) -> None:
|
||||
"""Create instance of Repository if not already existing.
|
||||
|
||||
Args:
|
||||
path: Path to repository.
|
||||
"""
|
||||
global _repository
|
||||
if _repository is not None:
|
||||
return
|
||||
_log.debug(f"Initializing repo object at {path}")
|
||||
_repository = Repository(path)
|
||||
|
||||
|
||||
@utils.moduleProperty
|
||||
def _repo() -> Optional[Repository]:
|
||||
"""Getter for _repository."""
|
||||
return _repository
|
||||
|
||||
|
||||
class Repository:
|
||||
"""Handle git and git-annex related functionalities."""
|
||||
|
||||
def __init__(self, repoPath: Path) -> None:
|
||||
if not repoPath.is_dir():
|
||||
# TODO: create dir if not existing
|
||||
raise NotImplementedError("Create repo dir")
|
||||
|
||||
try:
|
||||
self._repo = Repo(repoPath)
|
||||
except git.InvalidGitRepositoryError:
|
||||
# TODO: init repo
|
||||
raise NotImplementedError("Init git repo")
|
||||
|
||||
if not repoPath / ".git/annex":
|
||||
# TODO: git-annex init
|
||||
raise NotImplementedError("Annex verify")
|
||||
|
||||
def branchExists(self, branch: str) -> bool:
|
||||
"""Verifies the existence of a branch.
|
||||
|
||||
Args:
|
||||
branch: Name of branch to verify.
|
||||
|
||||
Returns:
|
||||
True iff branch `branch` exists.
|
||||
"""
|
||||
return branch in [b.name for b in self._repo.branches]
|
||||
|
||||
def treeIshExists(self, treeIsh: str) -> bool:
|
||||
"""Verifies the existence of a treeish.
|
||||
|
||||
Args:
|
||||
treeIsh: treeIsh structure to verify.
|
||||
|
||||
Returns:
|
||||
True iff treeish `treeIsh` exists.
|
||||
"""
|
||||
try:
|
||||
self._repo.tree(treeIsh)
|
||||
return True
|
||||
except git.exc.BadName:
|
||||
return False
|
|
@ -0,0 +1 @@
|
|||
from mia.archiveUtils import archive
|
|
@ -0,0 +1,128 @@
|
|||
""" Interact with the archive."""
|
||||
|
||||
from __future__ import (
|
||||
annotations,
|
||||
) # For forward references, should not be necessary in py 3.11
|
||||
|
||||
|
||||
import git
|
||||
from git.repo import Repo
|
||||
import logging
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
from utils import moduleProperty
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
|
||||
_arch: Optional[Archive] = None
|
||||
|
||||
|
||||
def init(archivePath: Path) -> None:
|
||||
"""Creates an instance of the archive."""
|
||||
global _arch
|
||||
|
||||
if _arch is not None:
|
||||
return
|
||||
|
||||
_arch = Archive(archivePath)
|
||||
|
||||
|
||||
@moduleProperty
|
||||
def _archive() -> Archive:
|
||||
"""Getter for the archive instance."""
|
||||
if _arch is None:
|
||||
raise Exception("Archive not Initialized")
|
||||
|
||||
return _arch
|
||||
|
||||
|
||||
@moduleProperty
|
||||
def _initialized() -> bool:
|
||||
"""Indicates if a archive instance has been initialized."""
|
||||
return _arch is not None
|
||||
|
||||
|
||||
class Archive:
|
||||
"""Represents an archive.
|
||||
|
||||
Wrapper for git, git-annex and file system related operations on the archive.
|
||||
"""
|
||||
|
||||
def __init__(self, repoPath: Path) -> None:
|
||||
self._root = repoPath
|
||||
|
||||
if not repoPath.is_dir():
|
||||
# TODO: create dir if not existing
|
||||
raise NotImplementedError("Create repo dir")
|
||||
|
||||
try:
|
||||
self._repo = Repo(repoPath)
|
||||
_log.debug(f"Initialized archive object for repo {repoPath}")
|
||||
except git.InvalidGitRepositoryError:
|
||||
# TODO: init repo
|
||||
raise NotImplementedError("Init git repo")
|
||||
|
||||
if not repoPath / ".git/annex":
|
||||
# TODO: git-annex init
|
||||
raise NotImplementedError("Annex verify")
|
||||
|
||||
@property
|
||||
def root(self) -> Path:
|
||||
"""Get the archive root path."""
|
||||
return self._root
|
||||
|
||||
def branchExists(self, branch: str) -> bool:
|
||||
"""Verifies the existence of a branch.
|
||||
|
||||
Args:
|
||||
branch: Name of branch to verify.
|
||||
|
||||
Returns:
|
||||
True iff branch `branch` exists.
|
||||
"""
|
||||
return branch in [b.name for b in self._repo.branches]
|
||||
|
||||
def commitExistsOnBranch(self, commitSha: str, branch: str) -> bool:
|
||||
"""Verifies the existence of a commit on a branch.
|
||||
|
||||
Args:
|
||||
commitSha: Sha of the commit to verify.
|
||||
branch: Destination branch to check.
|
||||
|
||||
Returns:
|
||||
True iff commit `commitSha` exists on `branch`.
|
||||
"""
|
||||
out = self._runGit("branch", branch, "--contains", commitSha)
|
||||
return branch == out.strip("*").strip()
|
||||
|
||||
def treeIshExists(self, treeIsh: str) -> bool:
|
||||
"""Verifies the existence of a treeish.
|
||||
|
||||
Args:
|
||||
treeIsh: treeIsh structure to verify.
|
||||
|
||||
Returns:
|
||||
True iff treeish `treeIsh` exists.
|
||||
"""
|
||||
try:
|
||||
self._repo.tree(treeIsh)
|
||||
return True
|
||||
except git.exc.BadName:
|
||||
return False
|
||||
|
||||
def _runGit(self, *args: str) -> str:
|
||||
"""Run a given git command on the repository.
|
||||
|
||||
Args:
|
||||
*args: Arguments of the git command.
|
||||
|
||||
Returns:
|
||||
Result string or empty string if command fails.
|
||||
"""
|
||||
_log.debug(f"Running git command {args}")
|
||||
try:
|
||||
return str(self._repo.git.execute(("git", *args)))
|
||||
except git.GitCommandError as e:
|
||||
_log.debug(f"Running git failed: {e}")
|
||||
return ""
|
Loading…
Reference in New Issue