akabeipackageparser.py 4.51 KB
Newer Older
Lisa's avatar
Lisa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#!/usr/bin/python

# A script to create an AkabeiPackage object from the information Pacman stores.
#
#   Copyright (C) 2011 Lisa Vitolo <shainer@chakra-project.org>

#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.

from akabeipackage import *
import sys
import os

class PacmanPackageParser:
    
    def __init__(self, d, i, f):
        self.desc = None
        self.install = None
        self.files = None
        
        if len(d) > 0:
            self.desc = open(d, "r")
        else:
            print "[!!] Error: this package doesn't contain a desc file."
            print "[!!] Your local database may be broken. Quitting now..."
            sys.exit(-1)
            
        if len(i) > 0:
            self.install = open(i, "r")
            
        if len(f) > 0:
            self.files = open(f, "r")
        
        self.package = AkabeiPackage()
        
        # Used by the parse* functions
        self.state = ""
        
        self.parseDescription()
        self.parseInstall()
        self.parseFiles()
        
    def parseDescription(self):
        self.state = ""
        
        for line in self.desc.readlines():
            line = line[:-1] # delete newline at the end
            
            if len(line) == 0:
                self.state = ""
                continue
            
            # lines in the form %SOMETHING% starts a section in the file
            if line[0] == "%":
                self.state = line[1:len(line)-1]
            else:
                if self.state == "NAME":
                    self.package.name = line
                elif self.state == "VERSION":
                    self.package.version = line
                elif self.state == "DESC":
                    self.package.description = line
                elif self.state == "GROUPS":
                    self.package.addGroup(line)
                elif self.state == "URL":
                    self.package.url = line
                elif self.state == "LICENSE":
                    self.package.license = line
                elif self.state == "ARCH":
                    self.package.arch = line
                elif self.state == "BUILDDATE":
                    self.package.builddate = int(line)
                elif self.state == "INSTALLDATE":
                    self.package.installdate = int(line)
                elif self.state == "PACKAGER":
                    self.package.packager = line
                elif self.state == "SIZE":
                    self.package.size = int(line)
                elif self.state == "DEPENDS":
                    self.package.addDependency(line)
                elif self.state == "OPTDEPENDS":
                    self.package.addOptional(line)
                elif self.state == "REASON":
                    self.package.reason = int(line)
                elif self.state == "PROVIDES":
                    self.package.addProvide(line)
                elif self.state == "REPLACES":
                    self.package.addReplace(line)
                elif self.state == "CONFLICTS":
                    self.package.addConflict(line)                        

    # Installation scriptlet
    def parseInstall(self):
        if self.install == None:
            return
        
        self.package.addScriptlet(self.install.read())
    
    def parseFiles(self):
        if self.files == None:
            return
            
        self.state = ""
        packagefiles = {}
        
        for line in self.files.readlines():
            line = line[:-1]
            
            if len(line) == 0:
                self.state = ""
                continue
            
            if line[0] == "%":
                self.state = line[1:len(line)-1]
            else:
                if self.state == "FILES":
                    packagefiles[line] = ""
                if self.state == "BACKUP":
                    separatedLine = line.partition("\t")
                    packagefiles[separatedLine[0]] = separatedLine[2]
                    
        for pf in packagefiles:
            self.package.addFile(pf, packagefiles[pf])
                     
    def getPackage(self):
        return self.package
    
    def __del__(self):
        if self.desc != None:
            self.desc.close()
        if self.install != None:
            self.install.close()
        if self.files != None:
            self.files.close()