def parse_config(configs): """ :type configs list :rtype: ConfigParser """ conf = ConfigParser() all_configs = [] while len(configs) > 0: all_configs += configs files = [] for mask in configs: for f in glob.glob(mask): if os.path.isfile(f): files.append(f) conf.read(files) configs = [] if conf.has_option(DEFAULT_SECTION, "include"): configs = list(set(re.split(r'\s+', conf.get(DEFAULT_SECTION, "include"))) - set(all_configs)) for section in conf.sections(): for k, v in conf.items(DEFAULT_SECTION): if not conf.has_option(section, k): conf.set(section, k, v) for k, v in conf.items(section): v = re.sub(r'^\s*"|"\s*$', '', v) # remove quotes conf.set(section, k, v) conf.remove_section(DEFAULT_SECTION) if not conf.sections(): usage("No sections found in config files " + ", ".join(all_configs)) return conf
def fixConfig(): conf = ConfigParser() conf_name = ['conf.ini'] for i in range(1, 40): conf_name.append('conf' + str(i) + '.ini') for each in conf_name: conf.read(each) sections = conf.sections() #if each == 'conf1.ini': # pdb.set_trace() conf.remove_option('slave', 'parsers') conf.remove_option('slave', 'sources') sections = conf.sections() sections.remove('slave') sections.remove('proxy') sections.remove('master') for ele in sections: try: res = dict(conf.items(ele)) conf.remove_section(ele) if res['source'] == 'britishairRound': res['source'] = 'britishairRoundFlight' conf.add_section(res['source']) conf.set(res['source'], 'class_name', res['class_name']) conf.set(res['source'], 'file_path', res['file_path'].split('../')[1]) conf.write(open(each, 'w')) except Exception, e: print str(e) continue
def rm(args): p = ConfigParser() vm_cfg = args.name + '.conf' p.read(vm_cfg) for section in p.sections(): if section.startswith('disk'): LOG.debug('Delete VM %s', args.name) try: f = p.get(section, 'file') LOG.info('Delete VM disk %s', f) os.unlink(f) except OSError as e: raise RuntimeError(e) LOG.debug('Delete VM config %s', vm_cfg) try: os.unlink(vm_cfg) except OSError: LOG.warn('%s is not exist' % vm_cfg) LOG.debug('Delete VM item from config file') p = ConfigParser() p.read(vman_config) p.remove_section(args.name) p.write(open(vman_config, 'w'))
def remove_section(self,section): if self.has_section(section): ConfigParser.remove_section(self, section) def clear_all(self): for section in self.sections(): self.remove_section(section)
def parse_config(configs): """ :type configs list :rtype: ConfigParser """ conf = ConfigParser() all_configs = [] while len(configs) > 0: all_configs += configs files = [] for mask in configs: for f in glob.glob(mask): if os.path.isfile(f): files.append(f) conf.read(files) configs = [] if conf.has_option(DEFAULT_SECTION, "include"): configs = list( set(re.split(r'\s+', conf.get(DEFAULT_SECTION, "include"))) - set(all_configs)) for section in conf.sections(): for k, v in conf.items(DEFAULT_SECTION): if not conf.has_option(section, k): conf.set(section, k, v) for k, v in conf.items(section): v = re.sub(r'^\s*"|"\s*$', '', v) # remove quotes conf.set(section, k, v) conf.remove_section(DEFAULT_SECTION) if not conf.sections(): usage("No sections found in config files " + ", ".join(all_configs)) return conf
class AntiLogoConfig: configfile = "/etc/enigma2/AntiLogo.conf" def __init__(self): self.configparser = ConfigParser() self.configparser.read(self.configfile) def setLastPreset(self,name): self.configparser.set(DEFAULTSECT, "lastpreset",name) self.writeConfig() def getLastPreset(self): last = self.configparser.get(DEFAULTSECT, "lastpreset") return self.getPreset(last) def getPresets(self): presets = [] sections = self.configparser.sections() for section in sections: presets.append(section) return presets def getPreset(self,name): if self.configparser.has_section(name) is True: print "loading preset ",name l = {} l["x"] = int(self.configparser.get(name, "x")) l["y"] = int(self.configparser.get(name, "y")) l["width"] = int(self.configparser.get(name, "width")) l["height"] = int(self.configparser.get(name, "height")) self.setLastPreset(name) return l else: print "couldn't find preset", name return False def setPreset(self,name,size,position): try: self.configparser.add_section(name) self.configparser.set(name, "x", position[0]) self.configparser.set(name, "y", position[1]) self.configparser.set(name, "width", size[0]) self.configparser.set(name, "height", size[1]) self.configparser.set(DEFAULTSECT, "lastpreset",name) self.writeConfig() return True except DuplicateSectionError: self.deletePreset(name) self.setPreset(name, size, position) def deletePreset(self,name): self.configparser.remove_section(name) self.writeConfig() def writeConfig(self): fp = open(self.configfile, "w") self.configparser.write(fp) fp.close()
def delMachine(self, mac): c = Cfg() c.read( self.__configLTSPFile ) try: c.remove_section(mac) c.write( open(self.__configLTSPFile,'w') ) return True except: return False
def delRange(self): section = 'range' c = Cfg() c.read(self.__configLTSPFile) ret = False if c.has_section( section ): c.remove_section( section ) c.write( open(self.__configLTSPFile,'w') ) ret = True del c return ret
class Database(object): def __init__(self, passphrase): self.dirty = False self.passphrase = passphrase def __enter__(self): if not os.path.exists(DATABASE) and not os.path.exists(DATABASE_UNENC): print "Database does not exist. Creating it..." fh = open(DATABASE_UNENC, 'w+') fh.close() self.dirty = True elif os.path.exists(DATABASE_UNENC): print "Database seems unencrypted. Attempting to encrypt..." call(['gpg', '-q', '--no-mdc-warning', '--passphrase', self.passphrase, '-c', DATABASE_UNENC]) else: if not call(['gpg', '-q', '--no-mdc-warning', '--passphrase', self.passphrase, DATABASE]) == 0: sys.exit(1) self.cp = ConfigParser() self.cp.read(DATABASE_UNENC) return self def __exit__(self, *args): if self.dirty: self.cp.write(open(DATABASE_UNENC, 'w')) if os.path.exists(DATABASE): os.unlink(DATABASE) call(['gpg', '-q', '--no-mdc-warning', '--passphrase', self.passphrase, '-c', DATABASE_UNENC]) if os.path.exists(DATABASE_UNENC): os.unlink(DATABASE_UNENC) def set(self, section, key, value): if not self.cp.has_section(section): self.cp.add_section(section) self.cp.set(section, key, value) self.dirty = True def get(self, section): try: return self.cp.items(section) except NoSectionError: return [] def sections(self): return self.cp.sections() def delete(self, section, key): self.cp.remove_option(section, key) if not self.cp.items(section): self.cp.remove_section(section) self.dirty = True
def remove_config(arrays): autossh_home = os.getenv('AUTOSSH_ROOT') ipconfig = ConfigParser() ip_config_file = os.path.join(autossh_home, 'ip_config.ini') ipconfig.read(ip_config_file) hostconfig = ConfigParser() hostname_config_file = os.path.join(autossh_home, 'hostname_config.ini') hostconfig.read(hostname_config_file) for str in arrays: if re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', str): ipconfig.remove_section(str) with open(ip_config_file, 'w') as f: ipconfig.write(f) secs = hostconfig.sections() for h in secs: if hostconfig.get(h, 'ip') == str: hostconfig.remove_section(h) with open(hostname_config_file, 'w') as f: hostconfig.write(f) break else: continue else: _ip = hostconfig.get(str, 'ip') ipconfig.remove_section(_ip) with open(ip_config_file, 'w') as f: ipconfig.write(f) hostconfig.remove_section(str) with open(hostname_config_file, 'w') as f: hostconfig.write(f) sys.exit(1)
def dump_config(self): config = conf.possible_configs('client.ini').next() parser = ConfigParser() parser.read(config) for section in parser.sections(): if section not in ['client', 'servers']: parser.remove_section(section) for server in self.serverinfos: server.dump(parser) with open(config, 'w') as fp: parser.write(fp)
def delete_section(source): config_dir = '/home/workspace/spider/SpiderClient/conf/' filelist = os.listdir(config_dir) from ConfigParser import ConfigParser for file in filelist: print file config = ConfigParser() config.read(config_dir + '/' + file) config.remove_section(source) with open(config_dir + '/' + file, "w+") as config_file: config.write(config_file)
def delFiltroFile(self, ip, ALL=False): if ALL: open('/etc/netcontrol/controle_banda/filtros', 'w').write('') return True arquivo = ConfigParser() arquivo.read('/etc/netcontrol/controle_banda/filtros') if arquivo.has_section(ip): arquivo.remove_section(ip) arquivo.write(open('/etc/netcontrol/controle_banda/filtros', 'w')) del arquivo return True else: del arquivo return False
def delFiltroFile(self,ip,ALL = False): if ALL: open('/etc/netcontrol/controle_banda/filtros','w').write('') return True arquivo = ConfigParser() arquivo.read('/etc/netcontrol/controle_banda/filtros') if arquivo.has_section(ip): arquivo.remove_section(ip) arquivo.write( open('/etc/netcontrol/controle_banda/filtros','w') ) del arquivo return True else: del arquivo return False
def delete_block(block, local): """ Delete the block passed in the local or global file :param block: The block to remove :param local: local ini file or global ini file :return: """ config = ConfigParser() config.read(get_file_path(local)) if config.has_section(block): config.remove_section(block) with open(get_file_path(local), 'w') as file_handler: config.write(file_handler)
def remove_watch(self, name): """ Remove a watch from the configuration file. """ try: cfgpr = ConfigParser() cfgpr.read(self.file_name) cfgpr.remove_section(name) except: self.specto.logger.log(_("There was an error writing to %s") % self.file_name, "critical", self.__class__) try: f = open(self.file_name, "w") cfgpr.write(f) except IOError: self.specto.logger.log(_("There was an error writing to %s") % self.file_name, "critical", self.__class__) finally: f.close()
def user_configure_ceph(): do_local_cmd("sed -i 's/cephx/none/g' ceph.conf") do_local_cmd("sed -e '1d' " + conf_ceph_file + " >> ceph.conf") conf = ConfigParser() conf.read('ceph.conf') # Remove our customer sections conf.remove_section("host-specific") # Get RGW host list from deploy.conf and add rgw_host to client section in ceph.conf if config.has_option("host-specific", "rgw_hosts"): hosts = config.get("host-specific", "rgw_hosts") if hosts: conf.set("client", "rgw_host", hosts) with open('ceph.conf', 'wb') as cfgfile: conf.write(cfgfile) return
def remove_section(section): path = CONFIGURATION_PATH path = os.path.expanduser(path) if not os.path.exists(path): raise Exception("File '{}' does not exist.".format(path)) parser = ConfigParser() parser.read(path) if not parser.has_section(section): raise Exception("Section '{}' does not exist.".format(section)) parser.remove_section(section) f = open(path, 'wb') parser.write(f) f.close() return
def delConfig(self, nome, ip,tipo =None): #self.decrementaRegras() ret = True idem = "%s%s"%( ip.replace('.','_').replace('/','_'), nome.replace( ' ','_' ) ) open('/tmp/teste','w').write( idem ) self.gerShaper.delRegra( idem ) self.arqCnf = tipo == "m" and self.arqMaquina or self.arqRede arquivo = ConfigParser() arquivo.read( self.arqCnf ) if arquivo.has_section(nome): arquivo.remove_section(nome) arquivo.write( open(self.arqCnf,'w') ) del arquivo ret = False #self.gerShaper.compilaRegras() return ret
class Profile(object): def __init__(self): self.__config = ConfigParser() self.__fileName = join( dirname(__file__.split('rexploit')[0]), join("rexploit", "data", "config", "profiles.ini")) def getAll(self): profiles = {} for section in self.__config.sections(): for (key, value) in self.__config.items(section): profiles[section] = value return profiles def getCommand(self, name): try: return self.__config.get(str(name), "command") except Error: return None def read(self): try: self.__config.read(self.__fileName) return True, None except Exception as e: return False, e def removeProfile(self, name): try: self.__config.remove_section(name) with open(self.__fileName, "w") as configfile: self.__config.write(configfile) return True except Error: return None def setProfile(self, name, command): if not self.__config.has_section(name): self.__config.add_section(name) self.__config.set(name, "command", command) with open(self.__fileName, "w") as configfile: self.__config.write(configfile) return True else: return None
def delConfig(self, nome, ip, tipo=None): #self.decrementaRegras() ret = True idem = "%s%s" % (ip.replace('.', '_').replace( '/', '_'), nome.replace(' ', '_')) open('/tmp/teste', 'w').write(idem) self.gerShaper.delRegra(idem) self.arqCnf = tipo == "m" and self.arqMaquina or self.arqRede arquivo = ConfigParser() arquivo.read(self.arqCnf) if arquivo.has_section(nome): arquivo.remove_section(nome) arquivo.write(open(self.arqCnf, 'w')) del arquivo ret = False #self.gerShaper.compilaRegras() return ret
def remove_watch(self, name): """ Remove a watch from the configuration file. """ self.remove_keyring(name) try: cfgpr = ConfigParser() cfgpr.read(self.file_name) cfgpr.remove_section(self.hide_brackets(name)) f = open(self.file_name, "w") cfgpr.write(open(self.file_name, "w")) except IOError: self.specto.logger.log( _("There was an error writing to the file %s") % self.file_name, "critical", "specto") return False finally: f.close()
class Profile(object): def __init__(self): self.__config = ConfigParser() self.__fileName = join(dirname(__file__.split('rexploit')[0]), join("rexploit", "data", "config", "profiles.ini")) def getAll(self): profiles = {} for section in self.__config.sections(): for (key, value) in self.__config.items(section): profiles[section] = value return profiles def getCommand(self, name): try: return self.__config.get(str(name), "command") except Error: return None def read(self): try: self.__config.read(self.__fileName) return True, None except Exception as e: return False, e def removeProfile(self, name): try: self.__config.remove_section(name) with open(self.__fileName, "w") as configfile: self.__config.write(configfile) return True except Error: return None def setProfile(self, name, command): if not self.__config.has_section(name): self.__config.add_section(name) self.__config.set(name, "command", command) with open(self.__fileName, "w") as configfile: self.__config.write(configfile) return True else: return None
def createConf(fpath, ftemplate, remove=[], keep=[], notify=False): "Create config file in fpath following the template in ftemplate" # Remove from the template the sections in "remove", and if "keep" # is used only keep those sections. # Create directory and backup if necessary. dname = os.path.dirname(fpath) if not exists(dname): os.makedirs(dname) elif exists(fpath): if not exists(join(dname, 'backups')): os.makedirs(join(dname, 'backups')) backup = join(dname, 'backups', '%s.%d' % (basename(fpath), int(time.time()))) print(yellow("* Creating backup: %s" % backup)) os.rename(fpath, backup) # Read the template configuration file. print(yellow("* Creating configuration file: %s" % fpath)) print("Please edit it to reflect the configuration of your system.\n") cf = ConfigParser() cf.optionxform = str # keep case (stackoverflow.com/questions/1611799) assert cf.read(ftemplate) != [], 'Missing file: %s' % ftemplate for section in set(remove) - set(keep): cf.remove_section(section) if keep: for section in set(cf.sections()) - set(keep): cf.remove_section(section) # Update with our guesses. if 'BUILD' in cf.sections(): for options in [guessJava(), guessMPI()]: for key in options: if key in cf.options('BUILD'): cf.set('BUILD', key, options[key]) # Collecting Protocol Usage Statistics elif 'VARIABLES' in cf.sections(): checkNotify(cf,notify) # Create the actual configuration file. cf.write(open(fpath, 'w'))
class Config(object): """ Config class to read and write options """ def __init__(self, file_name=None): self.file_name = file_name or 'config.cfg' self.config = ConfigParser() self.config.read(['config.example.cfg', self.file_name]) def set(self, section, option, value): if not self.config.has_section(section): self.config.add_section(section) self.config.set(section, option, value) def remove_option(self, section, option, value): if self.config.has_section(section): self.config.remove_option(section) if len(self.items(section)) == 0: self.remove_section(section) def remove_section(self, section): if self.config.has_section(section): self.config.remove_section(section) def get(self, section, option, raw=False): return self.config.get(section, option, raw) def items(self, section): return self.config.items(section) def has(self, section, option=None): if option: return self.config.has_option(section, option) return self.config.has_section(section) def write(self): with open(self.file_name, 'wb') as config_file: self.config.write(config_file)
class ChirpConfig: def __init__(self, basepath, name="chirp.config"): self.__basepath = basepath self.__name = name self._default_section = "global" self.__config = ConfigParser() cfg = os.path.join(basepath, name) if os.path.exists(cfg): self.__config.read(cfg) def save(self): cfg = os.path.join(self.__basepath, self.__name) cfg_file = file(cfg, "w") self.__config.write(cfg_file) cfg_file.close() def get(self, key, section, raw=False): if not self.__config.has_section(section): return None if not self.__config.has_option(section, key): return None return self.__config.get(section, key, raw=raw) def set(self, key, value, section): if not self.__config.has_section(section): self.__config.add_section(section) self.__config.set(section, key, value) def is_defined(self, key, section): return self.__config.has_option(section, key) def remove_option(self, section, key): self.__config.remove_option(section, key) if not self.__config.items(section): self.__config.remove_section(section)
class ChirpConfig(object): def __init__(self, basepath, name="chirp.config"): self.__basepath = basepath self.__name = name self._default_section = "global" self.__config = ConfigParser() cfg = os.path.join(basepath, name) if os.path.exists(cfg): self.__config.read(cfg) def save(self): cfg = os.path.join(self.__basepath, self.__name) cfg_file = open(cfg, "w") self.__config.write(cfg_file) cfg_file.close() def get(self, key, section, raw=False): if not self.__config.has_section(section): return None if not self.__config.has_option(section, key): return None return self.__config.get(section, key, raw=raw) def set(self, key, value, section): if not self.__config.has_section(section): self.__config.add_section(section) self.__config.set(section, key, value) def is_defined(self, key, section): return self.__config.has_option(section, key) def remove_option(self, section, key): self.__config.remove_option(section, key) if not self.__config.items(section): self.__config.remove_section(section)
def extract_ini_context(request): """Extract and return context from a text/ini (ConfigParser) request.""" from ConfigParser import ConfigParser, ParsingError from cStringIO import StringIO context = {} parser = ConfigParser() if not request.body: # Quick handling of empty requests. return context try: parser.readfp(StringIO('[__globals__]\n' + request.body)) except ParsingError: raise exceptions.DataParsingError('Failed to parse INI data.') for option, value in parser.items('__globals__'): context[option] = value parser.remove_section('__globals__') for section in parser.sections(): context[section] = {} for option, value in parser.items(section): context[section][option] = value return context
def execute(self, options_dict, non_option_args): if len(non_option_args) != 1: self.usage() return False repoid = non_option_args[0] repopath = self.find_repo_file(repoid) if repopath == None: rucktalk.error("Repository '%s' does not exist" % repoid) return False parser = ConfigParser() parser.read(repopath) parser.remove_section(repoid) if len(parser.sections()) == 0: os.unlink(repopath) else: parser.write(file(repopath, 'w+')) rucktalk.message("--- Successfully removed '%s' ---" % repoid)
def remove_watch(self, name): """ Remove a watch from the configuration file. """ try: cfgpr = ConfigParser() cfgpr.read(self.file_name) cfgpr.remove_section(name) except: self.specto.logger.log( _("There was an error writing to %s") % self.file_name, "critical", self.__class__) try: f = open(self.file_name, "w") cfgpr.write(f) except IOError: self.specto.logger.log( _("There was an error writing to %s") % self.file_name, "critical", self.__class__) finally: f.close()
def removeRepo(self, repo_id): """ Remove a given repository """ repos = self._get_repos('KCHREPOS0027E') if repo_id not in repos.keys(): raise NotFoundError("KCHREPOS0012E", {'repo_id': repo_id}) entry = repos.get(repo_id) parser = ConfigParser() with open(entry.repofile) as fd: parser.readfp(fd) if len(parser.sections()) == 1: os.remove(entry.repofile) return parser.remove_section(repo_id) with open(entry.repofile, "w") as fd: parser.write(fd)
def createConf(fpath, ftemplate, remove=[], keep=[], notify=False): "Create config file in fpath following the template in ftemplate" # Remove from the template the sections in "remove", and if "keep" # is used only keep those sections. # Create directory and backup if necessary. dname = os.path.dirname(fpath) if not exists(dname): os.makedirs(dname) elif exists(fpath): if not exists(join(dname, 'backups')): os.makedirs(join(dname, 'backups')) backup = join(dname, 'backups', '%s.%d' % (basename(fpath), int(time.time()))) print(yellow("* Creating backup: %s" % backup)) os.rename(fpath, backup) # Read the template configuration file. print(yellow("* Creating configuration file: %s" % fpath)) print("Please edit it to reflect the configuration of your system.\n") cf = ConfigParser() cf.optionxform = str # keep case (stackoverflow.com/questions/1611799) assert cf.read(ftemplate) != [], 'Missing file: %s' % ftemplate for section in set(remove) - set(keep): cf.remove_section(section) if keep: for section in set(cf.sections()) - set(keep): cf.remove_section(section) # Update with our guesses. if 'BUILD' in cf.sections(): for options in [guessJava(), guessMPI()]: for key in options: if key in cf.options('BUILD'): cf.set('BUILD', key, options[key]) # Collecting Protocol Usage Statistics elif 'VARIABLES' in cf.sections(): checkNotify(cf, notify) # Create the actual configuration file. cf.write(open(fpath, 'w'))
def read_cfg_file(conf_file, options): '''Read ini file that contains all the packages to be generated, ordered by dependencies.''' config = ConfigParser() config.read(conf_file) config.special = ['build-order'] if options.skip: skiplist = [name.strip() for name in options.skip.split(',')] chksectlst(skiplist, config.sections()) [config.remove_section(section) for section in skiplist] if options.only: onlylist = [name.strip() for name in options.only.split(',')] chksectlst(onlylist, config.sections()) [config.remove_section(section) for section in config.sections() \ if section not in onlylist and section not in config.special] return config
def removeRepo(self, repo_id): """ Remove a given repository """ kimchiLock.acquire() repos = self._yb().repos kimchiLock.release() if repo_id not in repos.repos.keys(): raise NotFoundError("KCHREPOS0012E", {'repo_id': repo_id}) entry = repos.getRepo(repo_id) parser = ConfigParser() with open(entry.repofile) as fd: parser.readfp(fd) if len(parser.sections()) == 1: os.remove(entry.repofile) return parser.remove_section(repo_id) with open(entry.repofile, "w") as fd: parser.write(fd)
def reset(self, name): # If custom conf file is not set then it is only read only configs if self.custom_conf_file is None: raise GconfNotConfigurable() # If a config can not be modified if name != "all" and not self._is_configurable(name): raise GconfNotConfigurable() cnf = ConfigParser() with open(self.custom_conf_file) as f: cnf.readfp(f) # Nothing to Reset, Not configured if name != "all": if not cnf.has_option("vars", name): return True # Remove option from custom conf file cnf.remove_option("vars", name) else: # Remove and add empty section, do not disturb if config file # already has any other section try: cnf.remove_section("vars") except NoSectionError: pass cnf.add_section("vars") with open(self.tmp_conf_file, "w") as fw: cnf.write(fw) os.rename(self.tmp_conf_file, self.custom_conf_file) self.reload() return True
def delete_plugin(self, name): ''' Removes the plugin from the server. ''' plug = self.get_plugin(name) # We are setting these here as I'm lazy and we will be using these a # lot in this function. pjoin = os.path.join exists = os.path.exists if exists(pjoin(self.plugin_path, plug['jar'])): os.remove(pjoin(self.plugin_path, plug['jar'])) if exists(pjoin(self.plugin_path, plug['jar'][:-4])): shutil.rmtree(pjoin(self.plugin_path, plug['jar'][:-4])) if exists(pjoin('%s_diabled' % self.plugin_path, plug['jar'])): os.remote(pjoin('%s_diabled' % self.plugin_path, plug['jar'])) conf = ConfigParser() conf.read(config.get_config_file()) conf.remove_section('Plugin: %s' % name.lower()) with open(config.get_config_file(), 'w') as cfile: conf.write(cfile) print 'Plugin %s removed and (hopefully) all associated data.' % plug['name']
def checkConf(fpath, ftemplate, remove=[], keep=[]): "Check that all the variables in the template are in the config file too" # Remove from the checks the sections in "remove", and if "keep" # is used only check those sections. # Read the config file fpath and the template ftemplate cf = ConfigParser() cf.optionxform = str # keep case (stackoverflow.com/questions/1611799) assert cf.read(fpath) != [], 'Missing file %s' % fpath ct = ConfigParser() ct.optionxform = str assert ct.read(ftemplate) != [], 'Missing file %s' % ftemplate # Keep only the sections we want to compare from the files. for section in set(remove) - set(keep): ct.remove_section(section) cf.remove_section(section) if keep: for section in set(ct.sections()) - set(keep): ct.remove_section(section) cf.remove_section(section) df = dict([(s, set(cf.options(s))) for s in cf.sections()]) dt = dict([(s, set(ct.options(s))) for s in ct.sections()]) # That funny syntax to create the dictionaries works with old pythons. if df == dt: print(green("All the expected sections and options found in " + fpath)) else: print("Found differences between the configuration file\n %s\n" "and the template file\n %s" % (fpath, ftemplate)) sf = set(df.keys()) st = set(dt.keys()) for s in sf - st: print("Section %s exists in the configuration file but " "not in the template." % red(s)) for s in st - sf: print("Section %s exists in the template but " "not in the configuration file." % red(s)) for s in st & sf: for o in df[s] - dt[s]: print("In section %s, option %s exists in the configuration " "file but not in the template." % (red(s), red(o))) for o in dt[s] - df[s]: print("In section %s, option %s exists in the template " "but not in the configuration file." % (red(s), red(o)))
model = load_model(data, seeAlso) for statement in model.find_statements( Statement(None, rdf.type, foaf.Person)): samefriend = statement.subject # maybe they have the same uri if friend.is_resource() and samefriend.is_resource( ) and friend == samefriend: foaf2config(model, subconfig, samefriend) return for ifp in ifps: object = model.get_target(samefriend, ifp) if object and object == friend_model.get_target(friend, ifp): foaf2config(model, subconfig, samefriend) return if __name__ == "__main__": import sys, urllib config = ConfigParser() for uri in sys.argv[1:]: config.add_section(uri) foaf2config(urllib.urlopen(uri), config, section=uri) config.remove_section(uri) config.write(sys.stdout)
# Graphite historically has an install prefix set in setup.cfg. Being in a # configuration file, it's not easy to override it or unset it (for installing # graphite in a virtualenv for instance). # The prefix is now set by ``setup.py`` and *unset* if an environment variable # named ``GRAPHITE_NO_PREFIX`` is present. # While ``setup.cfg`` doesn't contain the prefix anymore, the *unset* step is # required for installations from a source tarball because running # ``python setup.py sdist`` will re-add the prefix to the tarball's # ``setup.cfg``. with open('setup.cfg', 'r') as f: orig_setup_cfg = f.read() cf = ConfigParser() cf.readfp(StringIO(orig_setup_cfg), 'setup.cfg') if os.environ.get('GRAPHITE_NO_PREFIX') or os.environ.get('READTHEDOCS'): cf.remove_section('install') else: try: cf.add_section('install') except DuplicateSectionError: pass if not cf.has_option('install', 'prefix'): cf.set('install', 'prefix', '/opt/graphite') if not cf.has_option('install', 'install-lib'): cf.set('install', 'install-lib', '%(prefix)s/webapp') with open('setup.cfg', 'w') as f: cf.write(f) if os.environ.get('USE_SETUPTOOLS'): from setuptools import setup
class CATOMEmail(threading.Thread): ''' This class read email job from ATOM ''' bOccupied = False NO_EMAIL_IN_LIST = 1 ERROR_IN_CONFIG_FILE = 2 FUNCTIONS_DISABLED = 3 def __init__(self): super(CATOMEmail, self).__init__(name='CATOMEmail') self.run_flag=True self.bEnable = False self.email_config_file='conf/email.conf' self.configOutEmail = ConfigParser() self.configEmail=ConfigParser() self.configEmail.read(self.email_config_file) self.email_to=self.configEmail.get('recipient', 'ATOM_notification') self.email_from=self.configEmail.get('email', 'email_from') self.email_cc=self.configEmail.get('email', 'default_cc') self.bAddInProgress = False self.lockEmailList = threading.Lock() def run(self): library.Library.WriteProcessLog('Email worker starts') while(1): self.lockEmailList.acquire() self.SendEmail() time.sleep(1) if self.run_flag is not True: while(self.SendEmail() == 0): pass library.Library.WriteProcessLog('Email worker ends') break else: self.lockEmailList.release() return 0 def SendEmail(self): if self.bEnable == False: return CATOMEmail.FUNCTIONS_DISABLED if os.path.isfile('email\\out_box\\outmail.conf') == True: self.configOutEmail.read('email\\out_box\\outmail.conf') else: return CATOMEmail.ERROR_IN_CONFIG_FILE if self.configOutEmail.has_section('GENERAL') == False: return CATOMEmail.ERROR_IN_CONFIG_FILE intTotalNumber = self.configOutEmail.getint("GENERAL", "total_number") intSendNumber = self.configOutEmail.getint("GENERAL", "send_number") intAddNumber = self.configOutEmail.getint("GENERAL", "add_number") if intTotalNumber == 0: return CATOMEmail.NO_EMAIL_IN_LIST intNumber = intSendNumber intSendNumber = intSendNumber + 1 intTotalNumber = intTotalNumber - 1 strSection = "EMAIL" + str(intNumber) strEmailFrom = self.configOutEmail.get(strSection, "from") strEmailTo = self.configOutEmail.get(strSection,"to") strEmailCc = self.configOutEmail.get(strSection, "cc") strEmailSubject = self.configOutEmail.get(strSection, "subject") strEmailBody = self.configOutEmail.get(strSection, "body") strEmailAttach = self.configOutEmail.get(strSection, "attach") for int_email_sent_retry in range(5): try: send_email(strEmailFrom, strEmailTo, strEmailCc, strEmailSubject, strEmailBody, strEmailAttach).send() except: library.Library.WriteProcessLog('[ERROR][ATOMEmail]: Failed to send email - %s' % strEmailSubject) time.sleep(1) else: library.Library.WriteProcessLog('email send. subject:%s' % strEmailSubject) self.configOutEmail.remove_section(strSection) if intTotalNumber == 0: intAddNumber = -1 intSendNumber = 0 self.configOutEmail.set("GENERAL", "add_number", str(intAddNumber)) self.configOutEmail.set("GENERAL", "send_number", str(intSendNumber)) self.configOutEmail.set("GENERAL", "total_number", str(intTotalNumber)) with open('email\\out_box\\outmail.conf','wb') as fWrite: self.configOutEmail.write(fWrite) fWrite.close() break return 0 def AddEmail(self, type_id, platform, drop_date, strTime, args=None, strAddress = None): if self.bEnable == False: return CATOMEmail.FUNCTIONS_DISABLED self.lockEmailList.acquire() # Refresh the email config (email.conf) self.configEmail=ConfigParser() self.configEmail.read(self.email_config_file) self.platform = platform self.drop_date=drop_date self.time = strTime self.strLogFolder = '\\\\10.62.34.17\\ATOM_log\\' + self.platform + '\\' + self.drop_date self.email_attach = '' self.content = '' #make body file. while 1: r=random.randrange(1000) while(os.path.isdir('email/out_box')==False): try: os.makedirs('email/out_box') except: continue break self.email_body_file='email/out_box/%s_%s_%s' % \ (platform, drop_date, self.configEmail.get('template', type_id)) if os.path.exists(self.email_body_file) is not True: break else: break template_postfix = self.configEmail.get('email', 'template_postfix') email_body_file_template='email/template/' + self.configEmail.get('template', type_id) + template_postfix fd_template=open(email_body_file_template, 'rb') self.content=fd_template.read() # self.content -> email body fd_template.close() self.content=self.content.replace('$TIME', self.time, 1) self.content=self.content.replace('$PLATFORM', self.platform) self.content=self.content.replace('$CODE_DROP_DATE', self.drop_date) ATOM_admin_email=self.configEmail.get('recipient', 'ATOM_admin_email') ATOM_admin_name=self.configEmail.get('recipient', 'ATOM_admin_name') self.content=self.content.replace('$ATOM_ADMIN_NAME', ATOM_admin_name, 1) self.content=self.content.replace('$ATOM_ADMIN_EMAIL', ATOM_admin_email, 1) self.MakeBody(type_id, args) fd=open(self.email_body_file, 'wb') fd.write(self.content) fd.close() if strAddress == None: if type_id == 'TYPE_ALARM_AUTOFLASH_FAIL': self.email_to = self.configEmail.get('recipient', 'ATOM_alert') else: self.email_to = strAddress self.AddEmailToList() self.email_to = self.configEmail.get('recipient', 'ATOM_notification') self.lockEmailList.release() return 0 def MakeBody(self, type_id, args): ''' given : self.content, type_id, args output: self.content, self.email_subject, self.email_attach ''' ATOMInfo = ConfigParser() # Refresh the email config from (email.conf) self.configEmail=ConfigParser() self.configEmail.read(self.email_config_file) if os.path.isfile('conf\\ATOM.ver') == False: #'No version information available' strVersion = '' else: ATOMInfo.read('conf\\ATOM.ver') if ATOMInfo.has_option('VERSION', 'version')==False: # 'No version information available' strVersion = '' else: strVersion = ATOMInfo.get('VERSION', 'version') if type_id=='TYPE_ALARM_FILE_NOT_READY': #subject: self.email_subject='[FW set not ready] - %s %s' % \ (self.platform, self.drop_date) #body: $FW_IMG_MISSING and $FW_IMG_PRESENT str_missing='' #string to replace $FW_IMG_MISSING str_present='' #string to replace $FW_IMG_PRESENT #args should be like this: {''fw_type':'version', bios':None, '':None, '':None} if isinstance(args, dict) is not True: return if args.has_key('bios') == False or args['bios'] == '': str_missing+='<p class=MsoNormal><o:p>BIOS</o:p></p>\n'; else: str_present+='<p class=MsoNormal><o:p>BIOS: %s</o:p></p>\n' % args['bios']; if args.has_key('post') == False or args['post'] == '': str_missing+='<p class=MsoNormal><o:p>POST</o:p></p>\n'; else: str_present+='<p class=MsoNormal><o:p>POST: %s</o:p></p>\n' % args['post']; if args.has_key('fw-bundle') == False or args['fw-bundle'] == '': str_missing+='<p class=MsoNormal><o:p>FW-BUNDLE</o:p></p>\n'; else: str_present+='<p class=MsoNormal><o:p>FW-BUNDLE: %s</o:p></p>\n' % args['fw-bundle']; if args.has_key('bmc') == False or args['bmc'] == '': str_missing+='<p class=MsoNormal><o:p>BMC</o:p></p>\n'; else: str_present+='<p class=MsoNormal><o:p>BMC: %s</o:p></p>\n' % args['bmc']; #we have to replace the whole <p></p> 'line' old_missing='<p class=MsoNormal>$FW_IMG_MISSING<o:p></o:p></p>' old_present='<p class=MsoNormal>$FW_IMG_PRESENT<o:p></o:p></p>' self.content = self.content.replace(old_missing, str_missing) self.content = self.content.replace(old_present, str_present) self.email_attach = '' elif type_id=='TYPE_ALARM_AUTOFLASH_FAIL': #subject: self.email_subject='[Autoflash FAIL] - %s %s' % \ (self.platform, self.drop_date) #body: $FAIL_LOG #test if it is a filename; if not we just post it to the email body. #if args is None or isinstance(args, str) is not True: if args is None or isinstance(args, str) is not True: # 'args is None or not string.' return if os.path.exists(args): #is a file. self.email_attach=[] self.email_attach.append(args) #30 line fd=open(args, 'rb') q=Queue() line_count=0 for line in fd: if line_count>29: q.get() q.put(line) line_count+=1 fail_log='' while(q.empty() is not True): fail_log+='<p class=MsoNormal><o:p>%s</o:p></p>\n' % q.get(); self.content=self.content.replace('$FAIL_LOG', fail_log) else: #not file; just paste it to the email body. self.content=self.content.replace('$FAIL_LOG', args) self.email_attach = '' elif type_id=='TYPE_INFO_QUAL_START': #subject: self.email_subject='[Qual Start] - %s %s' % \ (self.platform, self.drop_date) #body: $BIOS_VERSION, $POST_VERSION, $FW-BUNDLE_VERSION and $BMC_VERSION if args is None or isinstance(args, dict) is not True: return if not args.has_key('bios'): args['bios'] = 'N/A' if not args.has_key('post'): args['post'] = 'N/A' if not args.has_key('bmc'): args['bmc'] = 'N/A' if not args.has_key('fw-bundle'): args['fw-bundle'] = 'N/A' self.email_attach = '' self.content=self.content.replace('$BIOS_VERSION', args['bios']) self.content=self.content.replace('$POST_VERSION', args['post']) self.content=self.content.replace('$FW-BUNDLE_VERSION', args['fw-bundle']) self.content=self.content.replace('$BMC_VERSION', args['bmc']) elif type_id=='TYPE_INFO_NEW_CYCLE_INITIALIZED': #subject: self.email_subject='[NEW CYCLE INITIALIZED] - %s %s' % \ (self.platform, self.drop_date) #body: $BIOS_VERSION, $POST_VERSION, $FW-BUNDLE_VERSION and $BMC_VERSION if args is None or isinstance(args, dict) is not True: return if not args.has_key('bios'): args['bios'] = 'N/A' if not args.has_key('post'): args['post'] = 'N/A' if not args.has_key('bmc'): args['bmc'] = 'N/A' if not args.has_key('fw-bundle'): args['fw-bundle'] = 'N/A' self.email_attach = '' self.content=self.content.replace('$BIOS_VERSION', args['bios']) self.content=self.content.replace('$POST_VERSION', args['post']) self.content=self.content.replace('$FW-BUNDLE_VERSION', args['fw-bundle']) self.content=self.content.replace('$BMC_VERSION', args['bmc']) elif type_id == 'TYPE_NEW_RELEASE_DETECTED': #subject: self.email_subject='[New Release] - %s %s' % \ (self.platform, self.drop_date) #body: $BIOS_VERSION, $POST_VERSION, $FW-BUNDLE_VERSION and $BMC_VERSION if args is None or isinstance(args, dict) is not True: return if not args.has_key('bios'): args['bios'] = 'N/A' if not args.has_key('post'): args['post'] = 'N/A' if not args.has_key('bmc'): args['bmc'] = 'N/A' if not args.has_key('fw-bundle'): args['fw-bundle'] = 'N/A' self.email_attach = '' self.content=self.content.replace('$BIOS_VERSION', args['bios']) self.content=self.content.replace('$POST_VERSION', args['post']) self.content=self.content.replace('$FW-BUNDLE_VERSION', args['fw-bundle']) self.content=self.content.replace('$BMC_VERSION', args['bmc']) elif type_id=='TYPE_INFO_QUAL_FINISH': #subject: self.email_subject='[Qual Complete] - %s %s' % \ (self.platform, self.drop_date) #body: $NUM_PASS, $NUM_FAIL, $NUM_SKIP $START_TIME $END_TIME #attach: full report self.email_attach = '' if args is None or isinstance(args, list) is not True: return #args[0] shall be the start time(string) #args[1] shall be a list. start_time=args[0] self.content=self.content.replace('$START_TIME', start_time, 1) self.content=self.content.replace('$END_TIME', self.time, 1) bin_rev=args[2] test_result=args[1] num_total=len(test_result) num_pass=0 num_fail=0 num_skip=0 for case in test_result: if case[1] is 'PASS': num_pass+=1 elif case[1] is 'FAIL': num_fail+=1 elif case[1] is 'SKIP': num_skip+=1 else: # '[ERROR] unrecognized case result:%s' %case[1] pass self.content=self.content.replace('$NUM_PASS', str(num_pass), 1) self.content=self.content.replace('$NUM_FAIL', str(num_fail), 1) self.content=self.content.replace('$NUM_SKIP', str(num_skip), 1) #make report #$PLATFORM $CODE_DROP_DATE $XXX_VERSION*4 $XX_TIME*2 $NUM_xxx*3 #and a table. template_postfix=self.configEmail.get('email', 'template_postfix') report_template='email/template/%s%s' % \ (self.configEmail.get('template', 'QUAL_REPORT'), template_postfix) fd=open(report_template, 'rb') self.report='' line_template='' for line in fd: self.report+=line if line.find('$CASE_NAME')!=-1: #found, should be table template line line_template=line fd.close() self.report=self.report.replace('$PLATFORM', self.platform) self.report=self.report.replace('$CODE_DROP_DATE', self.drop_date) self.report=self.report.replace('$START_TIME', start_time, 1) self.report=self.report.replace('$END_TIME', self.time, 1) if not bin_rev.has_key('bios'): bin_rev['bios'] = 'N/A' if not bin_rev.has_key('post'): bin_rev['post'] = 'N/A' if not bin_rev.has_key('bmc'): bin_rev['bmc'] = 'N/A' if not bin_rev.has_key('fw-bundle'): bin_rev['fw-bundle'] = 'N/A' self.report=self.report.replace('$BIOS_VERSION', bin_rev['bios']) self.report=self.report.replace('$BIOS_VERSION', bin_rev['bios']) self.report=self.report.replace('$POST_VERSION', bin_rev['post']) self.report=self.report.replace('$FW-BUNDLE_VERSION', bin_rev['fw-bundle']) self.report=self.report.replace('$BMC_VERSION', bin_rev['bmc']) self.report=self.report.replace('$NUM_TOTAL', str(num_total), 1) self.report=self.report.replace('$NUM_PASS', str(num_pass), 1) self.report=self.report.replace('$NUM_FAIL', str(num_fail), 1) self.report=self.report.replace('$NUM_SKIP', str(num_skip), 1) self.report = self.report.replace('$LOGFOLDER', self.strLogFolder) #make the table tableMail = '' # table in mail body should only include failed test cases tableReport = '' # table in report should include all the test cases configCase = ConfigParser() configCase.read('conf\\case.conf') strLinkGeneral = "" strLinkIndividual = "" if configCase.has_section('HYPERLINK') == True: if configCase.has_option('HYPERLINK', 'base'): strLinkGeneral = configCase.get('HYPERLINK', 'base').strip() if configCase.has_option('HYPERLINK', 'individual'): strLinkIndividual = configCase.get('HYPERLINK', 'individual').strip() for case in test_result: strCaseHyperLink = "" tmp=line_template tmp=tmp.replace('$CASE_NAME', case[0]) if configCase.has_section('CASEID') == False or configCase.has_option('CASEID', case[0]) == False: strCaseHyperLink = strLinkGeneral else: if strLinkIndividual == "": strCaseHyperLink = "" else: strCaseHyperLink = strLinkIndividual + configCase.get('CASEID', case[0]).strip() tmp=tmp.replace('$CASEHYPERLINK', strCaseHyperLink) tmp=tmp.replace('$CASE_RESULT', case[1]) tmp=tmp.replace('$CASE_NOTE', case[2]) if case[1]=='FAIL': tmp=tmp.replace('$COLOR', 'red') elif case[1]=='PASS': tmp=tmp.replace('$COLOR', 'green') elif case[1]=='SKIP': tmp=tmp.replace('$COLOR', 'blue') # only show cases whose result is not 'PASS' tableReport += tmp if case[1] is not 'PASS': tableMail += tmp self.report=self.report.replace(line_template, tableReport, 1) self.content=self.content.replace('$CASE_TABLE', tableMail, 1) while 1: r=random.randrange(1000) report_file='email/out_box/%s_%s_QUAL_REPORT.html' % \ (self.platform, self.drop_date) if os.path.exists(report_file) is not True: break else: break self.report = self.report.replace('$VERSION', strVersion) fd=open(report_file, 'wb') fd.write(self.report) fd.close() self.email_attach=report_file ## self.strPlatform.lower() + '_' + self.strTestingFolder.lower() + '.zip' strLogFileName = self.platform.lower() + '_' + self.drop_date.lower() + '.zip' if os.path.isfile('logs\\' + self.platform + '\\' + self.drop_date + '\\' + strLogFileName)==True: self.email_attach += ' ' + 'logs\\' + self.platform + '\\' + self.drop_date + '\\' + strLogFileName self.content = self.content.replace('$VERSION', strVersion) def AddEmailToList(self): configEmail = ConfigParser() intTotalNumber = 0 intAddNumber = -1 intSendNumber = -1 intNumber = -1 while(os.path.isdir('email\\out_box') == False): try: os.makedirs('email\\out_box') except: continue break while(os.path.isfile('email\\out_box\\outmail.conf') == False): try: file('email\\out_box\\outmail.conf', 'a').close except: pass break configEmail.read('email\\out_box\\outmail.conf') if configEmail.has_section("GENERAL") == True: intTotalNumber = configEmail.getint("GENERAL", "total_number") intSendNumber = configEmail.getint("GENERAL", "send_number") intAddNumber = configEmail.getint("GENERAL", "add_number") intAddNumber += 1 intNumber = intAddNumber intTotalNumber += 1 else: intNumber = 0 intTotalNumber = 1 intAddNumber = intNumber intSendNumber = intNumber configEmail.add_section('GENERAL') strSectionAdd = "EMAIL" + str(intNumber) configEmail.add_section(strSectionAdd) configEmail.set(strSectionAdd, "from", self.email_from) configEmail.set(strSectionAdd, "to" , self.email_to) configEmail.set(strSectionAdd, "cc" , self.email_cc) configEmail.set(strSectionAdd, "subject", self.email_subject) configEmail.set(strSectionAdd, "body", self.email_body_file) configEmail.set(strSectionAdd, "attach", self.email_attach) configEmail.set("GENERAL", "add_number", str(intAddNumber)) configEmail.set("GENERAL", "send_number", str(intSendNumber)) configEmail.set("GENERAL", "total_number", str(intTotalNumber)) with open('email\\out_box\\outmail.conf','wb') as fWrite: configEmail.write(fWrite) fWrite.close() return 0 def quit(self): self.run_flag=False def __del__(self): self.quit()
def _in_process_setup_ring(swift_conf, conf_src_dir, testdir): """ If SWIFT_TEST_POLICY is set: - look in swift.conf file for specified policy - move this to be policy-0 but preserving its options - copy its ring file to test dir, changing its devices to suit in process testing, and renaming it to suit policy-0 Otherwise, create a default ring file. """ conf = ConfigParser() conf.read(swift_conf) sp_prefix = 'storage-policy:' try: # policy index 0 will be created if no policy exists in conf policies = parse_storage_policies(conf) except PolicyError as e: raise InProcessException(e) # clear all policies from test swift.conf before adding test policy back for policy in policies: conf.remove_section(sp_prefix + str(policy.idx)) policy_specified = os.environ.get('SWIFT_TEST_POLICY') if policy_specified: policy_to_test = policies.get_by_name(policy_specified) if policy_to_test is None: raise InProcessException('Failed to find policy name "%s"' % policy_specified) _info('Using specified policy %s' % policy_to_test.name) else: policy_to_test = policies.default _info('Defaulting to policy %s' % policy_to_test.name) # make policy_to_test be policy index 0 and default for the test config sp_zero_section = sp_prefix + '0' conf.add_section(sp_zero_section) for (k, v) in policy_to_test.get_info(config=True).items(): conf.set(sp_zero_section, k, v) conf.set(sp_zero_section, 'default', True) with open(swift_conf, 'w') as fp: conf.write(fp) # look for a source ring file ring_file_src = ring_file_test = 'object.ring.gz' if policy_to_test.idx: ring_file_src = 'object-%s.ring.gz' % policy_to_test.idx try: ring_file_src = _in_process_find_conf_file(conf_src_dir, ring_file_src, use_sample=False) except InProcessException as e: if policy_specified: raise InProcessException('Failed to find ring file %s' % ring_file_src) ring_file_src = None ring_file_test = os.path.join(testdir, ring_file_test) if ring_file_src: # copy source ring file to a policy-0 test ring file, re-homing servers _info('Using source ring file %s' % ring_file_src) ring_data = ring.RingData.load(ring_file_src) obj_sockets = [] for dev in ring_data.devs: device = 'sd%c1' % chr(len(obj_sockets) + ord('a')) utils.mkdirs(os.path.join(_testdir, 'sda1')) utils.mkdirs(os.path.join(_testdir, 'sda1', 'tmp')) obj_socket = eventlet.listen(('localhost', 0)) obj_sockets.append(obj_socket) dev['port'] = obj_socket.getsockname()[1] dev['ip'] = '127.0.0.1' dev['device'] = device dev['replication_port'] = dev['port'] dev['replication_ip'] = dev['ip'] ring_data.save(ring_file_test) else: # make default test ring, 2 replicas, 4 partitions, 2 devices _info('No source object ring file, creating 2rep/4part/2dev ring') obj_sockets = [eventlet.listen(('localhost', 0)) for _ in (0, 1)] ring_data = ring.RingData( [[0, 1, 0, 1], [1, 0, 1, 0]], [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', 'port': obj_sockets[0].getsockname()[1]}, {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', 'port': obj_sockets[1].getsockname()[1]}], 30) with closing(GzipFile(ring_file_test, 'wb')) as f: pickle.dump(ring_data, f) for dev in ring_data.devs: _debug('Ring file dev: %s' % dev) return obj_sockets
# graphite in a virtualenv for instance). # The prefix is now set by ``setup.py`` and *unset* if an environment variable # named ``GRAPHITE_NO_PREFIX`` is present. # While ``setup.cfg`` doesn't contain the prefix anymore, the *unset* step is # required for installations from a source tarball because running # ``python setup.py sdist`` will re-add the prefix to the tarball's # ``setup.cfg``. cf = ConfigParser() with open('setup.cfg', 'r') as f: orig_setup_cfg = f.read() f.seek(0) cf.readfp(f, 'setup.cfg') if os.environ.get('GRAPHITE_NO_PREFIX'): cf.remove_section('install') else: print('#' * 80) print('') print('Carbon\'s default installation prefix is "/opt/graphite".') print('') print('To install Carbon in the Python\'s default location run:') print('$ GRAPHITE_NO_PREFIX=True python setup.py install') print('') print('#' * 80) try: cf.add_section('install') except DuplicateSectionError: pass if not cf.has_option('install', 'prefix'): cf.set('install', 'prefix', '/opt/graphite')
def _in_process_setup_ring(swift_conf, conf_src_dir, testdir): """ If SWIFT_TEST_POLICY is set: - look in swift.conf file for specified policy - move this to be policy-0 but preserving its options - copy its ring file to test dir, changing its devices to suit in process testing, and renaming it to suit policy-0 Otherwise, create a default ring file. """ conf = ConfigParser() conf.read(swift_conf) sp_prefix = 'storage-policy:' try: # policy index 0 will be created if no policy exists in conf policies = parse_storage_policies(conf) except PolicyError as e: raise InProcessException(e) # clear all policies from test swift.conf before adding test policy back for policy in policies: conf.remove_section(sp_prefix + str(policy.idx)) policy_specified = os.environ.get('SWIFT_TEST_POLICY') if policy_specified: policy_to_test = policies.get_by_name(policy_specified) if policy_to_test is None: raise InProcessException('Failed to find policy name "%s"' % policy_specified) _info('Using specified policy %s' % policy_to_test.name) else: policy_to_test = policies.default _info('Defaulting to policy %s' % policy_to_test.name) # make policy_to_test be policy index 0 and default for the test config sp_zero_section = sp_prefix + '0' conf.add_section(sp_zero_section) for (k, v) in policy_to_test.get_options().items(): conf.set(sp_zero_section, k, v) conf.set(sp_zero_section, 'default', True) with open(swift_conf, 'w') as fp: conf.write(fp) # look for a source ring file ring_file_src = ring_file_test = 'object.ring.gz' if policy_to_test.idx: ring_file_src = 'object-%s.ring.gz' % policy_to_test.idx try: ring_file_src = _in_process_find_conf_file(conf_src_dir, ring_file_src, use_sample=False) except InProcessException as e: if policy_specified: raise InProcessException('Failed to find ring file %s' % ring_file_src) ring_file_src = None ring_file_test = os.path.join(testdir, ring_file_test) if ring_file_src: # copy source ring file to a policy-0 test ring file, re-homing servers _info('Using source ring file %s' % ring_file_src) ring_data = ring.RingData.load(ring_file_src) obj_sockets = [] for dev in ring_data.devs: device = 'sd%c1' % chr(len(obj_sockets) + ord('a')) utils.mkdirs(os.path.join(_testdir, 'sda1')) utils.mkdirs(os.path.join(_testdir, 'sda1', 'tmp')) obj_socket = eventlet.listen(('localhost', 0)) obj_sockets.append(obj_socket) dev['port'] = obj_socket.getsockname()[1] dev['ip'] = '127.0.0.1' dev['device'] = device dev['replication_port'] = dev['port'] dev['replication_ip'] = dev['ip'] ring_data.save(ring_file_test) else: # make default test ring, 2 replicas, 4 partitions, 2 devices _info('No source object ring file, creating 2rep/4part/2dev ring') obj_sockets = [eventlet.listen(('localhost', 0)) for _ in (0, 1)] ring_data = ring.RingData( [[0, 1, 0, 1], [1, 0, 1, 0]], [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', 'port': obj_sockets[0].getsockname()[1]}, {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', 'port': obj_sockets[1].getsockname()[1]}], 30) with closing(GzipFile(ring_file_test, 'wb')) as f: pickle.dump(ring_data, f) for dev in ring_data.devs: _debug('Ring file dev: %s' % dev) return obj_sockets
def remove_section(self, section): section_str = to_utf8(section) ConfigParser.remove_section(self, section_str)
class Settings(Singleton): def __init__(self): # self.__isFirstStart = False self.cp = None if IsPathWritable(Constants.APP_DIR): setPath = Constants.APP_DIR else: userpath = os.path.expanduser("~") if userpath == "~": userpath = tempfile.gettempdir() setPath = userpath self.filename = os.path.join(setPath, '.%s' % Constants.APP_NAME) # if not os.path.isfile(self.filename): # self.__isFirstStart = True logging.debug("settings file: %s", self.filename) self.Load() def Load(self): if self.cp is None: self.cp = ConfigParser() if os.path.isfile(self.filename): self.cp.read(self.filename) if not self.cp.has_section("General"): self.cp.add_section("General") if not self.cp.has_section("History"): self.cp.add_section("History") if not self.cp.has_section("Profiles"): self.cp.add_section("Profiles") def Save(self): try: fd = open(self.filename, 'w') self.cp.write(fd) fd.close() except IOError: pass # def IsFirstStart(self): # return self.__isFirstStart def SetLanguage(self, lang): self.Load() self.cp.set("General", "Language", Encode(lang)) self.Save() def GetLanguage(self): self.Load() if self.cp.has_option("General", "Language"): return Decode(self.cp.get("General", "Language")) defLang = locale.getdefaultlocale()[0] if defLang is None: defLang = "" return defLang def SetFileHistory(self, fileList): self.Load() self.cp.remove_section("History") self.cp.add_section("History") for idx, filename in enumerate(fileList): if idx < 10 and os.path.exists(filename): self.cp.set("History", "%d" % idx, Encode(os.path.abspath(filename))) self.Save() def GetFileHistory(self): self.Load() fileList = [] for idx in range(10): if self.cp.has_option("History", str(idx)): filename = Decode(self.cp.get("History", str(idx))) if os.path.exists(filename) and filename not in fileList: fileList.append(filename) return fileList def SetProjectPath(self, path): self.Load() self.cp.set("General", "ProjectPath", Encode(path)) self.Save() def GetProjectPath(self): self.Load() if self.cp.has_option("General", "ProjectPath"): return Decode(self.cp.get("General", "ProjectPath")) return u"" def SetImagePath(self, path): self.Load() self.cp.set("General", "ImagePath", Encode(path)) self.Save() def GetImagePath(self): self.Load() if self.cp.has_option("General", "ImagePath"): return Decode(self.cp.get("General", "ImagePath")) return u"" def SetAudioPath(self, path): self.Load() self.cp.set("General", "AudioPath", Encode(path)) self.Save() def GetAudioPath(self): self.Load() if self.cp.has_option("General", "AudioPath"): return Decode(self.cp.get("General", "AudioPath")) return u"" def SetLastProfile(self, profile): self.Load() self.cp.set("General", "LastProfile", str(profile)) self.Save() def GetLastProfile(self): self.Load() if self.cp.has_option("General", "LastProfile"): try: return self.cp.getint("General", "LastProfile") except: pass return 3 def SetVideoType(self, typ): self.Load() self.cp.set("General", "VideoType", str(typ)) self.Save() def GetVideoType(self): self.Load() if self.cp.has_option("General", "VideoType"): try: return self.cp.getint("General", "VideoType") except: pass return 1 def SetUsedRenderer(self, renderer): self.Load() self.cp.set("General", "Renderer", str(renderer)) self.Save() def GetUsedRenderer(self): self.Load() if self.cp.has_option("General", "Renderer"): try: return self.cp.getint("General", "Renderer") except: pass return 1 def SetLastKnownVersion(self, version): self.Load() self.cp.set("General", "LastKnownVersion", version) self.Save() def GetLastKnownVersion(self): self.Load() if self.cp.has_option("General", "LastKnownVersion"): return self.cp.get("General", "LastKnownVersion") return "0.0.0" def SetRenderProperties(self, renderer, props): self.Load() if self.cp.has_section(renderer): self.cp.remove_section(renderer) self.cp.add_section(renderer) for prop, value in props.items(): self.cp.set(renderer, prop, Encode(value)) self.Save() def GetRenderProperties(self, renderer): self.Load() result = {} if not self.cp.has_section(renderer): return result for prop, value in self.cp.items(renderer): result[prop] = Decode(value) return result
def remove_section(self, section): ConfigParser.remove_section(self, section) self.__save()
class Conf: def __init__(self, filename): self._filename = filename try: self._config = ConfigParser(strict=False) except: self._config = ConfigParser() try: self._config.read(os.path.expanduser(filename)) except Exception as e: logging.error("[Conf]" + self._filename + ": " + str(e)) raise Exception("Error during loading file " + self._filename) def getSection(self, section): data={} try: if section in self._config.sections(): for name, value in self._config.items(section): data[name] = value except Exception as e: logging.error("[Conf]" + self._filename + ": " + str(e)) for key, value in data.items(): if ", " in value: data[key] = value.split(", ") return data def get(self, section, option, default=""): val = default try: val = self._config.get(section, option) except: val = default if ", " in val: return val.split(", ") return default def sections(self): return self._config.sections() def setSection(self, section, values): if not self._config.has_section(section): self._config.add_section(section) for k, v in values.items(): self._config.set(section, k, v) def setValue(self, section, option, value): if not self._config.has_section(section): self._config.add_section(section) self._config.set(section, option, value) def removeSection(self, section): if self._config.has_section(section): self._config.remove_section(section) def removeValue(self, section, option): if self._config.has_section(section) and self._config.has_option(section, option): self._config.remove_option(section, option) def save(self): with open(self._filename, 'w') as f: self._config.write(f) def getAll(self): data = {} for section in self.sections(): data[section] = self.getSection(section) return data
class Configurator: class DatabaseNotDefined(Exception): def __init__(self, db): Exception.__init__(self, "Database %s is not defined." % db) instance = None last_modified = None lock = threading.RLock() @staticmethod def Instance(): """ Callable method to get the singleton instance of this object. """ Configurator.lock.acquire() try: with FileLock(ADMINTOOLS_CONF): if os.path.exists(ADMINTOOLS_CONF): modified_time = time.localtime( os.path.getmtime(ADMINTOOLS_CONF)) else: modified_time = time.localtime() # now if Configurator.instance is None: Configurator.instance = Configurator(ADMINTOOLS_CONF) Configurator.last_modified = modified_time return Configurator.instance # XXX: If True? Once upon a time this worked. It was # accidentally disabled, it seems. Can we successfully # reenable it? if True or Configurator.last_modified != modified_time: Configurator.instance = Configurator(ADMINTOOLS_CONF) return Configurator.instance finally: Configurator.lock.release() @staticmethod def _transform_to_ip(name): # 从admintools移除 hostnames. 尝试解析hostnames到IP addresses. # 如果不成功,(i.e. 因为 DNS 此时不能工作) 单独留下. try: return util.resolve_to_one_ip(name) except StandardError as err: print( "WARNING: Unable to resolve hostname %s found in admintools.conf." % name, file=sys.stderr) return name def _move_configdict_section(self, from_sec, to_sec): """Moves all of the values from (and removes) `from_sec` to `to_sec` Any values in `to_sec`, if it exists, will remain. Values from `from_sec` will overwrite any shared keys. `to_sec` will exist after a call here, even if `from_sec` does not. """ if not self.configdict.has_section(to_sec): self.configdict.add_section(to_sec) if not self.configdict.has_section(from_sec): return for (key, value) in self.configdict.items(from_sec, raw=True): self.configdict.set(to_sec, key, value) self.configdict.remove_section(from_sec) def __load(self, filename): self.configdict.read(filename) ################################################################### # [Configuration] 进入self.config映射. # if self.configdict.has_section('Configuration'): self.config = dict(self.configdict.items('Configuration')) else: self.config = {} self.config.setdefault('controlmode', "broadcast") # last_port 保持了数据库最后使用的基本端口. # 新数据库能够被创建,使用下一个有效的端口集合(因此能够并发运行). # 对于配置接下来的逻辑将生成last_port,此配置并没有此值(例如. 更新逻辑). if "last_port" not in self.config: last_port = 0 for db in self.listDatabases(): dbport = self.configdict.get("Database:%s" % db, "port") if dbport and int(dbport) > last_port: last_port = int(dbport) if last_port == 0: last_port = 5433 self.configdict.set("Configuration", "last_port", str(last_port)) self.config['last_port'] = str(last_port) ################################################################### # [Cluster] 部分有一个单条目列出主机集合 # self.cluster = self.configdict.get('Cluster', 'hosts').split(',') self.cluster = [x.strip() for x in self.cluster if len(x.strip()) > 0] self.cluster = [self._transform_to_ip(x) for x in self.cluster] ################################################################### # [Nodes] 部分曾经是[sites]部分. # 每个数据库实例都有一个条目,带有唯一的实例名称. # 通常情况下,每个数据库都有节点的一个子集, # 如果不使用 --compat21,它们都是'node_xxxx'. # self._move_configdict_section('sites', 'Nodes') # self.sites: 节点名称到"addr,catalog,data"的映射 self.sites = self.configdict.items('Nodes') # 映射site的主机到IP地址 # 即使site值是畸形的,此部分也能够工作 def map_site_to_ip(site): tmp = site.split(',', 1) tmp[0] = self._transform_to_ip(tmp[0]) return ','.join(tmp) self.sites = [(x, map_site_to_ip(y)) for (x, y) in self.sites] for (name, data) in self.sites: self.configdict.set('Nodes', name, data) ################################################################### # [Database:*] 部分定义现有的数据库. # 迭代数据库擦掉不可用的host选项,可能包含一个主机名. # for db in self.listDatabases(): section = "Database:%s" % db if self.configdict.has_option(section, 'host'): self.configdict.remove_option(section, 'host') def __init__(self, filename): """ Constructor method - shouldn't be called. Use Configurator.Instance() instead. """ self.filename = filename self.configdict = ConfigParser() if filename is not None and os.path.exists(filename): self.__load(filename) else: self.init_defaults() # 我们进行更新操作, 对于old icky admin格式进行更好的测试, # 同时从这构建配置. self._legacy_port() # 进入有空主机值的配置文件. # 清除空值. 同时也删除重复值. self.cluster = [x.strip() for x in self.cluster if len(x.strip()) != 0] seen = set() uniq_cluster = [] for x in self.cluster: if x not in seen: seen.add(x) uniq_cluster.append(x) self.cluster = uniq_cluster self.configdict.set("Cluster", "hosts", ','.join(self.cluster)) def init_defaults(self): # 使用缺省的控制(重新)创建此文件. self.config = {} self.config['format'] = 3 self.config['install_opts'] = "" self.config[ 'default_base'] = "/home/dbadmin" #@todo - fix for install variable self.config['controlmode'] = "broadcast" self.config['controlsubnet'] = "default" self.config['spreadlog'] = "False" self.config['last_port'] = "5433" self.cluster = [] self.sites = [] def clear_section(section): if self.configdict.has_section(section): self.configdict.remove_section(section) self.configdict.add_section(section) clear_section("Configuration") for (k, v) in self.config.iteritems(): self.configdict.set("Configuration", k, v) # 清除集群. only has one value, hosts, which is empty. clear_section("Cluster") self.configdict.set("Cluster", "hosts", '') # 清除节点. Starts empty. (sites = []) clear_section("Nodes") def set_options(self, opts): """ Records the options used for install_mesadb so the user can look back at them. They should not be pulled and parsed for any reason, since they are not reliably written and any sensitive values are scrubbed. """ hide = ["-p", "--dba-user-password", "-P", "--ssh-password"] for x in hide: if x in opts: opts[opts.index(x) + 1] = "*******" val = util.shell_repr(opts) self.config['install_opts'] = val self.configdict.set("Configuration", "install_opts", val) def listDatabases(self): """ Returns a list of defined database names """ # 每个数据库定义在 [Database:foo] 部分中. # 找到每个部分匹配的部分,同时返回 'foo'. pat = re.compile("^Database:") sections = [ d.split(":")[1] for d in self.configdict.sections() if pat.match(d) ] return sections def incluster(self, hostname): """ Returns True if the given hostname is in the cluster. """ return (hostname in self.cluster) def isdefined(self, database): """ Returns True if the given database is already defined. """ return (database in self.listDatabases()) def gethosts(self): """ Returns a list of hosts in the cluster (IP address strings) """ return list(self.cluster) def getconfig(self, database): """ return a diction of properties about the initial host in the database. Other classes will use this data to bootstrap the system. """ if (not self.isdefined(database)): raise Configurator.DatabaseNotDefined(database) props = {} key = "Database:%s" % database options = self.configdict.options(key) for option in options: if option == 'nodes': props[option + "_new"] = [] for n in self.configdict.get(key, option).split(','): props[option + "_new"].append(self.getsiteconfig(n)) props[option] = self.configdict.get(key, option) props['id'] = database # upgrade from a daily bug fix # just default to never (the same as never setting it!) if not 'restartpolicy' in props.keys(): props['restartpolicy'] = 'never' return props def addsite(self, nodename, host, catalog_base, data_base): """ add a site to the configuration parameters """ # 从admintools清除主机名称 assert util.is_ip_address(host), \ "All sites must be added by IP address (%s)" % host c = catalog_base d = data_base if c.endswith("/"): c = c[:len(c) - 1] if d.endswith("/"): d = d[:len(d) - 1] data = "%s,%s,%s" % (host, c, d) self.configdict.set("Nodes", nodename, data) self.sites.append((nodename, data)) # getSite def getNode(self, node_name, or_none=False): rv = self.getNodeMap().get(node_name, None) if rv is None and not or_none: raise StandardError("Node not found: %s" % node_name) return rv # sites. def getNodeMap(self): all_nodes = {} for (node_name, data) in self.sites: data_list = data.split(',') assert len(data_list) == 3, "Error parsing Nodes section" all_nodes[node_name] = NodeInfoElement(node_name=node_name, host=data_list[0], catalog_dir=data_list[1], data_dir=data_list[2]) return all_nodes def getNodeList(self): return self.getNodeMap().values() # 使用getNode(nodename)进行替换 def getsiteconfig(self, nodename): """ return a dictionary of properties about the named node. """ props = {} p = self.configdict.get("Nodes", nodename).split(',') props['host'] = p[0] props['catalog_base'] = p[1] props['data_base'] = p[2] props['id'] = nodename return props def setrestart(self, database, restart_policy): """ set the restart policy for a given database """ key = "Database:%s" % database self.configdict.set(key, "restartpolicy", restart_policy) def setcontrolmode(self, controlmode): """ Set the mode that spread uses (broadcast or pt2pt) """ self.configdict.set('Configuration', 'controlmode', controlmode) def setcontrolsubnet(self, controlsubnet): """ Set the subnet that control (spread) traffic goes over """ self.configdict.set('Configuration', 'controlsubnet', controlsubnet) def setspreadlogging(self, path): self.configdict.set('Configuration', 'spreadlog', path) def setlargecluster(self, count): csize = len(self.gethosts()) if (count == 'off'): self.configdict.remove_option('Configuration', 'largecluster') count = 0 elif (count == 'default'): minc = min(csize, 3) count = max(int(math.sqrt(csize)), minc) else: count = max(1, min(128, csize, int(count))) self.configdict.set('Configuration', 'largecluster', str(count)) return count def save(self, nolock=False): """ simple method to force the file to write itself to disk this method must be explictly called or else changes will not be saved. """ Configurator.lock.acquire() try: if nolock: with open(self.filename, "w") as f: self.configdict.write(f) else: with FileLock(ADMINTOOLS_CONF): with open(self.filename, "w") as f: self.configdict.write(f) finally: Configurator.lock.release() from mesadb.tools import DBfunctions DBfunctions.record("Saved %s -> %s" % (str(self), ADMINTOOLS_CONF)) def addhost(self, host): """Adds a host to the cluster. Forcibly changes to an IP address.""" # 在admintools.conf中没有主机名称 assert util.is_ip_address(host), \ "All hosts must be added by IP address (%s)" % host if not self.incluster(host): self.cluster.append(host) self.configdict.set("Cluster", "hosts", ",".join(self.cluster)) return host def add(self, database, path, port, sites, restart="ksafe"): """ add a database to the configuration. only the initial 'startup' node needs to be defined -- we'll ask the catalog for the rest of the information on restarts. """ if (not self.isdefined(database)): key = "Database:%s" % database self.configdict.add_section(key) self.configdict.set(key, "restartpolicy", restart) self.configdict.set(key, "port", "%s" % port) self.configdict.set(key, "path", path) self.configdict.set(key, "nodes", ",".join(sites)) def update(self, database, path=None, port=None, nodes=None): """ update the properties of a database. you can supply a subset of the values, in which case the current values will continue to apply. """ if not self.isdefined(database): return props = self.getconfig(database) d = path p = port n = "" if nodes != None: n = ",".join(nodes) if d == None: d = props['path'] if p == None: p = props['port'] if n == None: n = props['nodes'] key = "Database:%s" % database self.configdict.set(key, "port", "%s" % p) self.configdict.set(key, "path", d) self.configdict.set(key, "nodes", n) def remove(self, name, type='database'): """ remove a database or host from the configuration. defaults to databases set type to 'host' to remove a host. WARNING: this only removes the info from the configuration file. """ if (type == 'database' and self.isdefined(name)): self.configdict.remove_section("Database:%s" % name) if (type == 'host'): self.cluster.remove(name) self.configdict.set("Cluster", "hosts", ",".join(self.cluster)) if (type == 'site'): self.configdict.remove_option("Nodes", name) def remove_node_from_db(self, database, nodename): key = "Database:%s" % database nodes = self.configdict.get(key, 'nodes').split(',') idx = 0 for n in nodes: if n == nodename: del (nodes[idx]) idx += 1 self.configdict.set(key, 'nodes', ','.join(nodes)) self.save() def __str__(self): databases = self.listDatabases() return "Configurator(clusterSize=%s, dbs=(%s))" % (len( self.gethosts()), ','.join(databases)) def _legacy_port(self): """Load the legacy configuration""" print("Upgrading admintools meta data format..", file=sys.stderr) print("scanning %s/config/users" % DBinclude.DB_DIR, file=sys.stderr) userdirs = [] try: userdirs = os.listdir("%s/config/users" % DBinclude.DB_DIR) except: pass ports = self._load_dict("%s/config/share/portinfo.dat" % DBinclude.DB_DIR) if ports is None: ports = {} self.configdict.set("Configuration", "last_port", ports.get("base", "5433")) for dir in userdirs: try: sites = self._load_dict("%s/config/users/%s/siteinfo.dat" % (DBinclude.DB_DIR, dir)) # if siteinfo.dat fails to load, forget about it. if sites is None: continue for node in sites.values(): if not self.incluster(node[1]): self.addhost(node[1]) if not self.configdict.has_section("Nodes"): self.configdict.add_section("Nodes") c = node[2] d = node[3] if c.endswith("/"): c = c[:len(c) - 1] if d.endswith("/"): d = d[:len(d) - 1] try: self.configdict.set( "Nodes", node[0], ','.join( [socket.gethostbyname_ex(node[1])[2][0], c, d])) except: self.configdict.set("Nodes", node[0], ','.join([node[1], c, d])) shutil.move(sites, "%s.old.%s" % (sites, time.time())) dbinfo = self._load_dict("%s/config/users/%s/dbinfo.dat" % (DBinclude.DB_DIR, dir)) if dbinfo is not None: databases = dbinfo['defined'].keys() for db in databases: if not self.isdefined(db): startinfo = dbinfo['startinfo'][db] try: host = socket.gethostbyname_ex( startinfo[6][0])[2][0] except: host = startinfo[6][0] path = startinfo[0] port = 5433 try: port = ports[1]['assignments'][db] except: pass policy = "never" try: if 'restartpolicy' in dbinfo.keys(): if db in dbinfo['restartpolicy'].keys(): policy = dbinfo['restartpolicy'][db] except: pass self.add(db, path, port, dbinfo['defined'][db], policy) shutil.move(dbinfo, "%s.old.%s" % (dbinfo, time.time())) except Exception as e: traceback.print_exc() print("failed to convert meta-data for %s: %s" % (dir, str(e)), file=sys.stderr) def _load_dict(self, fileName): try: with open(fileName, "r") as f: return pickle.load(f) except IOError: return None
def remove_section(self, section): res=ConfigParser.remove_section(self, section) if res and self.saveimmediately: self.save() return res
def __configuration(self, configfile ): """Read, parse and validate configuration files.""" # Create configparser and read file(s): cfg = ConfigParser() if configfile: fileread = cfg.read( self.filelist_config+(configfile,) ) else: fileread = cfg.read( self.filelist_config ) # Sort sections: if cfg.has_section( "xmpp-notify" ): cfg.add_section( "general" ) for k,v in cfg.items("xmpp-notify"): cfg.set( "general", k, v ) cfg.remove_section( "xmpp-notify" ) # Available fields, these will be read: fields = [ # field section required, type, default ("target", "general", True, cfg.get, None ), ("key", "general", False, cfg.get, None ), ("listen", "general", False, cfg.getint, 5222 ), ("bind", "general", False, cfg.get, "" ), ("log", "general", False, cfg.get, "file" ), ("logfile", "general", False, cfg.get, None ), ("loglevel", "general", False, cfg.get, "error" ), ("domain", "auth", True, cfg.get, False ), ("username", "auth", True, cfg.get, False ), ("password", "auth", True, cfg.get, False ), ("resource", "auth", False, cfg.get, "notify"), ("presence", "auth", False, cfg.getboolean, True ), ("host", "server", False, cfg.get, None ), ("port", "server", False, cfg.getint, 5222 ), ] # Get and parse fields: res = {} for field, sect, req, func, default in fields: try: try: value = func( sect, field ) except ValueError: t = "string" if func == cfg.getint: t = "integer" elif func == cfg.getboolean: t = "boolean" msg = "error: invalid configuration value: %s" \ " (should be: %s)" % (field, t) print >>sys.stderr, msg return False if func == cfg.get and len(value) < 1: raise NoOptionError(sect,field) except (NoSectionError, NoOptionError): if req: msg = "error: missing configuration field: %s"%field print >>sys.stderr, msg return False value = default if sect not in res: res[sect] = {} res[sect][field] = value # Act of certain config options like logging: general = res['general'] res['general']['loglevel'] = self.loglevels.get( general['loglevel'], LOG_ERR ) if general['log'] not in ("file", "syslog"): msg = "error: invalid configuation value: log = file | syslog" print >>sys.stderr, msg return False if general['log'] == "syslog": if not HAVE_SYSLOG: print >>sys.stderr, "error: syslog not supported" return False openlog( "xmpp-notify" ) setlogmask( LOG_UPTO(general['loglevel']) ) res['general']['logfile'] = None if general['log'] == "file" and general['logfile'] == None: for file in self.filelist_log: parent = os.path.dirname( file ) if os.access(file, os.W_OK) or os.access(parent, os.W_OK): res['general']['logfile'] = file break self.__config = res # Check whether the configfile(s) are readable to others: if sys.platform != "win32": for file in fileread: try: stats = os.stat( file ) mode = stat.S_IMODE( stats[stat.ST_MODE] ) except OSError: continue if (mode & 0077) != 0: self.log_message( "warning: config %s can be read by others!"%file, LOG_WARNING ) # Log configuration if loglevel is debug: if general['loglevel'] >= LOG_DEBUG and self.verbose: print "Configuration:" for section in sorted(res.keys()): for field in sorted(res[section].keys()): value = res[section][field] if field == "password": value = "*****" print " %s.%s = %r" % (section,field,value) print self.log_message( "configuration read: %s" % (repr(fileread)), LOG_DEBUG ) return True
class Config(object): def __init__(self, inifile=None, configs=None): self.inifile = 'data/config.ini' if inifile is None else inifile self.cfg = ConfigParser() with FileLock(self.inifile): if os.path.exists(self.inifile): self.cfg.read(self.inifile) # initialize configurations if configs is None: default_configs = { 'server': { 'ip': '*', 'port': '8888', 'forcehttps': 'off', # force use https 'lastcheckupdate': 0, 'updateinfo': '', 'sslkey': '/usr/local/inpanel/core/certificate/inpanel.key', 'sslcrt': '/usr/local/inpanel/core/certificate/inpanel.crt' }, 'auth': { 'username': '******', 'password': '', # empty password never validated 'passwordcheck': 'on', 'accesskey': '', # empty access key never validated 'accesskeyenable': 'off', }, 'runtime': { 'mode': '', # format: demo | dev | prod 'loginlock': 'off', 'loginfails': 0, 'loginlockexpire': 0, }, 'file': { 'lastdir': '/root', 'lastfile': '', }, 'time': { 'timezone': '' # format: timezone = Asia/Shanghai }, 'ecs': { 'accounts': '' }, 'inpanel': { 'Instance Name': 'Access key' } } else: default_configs = configs needupdate = False for sec, secdata in default_configs.items(): if not self.cfg.has_section(sec): self.cfg.add_section(sec) needupdate = True for opt, val in secdata.items(): if not self.cfg.has_option(sec, opt): self.cfg.set(sec, opt, val) needupdate = True # update ini file if needupdate: self.update(False) def update(self, lock=True): if lock: flock = FileLock(self.inifile) flock.acquire() try: inifp = open(self.inifile, 'w') self.cfg.write(inifp) inifp.close() if lock: flock.release() return True except: if lock: flock.release() return False def has_option(self, section, option): return self.cfg.has_option(section, option) def remove_option(self, section, option): return self.cfg.remove_option(section, option) def get(self, section, option): return self.cfg.get(section, option) def getboolean(self, section, option): return self.cfg.getboolean(section, option) def getint(self, section, option): return self.cfg.getint(section, option) def has_section(self, section): return self.cfg.has_section(section) def add_section(self, section): return self.cfg.add_section(section) def remove_section(self, section=None): if section is None: return False else: return self.cfg.remove_section(section) def set(self, section, option, value): try: self.cfg.set(section, option, value) except: return False return self.update() def get_section_list(self): '''Return a list of section names, excluding [DEFAULT]''' return self.cfg.sections() def get_option_list(self, section): '''Return a list of option names for the given section name.''' return self.cfg.options(section) def get_config_list(self): '''Return a list of all config for the given config file.''' config_list = [] sections = self.cfg.sections() for section in sections: sec = {'section': section, 'option': {}} options = self.cfg.options(section) for key in options: sec['option'][key] = self.cfg.get(section, key) config_list.append(sec) return config_list def get_config(self): '''Return a dict of all config for the given config file.''' config = {} for section in self.cfg.sections(): config[section] = {} for item in self.cfg.options(section): config[section][item] = self.cfg.get(section, item) return config def addsection(self, section, data): '''add one section''' try: if not self.cfg.has_section(section): self.cfg.add_section(section) for option, value in data.items(): self.cfg.set(section, option, value) return self.update(False) except: return False def addsections(self, section): '''add some sections''' try: for sec, data in section.items(): if not self.cfg.has_section(sec): self.cfg.add_section(sec) for option, value in data.items(): self.cfg.set(sec, option, value) return self.update(False) except: return False
def remove_section(self,section): if self.has_section(section): ConfigParser.remove_section(self, section)
class Config(object): section_handlers = {} @classmethod def register_handler(cls, name, handler, force=False): if not force and cls.section_handlers.get(name): # if it happens, don't avoid it but be verbose unless force is True print "Warning: duplicate registration of ConfigSection %s" % name raise ValueError cls.section_handlers[name] = handler @classmethod def get_handler(cls, name): return cls.section_handlers.get(name, ConfigSection) def __init__(self, inifile): self._inifile = inifile self._parser = None self._sections = {} self._dirty = False self.check_mode() def check_mode(self): try: mode = os.stat(self._inifile)[0] & 0777 except OSError: # file does not exist, this is ok return False if mode & 0177: print ("File mode %o for %s is not enough restrictive and is a " "security threat." % (mode, self._inifile)) print "Please chmod it to 600." sys.exit(1) return True def __call__(self, section=None): if not self._parser: from ConfigParser import SafeConfigParser as ConfigParser self._parser = ConfigParser() self._parser.read(self._inifile) if not section: return self else: return self[section] def __getitem__(self, section): if not self._sections.has_key(section): self._sections[section] = self.get_handler(section)(self, section) return self._sections[section] def __setitem__(self, section, options): if not self._parser.has_section(section): self._parser.add_section(section) self._sections[section] = self.get_handler(section)(self, section) sect = self._sections[section] for option in sect.keys(): sect.pop(option) for option, value in options: sect[option] = value self.touch() def __delitem__(self, section): self.touch() return self._parser.remove_section(section) def pop(self, section): self.touch() return self._parser.remove_section(section) def keys(self): return self._parser.sections() def has_key(self, section): return self._parser.has_section(section) def defaults(self): return self._parser.defaults() def touch(self): self._dirty = True def write(self, inifile=None): if not self._dirty: return if inifile is None: inifile = self._inifile try: ini = open(inifile, "w") except IOError: if os.environ.get("SSHPROXY_WIZARD", None): return print "Could not write configuration file: %s" % inifile print "Make sure %s is writable" % inifile print "If this is the first time you're running the program, try" print "the following command:" print "sshproxy-setup" sys.exit(1) try: # print 'writing', inifile return self._parser.write(ini) finally: self._dirty = False ini.close() os.chmod(self._inifile, 0600) def __str__(self): fp = StringIO() self._parser.write(fp) fp.seek(0L) return fp.read()