Commit c7d1e6b2 authored by Lisa's avatar Lisa

Make akabei-db-import more testable: move main functionality in separate...

Make akabei-db-import more testable: move main functionality in separate function, have the package parser return errors and ignore malformed packages instead of just exiting on the spot. Runned again and it works.
parent 2feb4b61
......@@ -91,6 +91,9 @@ def parseLocalDatabase(path):
# Calls the package parser
parser = PacmanPackageParser(desc, install, files)
# Ignore packages that we could not parse successfully.
if not parser.parsePackage():
continue
packages.append(parser.getPackage())
# show last progress in case the progress never became exactly 100 (almost always)
......@@ -109,6 +112,27 @@ def usage():
print " -o, --db-output <file> Prints the Akabei database on <file> (default: local.db)"
print " -s, --db-scheme <file> Reads the database scheme from <file> (default: /usr/share/akabei/dbschema.sql)"
def DoConversion(pacmanConf, outputFile, schemeFile):
# Gets the database path from the pacman configuration
print "[**] Reading Pacman database path from " + pacmanConf + "...",
dbpath = getDatabasePath(pacmanConf)
print "done."
if len(dbpath) == 0:
dbpath = "/var/lib/pacman" # if not explicitly set there, then go back to the default value
localPath = os.path.join(dbpath, "local")
# Get local packages from pacman, then write them into the akabei database in the right format
print "[**] Parsing Pacman local database..."
localPackages = parseLocalDatabase(localPath)
print "done."
dbwriter = AkabeiDbWriter(outputFile, schemeFile)
dbwriter.addPackages(localPackages)
print "[**] Database converted successfully. Now enjoy akabei!"
# Start point
if __name__ == "__main__":
......@@ -145,22 +169,4 @@ if __name__ == "__main__":
print "This script may take some time to finish."
print
# Gets the database path from the pacman configuration
print "[**] Reading Pacman database path from " + pacmanConf + "...",
dbpath = getDatabasePath(pacmanConf)
print "done."
if len(dbpath) == 0:
dbpath = "/var/lib/pacman" # if not explicitly set there, then go back to the default value
localPath = os.path.join(dbpath, "local")
# Get local packages from pacman, then write them into the akabei database in the right format
print "[**] Parsing Pacman local database..."
localPackages = parseLocalDatabase(localPath)
print "done."
dbwriter = AkabeiDbWriter(outputFile, schemeFile)
dbwriter.addPackages(localPackages)
print "[**] Database converted successfully. Now enjoy akabei!"
DoConversion(pacmanConf, outputFile, schemeFile)
......@@ -16,34 +16,48 @@ import os
class PacmanPackageParser:
def __init__(self, d, i, f):
self.descFileName = d
self.installFileName = i
self.filesFileName = 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")
def parsePackage(self):
"""Main public API. Returns True/False for success/failure."""
if not self.descFileName:
print("[!!] Found malformed package: no desc file.")
return False
self.desc = open(self.descFileName, "r")
if self.installFileName:
self.install = open(self.installFileName, "r")
if self.filesFileName:
self.files = open(self.filesFileName, "r")
self.package = AkabeiPackage()
# Used by the parse* functions
self.state = ""
self.parseDescription()
result = self.parseDescription()
# These are both optional files.
self.parseInstall()
self.parseFiles()
return result
def parseDescription(self):
"""Returns success/failure."""
self.state = ""
# Two required parameters for a package: NAME and
# VERSION. If at least one is missing, returns False.
hasName = False
hasVersion = False
for line in self.desc.readlines():
line = line[:-1] # delete newline at the end
......@@ -58,8 +72,10 @@ class PacmanPackageParser:
else:
if self.state == "NAME":
self.package.name = line
hasName = True
elif self.state == "VERSION":
self.package.version = line
hasVersion = True
elif self.state == "DESC":
self.package.description = line
elif self.state == "GROUPS":
......@@ -90,7 +106,14 @@ class PacmanPackageParser:
elif self.state == "REPLACES":
self.package.addReplace(line)
elif self.state == "CONFLICTS":
self.package.addConflict(line)
self.package.addConflict(line)
if not hasName:
print "[!!] Found malformed package with no name."
if not hasVersion:
print "[!!] Found malformed package with no version."
return hasName and hasVersion
# Installation scriptlet
def parseInstall(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment