Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius committed May 30, 2022
1 parent 5d1505b commit edd29e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

[tool.poetry]
name = "renku"
version = "1.2.4.dev17+geb9386e8" # placeholder, see poetry-dynamic-versioning
version = "0.0.0" # placeholder, see poetry-dynamic-versioning
description = "Python SDK and CLI for the Renku platform."
license = "Apache License 2.0"
keywords = ["Renku", "CLI"]
Expand Down
54 changes: 37 additions & 17 deletions renku/infrastructure/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,17 +689,11 @@ def close(self) -> None:
Cleans up dangling processes.
"""
if self._repository:
self._repository.close()
if getattr(self, "_repository", None) is not None:
self._repository.close() # type:ignore
del self._repository
self._repository = None

def refresh_repository(self):
"""Refreshes the underlying repository."""
self.close()

self._repository = git.Repo(self.path, search_parent_directories=False)

@staticmethod
def get_global_user() -> "Actor":
"""Return the global git user."""
Expand Down Expand Up @@ -890,7 +884,7 @@ def __init__(self, parent: git.Repo, name: str, path: Union[Path, str], url: str
self._name: str = name
self._url: str = url
try:
self._repository: git.Repo = _create_repository(path, search_parent_directories=False)
self._repository: Optional[git.Repo] = _create_repository(path, search_parent_directories=False)
except errors.GitError:
# NOTE: Submodule directory doesn't exist yet, so, we ignore the error
pass
Expand All @@ -908,7 +902,10 @@ def __repr__(self) -> str:
return f"<Submodule {self.relative_path}>"

def __del__(self) -> None:
self._repository.close()
if getattr(self, "_repository", None) is not None:
self._repository.close() # type:ignore
del self._repository
self._repository = None

@property
def name(self) -> str:
Expand All @@ -930,8 +927,8 @@ class SubmoduleManager:
"""Manage submodules of a Repository."""

def __init__(self, repository: git.Repo):
self._repository = repository
self._submodule_cache: Dict[git.Submodule, Submodule] = {} # type: ignore
self._repository: Optional[git.Repo] = repository
self._submodule_cache: Dict[str, Submodule] = {} # type: ignore
try:
self.update()
except errors.GitError:
Expand All @@ -940,12 +937,18 @@ def __init__(self, repository: git.Repo):

def _get_submodule(self, submodule: git.Submodule) -> Submodule: # type: ignore
"""Get a submodule from local cache."""
if submodule not in self._submodule_cache:
if self._repository is None:
raise errors.ParameterError("Repository not set.")

if submodule.name not in self._submodule_cache:
submodule_result = Submodule.from_submodule(self._repository, submodule)
self._submodule_cache[submodule] = submodule_result
return self._submodule_cache[submodule]
self._submodule_cache[submodule.name] = submodule_result
return self._submodule_cache[submodule.name]

def __getitem__(self, name: str) -> Submodule:
if self._repository is None:
raise errors.ParameterError("Repository not set.")

try:
submodule = self._repository.submodules[name]
except IndexError:
Expand All @@ -954,28 +957,45 @@ def __getitem__(self, name: str) -> Submodule:
return self._get_submodule(submodule)

def __iter__(self):
if self._repository is None:
raise errors.ParameterError("Repository not set.")

for s in self._repository.submodules:

yield self._get_submodule(s)

def __len__(self) -> int:
if self._repository is None:
raise errors.ParameterError("Repository not set.")

return len(self._repository.submodules)

def __repr__(self) -> str:
return str(list(self))

def remove(self, submodule: Union[Submodule, str], force: bool = False):
"""Remove an existing submodule."""
if self._repository is None:
raise errors.ParameterError("Repository not set.")

name = submodule if isinstance(submodule, str) else submodule.name

try:
submodule = self._repository.submodules[name]
submodule.remove(force=force)
git_submodule = self._repository.submodules[name]
git_submodule.remove(force=force)

if name in self._submodule_cache:
submodule = self._submodule_cache[name]
del self._submodule_cache[name]
submodule.close()
except git.GitError as e:
raise errors.GitError(f"Cannot delete submodule '{submodule}'") from e

def update(self, initialize: bool = True):
"""Update all submodule."""
if self._repository is None:
raise errors.ParameterError("Repository not set.")

# NOTE: Git complains if ``--init`` comes before ``update``
args = ("update", "--init") if initialize else ("update",)
_run_git_command(self._repository, "submodule", *args)
Expand Down
2 changes: 1 addition & 1 deletion renku/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
except ImportError:
from importlib_metadata import distribution

__version__ = "1.2.4.dev17+geb9386e8"
__version__ = "0.0.0"
__template_version__ = "0.3.1"
__minimum_project_version__ = "1.2.0"

Expand Down

0 comments on commit edd29e2

Please sign in to comment.