def read_file(self, file, section=''): self.section = section if len(file): self.file = file # any valid conf-file written by a ConfigParser? (path, tail) = os.path.split(self.file) if os.path.isfile(self.file): # read a file created by a self.rCP instance self.rCP.read(self.file) self.isModified = False else: old = os.path.join(path, 'config.txt') if os.path.isfile(old): # note config.txt is a file not created by self.rCP, # conversion. with open(old) as r: self.isModified = True for line in r: line = qa_util.clear_line(line) if len(line): sz1 = len(line) - 1 if line[sz1] == ':': ny_sect = line[0:sz1] # rm ':' self.rCP.add_section(ny_sect) else: k = line.split('=', 1) self.rCP.set(ny_sect, k[0], k[1]) return
def readConfigFiles(self): if len(self.ldOpts) == 0: self.ldOpts.append({}) # list config files, if any. lCfgFiles=[] # At first, any item specified by QA_CONF on the command-line if 'QA_CONF' in self.ldOpts[self.cl_ix]: lCfgFiles.append(self.ldOpts[self.cl_ix]['QA_CONF']) if 'TASK' in self.ldOpts[self.cl_ix]: lCfgFiles.append(self.ldOpts[self.cl_ix]['TASK']) if len(lCfgFiles) == 0: lCfgFiles.append('') # force to enter the for-loop for cfg in lCfgFiles: self.ldOpts.append({}) _ldo = self.ldOpts[len(self.ldOpts)-1] if len(cfg): with open(cfg, 'r') as f: select='' lock='' for line in f: line = qa_util.clear_line(line) if len(line) == 0: continue # SELECT|LOCK may have '=', '+=', or ' '; the latter # two accumulate and the value may have embedded '=' if line[:6] == 'SELECT': str0=self.setSelLock(line) if str0[0] == '=': select = str0[1:] else: if len(select): select += ',' select += str0[2:] continue elif line[:4] == 'LOCK': str0=self.setSelLock(line) if str0[0] == '=': lock = str0[1:] else: if len(lock): lock += ',' lock += str0[2:] continue else: pos = line.find('=') if pos == -1: key=line.upper() val='t' if key == 'NEXT': val=1 elif key == 'SHOW_NEXT': val=1 else: (key, val) = line.split('=', 1) key=key.upper() if key == 'PROJECT' and len(self.project) == 0: # for PROJECT_AS, too self.project = val elif len(key) > 2 and key[0:3] == 'QC_': # --> backward compatible key = 'QA' + key[2:] # comma-sep list --> python list, but protect # those containing braces if val.find('{') == -1: try: # --> list of integers val=[ int(x) for x in val.split(',') ] except ValueError: # --> list of strings s=repr(val) if s.find(',') > -1: val=val.split(',') except: pass _ldo[key] = val # fill in regular QA options if len(select): self.lSelect.extend(select.split(',')) if len(lock): self.lLock.extend(lock.split(',')) if 'QA_CONF' in _ldo.keys(): str0 = _ldo['QA_CONF'] del _ldo['QA_CONF'] else: str0='' # default configuration self.searchQA_CONF_chain(str0, lCfgFiles) # preserve the list of configuration files if len(self.ldOpts): val='' for cfg in lCfgFiles: if len(cfg): if len(val): val += ',' val += cfg self.ldOpts[0]['QA_CONF'] = val return
def readQA_CONF_files(self): # list config files, if any. lCfgFiles = [] # At first, any item specified by QA_CONF on the command-line, # which is given by index 1 _ldo = self.ldOpts[1] if 'QA_CONF' in _ldo: lCfgFiles.append(_ldo['QA_CONF']) if 'TASK' in _ldo: lCfgFiles.append(_ldo['TASK']) if len(lCfgFiles) == 0: lCfgFiles.append('') # force to enter the for-loop for cfg in lCfgFiles: self.ldOpts.append({}) _ldo = self.ldOpts[len(self.ldOpts) - 1] if len(cfg): if not os.path.isfile(cfg): print 'no such file ' + cfg sys.exit(1) with open(cfg, 'r') as f: select = '' lock = '' for line in f: line = qa_util.clear_line(line) if len(line) == 0: continue # SELECT|LOCK may have '=', '+=', or ' '; the latter # two accumulate and the value may have embedded '=' if line[:6] == 'SELECT': str0 = self.setSelLock(line) if str0[0] == '=': select = str0[1:] else: if len(select): select += ',' select += str0[2:] continue elif line[:4] == 'LOCK': str0 = self.setSelLock(line) if str0[0] == '=': lock = str0[1:] else: if len(lock): lock += ',' lock += str0[2:] continue else: pos = line.find('=') if pos == -1: key = line.upper() val = 't' if key == 'NEXT' or key == 'NEXT_VAR': val = 1 elif key == 'SHOW_NEXT': val = 1 else: (key, val) = line.split('=', 1) key = key.upper() if key == 'PROJECT' and len(self.project) == 0: # self.project is set to give precedence to 1st key self.project = val elif key == 'PROJECT_AS' and len(self.project_as) == 0: self.project_as = val elif len(key) > 2 and key[0:3] == 'QC_': # --> backward compatible key = 'QA' + key[2:] # comma-sep list --> python list, but protect # those containing braces if not '{' in val: try: # --> list of integers val = [int(x) for x in val.split(',')] # but, only a single one if len(val) == 1: val = val[0] except ValueError: # --> list of strings s = repr(val) if ',' in s: val = val.split(',') except: pass _ldo[key] = val # fill in regular QA options if len(select): self.lSelect.extend(select.split(',')) if len(lock): self.lLock.extend(lock.split(',')) if 'QA_CONF' in _ldo: str0 = _ldo['QA_CONF'] del _ldo['QA_CONF'] else: str0 = '' # default configuration self.searchQA_CONF_chain(str0, lCfgFiles) # special if 'NEXT' in _ldo: if not 'NEXT_VAR' in _ldo: _ldo['NEXT_VAR'] = 1 # preserve the list of configuration files if len(self.ldOpts): val = '' for cfg in lCfgFiles: if len(cfg): if len(val): val += ',' val += cfg self.ldOpts[0]['QA_CONF'] = val return
def read_file(self, file, section=''): self.section = section if len(file): self.file = file # for .qa.cfg formatted (new) isConvert = False isOld = False isNew = False (path, fname) = os.path.split(self.file) if fname == '.qa-config.txt': old = self.file isConvert = True isOld = True self.is_read_only = True elif fname == '.qa.cfg': new = self.file isNew = True self.is_read_only = True else: self.is_read_only = True # as long as installation/update stuff is done by bash scripts, # the old config file format has precedence old = os.path.join(path, 'config.txt') if os.path.isfile(old): isConvert = True isOld = True new = os.path.join(path, 'qa.cfg') if os.path.isfile(new): isNew = True if not isNew and not isOld: self.rCP.add_section(section) return #from scratch if isConvert: if not self.is_read_only: # at this point old exists if isNew: old_modTime = qa_util.f_get_mod_time(old) new_modTime = qa_util.f_get_mod_time(new) if old_modTime > new_modTime: isConvert = True else: isConvert = False if isConvert: # conversion with open(old) as r: self.isModified = True for line in r: line = qa_util.clear_line(line) if len(line): sz1 = len(line) - 1 if line[sz1] == ':': ny_sect = line[0:sz1] # rm ':' self.rCP.add_section(ny_sect) else: k = line.split('=', 1) # some backward compatibility if k[0] == "QA_HOME": k[0] = "QA_TABLES" self.rCP.set(ny_sect, k[0], k[1]) else: # read a file created by a self.rCP instance self.rCP.read(self.file) self.isModified = False return