Blame tests/z_rpminfo/check-rpminfo.py

0b568f
#!/usr/bin/python -tt
0b568f
'''
0b568f
Author: Brian Stinson <brian@bstinson.com>
0b568f
Check rpminfo attributes that must be set before release in the repositories (e.g. Vendor, Packager)
0b568f
'''
0b568f
0b568f
from __future__ import print_function, with_statement
0b568f
import os
0b568f
import sys
0b568f
import yum
0b568f
0b568f
from datetime import datetime
0b568f
from fnmatch import fnmatch
0b568f
0b568f
0b568f
def log(methodstring):
0b568f
    localtime = datetime.now()
0b568f
    print("[+] {0} -> {1}".format(localtime.strftime("%a %b %e %H:%M:%S %Z %Y"), methodstring))
0b568f
0b568f
0b568f
def fail(failstring):
0b568f
    log('FAIL {0}'.format(failstring))
0b568f
0b568f
0b568f
def skip(skipstring):
0b568f
    log('SKIP {0}'.format(skipstring))
0b568f
0b568f
log('Running check-rpminfo.py - Check rpminfo Attributes')
0b568f
0b568f
# Set the required attributes and their values here. You may use shell-style
0b568f
# globs in the value if necessary.
0b568f
required_attrs = {
0b568f
    'vendor': 'CentOS',
0b568f
    'packager': '*centos.org*',
0b568f
    }
0b568f
0b568f
# special_overrides is a dictionary of packages and a list of attributes
0b568f
# to ignore for that package. When updating this list add a comment to
0b568f
# the line describing why the override is in place
0b568f
special_overrides = {
0b568f
    'epel-release': ['vendor', 'packager'],                   #Rebuilt directly from Fedora, so the vendor remains Fedora Project
73ff35
    'elrepo-release': ['vendor', 'packager'],                 #Imported from elrepo, so the vendor remains "The ELRepo Project"
0b568f
    'redhat-support-lib-python': ['vendor'],
0b568f
    'redhat-support-tool': ['vendor'],
0b568f
    }
0b568f
0b568f
# you can also add the NVR, and the attribute to ignore to the special_overrides.txt file
0b568f
with open(os.path.join(sys.path[0],'special_overrides.txt'),'r') as thefile:
0b568f
    for line in thefile.readlines():
0b568f
        if not line.strip() or line.startswith('#'):
0b568f
            continue
0b568f
        pkg, ignoreattr = map(str.strip,line.split(':'))
0b568f
        special_overrides.setdefault(pkg, []).append(ignoreattr)
0b568f
0b568f
yb = yum.YumBase()
0b568f
yb.conf.cache = 0
0b568f
0b568f
yb.repos.doSetup()
e1cfbe
log("Checking repos: {0}".format(', '.join([r.name for r in yb.repos.listEnabled()])))
0b568f
0b568f
sack = yb.pkgSack
0b568f
0b568f
finalret = 0
0b568f
for pkg in sack:
0b568f
    for attr, val in required_attrs.iteritems():
0b568f
        nvr = '{0}-{1}-{2}'.format(pkg['name'], pkg['version'], pkg['release'])
5dfcdd
        nvra = '{0}-{1}-{2}.{3}'.format(pkg['name'], pkg['version'], pkg['release'], pkg['arch'])
0b568f
        if pkg.name in special_overrides:
0b568f
            if attr in special_overrides[pkg.name]:
0b568f
                skip('{0}: {1} listed in special_overrides'.format(pkg.remote_path, attr))
0b568f
                continue
0b568f
        elif nvr in special_overrides:
0b568f
            if attr in special_overrides[nvr]:
0b568f
                skip('{0}: {1} listed in special_overrides'.format(pkg.remote_path, attr))
0b568f
                continue
5dfcdd
        elif nvra in special_overrides:
5dfcdd
            if attr in special_overrides[nvra]:
5dfcdd
                skip('{0}: {1} listed in special_overrides'.format(pkg.remote_path, attr))
5dfcdd
                continue
0b568f
0b568f
        if not pkg[attr]:
0b568f
            fail('{0}: Missing {1}'.format(pkg.remote_path, attr))
0b568f
            finalret = 1
0b568f
            continue
0b568f
0b568f
        if not fnmatch(str.upper(pkg[attr]), str.upper(val)):
0b568f
            fail('{0}: {1}: {2} does not match {3}'.format(pkg.remote_path, attr, pkg[attr], val))
0b568f
            finalret = 1
0b568f
0b568f
sys.exit(finalret)