Example #1
0
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
Example #2
0
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)
Example #4
0
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()
Example #5
0
 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
Example #6
0
 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
Example #7
0
File: fort.py Project: tueabra/fort
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
Example #8
0
 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 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 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
Example #11
0
 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()
Example #12
0
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
Example #13
0
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)
Example #14
0
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'))
Example #15
0
    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)
Example #16
0
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
Example #17
0
    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)
Example #18
0
    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)
Example #19
0
    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
Example #20
0
    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']
Example #21
0
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)))
Example #22
0
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()
Example #23
0
# 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')
Example #24
0
 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
Example #26
0
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()
Example #27
0
 def remove_section(self,section):
     if self.has_section(section):
         ConfigParser.remove_section(self, section)
Example #28
0
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
        
Example #29
0
    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
Example #30
0
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