# -*- coding: utf-8 -*- # # Copyright (C) 2005 - 2008, TUBITAK/UEKAE # # 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. # # Please read the COPYING file. # # PiSi Configuration File module, obviously, is used to read from the # configuration file. Module also defines default values for # configuration parameters. # # Configuration file is located in /etc/pisi/pisi.conf by default, # having an INI like format like below. # #[general] #destinationdirectory = / #autoclean = False #bandwidth_limit = 0 # #[build] #host = i686-pc-linux-gnu #generateDebug = False #enableSandbox = False #jobs = "-j1" #CFLAGS= -mtune=generic -march=i686 -O2 -pipe -fomit-frame-pointer -fstack-protector -D_FORTIFY_SOURCE=2 #CXXFLAGS= -mtune=generic -march=i686 -O2 -pipe -fomit-frame-pointer -fstack-protector -D_FORTIFY_SOURCE=2 #LDFLAGS= -Wl,-O1 -Wl,-z,relro -Wl,--hash-style=gnu #buildno=True # necessary for generating build nos #buildhelper = None / ccache / icecream #compressionlevel = 7 #fallback = "ftp://ftp.pardus.org.tr/pub/source/2008" # #[directories] #lib_dir = /var/lib/pisi #info_dir = "/var/lib/pisi/info" #history_dir = /var/lib/pisi/history #archives_dir = /var/cache/pisi/archives #cached_packages_dir = /var/cache/pisi/packages #compiled_packages_dir = "/var/cache/pisi/packages" #index_dir = /var/cache/pisi/index #packages_dir = /var/cache/pisi/package #tmp_dir = /var/pisi #kde_dir = /usr/kde/3.5 #qt_dir = /usr/qt/3 import os import re import StringIO import ConfigParser import gettext __trans = gettext.translation('pisi', fallback=True) _ = __trans.ugettext import pisi class Error(pisi.Error): pass class GeneralDefaults: """Default values for [general] section""" destinationdirectory = "/" autoclean = False distribution = "Pardus" distribution_release = "2008" architecture = "i686" http_proxy = os.getenv("HTTP_PROXY") or None https_proxy = os.getenv("HTTPS_PROXY") or None ftp_proxy = os.getenv("FTP_PROXY") or None package_cache = False package_cache_limit = 0 bandwidth_limit = 0 class BuildDefaults: """Default values for [build] section""" host = "i686-pc-linux-gnu" jobs = "-j1" generateDebug = False enableSandbox = False cflags = "-mtune=generic -march=i686 -O2 -pipe -fomit-frame-pointer -fstack-protector -D_FORTIFY_SOURCE=2" cxxflags = "-mtune=generic -march=i686 -O2 -pipe -fomit-frame-pointer -fstack-protector -D_FORTIFY_SOURCE=2" ldflags = "-Wl,-O1 -Wl,-z,relro -Wl,--hash-style=gnu" buildno = False buildhelper = None compressionlevel = 7 fallback = "ftp://ftp.pardus.org.tr/pub/source/2008" class DirectoriesDefaults: "Default values for [directories] section" lib_dir = "/var/lib/pisi" log_dir = "/var/log" info_dir = "/var/lib/pisi/info" history_dir = "/var/lib/pisi/history" archives_dir = "/var/cache/pisi/archives" cached_packages_dir = "/var/cache/pisi/packages" compiled_packages_dir = "/var/cache/pisi/packages" debug_packages_dir = "/var/cache/pisi/packages" packages_dir = "/var/lib/pisi/package" lock_dir = "/var/lock/subsys" index_dir = "/var/lib/pisi/index" tmp_dir = "/var/pisi" kde_dir = "/usr/kde/3.5" qt_dir = "/usr/qt/3" class ConfigurationSection(object): """ConfigurationSection class defines a section in the configuration file, using defaults (above) as a fallback.""" def __init__(self, section, items=[]): self.items = items if section == "general": self.defaults = GeneralDefaults elif section == "build": self.defaults = BuildDefaults elif section == "directories": self.defaults = DirectoriesDefaults else: e = _("No section by name '%s'") % section raise Error, e self.section = section def __getattr__(self, attr): # first search for attribute in the items provided in the # configuration file. if self.items: for item in self.items: if item[0] == attr: # all values are returned as string types by ConfigParser. # evaluate "True" or "False" strings to boolean. if item[1] in ["True", "False", "None"]: return eval(item[1]) else: return item[1] # then fall back to defaults if hasattr(self.defaults, attr): return getattr(self.defaults, attr) return "" # We'll need to access configuration keys by their names as a # string. Like; ["default"]... def __getitem__(self, key): return self.__getattr__(key) class ConfigurationFile(object): """Parse and get configuration values from the configuration file""" def __init__(self, filePath): self.parser = ConfigParser.ConfigParser() self.filePath = filePath self.parser.read(self.filePath) try: generalitems = self.parser.items("general") except ConfigParser.NoSectionError: generalitems = [] self.general = ConfigurationSection("general", generalitems) try: builditems = self.parser.items("build") except ConfigParser.NoSectionError: builditems = [] self.build = ConfigurationSection("build", builditems) try: dirsitems = self.parser.items("directories") except ConfigParser.NoSectionError: dirsitems = [] self.dirs = ConfigurationSection("directories", dirsitems) # get, set and write_config methods added for manipulation of pisi.conf file from Comar to solve bug #5668. # Current ConfigParser does not keep the comments and white spaces, which we do not want for pisi.conf. There # are patches floating in the python sourceforge to add this feature. The write_config code is from python # sourceforge tracker id: #1410680, modified a little to make it turn into a function. def get(self, section, option): try: return self.parser.get(section, option) except ConfigParser.NoOptionError: return None def set(self, section, option, value): self.parser.set(section, option, value) def write_config(self, add_missing=True): sections = {} current = StringIO.StringIO() replacement = [current] sect = None opt = None written = [] optcre = re.compile( r'(?P