@utils.Memoize
def analyze_installed_pkgs():
    'Analyze catalog data and installed packages in an attempt to determine\n    what is installed.'
    pkgdata = {}
    itemname_to_pkgid = {}
    pkgid_to_itemname = {}
    for catalogname in _CATALOG:
        catalogitems = _CATALOG[catalogname]['items']
        add_package_ids(catalogitems, itemname_to_pkgid, pkgid_to_itemname)
    installedpkgs = pkgutils.getInstalledPackages()
    installed = []
    partiallyinstalled = []
    installedpkgsmatchedtoname = {}
    for name in itemname_to_pkgid:
        foundpkgcount = 0
        for pkgid in itemname_to_pkgid[name]:
            if (pkgid in installedpkgs):
                foundpkgcount += 1
                if (not (name in installedpkgsmatchedtoname)):
                    installedpkgsmatchedtoname[name] = []
                installedpkgsmatchedtoname[name].append(pkgid)
        if (foundpkgcount > 0):
            if (foundpkgcount == len(itemname_to_pkgid[name])):
                installed.append(name)
            else:
                partiallyinstalled.append(name)
    for name in partiallyinstalled:
        pkgsforthisname = installedpkgsmatchedtoname[name]
        allotherpkgs = []
        for othername in installed:
            allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        for othername in partiallyinstalled:
            if (othername != name):
                allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        uniquepkgs = list((set(pkgsforthisname) - set(allotherpkgs)))
        if uniquepkgs:
            installed.append(name)
    partiallyinstalled = [item for item in partiallyinstalled if (item not in installed)]
    references = {}
    for name in installed:
        for pkgid in installedpkgsmatchedtoname[name]:
            if (not (pkgid in references)):
                references[pkgid] = []
            references[pkgid].append(name)
    orphans = [pkgid for pkgid in installedpkgs if (pkgid not in references)]
    matched_orphans = []
    for pkgid in orphans:
        if (pkgid in pkgid_to_itemname):
            installed_pkgid_version = installedpkgs[pkgid]
            possible_match_items = pkgid_to_itemname[pkgid]
            best_match = best_version_match(installed_pkgid_version, possible_match_items)
            if best_match:
                matched_orphans.append(best_match)
    for name in matched_orphans:
        if (name not in installed):
            installed.append(name)
        if (name in partiallyinstalled):
            partiallyinstalled.remove(name)
        for pkgid in installedpkgsmatchedtoname[name]:
            if (not (pkgid in references)):
                references[pkgid] = []
            if (not (name in references[pkgid])):
                references[pkgid].append(name)
    pkgdata['receipts_for_name'] = installedpkgsmatchedtoname
    pkgdata['installed_names'] = installed
    pkgdata['pkg_references'] = references
    return pkgdata
Analyze catalog data and installed packages in an attempt to determine
what is installed.
@utils.Memoize
def analyze_installed_pkgs():
    'Analyze catalog data and installed packages in an attempt to determine\n    what is installed.'
    pkgdata = {}
    itemname_to_pkgid = {}
    pkgid_to_itemname = {}
    for catalogname in _CATALOG:
        catalogitems = _CATALOG[catalogname]['items']
        add_package_ids(catalogitems, itemname_to_pkgid, pkgid_to_itemname)
    installedpkgs = pkgutils.getInstalledPackages()
    installed = []
    partiallyinstalled = []
    installedpkgsmatchedtoname = {}
    for name in itemname_to_pkgid:
        foundpkgcount = 0
        for pkgid in itemname_to_pkgid[name]:
            if (pkgid in installedpkgs):
                foundpkgcount += 1
                if (not (name in installedpkgsmatchedtoname)):
                    installedpkgsmatchedtoname[name] = []
                installedpkgsmatchedtoname[name].append(pkgid)
        if (foundpkgcount > 0):
            if (foundpkgcount == len(itemname_to_pkgid[name])):
                installed.append(name)
            else:
                partiallyinstalled.append(name)
    for name in partiallyinstalled:
        pkgsforthisname = installedpkgsmatchedtoname[name]
        allotherpkgs = []
        for othername in installed:
            allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        for othername in partiallyinstalled:
            if (othername != name):
                allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        uniquepkgs = list((set(pkgsforthisname) - set(allotherpkgs)))
        if uniquepkgs:
            installed.append(name)
    partiallyinstalled = [item for item in partiallyinstalled if (item not in installed)]
    references = {}
    for name in installed:
        for pkgid in installedpkgsmatchedtoname[name]:
            if (not (pkgid in references)):
                references[pkgid] = []
            references[pkgid].append(name)
    orphans = [pkgid for pkgid in installedpkgs if (pkgid not in references)]
    matched_orphans = []
    for pkgid in orphans:
        if (pkgid in pkgid_to_itemname):
            installed_pkgid_version = installedpkgs[pkgid]
            possible_match_items = pkgid_to_itemname[pkgid]
            best_match = best_version_match(installed_pkgid_version, possible_match_items)
            if best_match:
                matched_orphans.append(best_match)
    for name in matched_orphans:
        if (name not in installed):
            installed.append(name)
        if (name in partiallyinstalled):
            partiallyinstalled.remove(name)
        for pkgid in installedpkgsmatchedtoname[name]:
            if (not (pkgid in references)):
                references[pkgid] = []
            if (not (name in references[pkgid])):
                references[pkgid].append(name)
    pkgdata['receipts_for_name'] = installedpkgsmatchedtoname
    pkgdata['installed_names'] = installed
    pkgdata['pkg_references'] = references
    return pkgdata