コード例 #1
0
ファイル: bootloader.py プロジェクト: blue-devil/kuller
class grubParser:
    def __init__(self, _dir, write=False, timeout=-1):
        self.dir = _dir
        self.write = write
        self.grub_conf = os.path.join(_dir, "grub.conf")
        
        self.lock = FileLock("%s/.grub.lock" % _dir)
        try:
            self.lock.lock(write, timeout)
        except IOError:
            fail(FAIL_TIMEOUT)
        
        # Fail if grub is not installed
        if os.path.exists(self.grub_conf):
            self.config = grubConf()
            self.config.parseConf(self.grub_conf)
        else:
            self.fail(FAIL_NOGRUB)
    
    def fail(self, msg):
        self.lock.unlock()
        fail(msg)
    
    def release(self, save=True):
        if save and self.write:
            self.config.write(self.grub_conf)
        self.lock.unlock()
コード例 #2
0
class grubParser:
    def __init__(self, _dir, write=False, timeout=-1):
        self.dir = _dir
        self.write = write
        self.grub_conf = os.path.join(_dir, "grub.conf")

        self.lock = FileLock("%s/.grub.lock" % _dir)
        try:
            self.lock.lock(write, timeout)
        except IOError:
            fail(FAIL_TIMEOUT)

        # Fail if grub is not installed
        if os.path.exists(self.grub_conf):
            self.config = grubConf()
            self.config.parseConf(self.grub_conf)
        else:
            self.fail(FAIL_NOGRUB)

    def fail(self, msg):
        self.lock.unlock()
        fail(msg)

    def release(self, save=True):
        if save and self.write:
            self.config.write(self.grub_conf)
        self.lock.unlock()
コード例 #3
0
ファイル: backend.py プロジェクト: andytengca/Packages
class Lock:
    def __init__(self, _file, shared=False):
        lockfile = os.path.join(os.path.dirname(_file), ".%s" % os.path.basename(_file))
        try:
            self.lock = FileLock(_file)
            self.lock.lock(timeout=TIMEOUT, shared=shared)
        except IOError:
            fail(FAIL_TIMEOUT)

    def release(self):
        self.lock.unlock()
コード例 #4
0
ファイル: bootmodules.py プロジェクト: blue-devil/kuller
class Lock:
    def __init__(self, _file, shared=False):
        lockfile = os.path.join(os.path.dirname(_file), ".%s" % os.path.basename(_file))
        try:
            self.lock = FileLock(_file)
            self.lock.lock(timeout=TIMEOUT, shared=shared)
        except IOError:
            fail(FAIL_TIMEOUT)
    
    def release(self):
        self.lock.unlock()
コード例 #5
0
ファイル: system.py プロジェクト: AlpyDemirok/PisiLinux
def setConf(key, value=None):
    lock = FileLock(CONF)
    lock.lock(shared=False)
    data = file(CONF).read()
    lines = data.split("\n")
    for index, line in enumerate(lines):
        try:
            _key, _value = line.split("=", 1)
        except ValueError:
            continue
        _key = _key.strip()
        if key == _key or (_key.startswith("#") and _key[1:].strip() == key):
            if value:
                lines[index] = "%s = '%s'" % (key, value)
            elif not line.startswith("#"):
                lines[index] = "# %s" % line
    file(CONF, "w").write("\n".join(lines))
    lock.unlock()
コード例 #6
0
ファイル: system.py プロジェクト: AlpyDemirok/PisiLinux
def getConf(key, default=None):
    lock = FileLock(CONF)
    lock.lock(shared=True)
    value = default
    for line in file(CONF):
        line = line.strip()
        try:
            _key, _value = line.split("=", 1)
        except ValueError:
            continue
        _key = _key.strip()
        if key == _key:
            value = _value.strip()
            if value.startswith('"') or value.startswith("'"):
                value = value[1:-1]
            break
    lock.unlock()
    return value
