#!/usr/bin/env python import ConfigParser import sys if len(sys.argv)!=3: print "USAGE:" print "\tltsmerger global_ltsconf local_ltsconf" sys.exit(0) def is_inside_tuple(tuple_list,value): pos=0 for t in tuple_list: if value in t: return [True,pos] pos+=1 return [False,None] #def is_inside_tuple try: global_cfg = ConfigParser.ConfigParser() global_cfg.optionxform=str global_cfg.read(sys.argv[1]) local_cfg = ConfigParser.ConfigParser() local_cfg.optionxform=str local_cfg.read(sys.argv[2]) for section in local_cfg.sections(): for item in local_cfg.items(section): k,v=item if global_cfg.has_section(section) and global_cfg.has_option(section,k): global_cfg.remove_option(section,k) new_cfg={} new_cfg["Default"]=[] cfg_list=[global_cfg,local_cfg] for cfg in cfg_list: if len(cfg.defaults())>0: defaults=cfg.defaults().items() for option in defaults: ret,pos=is_inside_tuple(new_cfg["Default"],option[0]) if ret: new_cfg["Default"][pos]=(new_cfg["Default"][pos][0],option[1]) else: new_cfg["Default"].append(option) # ConfgParser DEFAULT values appear in every section. # In our case, we don't want them in other sections cfg.remove_option("DEFAULT",option[0]) for section in cfg.sections(): if section not in new_cfg and section.lower()!="default": new_cfg[section]=[] for option in cfg.items(section): if section.lower()=="default": section="Default" ret,pos=is_inside_tuple(new_cfg[section],option[0]) if ret: new_cfg[section][pos]=(new_cfg[section][pos][0],option[1]) else: new_cfg[section].append(option) print "[Default]" for option in new_cfg["Default"]: print "%s = %s"%(option[0],option[1]) for section in new_cfg: if section.lower() != "default": print "[%s]"%section for option in new_cfg[section]: print "%s = %s"%(option[0],option[1]) except Exception as e: print "# Exception when merging lts.conf files" print "# Exception: ", str(e).strip('\n') print "[Default]"