| |
| |
| ''' |
| Top level function library for centpkg |
| ''' |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import re |
| import warnings |
| |
| import git |
| from pyrpkg import Commands, rpkgError |
| from pyrpkg.utils import cached_property |
| |
| |
| from . import cli |
| from .lookaside import StreamLookasideCache, SIGLookasideCache, CLLookasideCache |
| |
| |
| _DEFAULT_VERSION = '9' |
| |
| |
| class DistGitDirectory(object): |
| |
| signame = None |
| centosversion = _DEFAULT_VERSION |
| projectname = None |
| releasename = None |
| distrobranch = False |
| sigbranch = False |
| repo = None |
| git_origin_substr = 'git@gitlab.com/redhat/centos-stream' |
| |
| def __init__(self, branchtext, repo_path=None): |
| if repo_path: |
| |
| self.repo = git.repo.Repo(repo_path) |
| rhelbranchre = r'rhel-(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<appstream>\d+))?' |
| sigtobranchre = r'c(?P<centosversion>\d+[s]?)-sig-(?P<signame>\w+)-?(?P<projectname>\w+)?-?(?P<releasename>\w+)?' |
| distrobranchre = r'c(?P<centosversion>\d+)-?(?P<projectname>\w+)?' |
| oldbranchre = r'(?P<signame>\w+)(?P<centosversion>\d)' |
| rhelmatch = re.search(rhelbranchre, branchtext) |
| sigmatch = re.match(sigtobranchre, branchtext) |
| distromatch = re.match(distrobranchre, branchtext) |
| oldbranchmatch = re.match(oldbranchre, branchtext) |
| if rhelmatch: |
| gd = rhelmatch.groupdict() |
| self.distrobranch = True |
| self.signame = 'centos' |
| self.centosversion = gd['major'] |
| elif sigmatch: |
| gd = sigmatch.groupdict() |
| self.sigbranch = True |
| self.signame = gd['signame'] |
| self.centosversion = gd['centosversion'] |
| |
| |
| |
| |
| if gd['projectname'] != 'common': |
| self.projectname = gd['projectname'] |
| if gd['releasename'] != 'common': |
| self.releasename = gd['releasename'] |
| elif distromatch: |
| gd = distromatch.groupdict() |
| self.distrobranch = True |
| self.signame = 'centos' |
| self.centosversion = gd['centosversion'] |
| |
| if gd['projectname'] != 'common': |
| self.projectname = gd['projectname'] |
| elif oldbranchmatch: |
| warnings.warn("This branch is deprecated and will be removed soon", |
| DeprecationWarning) |
| else: |
| if not self.is_fork(): |
| raise ValueError("Branchname: {0} is not valid".format(branchtext)) |
| else: |
| self.distrobranch = True |
| self.signame = 'centos' |
| self.projectname = self.get_origin().split('_')[-1].replace('.git', '') |
| |
| warnings.warn('Remote "origin" was detected as a fork, ignoring branch name checking') |
| |
| def get_origin(self): |
| if self.repo is None: |
| return '' |
| if 'origin' not in self.repo.remotes: |
| return '' |
| urls = [u for u in self.repo.remotes['origin'].urls] |
| if len(urls) == 0: |
| return '' |
| return urls[0] |
| |
| def is_fork(self): |
| """ |
| Check if origin remote repository is using a fork url. |
| |
| Returns |
| bool |
| A boolean flag indicating if origin remote url is using |
| a forked repository url. |
| """ |
| |
| if self.repo is None: |
| return False |
| return self.git_origin_substr not in self.get_origin() |
| |
| |
| @property |
| def target(self): |
| projectorcommon = self.projectname |
| releaseorcommon = self.releasename |
| |
| if self.distrobranch: |
| if self.centosversion not in ('6', '7'): |
| return 'c{}s-candidate'.format(self.centosversion) |
| else: |
| return '-'.join(filter(None, ['c'+self.centosversion, |
| projectorcommon])) |
| |
| if not releaseorcommon: |
| if not projectorcommon or projectorcommon == 'common': |
| projectorcommon = 'common' |
| else: |
| releaseorcommon = 'common' |
| |
| return '-'.join(filter(None, [self.signame+self.centosversion, |
| projectorcommon, releaseorcommon])) + '-el{0}'.format(self.centosversion) |
| |
| |
| class Commands(Commands): |
| ''' |
| For the pyrpkg commands with centpkg behavior |
| ''' |
| def __init__(self, *args, **kwargs): |
| ''' |
| Init the object and some configuration details. |
| ''' |
| super(Commands, self).__init__(*args, **kwargs) |
| |
| |
| self.source_entry_type = 'bsd' if self.lookasidehash != 'md5' else 'old' |
| self.branchre = 'c\d{1,}(s)?(tream)?|master' |
| |
| @property |
| def distgitdir(self): |
| return DistGitDirectory(self.branch_merge, repo_path=self.path) |
| |
| @cached_property |
| def lookasidecache(self): |
| if self.layout.sources_file_template == "sources": |
| return StreamLookasideCache(self.lookasidehash, |
| self.lookaside, |
| self.lookaside_cgi, |
| ) |
| else: |
| if self.distgitdir.sigbranch: |
| return SIGLookasideCache(self.lookasidehash, |
| self.lookaside, |
| self.lookaside_cgi, |
| self.repo_name, |
| self.branch_merge) |
| else: |
| return CLLookasideCache(self.lookasidehash, |
| self.lookaside, |
| self.lookaside_cgi, |
| self.repo_name, |
| self.branch_merge, |
| ) |
| |
| |
| def load_rpmdefines(self): |
| ''' |
| Populate rpmdefines based on branch data |
| ''' |
| |
| if not self.distgitdir.centosversion: |
| raise rpkgError('Could not get the OS version from the branch:{0}'.format(self.branch_merge)) |
| |
| self._distvar = self.distgitdir.centosversion |
| self._distval = self._distvar.replace('.', '_') |
| |
| self._disttag = 'el%s' % self._distval |
| self._rpmdefines = ['--define', '_sourcedir %s' % self.layout.sourcedir, |
| '--define', '_specdir %s' % self.layout.specdir, |
| '--define', '_builddir %s' % self.layout.builddir, |
| '--define', '_srcrpmdir %s' % self.layout.srcrpmdir, |
| '--define', '_rpmdir %s' % self.layout.rpmdir, |
| '--define', 'dist .%s' % self._disttag, |
| |
| '--define', '%s 1' % self._disttag, |
| |
| '--define', 'rhel %s' % self._distval.split('_')[0]] |
| self.log.debug("RPMDefines: %s" % self._rpmdefines) |
| |
| def construct_build_url(self, *args, **kwargs): |
| """Override build URL for CentOS/Fedora Koji build |
| |
| In CentOS/Fedora Koji, anonymous URL should have prefix "git+https://" |
| """ |
| url = super(Commands, self).construct_build_url(*args, **kwargs) |
| return 'git+{0}'.format(url) |
| |
| def load_target(self): |
| """ This sets the target attribute (used for mock and koji) """ |
| |
| self._target = self.distgitdir.target |