コード例 #7
0
ファイル: system.py プロジェクト: ayhanyalcinsoy/BuildFiles
def setConf(key, value=None):
    lock = FileLock(CONF)
    lock.lock(shared=False)
    data = file(CONF).read()
    lines = data.split("\n")
    for index, line in enumerate(lines):
        try:
            _key, _value = line.split("=", 1)
        except ValueError:
            continue
        _key = _key.strip()
        if key == _key or (_key.startswith("#") and _key[1:].strip() == key):
            if value:
                lines[index] = "%s = '%s'" % (key, value)
            elif not line.startswith("#"):
                lines[index] = "# %s" % line
    file(CONF, "w").write("\n".join(lines))
    lock.unlock()
コード例 #8
0
ファイル: system.py プロジェクト: ayhanyalcinsoy/BuildFiles
def getConf(key, default=None):
    lock = FileLock(CONF)
    lock.lock(shared=True)
    value = default
    for line in file(CONF):
        line = line.strip()
        try:
            _key, _value = line.split("=", 1)
        except ValueError:
            continue
        _key = _key.strip()
        if key == _key:
            value = _value.strip()
            if value.startswith('"') or value.startswith("'"):
                value = value[1:-1]
            break
    lock.unlock()
    return value
コード例 #9
0
ファイル: iniutils.py プロジェクト: Tayyib/uludag
class iniDB:
    def __init__(self, db_file, db_mode=0600):
        try:
            os.makedirs(os.path.dirname(db_file))
        except OSError:
            pass
        self.db_file = db_file
        self.lock_file = os.path.join(os.path.dirname(db_file), '.%s' % os.path.basename(db_file))
        if not os.path.exists(db_file):
            self.__writelock()
            file(db_file, "w").close()
            os.chmod(db_file, db_mode)
            self.__unlock()
        self.__readlock()
        self.cp = ConfigParser.ConfigParser()
        self.cp.read(db_file)
        self.__unlock()

    def __writelock(self):
        self.fl = FileLock(self.lock_file)
        self.fl.lock(shared=False)

    def __readlock(self):
        self.fl = FileLock(self.lock_file)
        self.fl.lock(shared=True)

    def __unlock(self):
        self.fl.unlock()

    def listDB(self):
        profiles = self.cp.sections()
        if "general" in profiles:
            profiles.remove("general")
        return profiles

    def getDB(self, name):
        dct = {}
        if name in self.cp.sections():
            dct = dict(self.cp.items(name))
        return dct

    def setDB(self, name, dct):
        for key, value in dct.iteritems():
            if value:
                if name not in self.cp.sections():
                    self.cp.add_section(name)
                self.cp.set(name, key, value)
            elif name in self.cp.sections():
                self.cp.remove_option(name, key)
        self.__writelock()
        fp = open(self.db_file, "w")
        self.cp.write(fp)
        fp.close()
        self.__unlock()

    def remDB(self, name):
        self.cp.remove_section(name)
        self.__writelock()
        fp = open(self.db_file, "w")
        self.cp.write(fp)
        fp.close()
        self.__unlock()
