[pacman-dev] [RFC] Package parser in python

Allan McRae allan at archlinux.org
Thu Oct 1 08:52:26 EDT 2009


Hi,

This is one part of the makepkg test suite I am working on.   It is 
fairly a simple class that takes a pacman package filename and does some 
parsing.  Currently that involves getting the file list and the info 
from the .PKGINFO file.

I am still fairly new to python so I am looking for comments on how this 
could be improved. 

One idea I have had is to not initialize all the fields in the pkginfo 
dict and add a test if an array exists before appending.  It would make 
the code tidier but that would mean the need to test if the field exists 
when comparing it later.


---

#!/usr/bin/env python3

import os, tarfile

class package:
    '''
    parse information from a pacman package
   
    member variables:
    file (string) - name of package file
    file_list (array) - list of files in the package archive
    pkginfo (dict) - package information parsed from .PKGINFO
    '''


    def __init__ (self, file):
        self.file = file

        if not os.path.exists(file):
            raise IOError('{} does not exist'.format(file))

        if not tarfile.is_tarfile(file):
            raise TypeError('{} is not a tar file'.format(file))

        pkg = tarfile.open(file)

        self.file_list = pkg.getnames()
        if not ".PKGINFO" in self.file_list:
            raise TypeError('{} is not a package file'.format(file))

        self.__parse_pkginfo(pkg)

        pkg.close()


    def __parse_pkginfo(self, pkg):
        self.pkginfo = {}
        self.pkginfo['pkgname'] = ""
        self.pkginfo['pkgbase'] = ""
        self.pkginfo['pkgver'] = ""
        self.pkginfo['pkgdesc'] = ""
        self.pkginfo['url'] = ""
        self.pkginfo['builddate'] = ""
        self.pkginfo['packager'] = ""
        self.pkginfo['size'] = ""
        self.pkginfo['arch'] = ""
        self.pkginfo['force'] = ""
        self.pkginfo['license'] = []
        self.pkginfo['replaces'] = []
        self.pkginfo['group'] = []
        self.pkginfo['depend'] = []
        self.pkginfo['optdepend'] = []
        self.pkginfo['conflict'] = []
        self.pkginfo['provides'] = []
        self.pkginfo['backup'] = []
        self.pkginfo['makepkgopt'] = []   
       
        arrays = ['license', 'replaces', 'group', 'depend', 'optdepend',
                  'conflict', 'provides', 'backup', 'makepkgopt']
       
        pkginfo = pkg.extractfile(".PKGINFO")
        for line in pkginfo:
            if (line[0] == '#'.encode('utf-8')[0]):
                continue
            (key, value) = line.decode('utf-8').split(" = ")

            if key in arrays:
                self.pkginfo[key].append(value.strip())
            else:
                self.pkginfo[key] = value.strip()

        pkginfo.close()

if __name__ == '__main__':
    pkg = package('/var/cache/pacman/pkg/pacman-3.3.1-1-i686.pkg.tar.gz')
    print('Package file: \n\t{}\n'.format(pkg.file))
    print('Package info: \n\t{}\n'.format(pkg.pkginfo))
    print('File list: \n\t{}\n'.format(pkg.file_list))

---


Example output:

 > python3 package.py
Package file:
    /var/cache/pacman/pkg/pacman-3.3.1-1-i686.pkg.tar.gz

Package info:
    {'license': ['GPL'], 'backup': ['etc/pacman.conf', 
'etc/makepkg.conf'], 'replaces': [], 'pkgname': 'pacman', 'builddate': 
'1253674558', 'pkgdesc': 'A library-based package manager with 
dependency support', 'makepkgopt': ['strip', '!docs', '!libtool', 
'emptydirs', 'zipman', 'purge'], 'url': 
'http://www.archlinux.org/pacman/', 'optdepend': ['fakeroot: for makepkg 
usage as normal user', 'python: for rankmirrors script usage'], 
'depend': ['bash', 'libarchive>=2.7.0-2', 'libfetch>=2.20', 
'pacman-mirrorlist'], 'group': ['base'], 'pkgbase': '', 'provides': [], 
'force': '', 'packager': 'Dan McGee <dan at archlinux.org>', 'size': 
'2093056', 'arch': 'i686', 'conflict': [], 'pkgver': '3.3.1-1'}

File list:
    ['.PKGINFO', '.INSTALL', 'etc', 'etc/makepkg.conf', 
'etc/pacman.conf', 'etc/bash_completion.d', 
'etc/bash_completion.d/pacman', 'usr', 'usr/include', 'usr/share', 
'usr/bin', 'usr/lib', 'usr/lib/libalpm.so.4', 
'usr/lib/libalpm.so.4.0.1', 'usr/lib/libalpm.so', 'usr/lib/libalpm.a',  ....

-------------- next part --------------
A non-text attachment was scrubbed...
Name: package.py
Type: text/x-python
Size: 2045 bytes
Desc: not available
URL: <http://mailman.archlinux.org/pipermail/pacman-dev/attachments/20091001/646e2388/attachment.py>


More information about the pacman-dev mailing list