On 02/09/2011 09:31 PM, seth vidal wrote:
1. to read the desktop files out of the rpms themselves - that code
exists
Well, I have a very early version...

Be aware that it does only work with xz compressed content (for now). The selection of files is too promiscuous and the code may require tweaking. If someone has something better he shall speak up now.

Florian

PS: Alternative is to use rpm2cpio. Nevertheless we should read the header first to check if there are any desktop files at all.
#!/usr/bin/python

import rpm
import glob
import os.path
import cpio
import lzma
import re

_ts = rpm.ts()
_ts.setVSFlags(0x7FFFFFFF)

tags = {}
for name in dir(rpm):
    if name.startswith("RPMTAG_"):
        tags[getattr(rpm, name)] = name[7:]

def read_menu_entry(filename, content):
    print filename
    m = re.search("Categories=(.+);\n", content)
    if m:
        categories = m.group(1).split(";")
        print categories

def write_desktop_file(directory, hdr, cpioarchive):
    path = os.path.join(directory, hdr["name"])
    if not os.path.exists(path):
        os.makedirs(path)
    f = open(os.path.join(path, os.path.basename(cpioarchive.filename)), "w")
    f.write(cpioarchive.read())
    f.close()


class LZMA:

    def __init__(self, f):
        self._f = f
        self.decomp = lzma.LZMADecompressor()
        self.leftover = ""

    def read(self, size):
        # highly inefficient
        while not size < len(self.leftover):
            c = self._f.read(size)
            if not c:
                break
            d = self.decomp.decompress(c)
            self.leftover += d
        result = self.leftover[:size]
        self.leftover = self.leftover[size:]
        return result

def read_package(path, targetpath):
    fd = os.open(path, os.O_RDONLY)
    hdr = _ts.hdrFromFdno(fd)

    fi = hdr.fiFromHeader()
    apps = []
    for (fname, size, mode, mtime, flags, dev, inode,
         nlink, state, vflags, user, group, digest) in fi:
        if fname.endswith(".desktop"):
            apps.append(fname)

    if not apps: 
        os.close(fd)
        return

    f = os.fdopen(fd)

    lf = LZMA(f)
    cpioarchive = cpio.CpioArchive(lf)

    for filename in cpioarchive:
        if filename.endswith(".desktop"):
            write_desktop_file(targetpath, hdr, cpioarchive)

    f.close()
    

def check_dir(rpmpath, targetpath):
    for f in glob.glob(os.path.join(rpmpath, "*.rpm")):
        read_package(f, targetpath)


check_dir("testrpms/", "desktopfiles/")
_______________________________________________
Distributions mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/distributions

Reply via email to