tty1
  #   ˆl–ß=¿J *j"TíÀ3q¦”Å£Ã„Î‰ÿÂÁ ?÷     
import os.path
import re

from yum.i18n import _, P_

from yum.constants import *

from yum.logginglevels import INFO_1

import rpmUtils.miscutils

import misc

import fnmatch

# newpackages is weird, in that we'll never display that because we filter to
# things relevant to installed pkgs...
_update_info_types_ = ("security", "bugfix", "enhancement",
                       "recommended", "newpackage")

def _rpm_tup_vercmp(tup1, tup2):
    """ Compare two "std." tuples, (n, a, e, v, r). """
    return rpmUtils.miscutils.compareEVR((tup1[2], tup1[3], tup1[4]),
                                         (tup2[2], tup2[3], tup2[4]))


def _ysp_safe_refs(refs):
    """ Sometimes refs == None, if so return the empty list here. 
        So we don't have to check everywhere. """
    if not refs:
        return []
    return refs

def _match_sec_cmd(sec_cmds, pkgname, notice):
    for i in sec_cmds:
        if fnmatch.fnmatch(pkgname, i):
            return i
        if fnmatch.fnmatch(notice['update_id'], i):
            return i

        cvei = i
        if not (i.startswith("CVE-") or i.startswith("*")):
            cvei = 'CVE-' + i
        for ref in _ysp_safe_refs(notice['references']):
            if ref['id'] is None:
                continue
            if fnmatch.fnmatch(ref['id'], i):
                return i
            if fnmatch.fnmatch(ref['id'], cvei):
                return i
    return None

def _has_id(used_map, refs, ref_type, ref_ids):
    ''' Check if the given ID is a match. '''
    for ref in _ysp_safe_refs(refs):
        if ref['type'] != ref_type:
            continue
        if ref['id'] not in ref_ids:
            continue
        used_map[ref_type][ref['id']] = True
        return ref
    return None
    
def _ysp_should_filter_pkg(opts, pkgname, notice, used_map):
    """ Do the package filtering for should_show and should_keep. """
    
    rcmd = _match_sec_cmd(opts.sec_cmds, pkgname, notice)
    if rcmd:
        used_map['cmd'][rcmd] = True
        return True
    elif opts.advisory and notice['update_id'] in opts.advisory:
        used_map['id'][notice['update_id']] = True
        return True
    elif (opts.severity and notice['type'] == 'security' and
          notice['severity'] in opts.severity):
        used_map['sev'][notice['severity']] = True
        return True
    elif opts.cve and _has_id(used_map, notice['references'], "cve", opts.cve):
        return True
    elif opts.bz and _has_id(used_map, notice['references'],"bugzilla",opts.bz):
        return True
    # FIXME: Add opts for enhancement/etc.? -- __update_info_types__
    elif (opts.security and notice['type'] == 'security' and
          (not opts.severity or 'severity' not in notice or
           not notice['severity'])):
        return True
    elif opts.bugfixes and notice['type'] == 'bugfix':
        return True
    elif not (opts.advisory or opts.cve or opts.bz or
              opts.security or opts.bugfixes or opts.sec_cmds or opts.severity):
        return True # This is only possible from should_show_pkg

    return False

def _ysp_has_info_md(rname, md):
    if rname in _update_info_types_:
        if md['type'] == rname:
            return md
    for ref in _ysp_safe_refs(md['references']):
        if ref['type'] != rname:
            continue
        return md

def _no_options(opts):
    return not (opts.security or opts.bugfixes or
                opts.advisory or opts.bz or opts.cve or opts.severity)

def _updateinfofilter2opts(updateinfo_filters):
    opts = misc.GenericHolder()
    opts.sec_cmds = []

    opts.advisory = updateinfo_filters.get('advs', [])
    opts.bz       = updateinfo_filters.get('bzs',  [])
    opts.cve      = updateinfo_filters.get('cves', [])
    opts.severity = updateinfo_filters.get('sevs', [])

    opts.bugfixes = updateinfo_filters.get('bugfix', False)
    opts.security = updateinfo_filters.get('security', False)

    return opts

def _args2filters(args):
    # Basically allow args to turn into security filters, for shell command etc.