def analyzeInstalledPkgs():
    #global PKGDATA
    itemname_to_pkgid = {}
    pkgid_to_itemname = {}
    for catalogname in CATALOG.keys():
        catalogitems = CATALOG[catalogname]['items']
        addPackageids(catalogitems, itemname_to_pkgid, pkgid_to_itemname)
    # itemname_to_pkgid now contains all receipts (pkgids) we know about
    # from items in all available catalogs

    if not INSTALLEDPKGS:
        getInstalledPackages()
    # INSTALLEDPKGS now contains all receipts found on this machine

    installed = []
    partiallyinstalled = []
    installedpkgsmatchedtoname = {}
    for name in itemname_to_pkgid.keys():
        # name is a Munki install item name
        somepkgsfound = False
        allpkgsfound = True
        for pkgid in itemname_to_pkgid[name].keys():
            if pkgid in INSTALLEDPKGS.keys():
                somepkgsfound = True
                if not name in installedpkgsmatchedtoname:
                    installedpkgsmatchedtoname[name] = []
                # record this pkgid for Munki install item name
                installedpkgsmatchedtoname[name].append(pkgid)
            else:
                # didn't find pkgid in INSTALLEDPKGS
                allpkgsfound = False
        if allpkgsfound:
            # we found all receipts by pkgid on disk
            installed.append(name)
        elif somepkgsfound:
            # we found only some receipts for the item
            # on disk
            partiallyinstalled.append(name)

    # we pay special attention to the items that seem partially installed.
    # we need to see if there are any packages that are unique to this item
    # if there aren't, then this item probably isn't installed, and we're
    # just finding receipts that are shared with other items.
    for name in partiallyinstalled:
        # get a list of pkgs for this item that are installed
        pkgsforthisname = installedpkgsmatchedtoname[name]
        # now build a list of all the pkgs referred to by all the other
        # items that are either partially or entirely installed
        allotherpkgs = []
        for othername in installed:
            allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        for othername in partiallyinstalled:
            if othername != name:
                allotherpkgs.extend(installedpkgsmatchedtoname[othername])
        # use Python sets to find pkgs that are unique to this name
        uniquepkgs = list(set(pkgsforthisname) - set(allotherpkgs))
        if uniquepkgs:
            installed.append(name)

    # now filter partiallyinstalled to remove those items we moved to installed
    partiallyinstalled = [item for item in partiallyinstalled
                          if item not in installed]

    # build our reference table. For each item we think is installed,
    # record the receipts on disk matched to the item
    references = {}
    for name in installed:
        for pkgid in installedpkgsmatchedtoname[name]:
            if not pkgid in references:
                references[pkgid] = []
            references[pkgid].append(name)

    # look through all our INSTALLEDPKGS, looking for ones that have not been
    # attached to any Munki names yet
    orphans = [pkgid for pkgid in INSTALLEDPKGS.keys()
               if pkgid not in references]

    # attempt to match orphans to Munki item names
    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 = bestVersionMatch(
                installed_pkgid_version, possible_match_items)
            if best_match:
                matched_orphans.append(best_match)

    # process matched_orphans
    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

    # left here for future debugging/testing use....
    #PKGDATA['itemname_to_pkgid'] = itemname_to_pkgid
    #PKGDATA['pkgid_to_itemname'] = pkgid_to_itemname
    #PKGDATA['partiallyinstalled_names'] = partiallyinstalled
    #PKGDATA['orphans'] = orphans
    #PKGDATA['matched_orphans'] = matched_orphans
    #ManagedInstallDir = munkicommon.pref('ManagedInstallDir')
    #pkgdatapath = os.path.join(ManagedInstallDir, 'PackageData.plist')
    #try:
    #    FoundationPlist.writePlist(PKGDATA, pkgdatapath)
    #except FoundationPlist.NSPropertyListWriteException:
    #    pass
    #catalogdbpath =  os.path.join(ManagedInstallDir, 'CatalogDB.plist')
    #try:
    #    FoundationPlist.writePlist(CATALOG, catalogdbpath)
    #except FoundationPlist.NSPropertyListWriteException:
    #    pass
Analyzed installed packages in an attempt to determine what is
dataset/ETHPy150Open munki/munki/code/client/munkilib/updatecheck.py/analyzeInstalledPkgs