From 941fa38a2d37ffaad0fa49f8de0298a6b3573cbf Mon Sep 17 00:00:00 2001 From: Henning Schild Date: Fri, 5 Jan 2018 16:00:24 +0100 Subject: [PATCH] repos: move repository functions into repos.py This commit just moves core around and does not contain any logical changes. Signed-off-by: Henning Schild --- kas/config.py | 4 +-- kas/libcmds.py | 4 +-- kas/libkas.py | 89 ++------------------------------------------------ kas/repos.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 91 deletions(-) diff --git a/kas/config.py b/kas/config.py index e27a2e4..b8b3cd3 100644 --- a/kas/config.py +++ b/kas/config.py @@ -49,7 +49,7 @@ except ImportError: return platform.dist()[0] from .repos import Repo -from .libkas import run_cmd, repos_fetch, repo_checkout +from .libkas import run_cmd, repos_fetch __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' @@ -91,7 +91,7 @@ class Config: repos_fetch(self, missing_repos) for repo in missing_repos: - repo_checkout(self, repo) + repo.checkout(self) repo_paths = {r: repo_dict[r].path for r in repo_dict} diff --git a/kas/libcmds.py b/kas/libcmds.py index 07e0e56..76443f9 100644 --- a/kas/libcmds.py +++ b/kas/libcmds.py @@ -28,7 +28,7 @@ import logging import shutil import os from .libkas import (ssh_cleanup_agent, ssh_setup_agent, ssh_no_host_key_check, - get_build_environ, repos_fetch, repo_checkout) + get_build_environ, repos_fetch) __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' @@ -214,4 +214,4 @@ class ReposCheckout(Command): def execute(self, config): for repo in config.get_repos(): - repo_checkout(config, repo) + repo.checkout(config) diff --git a/kas/libkas.py b/kas/libkas.py index 910d269..7329fa2 100644 --- a/kas/libkas.py +++ b/kas/libkas.py @@ -153,61 +153,6 @@ def find_program(paths, name): return None -@asyncio.coroutine -def _repo_fetch_async(config, repo): - """ - Start asynchronous repository fetch. - """ - if repo.operations_disabled: - return 0 - - if not os.path.exists(repo.path): - os.makedirs(os.path.dirname(repo.path), exist_ok=True) - gitsrcdir = os.path.join(config.get_repo_ref_dir() or '', - repo.qualified_name) - logging.debug('Looking for repo ref dir in %s', gitsrcdir) - - cmd = ['git', 'clone', '-q', repo.url, repo.path] - if config.get_repo_ref_dir() and os.path.exists(gitsrcdir): - cmd.extend(['--reference', gitsrcdir]) - (retc, _) = yield from run_cmd_async(cmd, - env=config.environ, - cwd=config.kas_work_dir) - if retc == 0: - logging.info('Repository %s cloned', repo.name) - return retc - - # take what came out of clone and stick to that forever - if repo.refspec is None: - return 0 - - # Does refspec exist in the current repository? - (retc, output) = yield from run_cmd_async(['git', - 'cat-file', '-t', - repo.refspec], - env=config.environ, - cwd=repo.path, - fail=False, - liveupdate=False) - if retc == 0: - logging.info('Repository %s already contains %s as %s', - repo.name, repo.refspec, output.strip()) - return retc - - # No it is missing, try to fetch - (retc, output) = yield from run_cmd_async(['git', - 'fetch', '--all'], - env=config.environ, - cwd=repo.path, - fail=False) - if retc: - logging.warning('Could not update repository %s: %s', - repo.name, output) - else: - logging.info('Repository %s updated', repo.name) - return 0 - - def repos_fetch(config, repos): """ Fetches the list of repositories to the kas_work_dir. @@ -216,9 +161,9 @@ def repos_fetch(config, repos): for repo in repos: if not hasattr(asyncio, 'ensure_future'): # pylint: disable=no-member,deprecated-method - task = asyncio.async(_repo_fetch_async(config, repo)) + task = asyncio.async(repo.fetch_async(config)) else: - task = asyncio.ensure_future(_repo_fetch_async(config, repo)) + task = asyncio.ensure_future(repo.fetch_async(config)) tasks.append(task) loop = asyncio.get_event_loop() @@ -229,36 +174,6 @@ def repos_fetch(config, repos): sys.exit(task.result()) -def repo_checkout(config, repo): - """ - Checks out the correct revision of the repo. - """ - if repo.operations_disabled or repo.refspec is None: - return - - # Check if repos is dirty - (_, output) = run_cmd(['git', 'diff', '--shortstat'], - env=config.environ, cwd=repo.path, - fail=False) - if output: - logging.warning('Repo %s is dirty. no checkout', repo.name) - return - - # Check if current HEAD is what in the config file is defined. - (_, output) = run_cmd(['git', 'rev-parse', - '--verify', 'HEAD'], - env=config.environ, cwd=repo.path) - - if output.strip() == repo.refspec: - logging.info('Repo %s has already checkout out correct ' - 'refspec. nothing to do', repo.name) - return - - run_cmd(['git', 'checkout', '-q', - '{refspec}'.format(refspec=repo.refspec)], - cwd=repo.path) - - def get_build_environ(config, build_dir): """ Create the build environment variables. diff --git a/kas/repos.py b/kas/repos.py index 75bc413..0b5fb53 100644 --- a/kas/repos.py +++ b/kas/repos.py @@ -24,7 +24,10 @@ """ import os +import asyncio +import logging from urllib.parse import urlparse +from .libkas import run_cmd_async, run_cmd __license__ = 'MIT' __copyright__ = 'Copyright (c) Siemens AG, 2017' @@ -69,3 +72,86 @@ class Repo: def __str__(self): return '%s:%s %s %s' % (self.url, self.refspec, self.path, self._layers) + + @asyncio.coroutine + def fetch_async(self, config): + """ + Start asynchronous repository fetch. + """ + if self.operations_disabled: + return 0 + + if not os.path.exists(self.path): + os.makedirs(os.path.dirname(self.path), exist_ok=True) + gitsrcdir = os.path.join(config.get_repo_ref_dir() or '', + self.qualified_name) + logging.debug('Looking for repo ref dir in %s', gitsrcdir) + + cmd = ['git', 'clone', '-q', self.url, self.path] + if config.get_repo_ref_dir() and os.path.exists(gitsrcdir): + cmd.extend(['--reference', gitsrcdir]) + (retc, _) = yield from run_cmd_async(cmd, + env=config.environ, + cwd=config.kas_work_dir) + if retc == 0: + logging.info('Repository %s cloned', self.name) + return retc + + # take what came out of clone and stick to that forever + if self.refspec is None: + return 0 + + # Does refspec exist in the current repository? + (retc, output) = yield from run_cmd_async(['git', + 'cat-file', '-t', + self.refspec], + env=config.environ, + cwd=self.path, + fail=False, + liveupdate=False) + if retc == 0: + logging.info('Repository %s already contains %s as %s', + self.name, self.refspec, output.strip()) + return retc + + # No it is missing, try to fetch + (retc, output) = yield from run_cmd_async(['git', + 'fetch', '--all'], + env=config.environ, + cwd=self.path, + fail=False) + if retc: + logging.warning('Could not update repository %s: %s', + self.name, output) + else: + logging.info('Repository %s updated', self.name) + return 0 + + def checkout(self, config): + """ + Checks out the correct revision of the repo. + """ + if self.operations_disabled or self.refspec is None: + return + + # Check if repos is dirty + (_, output) = run_cmd(['git', 'diff', '--shortstat'], + env=config.environ, cwd=self.path, + fail=False) + if output: + logging.warning('Repo %s is dirty. no checkout', self.name) + return + + # Check if current HEAD is what in the config file is defined. + (_, output) = run_cmd(['git', 'rev-parse', + '--verify', 'HEAD'], + env=config.environ, cwd=self.path) + + if output.strip() == self.refspec: + logging.info('Repo %s has already checkout out correct ' + 'refspec. nothing to do', self.name) + return + + run_cmd(['git', 'checkout', '-q', + '{refspec}'.format(refspec=self.refspec)], + cwd=self.path)