2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-03-15 16:36:51 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
2017-04-25 09:32:05 +02:00
|
|
|
import sys
|
2019-04-23 15:04:45 +02:00
|
|
|
import configparser
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
def print_message(message_to_print, verbose):
|
|
|
|
if verbose:
|
|
|
|
print(message_to_print)
|
2018-04-16 14:50:04 +02:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
def update_config(config_file, config_file_sample, config_file_backup=False):
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
verbose = True
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
# Check if confile file exist
|
|
|
|
if not os.path.isfile(config_file):
|
|
|
|
# create config file
|
|
|
|
with open(config_file, 'w') as configfile:
|
|
|
|
with open(config_file_sample, 'r') as config_file_sample:
|
|
|
|
configfile.write(config_file_sample.read())
|
|
|
|
print_message('Config File Created', verbose)
|
2017-03-15 16:36:51 +01:00
|
|
|
else:
|
2019-04-23 15:04:45 +02:00
|
|
|
config_server = configparser.ConfigParser()
|
|
|
|
config_server.read(config_file)
|
|
|
|
config_sections = config_server.sections()
|
|
|
|
|
|
|
|
config_sample = configparser.ConfigParser()
|
|
|
|
config_sample.read(config_file_sample)
|
|
|
|
sample_sections = config_sample.sections()
|
|
|
|
|
|
|
|
mew_content_added = False
|
|
|
|
for section in sample_sections:
|
|
|
|
new_key_added = False
|
|
|
|
if section not in config_sections:
|
|
|
|
# add new section
|
|
|
|
config_server.add_section(section)
|
|
|
|
mew_content_added = True
|
|
|
|
for key in config_sample[section]:
|
|
|
|
if key not in config_server[section]:
|
|
|
|
# add new section key
|
|
|
|
config_server.set(section, key, config_sample[section][key])
|
|
|
|
if not new_key_added:
|
|
|
|
print_message('[{}]'.format(section), verbose)
|
|
|
|
new_key_added = True
|
|
|
|
mew_content_added = True
|
|
|
|
print_message(' {} = {}'.format(key, config_sample[section][key]), verbose)
|
|
|
|
|
|
|
|
# new keys have been added to config file
|
|
|
|
if mew_content_added:
|
|
|
|
# backup config file
|
|
|
|
if config_file_backup:
|
|
|
|
with open(config_file_backup, 'w') as configfile:
|
|
|
|
with open(config_file, 'r') as configfile_origin:
|
|
|
|
configfile.write(configfile_origin.read())
|
|
|
|
print_message('New Backup Created', verbose)
|
|
|
|
# create new config file
|
|
|
|
with open(config_file, 'w') as configfile:
|
|
|
|
config_server.write(configfile)
|
|
|
|
print_message('Config file updated', verbose)
|
2018-07-24 10:41:10 +02:00
|
|
|
else:
|
2019-04-25 17:17:12 +02:00
|
|
|
print_message('Config File: Nothing to update', verbose)
|
2018-07-24 10:41:10 +02:00
|
|
|
|
2017-04-25 09:32:05 +02:00
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
# return true if the configuration is up-to-date
|
2019-04-23 15:04:45 +02:00
|
|
|
def main():
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-11-05 15:18:03 +01:00
|
|
|
config_file_default = os.path.join(os.environ['AIL_HOME'], 'configs/core.cfg')
|
|
|
|
config_file_default_sample = os.path.join(os.environ['AIL_HOME'], 'configs/core.cfg.sample')
|
|
|
|
config_file_default_backup = os.path.join(os.environ['AIL_HOME'], 'configs/core.cfg.backup')
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
config_file_update = os.path.join(os.environ['AIL_HOME'], 'configs/update.cfg')
|
|
|
|
config_file_update_sample = os.path.join(os.environ['AIL_HOME'], 'configs/update.cfg.sample')
|
2017-03-15 16:36:51 +01:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
if not os.path.exists(config_file_default_sample):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
2018-04-16 14:50:04 +02:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
update_config(config_file_default, config_file_default_sample, config_file_backup=config_file_default_backup)
|
|
|
|
update_config(config_file_update, config_file_update_sample)
|
2017-03-28 09:54:24 +02:00
|
|
|
|
2019-04-23 15:04:45 +02:00
|
|
|
return True
|
2017-03-28 09:54:24 +02:00
|
|
|
|
|
|
|
|
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)
|