repos: introduce a factory to abstract away from Repo implementation

The user just calls that factory and gets something that is a Repo, in
fact just a GitRepo for now.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
This commit is contained in:
Henning Schild 2018-01-05 16:00:26 +01:00 committed by Daniel Wagner
parent b5b2766b24
commit 3594ad50a0
2 changed files with 34 additions and 25 deletions

View File

@ -199,16 +199,12 @@ class Config:
name)
url = path
rep = Repo(url=url,
path=path,
layers=layers)
rep = Repo.factory(url=url, path=path, layers=layers)
rep.disable_operations()
else:
path = path or os.path.join(self.kas_work_dir, name)
rep = Repo(url=url,
path=path,
refspec=refspec,
layers=layers)
rep = Repo.factory(url=url, path=path, refspec=refspec,
layers=layers)
repo_dict[repo] = rep
return repo_dict

View File

@ -38,7 +38,7 @@ class Repo:
Represents a repository in the kas configuration.
"""
def __init__(self, url, path, refspec=None, layers=None):
def __init__(self, url, path, refspec, layers):
self.url = url
self.path = path
self.refspec = refspec
@ -73,6 +73,36 @@ class Repo:
return '%s:%s %s %s' % (self.url, self.refspec,
self.path, self._layers)
@staticmethod
def factory(url, path, refspec=None, layers=None):
"""
Return an instance Repo depending on params.
"""
return GitRepo(url, path, refspec, layers)
@staticmethod
def get_root_path(path, environ):
"""
Check if path is a version control repo and return its root path.
"""
(ret, output) = run_cmd(['git',
'rev-parse',
'--show-toplevel'],
cwd=path,
env=environ,
fail=False,
liveupdate=False)
if ret == 0:
return output.strip()
return path
class GitRepo(Repo):
"""
Provides the git implementations for a Repo.
"""
@asyncio.coroutine
def fetch_async(self, config):
"""
@ -155,20 +185,3 @@ class Repo:
run_cmd(['git', 'checkout', '-q',
'{refspec}'.format(refspec=self.refspec)],
cwd=self.path)
@staticmethod
def get_root_path(path, environ):
"""
Check if path is a version control repo and return its root path.
"""
(ret, output) = run_cmd(['git',
'rev-parse',
'--show-toplevel'],
cwd=path,
env=environ,
fail=False,
liveupdate=False)
if ret == 0:
return output.strip()
return path