2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-03-15 16:36:51 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2018-04-16 14:50:04 +02:00
|
|
|
import configparser
|
|
|
|
from configparser import ConfigParser as cfgP
|
2017-03-15 16:36:51 +01:00
|
|
|
import os
|
2017-03-28 09:54:24 +02:00
|
|
|
from collections import OrderedDict
|
2017-04-25 09:32:05 +02:00
|
|
|
import sys
|
|
|
|
import shutil
|
2017-03-15 16:36:51 +01:00
|
|
|
|
|
|
|
|
2017-04-25 09:32:05 +02:00
|
|
|
#return true if the configuration is up-to-date
|
2017-03-15 16:36:51 +01:00
|
|
|
def main():
|
2018-07-24 10:41:10 +02:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print('usage:', 'Update-conf.py', 'Automatic (boolean)')
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
automatic = sys.argv[1]
|
|
|
|
if automatic == 'True':
|
|
|
|
automatic = True
|
|
|
|
else:
|
|
|
|
automatic = False
|
2017-03-15 16:36:51 +01:00
|
|
|
|
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
2017-04-25 09:32:05 +02:00
|
|
|
configfileBackup = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg') + '.backup'
|
2017-03-15 16:36:51 +01:00
|
|
|
if not os.path.exists(configfile):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
|
|
|
configfileSample = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg.sample')
|
|
|
|
|
2018-04-16 14:50:04 +02:00
|
|
|
cfg = configparser.ConfigParser()
|
2017-03-15 16:36:51 +01:00
|
|
|
cfg.read(configfile)
|
2018-04-16 14:50:04 +02:00
|
|
|
cfgSample = configparser.ConfigParser()
|
2017-03-15 16:36:51 +01:00
|
|
|
cfgSample.read(configfileSample)
|
|
|
|
|
2017-03-28 09:54:24 +02:00
|
|
|
sections = cfgP.sections(cfg)
|
|
|
|
sectionsSample = cfgP.sections(cfgSample)
|
2018-04-16 14:50:04 +02:00
|
|
|
|
2017-03-15 16:36:51 +01:00
|
|
|
missingSection = []
|
|
|
|
dicoMissingSection = {}
|
|
|
|
missingItem = []
|
|
|
|
dicoMissingItem = {}
|
|
|
|
|
|
|
|
for sec in sectionsSample:
|
|
|
|
if sec not in sections:
|
|
|
|
missingSection += [sec]
|
2017-03-28 09:54:24 +02:00
|
|
|
dicoMissingSection[sec] = cfgP.items(cfgSample, sec)
|
2017-03-15 16:36:51 +01:00
|
|
|
else:
|
2017-03-28 09:54:24 +02:00
|
|
|
setSample = set(cfgP.options(cfgSample, sec))
|
|
|
|
setNormal = set(cfgP.options(cfg, sec))
|
2017-03-15 16:36:51 +01:00
|
|
|
if setSample != setNormal:
|
|
|
|
missing_items = list(setSample.difference(setNormal))
|
|
|
|
missingItem += [sec]
|
|
|
|
list_items = []
|
|
|
|
for i in missing_items:
|
|
|
|
list_items.append( (i, cfgSample.get(sec, i)) )
|
|
|
|
dicoMissingItem[sec] = list_items
|
|
|
|
|
|
|
|
if len(missingSection) == 0 and len(missingItem) == 0:
|
2017-04-25 09:32:05 +02:00
|
|
|
#print("Configuration up-to-date")
|
|
|
|
return True
|
2017-03-15 16:36:51 +01:00
|
|
|
print("/!\\ Configuration not complete. Missing following configuration: /!\\")
|
|
|
|
print("+--------------------------------------------------------------------+")
|
|
|
|
for section in missingSection:
|
|
|
|
print("["+section+"]")
|
|
|
|
for item in dicoMissingSection[section]:
|
|
|
|
print(" - "+item[0])
|
|
|
|
for section in missingItem:
|
|
|
|
print("["+section+"]")
|
|
|
|
for item in dicoMissingItem[section]:
|
|
|
|
print(" - "+item[0])
|
|
|
|
print("+--------------------------------------------------------------------+")
|
2017-03-28 09:54:24 +02:00
|
|
|
|
2018-07-24 10:41:10 +02:00
|
|
|
if automatic:
|
|
|
|
resp = 'y'
|
|
|
|
else:
|
|
|
|
resp = input("Do you want to auto fix it? [y/n] ")
|
2017-03-15 16:36:51 +01:00
|
|
|
|
|
|
|
if resp != 'y':
|
2017-04-25 09:32:05 +02:00
|
|
|
return False
|
2017-03-15 16:36:51 +01:00
|
|
|
else:
|
2018-07-24 10:41:10 +02:00
|
|
|
if automatic:
|
|
|
|
resp2 = 'y'
|
|
|
|
else:
|
|
|
|
resp2 = input("Do you want to keep a backup of the old configuration file? [y/n] ")
|
|
|
|
|
2017-04-25 09:32:05 +02:00
|
|
|
if resp2 == 'y':
|
|
|
|
shutil.move(configfile, configfileBackup)
|
|
|
|
|
2017-03-28 09:55:59 +02:00
|
|
|
#Do not keep item ordering in section. New items appened
|
2017-03-15 16:36:51 +01:00
|
|
|
for section in missingItem:
|
|
|
|
for item, value in dicoMissingItem[section]:
|
|
|
|
cfg.set(section, item, value)
|
|
|
|
|
2017-03-28 09:55:59 +02:00
|
|
|
#Keep sections ordering while updating the config file
|
2017-03-28 09:54:24 +02:00
|
|
|
new_dico = add_items_to_correct_position(cfgSample._sections, cfg._sections, missingSection, dicoMissingSection)
|
|
|
|
cfg._sections = new_dico
|
|
|
|
|
2017-03-15 16:36:51 +01:00
|
|
|
with open(configfile, 'w') as f:
|
|
|
|
cfg.write(f)
|
2017-04-25 09:32:05 +02:00
|
|
|
return True
|
2017-03-15 16:36:51 +01:00
|
|
|
|
|
|
|
|
2017-03-28 09:54:24 +02:00
|
|
|
''' Return a new dico with the section ordered as the old configuration with the updated one added '''
|
|
|
|
def add_items_to_correct_position(sample_dico, old_dico, missingSection, dicoMissingSection):
|
|
|
|
new_dico = OrderedDict()
|
2018-04-16 14:50:04 +02:00
|
|
|
|
2017-03-28 09:54:24 +02:00
|
|
|
positions = {}
|
|
|
|
for pos_i, sec in enumerate(sample_dico):
|
|
|
|
if sec in missingSection:
|
|
|
|
positions[pos_i] = sec
|
|
|
|
|
|
|
|
for pos_i, sec in enumerate(old_dico):
|
|
|
|
if pos_i in positions:
|
|
|
|
missSection = positions[pos_i]
|
|
|
|
new_dico[missSection] = sample_dico[missSection]
|
|
|
|
|
|
|
|
new_dico[sec] = old_dico[sec]
|
|
|
|
return new_dico
|
|
|
|
|
|
|
|
|
2017-03-15 16:36:51 +01:00
|
|
|
if __name__ == "__main__":
|
2017-04-25 09:32:05 +02:00
|
|
|
if main():
|
|
|
|
sys.exit()
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|