def ImportFromPkgutil(pkgname, curs):

    timestamp = 0
    owner = 0
    pkgid = pkgname
    vers = "1.0"
    ppath = ""

    #get metadata from applepkgdb
    proc = subprocess.Popen(["/usr/sbin/pkgutil", "--pkg-info-plist", pkgid],
                            bufsize=1, stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    (pliststr, dummy_err) = proc.communicate()
    if pliststr:
        plist = FoundationPlist.readPlistFromString(pliststr)
        if "pkg-version" in plist:
            vers = plist["pkg-version"]
        if "install-time" in plist:
            timestamp = plist["install-time"]
        if "install-location" in plist:
            ppath = plist["install-location"]
            ppath = ppath.lstrip('./').rstrip('/')
        else:
            # there _should_ be an install-location. If there's not, let's
            # check the old /Library/Receipts.
            # (Workaround for QuarkXPress 8.1 packages)
            receiptpath = findBundleReceiptFromID(pkgid)
            if receiptpath:
                infopath = os.path.join(receiptpath, 'Contents/Info.plist')
                if os.path.exists(infopath):
                    infopl = FoundationPlist.readPlist(infopath)
                    if "IFPkgRelocatedPath" in infopl:
                        ppath = infopl["IFPkgRelocatedPath"]
                        ppath = ppath.lstrip('./').rstrip('/')

    values_t = (timestamp, owner, pkgid, vers, ppath, pkgname)
    curs.execute(
        '''INSERT INTO pkgs (timestamp, owner, pkgid, vers, ppath, pkgname)
           values (?, ?, ?, ?, ?, ?)''', values_t)
    pkgkey = curs.lastrowid

    cmd = ["/usr/sbin/pkgutil", "--files", pkgid]
    proc = subprocess.Popen(cmd, shell=False, bufsize=1,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    while True:
        line = proc.stdout.readline().decode('UTF-8')
        if not line and (proc.poll() != None):
            break
        path = line.rstrip("\n")

        # pkgutil --files pkgid only gives us path info.  We don't
        # really need perms, uid and gid, so we'll just fake them.
        # if we needed them, we'd have to call
        # pkgutil --export-plist pkgid and iterate through the
        # plist.  That would be slower, so we'll do things this way...
        perms = "0000"
        uid = "0"
        gid = "0"
        if path != ".":
            # special case for MS Office 2008 installers
            # /tmp/com.microsoft.updater/office_location
            if ppath == "tmp/com.microsoft.updater/office_location":
                ppath = "Applications"
            # another special case for Office 2011 updaters
            if ppath.startswith(
                    'tmp/com.microsoft.office.updater/com.microsoft.office.'):
                ppath = ""
            #prepend the ppath so the paths match the actual install locations
            path = path.lstrip("./")
            if ppath:
                path = ppath + "/" + path

            values_t = (path, )
            row = curs.execute(
                'SELECT path_key from paths where path = ?',
                values_t).fetchone()
            if not row:
                curs.execute(
                    'INSERT INTO paths (path) values (?)', values_t)
                pathkey = curs.lastrowid
            else:
                pathkey = row[0]

            values_t = (pkgkey, pathkey, uid, gid, perms)
            curs.execute(
                'INSERT INTO pkgs_paths (pkg_key, path_key, uid, gid, perms) '
                'values (?, ?, ?, ?, ?)', values_t)
Imports package data from pkgutil into our internal package database.
dataset/ETHPy150Open munki/munki/code/client/munkilib/removepackages.py/ImportFromPkgutil