Best Python code snippet using molecule_python
verify.py
Source:verify.py  
1#! /usr/bin/python -tt2# This program is free software; you can redistribute it and/or modify3# it under the terms of the GNU General Public License as published by4# the Free Software Foundation; either version 2 of the License, or5# (at your option) any later version.6#7# This program is distributed in the hope that it will be useful,8# but WITHOUT ANY WARRANTY; without even the implied warranty of9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the10# GNU Library General Public License for more details.11#12# You should have received a copy of the GNU General Public License13# along with this program; if not, write to the Free Software14# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.15#16#17# Copyright Red Hat Inc. 200818#19# Author: James Antill <james.antill@redhat.com>20#21# Examples:22#23#  yum verify24#  yum verify yum*25#  yum verify all26#  yum verify extras27from yum.plugins import TYPE_INTERACTIVE28import logging # for commands29from yum import logginglevels30import time31import stat32requires_api_version = '2.5'33plugin_type = (TYPE_INTERACTIVE,)34def nevr(pkg):35    """ Identify a pkg without arch. """36    return "%s-%s:%s-%s" % (pkg.name, pkg.epoch, pkg.version, pkg.release)37def fmt_rwx(mode, r, w, x):38    ret = []39    40    if w & mode:41        ret.append("w")42    else:43        ret.append("-")44    if r & mode:45        ret.append("r")46    else:47        ret.append("-")48    if x & mode:49        ret.append("x")50    else:51        ret.append("-")52    return "".join(ret)    53def format_mode(mode):54    ret = []55    tmp = []56    if stat.S_ISUID & mode:57        tmp.append("set user (setuid)")58    if stat.S_ISGID & mode:59        tmp.append("set group (setgid)")60    if stat.S_ISVTX & mode:61        tmp.append("sticky")62    if tmp:63        ret.append("/".join(tmp))64    65    ret.append("user:"  + fmt_rwx(mode, stat.S_IRUSR,stat.S_IWUSR,stat.S_IXUSR))66    ret.append("group:" + fmt_rwx(mode, stat.S_IRGRP,stat.S_IWGRP,stat.S_IXGRP))67    ret.append("other:" + fmt_rwx(mode, stat.S_IROTH,stat.S_IWOTH,stat.S_IXOTH))68    return ", ".join(ret)69import datetime70def format_time_diff(x, y):71    frm = datetime.datetime.fromtimestamp72    if x > y:73        return str(frm(x) - frm(y)) + " later"74    else:75        return str(frm(y) - frm(x)) + " earlier"76def problem_contains(problems, types):77    for problem in problems:78        if problem.type in types:79            return problem80    return None81def pkg_multilib_file(data, pkg, pkgs, fname, verify_cb=None):82    problems = data[pkg][fname]83    ml_csum = problem_contains(problems, ['checksum'])84    ml_size = problem_contains(problems, ['size'])85    ml_time = problem_contains(problems, ['mtime'])86    if not (ml_csum or ml_size or ml_time):87        return False88    for opkg in pkgs:89        if opkg == pkg:90            continue91        if opkg not in data:92            data[opkg] = opkg.verify(callback=verify_cb)93        if fname not in data[opkg]:94            return True95        if problem_contains(problems, ['state', 'missingok', 'ghost']):96            continue97        98        problems = data[opkg][fname]99        srch = []100        if ml_csum:101            srch.append('checksum')102        if ml_size:103            srch.append('size')104        if ml_time:105            srch.append('mtime')106        if problem_contains(problems, srch):107            continue108        return True109        110    return False111# We make decisions based on these112_verify_multilib = ['mtime', 'size', 'checksum']113_verify_missingok= ['missingok', 'ghost']114_verify_none     = ['state'] + _verify_missingok115_verify_missing  = ['missing', 'permissions-missing','genchecksum']+_verify_none116# These are user configurable, for output117_verify_low      = ['mtime', 'genchecksum', 'permissions-missing'] +_verify_none118_verify_onohi    = ['mtime', 'checksum']119_verify_nnohi    = ['mtime', 'checksum']120_verify_configs  = False121class VerifyCommand:122    def __init__(self, names, conf, multilib=True, verify_configs_override=None,123                 all=False, verify_callback=None):124        self.names = names125        self.conf  = conf126        self.all = all127        self.multilib = multilib128        self.verify_configs_override = verify_configs_override129        self.verify_callback = verify_callback130    def getNames(self):131        return self.names132    def getUsage(self):133        return "[PACKAGE|all|extras]"134    def getSummary(self):135        return """\136Verify packages and display data on bad verifications"""137    def doCheck(self, base, basecmd, extcmds):138        pass139    def show_pkgs(self, msg, pkgs):140        pass141    @staticmethod142    def _filter_results(oresults, verify_none=None):143        if verify_none is None:144            verify_none = _verify_none145        results = {}146        for fn in oresults:147            probs = []148            for problem in oresults[fn]:149                if problem.type not in verify_none:150                    probs.append(problem)151            if probs:152                results[fn] = probs153        return results154    @staticmethod155    def _filter_empty(oresults):156        results = {}157        for fname in oresults:158            if oresults[fname]:159                results[fname] = oresults[fname]160        return results161    def _filter_multilib(self, data, pkg, results):162        for fname in results:163            problems = results[fname]164            mpkgs = self._multilib[nevr(pkg)]165            if not pkg_multilib_file(data, pkg, mpkgs, fname, verify_cb=self.verify_callback):166                continue167            tmp = []168            for problem in problems:169                if problem.type in _verify_multilib:170                    continue171                tmp.append(problem)172            results[fname] = tmp173        return self._filter_empty(results)174    def filter_data(self, msg, pkgs):175        data = {}176        for pkg in sorted(pkgs):177            oresults = pkg.verify(patterns=self._filename_globs, all=self.all, 178                                  callback=self.verify_callback)179            if not _verify_configs and not self.verify_configs_override:180                for fn in oresults.keys():181                    if 'configuration' in oresults[fn][0].file_types:182                        del oresults[fn]183                184            if self.multilib:185                data[pkg] = oresults186            else:187                if self.all:188                    results = self._filter_results(oresults, _verify_missingok)189                else:190                    results = self._filter_results(oresults)191                if results:192                    yield (pkg, results)193            194        if not self.multilib:195            return196        ndata = {}197        for pkg in data:198            results = self._filter_results(data[pkg])199            if nevr(pkg) in self._multilib:200                ndata[pkg] = self._filter_multilib(data, pkg, results)201            else:202                ndata[pkg] = results203        204        for pkg in sorted(pkgs):205            if pkg in ndata and ndata[pkg]:206                yield (pkg, ndata[pkg])207    def _mode_except(self, base, line, problem=None, exceptions=None):208        if exceptions is None:209            exceptions = _verify_low210        if problem is not None and problem.type in exceptions:211            return ("", "")212        213        hib = ""214        hie = ""215        name = 'fg_' + line216        if name in self.conf and self.conf[name] in base.term.FG_COLOR:217            hib += base.term.FG_COLOR[self.conf[name]]218        name = 'bg_' + line219        if name in self.conf and self.conf[name] in base.term.BG_COLOR:220            hib += base.term.BG_COLOR[self.conf[name]]221        name = 'hi_' + line222        if name in self.conf and self.conf[name] in base.term.MODE:223            hib += base.term.MODE[self.conf[name]]224            225        hie = base.term.MODE['normal']226        return (hib, hie)227    def show_problem(self, base, msg, problem, done):228        if done:229            msg("%s%s%s" % (' ' * 35, '-' * 8, ' ' * 35))230        (hib, hie) = self._mode_except(base, 'prob', problem)231        msg("        Problem:  " + hib + problem.message + hie)232        if problem.type not in _verify_missing:233            cv = problem.disk_value234            ov = problem.database_value235            if problem.type == 'mtime':236                cv = time.ctime(cv) + " (%s)" % format_time_diff(cv, ov)237                ov = time.ctime(ov)238            if problem.type == 'mode':239                cv = format_mode(cv)240                ov = format_mode(ov)241            if problem.type == 'size':242                cv = "%*s" % (5, base.format_number(cv))243                ov = "%*s" % (5, base.format_number(ov))244                if cv == ov: # ignore human units, so we can see the diff.245                    cv = "%*s B" % (12, str(problem.disk_value))246                    ov = "%*s B" % (12, str(problem.database_value))247            (hib, hie) = self._mode_except(base, 'new', problem, _verify_nnohi)248            msg("        Current:  " + hib + cv + hie)249            (hib, hie) = self._mode_except(base, 'old', problem, _verify_onohi)250            msg("        Original: " + hib + ov + hie)251        252    def show_data(self, base, msg, pkgs, name):253        done = False254        mcb = lambda x: base.matchcallback(x, [])255        for (pkg, results) in self.filter_data(msg, pkgs):256            if not done:257                msg("%s %s %s" % ('=' * 20, name, '=' * 20))258            else:259                msg('')260            done = True261            262            mcb(pkg)263            for fname in sorted(results):264                hiprobs = len(filter(lambda x: x.type not in _verify_low,265                                     results[fname]))266                if hiprobs:267                    (hib, hie) = self._mode_except(base, 'file')268                else:269                    (hib, hie) = ("", "")270                msg("    File: " + hib + fname + hie)271                if hiprobs:272                    (hib, hie) = self._mode_except(base, 'tags')273                else:274                    (hib, hie) = ("", "")275                done_prob = False276                for problem in sorted(results[fname]):277                    if not done_prob and problem.file_types:278                        tags = ", ".join(problem.file_types)279                        msg("    Tags: " + hib + tags + hie)280                    self.show_problem(base, msg, problem, done_prob)281                    done_prob = True282    def doCommand(self, base, basecmd, extcmds):283        global _verify_configs284        logger = logging.getLogger("yum.verbose.main")285        def msg(x):286            logger.log(logginglevels.INFO_2, x)287        def msg_warn(x):288            logger.warn(x)289        opts = base.plugins.cmdline[0]290        if opts.verify_configuration_files is not None:291            val = opts.verify_configuration_files292            if False: pass293            elif val.lower() in ["0", "no", "false", "off"]:294                _verify_configs = False295            elif val.lower() in ["1", "yes", "true", "on"]:296                _verify_configs = True297            else:298                msg_warn("Ignoring bad value \"%s\" for the option %s" %299                         (val, "--verify-configuration-files"))300        self._filename_globs = None301        if opts.verify_filenames:302            self._filename_globs = opts.verify_filenames303            304        subgroup = ["installed"]305        if len(extcmds):306            if extcmds[0] == "all":307                extcmds = extcmds[1:]308            elif extcmds[0] == "extras":309                subgroup = ["extras"]310                extcmds = extcmds[1:]311        if self.multilib:312            pkgs = base.returnPkgLists(["installed"]).installed313            self._multilib = {}314            for pkg in pkgs:315                self._multilib.setdefault(nevr(pkg), []).append(pkg)316            for pkg in pkgs:317                if len(self._multilib[nevr(pkg)]) == 1:318                    del self._multilib[nevr(pkg)]319            # self._multilib is now a dict of all pkgs that have more than one320            # nevr() match321            322        ypl = base.returnPkgLists(subgroup + extcmds)323        self.show_data(base, msg, ypl.installed, 'Installed Packages')324        self.show_data(base, msg, ypl.extras,    'Extra Packages')325        return 0, [basecmd + ' done']326    def needTs(self, base, basecmd, extcmds):327        if not len(extcmds) or extcmds[0] != 'extras':328            return False329        330        return True331def config_hook(conduit):332    '''333    Yum Plugin Config Hook: 334    Add the 'verify' and 'verify-no-multilib' commands.335    '''336    global _verify_configs337    global _verify_low338    global _verify_onohi339    global _verify_nnohi340    _verify_configs = conduit.confBool('main', 'configuration-files',341                                       default=False)342    343    low = conduit.confString('highlight', 'low-priority', default=None)344    if low:345        _verify_low   = filter(len, low.replace(',', ' ').split())346    fold = conduit.confString('highlight', 'filter-old', default=None)347    if fold:348        _verify_onohi = filter(len, fold.replace(',', ' ').split())349    fnew = conduit.confString('highlight', 'filter-new', default=None)350    if fnew:351        _verify_nnohi = filter(len, fnew.replace(',', ' ').split())352    conf = {}353    conf['hi_prob'] = conduit.confString('highlight', 'problem', default='bold')354    conf['fg_prob'] = conduit.confString('highlight', 'problem-fg',default=None)355    conf['bg_prob'] = conduit.confString('highlight', 'problem-bg',default=None)356    357    conf['hi_new'] = conduit.confString('highlight', 'new', default='reverse')358    conf['fg_new'] = conduit.confString('highlight', 'new-fg', default=None)359    conf['bg_new'] = conduit.confString('highlight', 'new-bg', default=None)360    361    conf['hi_old'] = conduit.confString('highlight', 'old',    default=None)362    conf['fg_old'] = conduit.confString('highlight', 'old-fg', default='red')363    conf['bg_old'] = conduit.confString('highlight', 'old-bg', default=None)364    365    conf['hi_file'] = conduit.confString('highlight', 'file',366                                         default='underline')367    conf['fg_file'] = conduit.confString('highlight', 'file-fg',368                                         default='green')369    conf['bg_file'] = conduit.confString('highlight', 'file-bg', default=None)370    371    conf['hi_tags'] = conduit.confString('highlight', 'tags',    default='bold')372    conf['fg_tags'] = conduit.confString('highlight', 'tags-fg',373                                         default='yellow')374    conf['bg_tags'] = conduit.confString('highlight', 'tags-bg',375                                         default='black')376    reg = conduit.registerCommand377    reg(VerifyCommand(['verify-all'], conf, multilib=False,378                      verify_configs_override=True, all=True, 379                      verify_callback=conduit._base.verify_plugins_cb))380    reg(VerifyCommand(['verify-rpm'], conf, multilib=False,381                      verify_configs_override=True,382                      verify_callback=conduit._base.verify_plugins_cb))383    reg(VerifyCommand(['verify-multilib','verify'], conf,384                      verify_callback=conduit._base.verify_plugins_cb))385    parser = conduit.getOptParser()386    if not parser:387        return388    def make_nopt(attrs):389        attrs = attrs.replace("-", "_")390        def func(opt, key, val, parser):391            vals = str(val).replace(",", " ").split()392            vals = filter(len, vals)393            getattr(parser.values, 'verify_' + attrs).extend(vals)394        return func395    if hasattr(parser, 'plugin_option_group'):396        parser = parser.plugin_option_group397    parser.add_option('--verify-filenames', action="callback",398                      callback=make_nopt('filenames'), default=[],type="string",399                      help='Only verify files matching this')400    parser.add_option('--verify-configuration-files', action="store",401                      type="string",...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
