2017-06-14 13:36:37 +02:00
|
|
|
# kas - setup tool for bitbake based projects
|
|
|
|
#
|
|
|
|
# Copyright (c) Siemens AG, 2017
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
This module contains the Repo class.
|
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
import os
|
2018-01-05 16:00:24 +01:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2017-06-14 13:36:37 +02:00
|
|
|
from urllib.parse import urlparse
|
2018-08-24 19:18:02 +02:00
|
|
|
from .context import get_context
|
2018-01-05 16:00:24 +01:00
|
|
|
from .libkas import run_cmd_async, run_cmd
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
__license__ = 'MIT'
|
|
|
|
__copyright__ = 'Copyright (c) Siemens AG, 2017'
|
|
|
|
|
|
|
|
|
|
|
|
class Repo:
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Represents a repository in the kas configuration.
|
|
|
|
"""
|
|
|
|
|
2018-03-09 08:22:52 +01:00
|
|
|
def __init__(self, url, path, refspec, layers, patches,
|
|
|
|
disable_operations):
|
2018-01-05 16:00:27 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2017-06-14 13:36:37 +02:00
|
|
|
self.url = url
|
|
|
|
self.path = path
|
|
|
|
self.refspec = refspec
|
2017-06-21 13:32:57 +02:00
|
|
|
self._layers = layers
|
2018-03-09 08:22:52 +01:00
|
|
|
self._patches = patches
|
2017-06-14 13:36:37 +02:00
|
|
|
self.name = os.path.basename(self.path)
|
2018-01-05 16:00:27 +01:00
|
|
|
self.operations_disabled = disable_operations
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
def __getattr__(self, item):
|
2018-07-18 14:50:19 +02:00
|
|
|
# pylint: disable=no-else-return
|
2017-06-14 13:36:37 +02:00
|
|
|
if item == 'layers':
|
2017-06-21 13:32:57 +02:00
|
|
|
if not self._layers:
|
2017-06-14 13:36:37 +02:00
|
|
|
return [self.path]
|
2017-06-28 14:48:40 +02:00
|
|
|
return [self.path + '/' + l for l in self._layers]
|
2017-06-14 13:36:37 +02:00
|
|
|
elif item == 'qualified_name':
|
|
|
|
url = urlparse(self.url)
|
2017-06-21 13:32:56 +02:00
|
|
|
return ('{url.netloc}{url.path}'
|
|
|
|
.format(url=url)
|
|
|
|
.replace('@', '.')
|
|
|
|
.replace(':', '.')
|
|
|
|
.replace('/', '.')
|
|
|
|
.replace('*', '.'))
|
2018-07-18 14:50:19 +02:00
|
|
|
# Default behaviour
|
|
|
|
raise AttributeError
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-06-21 13:32:57 +02:00
|
|
|
return '%s:%s %s %s' % (self.url, self.refspec,
|
|
|
|
self.path, self._layers)
|
2018-01-05 16:00:24 +01:00
|
|
|
|
2018-01-05 16:00:26 +01:00
|
|
|
@staticmethod
|
2018-08-24 19:18:02 +02:00
|
|
|
def factory(name, repo_config, repo_fallback_path):
|
2018-01-05 16:00:26 +01:00
|
|
|
"""
|
|
|
|
Return an instance Repo depending on params.
|
|
|
|
"""
|
2018-01-05 16:00:29 +01:00
|
|
|
layers_dict = repo_config.get('layers', {})
|
|
|
|
layers = list(filter(lambda x, laydict=layers_dict:
|
|
|
|
str(laydict[x]).lower() not in
|
|
|
|
['disabled', 'excluded', 'n', 'no', '0', 'false'],
|
|
|
|
layers_dict))
|
2018-03-09 08:22:52 +01:00
|
|
|
patches_dict = repo_config.get('patches', {})
|
|
|
|
patches = list(
|
|
|
|
{
|
|
|
|
'id': p,
|
|
|
|
'repo': patches_dict[p]['repo'],
|
|
|
|
'path': patches_dict[p]['path'],
|
|
|
|
}
|
|
|
|
for p in sorted(patches_dict)
|
|
|
|
if patches_dict[p])
|
2018-01-05 16:00:29 +01:00
|
|
|
url = repo_config.get('url', None)
|
|
|
|
name = repo_config.get('name', name)
|
|
|
|
typ = repo_config.get('type', 'git')
|
|
|
|
refspec = repo_config.get('refspec', None)
|
|
|
|
path = repo_config.get('path', None)
|
|
|
|
disable_operations = False
|
|
|
|
|
|
|
|
if url is None:
|
2018-01-05 16:00:30 +01:00
|
|
|
# No version control operation on repository
|
2018-01-05 16:00:29 +01:00
|
|
|
if path is None:
|
2018-08-24 19:18:02 +02:00
|
|
|
path = Repo.get_root_path(repo_fallback_path)
|
2018-01-05 16:00:29 +01:00
|
|
|
logging.info('Using %s as root for repository %s', path,
|
|
|
|
name)
|
|
|
|
|
|
|
|
url = path
|
|
|
|
disable_operations = True
|
|
|
|
else:
|
2018-01-25 11:12:58 +01:00
|
|
|
if path is None:
|
2018-08-24 19:18:02 +02:00
|
|
|
path = os.path.join(get_context().kas_work_dir, name)
|
2018-01-25 11:12:58 +01:00
|
|
|
else:
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
# Relative pathes are assumed to start from work_dir
|
2018-08-24 19:18:02 +02:00
|
|
|
path = os.path.join(get_context().kas_work_dir, path)
|
2018-01-05 16:00:29 +01:00
|
|
|
|
2018-01-05 16:00:28 +01:00
|
|
|
if typ == 'git':
|
2018-03-09 08:22:52 +01:00
|
|
|
return GitRepo(url, path, refspec, layers, patches,
|
|
|
|
disable_operations)
|
2018-01-05 16:00:31 +01:00
|
|
|
if typ == 'hg':
|
2018-03-09 08:22:52 +01:00
|
|
|
return MercurialRepo(url, path, refspec, layers, patches,
|
2018-01-05 16:00:31 +01:00
|
|
|
disable_operations)
|
2018-01-05 16:00:28 +01:00
|
|
|
raise NotImplementedError('Repo typ "%s" not supported.' % typ)
|
2018-01-05 16:00:26 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2018-08-24 19:18:01 +02:00
|
|
|
def get_root_path(path):
|
2018-01-05 16:00:26 +01:00
|
|
|
"""
|
|
|
|
Check if path is a version control repo and return its root path.
|
|
|
|
"""
|
2018-01-05 16:00:31 +01:00
|
|
|
(ret, output) = run_cmd(['git', 'rev-parse', '--show-toplevel'],
|
2018-08-24 19:18:01 +02:00
|
|
|
cwd=path, fail=False, liveupdate=False)
|
2018-01-05 16:00:31 +01:00
|
|
|
if ret == 0:
|
|
|
|
return output.strip()
|
|
|
|
|
|
|
|
(ret, output) = run_cmd(['hg', 'root'],
|
2018-08-24 19:18:01 +02:00
|
|
|
cwd=path, fail=False, liveupdate=False)
|
2018-01-05 16:00:26 +01:00
|
|
|
if ret == 0:
|
|
|
|
return output.strip()
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2018-01-05 16:00:30 +01:00
|
|
|
class RepoImpl(Repo):
|
2018-01-05 16:00:26 +01:00
|
|
|
"""
|
2018-01-05 16:00:30 +01:00
|
|
|
Provides a generic implementation for a Repo.
|
2018-01-05 16:00:26 +01:00
|
|
|
"""
|
|
|
|
|
2018-01-05 16:00:24 +01:00
|
|
|
@asyncio.coroutine
|
2018-08-13 14:11:22 +02:00
|
|
|
def fetch_async(self, ctx):
|
2018-01-05 16:00:24 +01:00
|
|
|
"""
|
|
|
|
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)
|
2018-08-13 14:11:22 +02:00
|
|
|
sdir = os.path.join(ctx.kas_repo_ref_dir or '',
|
2018-01-05 16:00:30 +01:00
|
|
|
self.qualified_name)
|
|
|
|
logging.debug('Looking for repo ref dir in %s', sdir)
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
(retc, _) = yield from run_cmd_async(self.clone_cmd(sdir, ctx),
|
|
|
|
cwd=ctx.kas_work_dir)
|
2018-01-05 16:00:24 +01:00
|
|
|
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?
|
2018-01-05 16:00:30 +01:00
|
|
|
(retc, output) = yield from run_cmd_async(self.contains_refspec_cmd(),
|
2018-01-05 16:00:24 +01:00
|
|
|
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
|
2018-01-05 16:00:30 +01:00
|
|
|
(retc, output) = yield from run_cmd_async(self.fetch_cmd(),
|
2018-01-05 16:00:24 +01:00
|
|
|
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
|
|
|
|
|
2018-08-24 19:18:00 +02:00
|
|
|
def checkout(self, _):
|
2018-01-05 16:00:24 +01:00
|
|
|
"""
|
|
|
|
Checks out the correct revision of the repo.
|
|
|
|
"""
|
|
|
|
if self.operations_disabled or self.refspec is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Check if repos is dirty
|
2018-01-05 16:00:30 +01:00
|
|
|
(_, output) = run_cmd(self.is_dirty_cmd(),
|
2018-08-24 19:18:00 +02:00
|
|
|
cwd=self.path,
|
2018-01-05 16:00:24 +01:00
|
|
|
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.
|
2018-01-05 16:00:30 +01:00
|
|
|
(_, output) = run_cmd(self.current_rev_cmd(),
|
2018-08-24 19:18:00 +02:00
|
|
|
cwd=self.path)
|
2018-01-05 16:00:24 +01:00
|
|
|
|
|
|
|
if output.strip() == self.refspec:
|
|
|
|
logging.info('Repo %s has already checkout out correct '
|
|
|
|
'refspec. nothing to do', self.name)
|
|
|
|
return
|
|
|
|
|
2018-01-05 16:00:30 +01:00
|
|
|
run_cmd(self.checkout_cmd(), cwd=self.path)
|
|
|
|
|
2018-03-09 08:22:52 +01:00
|
|
|
@asyncio.coroutine
|
2018-08-13 14:11:22 +02:00
|
|
|
def apply_patches_async(self, ctx):
|
2018-03-09 08:22:52 +01:00
|
|
|
"""
|
|
|
|
Applies patches to repository asynchronously.
|
|
|
|
"""
|
|
|
|
if self.operations_disabled:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
for patch in self._patches:
|
2018-08-13 14:11:22 +02:00
|
|
|
other_repo = ctx.config.repo_dict.get(patch['repo'], None)
|
2018-03-09 08:22:52 +01:00
|
|
|
|
|
|
|
if not other_repo:
|
|
|
|
logging.warning('Could not find referenced repo. '
|
|
|
|
'(missing repo: %s, repo: %s, '
|
|
|
|
'patch entry: %s)',
|
|
|
|
patch['repo'],
|
|
|
|
self.name,
|
|
|
|
patch['id'])
|
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(other_repo.path, patch['path'])
|
|
|
|
cmd = []
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
cmd = self.apply_patches_file_cmd(path)
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
cmd = self.apply_patches_quilt_cmd(path)
|
|
|
|
else:
|
|
|
|
logging.warning('Could not find patch. '
|
|
|
|
'(patch path: %s, repo: %s, patch entry: %s)',
|
|
|
|
path,
|
|
|
|
self.name,
|
|
|
|
patch['id'])
|
|
|
|
continue
|
|
|
|
|
|
|
|
(retc, output) = yield from run_cmd_async(cmd,
|
|
|
|
cwd=self.path,
|
|
|
|
fail=False)
|
2018-08-13 14:11:22 +02:00
|
|
|
# pylint: disable=no-else-return
|
2018-03-09 08:22:52 +01:00
|
|
|
if retc:
|
|
|
|
logging.error('Could not apply patch. Please fix repos and '
|
|
|
|
'patches. (patch path: %s, repo: %s, patch '
|
|
|
|
'entry: %s, vcs output: %s)',
|
|
|
|
path, self.name, patch['id'], output)
|
|
|
|
return 1
|
2018-08-13 14:11:22 +02:00
|
|
|
else:
|
|
|
|
logging.info('Patch applied. '
|
|
|
|
'(patch path: %s, repo: %s, patch entry: %s)',
|
|
|
|
path, self.name, patch['id'])
|
2018-03-09 08:22:52 +01:00
|
|
|
return 0
|
|
|
|
|
2018-01-05 16:00:30 +01:00
|
|
|
|
|
|
|
class GitRepo(RepoImpl):
|
|
|
|
"""
|
|
|
|
Provides the git implementations for a Repo.
|
|
|
|
"""
|
|
|
|
# pylint: disable=no-self-use,missing-docstring
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def clone_cmd(self, gitsrcdir, ctx):
|
2018-01-05 16:00:30 +01:00
|
|
|
cmd = ['git', 'clone', '-q', self.url, self.path]
|
2018-08-13 14:11:22 +02:00
|
|
|
if ctx.kas_repo_ref_dir and os.path.exists(gitsrcdir):
|
2018-01-05 16:00:30 +01:00
|
|
|
cmd.extend(['--reference', gitsrcdir])
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
def contains_refspec_cmd(self):
|
|
|
|
return ['git', 'cat-file', '-t', self.refspec]
|
|
|
|
|
|
|
|
def fetch_cmd(self):
|
|
|
|
return ['git', 'fetch', '--all']
|
|
|
|
|
|
|
|
def is_dirty_cmd(self):
|
2018-08-24 16:11:20 +02:00
|
|
|
return ['git', 'status', '-s']
|
2018-01-05 16:00:30 +01:00
|
|
|
|
|
|
|
def current_rev_cmd(self):
|
|
|
|
return ['git', 'rev-parse', '--verify', 'HEAD']
|
|
|
|
|
|
|
|
def checkout_cmd(self):
|
|
|
|
return ['git', 'checkout', '-q',
|
|
|
|
'{refspec}'.format(refspec=self.refspec)]
|
2018-01-05 16:00:31 +01:00
|
|
|
|
2018-03-09 08:22:52 +01:00
|
|
|
def apply_patches_file_cmd(self, path):
|
|
|
|
return ['git', 'am', '-q', path]
|
|
|
|
|
|
|
|
def apply_patches_quilt_cmd(self, path):
|
|
|
|
return ['git', 'quiltimport', '--author', 'kas <kas@example.com>',
|
|
|
|
'--patches', path]
|
|
|
|
|
2018-01-05 16:00:31 +01:00
|
|
|
|
|
|
|
class MercurialRepo(RepoImpl):
|
|
|
|
"""
|
|
|
|
Provides the hg implementations for a Repo.
|
|
|
|
"""
|
|
|
|
# pylint: disable=no-self-use,missing-docstring,unused-argument
|
|
|
|
|
|
|
|
def clone_cmd(self, gitsrcdir, config):
|
|
|
|
return ['hg', 'clone', self.url, self.path]
|
|
|
|
|
|
|
|
def contains_refspec_cmd(self):
|
|
|
|
return ['hg', 'log', '-r', self.refspec]
|
|
|
|
|
|
|
|
def fetch_cmd(self):
|
|
|
|
return ['hg', 'pull']
|
|
|
|
|
|
|
|
def is_dirty_cmd(self):
|
|
|
|
return ['hg', 'diff']
|
|
|
|
|
|
|
|
def current_rev_cmd(self):
|
|
|
|
return ['hg', 'identify', '-i', '--debug']
|
|
|
|
|
|
|
|
def checkout_cmd(self):
|
|
|
|
return ['hg', 'checkout', '{refspec}'.format(refspec=self.refspec)]
|
2018-03-09 08:22:52 +01:00
|
|
|
|
|
|
|
def apply_patches_file_cmd(self, path):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def apply_patches_quilt_cmd(self, path):
|
|
|
|
raise NotImplementedError()
|