コード例 #10
0
class iniParser:
    """
        INI file parsing and manipulation class.

        ip = iniParser("my.ini", [chmod=0600, [quiet=False]])
        ip.listSections() => ["section1", "section2", ...]
        ip.getSection("section1") => {"field1": "value1", "field2": "value2"}
        ip.setSection("section1",{"field1": "value1", "field2": "value2"})
        ip.removeSection("section2")

    """

    def __init__(self, inifile, chmod=0o600, quiet=False):
        """
            Constuctor. Creates INI file if it doesn't exist and sets file mode.
        """
        self.inifile = inifile
        self.chmod = chmod
        self.quiet = quiet
        try:
            os.makedirs(os.path.dirname(inifile))
        except OSError:
            pass
        if not os.path.exists(inifile):
            self.__writeLock()
            open(inifile, "w").close()
            self.__unlock()
        os.chmod(inifile, chmod)

    def __writeLock(self):
        """
            Puts a write lock to file.
        """
        self.fl = FileLock(self.inifile)
        self.fl.lock(shared=False)

    def __readLock(self):
        """
            Puts a read lock to file.
        """
        self.fl = FileLock(self.inifile)
        self.fl.lock(shared=True)

    def __unlock(self):
        """
            Removes lock from file.
        """
        self.fl.unlock()

    def __readIni(self):
        """
            Gets content of the INI.
        """
        ini = configparser.ConfigParser()
        try:
            ini.read(self.inifile)
        except configparser.Error:
            ini = None
        return ini

    def __writeIni(self, ini):
        """
            Writes INI to file.
        """
        fp = open(self.inifile, "w")
        ini.write(fp)
        fp.close()

    def listSections(self):
        """
            Lists sections of INI file.
        """
        self.__readLock()
        ini = self.__readIni()
        self.__unlock()
        if not ini:
            if self.quiet:
                self.__fixIniFile()
                return []
            else:
                raise iniParserError("File is corrupt: %s" % self.inifile)
        return ini.sections()

    def getSection(self, section):
        """
            Returns a section of INI file.
        """
        self.__readLock()
        ini = self.__readIni()
        self.__unlock()
        if not ini:
            if self.quiet:
                self.__fixIniFile()
                return {}
            else:
                raise iniParserError("File is corrupt: %s" % self.inifile)
        if section not in ini.sections():
            return {}
        dct = {}
        if section in ini.sections():
            dct = dict(ini.items(section))
        return dct

    def setSection(self, section, dct):
        """
            Sets a section of INI file.
        """
        self.__writeLock()
        ini = self.__readIni()
        if not ini:
            self.__unlock()
            if self.quiet:
                self.__fixIniFile()
                self.setSection(section, dct)
                return
            else:
                raise iniParserError("File is corrupt: %s" % self.inifile)
        if section not in ini.sections():
            ini.add_section(section)
        for key, value in dct.items():
            if value:
                ini.set(section, key, value)
            elif section in ini.sections():
                ini.remove_option(section, key)
        self.__writeIni(ini)
        self.__unlock()

    def removeSection(self, section):
        """
            Removes a section from INI file.
        """
        self.__writeLock()
        ini = self.__readIni()
        if not ini:
            self.__unlock()
            if self.quiet:
                self.__fixIniFile()
                return
            else:
                raise iniParserError("File is corrupt: %s" % self.inifile)
        ini.remove_section(section)
        self.__writeIni(ini)
        self.__unlock()

    def __fixIniFile(self):
        """
            Cleans bogus ini file.
        """
        self.__writeLock()
        open(self.inifile, "w").write("")
        self.__unlock()
コード例 #11
0
class iniDB:
    def __init__(self, db_file, db_mode=0o600):
        try:
            os.makedirs(os.path.dirname(db_file))
        except OSError:
            pass
        self.db_file = db_file
        if not os.path.exists(db_file):
            self.__writelock()
            open(db_file, "w").close()
            os.chmod(db_file, db_mode)
            self.__unlock()
        self.__readlock()
        self.cp = configparser.ConfigParser()
        try:
            self.cp.read(db_file)
        except:
            print("Network configuration file %s is corrupt" % db_file)
        self.__unlock()

    def __writelock(self):
        self.fl = FileLock(self.db_file)
        self.fl.lock(shared=False)

    def __readlock(self):
        self.fl = FileLock(self.db_file)
        self.fl.lock(shared=True)

    def __unlock(self):
        self.fl.unlock()

    def listDB(self):
        profiles = self.cp.sections()
        if "general" in profiles:
            profiles.remove("general")
        return profiles

    def getDB(self, name):
        dct = {}
        if name in self.cp.sections():
            dct = dict(self.cp.items(name))
        return dct

    def setDB(self, name, dct):
        for key, value in dct.items():
            if value:
                if name not in self.cp.sections():
                    self.cp.add_section(name)
                self.cp.set(name, key, value)
            elif name in self.cp.sections():
                self.cp.remove_option(name, key)
        # FIXME: This is an ugly hack...
        db = iniDB(self.db_file)
        for nm in db.listDB():
            if nm == name:
                continue
            for key, value in db.getDB(nm).items():
                self.cp.set(nm, key, value)
        self.__writelock()
        fp = open(self.db_file, "w")
        self.cp.write(fp)
        fp.close()
        self.__unlock()

    def remDB(self, name):
        self.cp.remove_section(name)
        self.__writelock()
        fp = open(self.db_file, "w")
        self.cp.write(fp)
        fp.close()
        self.__unlock()