dfan / centos / centpkg

Forked from centos/centpkg 2 years ago
Clone
Blob Blame History Raw
#pylint: disable=line-too-long,abstract-class-not-used
'''
    Top level function library for centpkg
'''
#
# Author(s):
#            Jesse Keating <jkeating@redhat.com>
#            Pat Riehecky <riehecky@fnal.gov>
#            Brian Stinson <bstinson@ksu.edu>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.



import os
import re
import sys
import urlparse
import warnings

from pyrpkg import Commands, rpkgError
from pyrpkg.layout import ExplodedSRPMLayout, DistGitLayout
from centos import centos_cert
from .lookaside import CentOSLookasideCache
from pyrpkg.utils import cached_property
from . import cli


class DistGitDirectory(object):

    signame = None
    centosversion = None
    projectname = None
    releasename = None
    distrobranch = False

    def __init__(self, branchtext):
        sigtobranchre = r'c(?P<centosversion>\d+)-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)'
        sigmatch = re.match(sigtobranchre, branchtext)
        distromatch = re.match(distrobranchre, branchtext)
        oldbranchmatch = re.match(oldbranchre, branchtext)
        if sigmatch:
            gd = sigmatch.groupdict()
            self.signame = gd['signame']
            self.centosversion = gd['centosversion']

            # Users have the option to specify (or not specify) common in their
            # git repos. Ww need to handle these cases because common is not a
            # project nor is it a release.
            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:
            raise ValueError("Branchname: {0} is not valid".format(branchtext))

    @property
    def target(self):
        projectorcommon = self.projectname
        releaseorcommon = self.releasename

        if self.distrobranch:
            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 = 'old'

    @property
    def distgitdir(self):
        return DistGitDirectory(self.branch_merge)

    @cached_property
    def lookasidecache(self):
        return CentOSLookasideCache(self.lookasidehash,
                                    self.lookaside,
                                    self.lookaside_cgi,
                                    self.repo_name,
                                    self.branch_merge)

    # redefined loaders
    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,
                            # int and float this to remove the decimal
                            "--define '%s 1'" % self._disttag]
        self.log.debug("RPMDefines: %s" % self._rpmdefines)

    def load_target(self):
        """ This sets the target attribute (used for mock and koji) """

        self._target = self.distgitdir.target

    def load_user(self):
        try:
            self._user = centos_cert.CentOSUserCert().CN
        except Exception:
            print >>sys.stderr, "Could not load user from cert file"
            super(Commands, self).load_user()

    # These are the commands defined in the base pyrpkg.Commands class
    # and have been implemented here

    def upload(self, *args, **kwargs):
        if not self.distgitdir.distrobranch:
            self.source_entry_type = 'bsd'
        return super(Commands, self).upload(*args, **kwargs)

    def import_srpm(self, *args, **kwargs):
        raise NotImplementedError("import_srpm is not yet implemented in centpkg")

    def patch(self, *args, **kwargs):
        raise NotImplementedError("patch is not yet implemented in centpkg")

    def install(self, *args, **kwargs):
        raise NotImplementedError("install is not yet implemented in centpkg")

    def lint(self, *args, **kwargs):
        raise NotImplementedError("lint is not yet implemented in centpkg")