Cloner: clone() recursion support, TODO: recursion tests

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
2022-07-31 02:08:27 +02:00
parent ae34a5e95f
commit d41ead74db
3 changed files with 49 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ from repo_cloner.lib.dir_not_found_error import DirNotFoundError
from repo_cloner.lib.repo_tool import RepoTool
from repo_cloner.lib.checksum import gen_repo_hashed_name
from pathlib import Path
from typing import Optional
from time import time
import os
import logging
@@ -15,6 +16,7 @@ class Cloner:
_dirs: RepoDirStructure = None
_config: ClonerConfig = None
_interval_file: str = "last-check-time"
__submodule_cache: str = None
_repo: RepoTool = None
_repo_url: str = ""
@@ -32,6 +34,12 @@ class Cloner:
log.info(f"Cache dir for project {self._config.cloner_project_name} not found -> creating")
Path(self._dirs.cache_dir).mkdir()
log.debug(f"Cache dir created")
# submodule cache
self.__submodule_cache = os.path.join(self._dirs.cache_dir, "submodules")
if not os.path.exists(self.__submodule_cache):
log.info("Submodule cache dir does not exist! -> creating")
Path(self.__submodule_cache).mkdir(parents = True)
def check_interval(self):
log.debug(f"Checking interval for {self._config.cloner_project_name}")
@@ -87,11 +95,26 @@ class Cloner:
self.sync()
log.info(f"Check finished")
def clone_from_url(self, url: str) -> bool:
def clone(self, url: Optional[str] = None) -> bool:
# optional parameters - othervise use config
if not url:
url = self._config.cloner_repo_url
# generate path
path = self._repo_path_by_url(url)
self._repo_url = url
self._repo = RepoTool(path)
# uninitialized repo
if self._repo.initialized:
log.critical(f"Repo path {path} is initialized... Refusing clone!")
return False
return self._repo.clone(url)
# recursive or standard?
if not self._config.cloner_submodules:
return self._repo.clone(url)
else:
scan_depth_limit = self._config.cloner_submodule_depth
# handle dept limit for submodule discovery
if scan_depth_limit == 0:
scan_depth_limit = None
# another levels are handled internally as non-recursive clones and discovers by repo-tool
return self._repo.clone_recursive(url, self.__submodule_cache, scan_depth = scan_depth_limit)