akabei-conf-import.py 4.27 KB
Newer Older
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
#!/usr/bin/python

# A script to convert a Pacman config file into an Akabei config file
#
#   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.

import sys
import getopt

# A dictionary for translating the Pacman options.
# Those which map to an empty string aren't supported by Akabei, so they are deleted when found.
correspondences = {
                   "RootDir":"RootDir", "DBPath":"DatabaseDir", "CacheDir":"CacheDir",
                   "LogFile":"LogFile", "HoldPkg":"HoldPkg", "IgnorePkg":"IgnorePkg",
                   "SyncFirst":"SyncFirst", "IgnoreGroup":"IgnoreGroup", "Include":"Include",
                   "NoUpgrade":"NoUpgrade", "NoExtract":"NoExtract", "CleanMethod":"CleanMethod",
                   "UseSyslog":"UseSyslog", "ShowSize":"ShowSize", "TotalDownload":"TotalDownload",
                   "Server":"Server", "Include":"Include",
                   "Architecture":"", "XferCommand":"", "UseDelta":"", "ILoveCandy":""
                  }

def parseOptionLine(line, akabeiConf):
    separatedOption = line.partition('=')
    name = separatedOption[0]
    sep = separatedOption[1]
    value = separatedOption[2]
    
    nameNoSpace = name.rstrip()
    
    # Lines like [core] are copied verbatim
    if nameNoSpace.startswith("["):
        akabeiConf.write(name)
        return
    
    if nameNoSpace in correspondences:
        if len(correspondences[nameNoSpace]) > 0:
            
            # Replaces the option name with that used by Akabei and writes again the line
            name = name.replace(nameNoSpace, correspondences[nameNoSpace])
            akabeiConf.write(name)
            akabeiConf.write(sep)
            akabeiConf.write(value)
    # If it's not present in our dictionary, we assume it's a comment
    else:
        akabeiConf.write(name)
        akabeiConf.write(sep)
        akabeiConf.write(value)

# Prints an help message
def usage():
    print "usage: " + sys.argv[0] + " [--help] [--pacman=<path to pacman.conf] [--akabei=<path to akabei.conf>]"
    print
    print "Options:"
    print "    -h, --help           Prints this help message"
    print "    -p, --pacman <file>  Reads pacman configuration from <file> (default: /etc/pacman.conf)"
    print "    -a, --akabei <file>  Writes akabei configuration in <file> (default: /etc/akabei.conf)"

# Start point
if __name__ == "__main__":
    pacmanFile = "/etc/pacman.conf"
    akabeiFile = "/etc/akabei.conf"
    opts = None
    
    try:
        opts = getopt.getopt(sys.argv[1:], "hp:a:", ["help", "pacman=", "akabei="])
    except getopt.GetoptError, error:
        print str(error)
        usage()
        sys.exit(-1)
    
    for opt, arg in opts[0]:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-p", "--pacman"):
            pacmanFile = arg
        elif opt in ("-a", "--akabei"):
            akabeiFile = arg
    
    pacmanConf = None
    akabeiConf = None
    
    try:
        pacmanConf = open(pacmanFile, "r")
        akabeiConf = open(akabeiFile, "w")
    except IOError, error:
        print str(error)
        sys.exit(-1)
    
    for pacmanLine in pacmanConf.readlines():
        if pacmanLine == "\n":
            akabeiConf.write("\n")
            continue
        
        pacmanLine = pacmanLine.replace("pacman", "akabei")
        
        # Skips the trailing whitespaces
        ch = 0
        while pacmanLine[ch] == ' ':
            ch += 1
        
        separatedLine = pacmanLine.partition('#')
        
        # These are all the normal lines in the form name=value
        parseOptionLine(separatedLine[0], akabeiConf)
        
        # The separator if present
        akabeiConf.write(separatedLine[1])
        
        # After the separator there could be some commented options.
        # If they're not supported by akabei or changed name
        # they are be deleted/modified to avoid confusion for the reader
        parseOptionLine(separatedLine[2], akabeiConf)

    print "[**] Your akabei configuration was created successfully. Exiting..."
    pacmanConf.close()
    akabeiConf.close()