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)
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()
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 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
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()
def __init__(self, for_read=False): self.lock = FileLock(self.lock_path) self.lock.lock(shared=for_read) self.users = {} self.users_by_name = {} self.groups = {} self.groups_by_name = {} for line in file(self.passwd_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") user = User() user.name = parts[0] user.uid = int(parts[2]) user.gid = int(parts[3]) user.realname = parts[4] user.homedir = parts[5] user.shell = parts[6] self.users[user.uid] = user self.users_by_name[user.name] = user for line in file(self.shadow_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") if self.users_by_name.has_key(parts[0]): user = self.users_by_name[parts[0]] user.password = parts[1] user.pwrest = parts[2:] for line in file(self.group_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") group = Group() group.name = parts[0] group.gid = int(parts[2]) group.members = parts[3].split(",") if "" in group.members: group.members.remove("") self.groups[group.gid] = group self.groups_by_name[group.name] = group
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()
def __readlock(self): self.fl = FileLock(self.lock_file) self.fl.lock(shared=True)
class Database: passwd_path = "/etc/passwd" shadow_path = "/etc/shadow" group_path = "/etc/group" lock_path = "/etc/.pwd.lock" def __init__(self, for_read=False): self.lock = FileLock(self.lock_path) self.lock.lock(shared=for_read) self.users = {} self.users_by_name = {} self.groups = {} self.groups_by_name = {} for line in file(self.passwd_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") user = User() user.name = parts[0] user.uid = int(parts[2]) user.gid = int(parts[3]) user.realname = parts[4] user.homedir = parts[5] user.shell = parts[6] self.users[user.uid] = user self.users_by_name[user.name] = user for line in file(self.shadow_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") if self.users_by_name.has_key(parts[0]): user = self.users_by_name[parts[0]] user.password = parts[1] user.pwrest = parts[2:] for line in file(self.group_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") group = Group() group.name = parts[0] group.gid = int(parts[2]) group.members = parts[3].split(",") if "" in group.members: group.members.remove("") self.groups[group.gid] = group self.groups_by_name[group.name] = group def sync(self): lines = [] keys = self.users.keys() keys.sort() for uid in keys: user = self.users[uid] lines.append("%s:x:%d:%d:%s:%s:%s\n" % ( user.name, uid, user.gid, user.realname, user.homedir, user.shell )) f = file(self.passwd_path, "w") f.writelines(lines) f.close() lines = [] keys = self.users.keys() keys.sort() for uid in keys: user = self.users[uid] if user.password: lines.append("%s:%s:%s\n" % ( user.name, user.password, ":".join(user.pwrest) )) else: lines.append("%s::13094:0:99999:7:::\n" % user.name) f = file(self.shadow_path, "w") f.writelines(lines) f.close() lines = [] keys = self.groups.keys() keys.sort() for gid in keys: group = self.groups[gid] lines.append("%s:x:%s:%s\n" % (group.name, gid, ",".join(group.members))) f = file(self.group_path, "w") f.writelines(lines) f.close() def set_groups(self, name, grouplist): for gid in self.groups.keys(): g = self.groups[gid] if name in g.members: if not g.name in grouplist: g.members.remove(name) else: if g.name in grouplist: g.members.append(name) def next_uid(self): for i in range(uid_minimum, uid_maximum): if not self.users.has_key(i): return i def next_gid(self): for i in range(uid_minimum, uid_maximum): if not self.groups.has_key(i): return i
def __readLock(self): """ Puts a read lock to file. """ self.fl = FileLock(self.inifile) self.fl.lock(shared=True)
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()
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)
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()
class Database: passwd_path = "/etc/passwd" shadow_path = "/etc/shadow" group_path = "/etc/group" lock_path = "/etc/.pwd.lock" def __init__(self, for_read=False): self.lock = FileLock(self.lock_path) self.lock.lock(shared=for_read) self.users = {} self.users_by_name = {} self.groups = {} self.groups_by_name = {} for line in file(self.passwd_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") user = User() user.name = parts[0] user.uid = int(parts[2]) user.gid = int(parts[3]) user.realname = parts[4] user.homedir = parts[5] user.shell = parts[6] self.users[user.uid] = user self.users_by_name[user.name] = user for line in file(self.shadow_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") if self.users_by_name.has_key(parts[0]): user = self.users_by_name[parts[0]] user.password = parts[1] user.pwrest = parts[2:] for line in file(self.group_path): if line != "" and line != "\n": parts = line.rstrip("\n").split(":") group = Group() group.name = parts[0] group.gid = int(parts[2]) group.members = parts[3].split(",") if "" in group.members: group.members.remove("") self.groups[group.gid] = group self.groups_by_name[group.name] = group def sync(self): lines = [] keys = self.users.keys() keys.sort() for uid in keys: user = self.users[uid] lines.append("%s:x:%d:%d:%s:%s:%s\n" % ( user.name, uid, user.gid, user.realname, user.homedir, user.shell )) f = file(self.passwd_path, "w") f.writelines(lines) f.close() lines = [] keys = self.users.keys() keys.sort() for uid in keys: user = self.users[uid] if user.password: lines.append("%s:%s:%s\n" % ( user.name, user.password, ":".join(user.pwrest) )) f = file(self.shadow_path, "w") f.writelines(lines) f.close() lines = [] keys = self.groups.keys() keys.sort() for gid in keys: group = self.groups[gid] lines.append("%s:x:%s:%s\n" % (group.name, gid, ",".join(group.members))) f = file(self.group_path, "w") f.writelines(lines) f.close() def set_groups(self, name, grouplist): for gid in self.groups.keys(): g = self.groups[gid] if name in g.members: if not g.name in grouplist: g.members.remove(name) else: if g.name in grouplist: g.members.append(name) def next_uid(self): for i in range(uid_minimum, uid_maximum): if not self.users.has_key(i): return i def next_gid(self): for i in range(uid_minimum, uid_maximum): if not self.groups.has_key(i): return i
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()
def __writeLock(self): """ Puts a write lock to file. """ self.fl = FileLock(self.inifile) self.fl.lock(shared=False)
def __writelock(self): self.fl = FileLock(self.lock_file) self.fl.lock(shared=False)