2018-12-02 22:09:55 -06:00
|
|
|
import click
|
2018-12-07 01:03:17 -06:00
|
|
|
from pydoc import importfile, ErrorDuringImport
|
|
|
|
import yaml
|
|
|
|
from site import exit
|
|
|
|
from shutil import copy
|
|
|
|
from os import path, chmod, makedirs
|
|
|
|
import stat
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
APP_NAME = 'twservices'
|
2018-12-07 01:03:17 -06:00
|
|
|
SAMPLE_CONFIG_FILE = 'sample.config.yml'
|
|
|
|
CONFIG_FILE = 'config.yml'
|
|
|
|
CONFIG_DIR = click.get_app_dir(APP_NAME)
|
|
|
|
PROJ_DIR = os.path.dirname(os.path.realpath(__file__))
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
def __create_config_file():
|
2018-12-07 01:03:17 -06:00
|
|
|
user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE)
|
|
|
|
sample_cfg_file = path.join(PROJ_DIR, SAMPLE_CONFIG)
|
|
|
|
if not os.path.exists(CONFIG_DIR):
|
|
|
|
print('Config {} director does not exist, create...'.format(CONFIG_DIR))
|
|
|
|
makedirs(CONFIG_DIR)
|
|
|
|
|
|
|
|
copy(sample_cfg_file, user_cfg_file)
|
|
|
|
print('Copying {} to {}'.format(sample_cfg_file, user_cfg_file))
|
|
|
|
chmod(user_cfg_file, 0o664)
|
|
|
|
|
|
|
|
return user_cfg_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.group()
|
|
|
|
def main():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_config_file()
|
|
|
|
no_config_msg = 'A config file was not found ' +
|
|
|
|
'and will be created under {0}. Is that Ok?'.format(click.get_app_dir(APP_NAME))
|
|
|
|
cfg_file = path.join(click.get_app_dir(APP_NAME), 'config.yml')
|
|
|
|
if not path.exists(cfg_file):
|
|
|
|
create_config_app_dir = click.confirm(no_config_msg)
|
|
|
|
if create_config_app_dir:
|
2018-12-07 01:03:17 -06:00
|
|
|
cfg_file = __create_config_file()
|
|
|
|
else:
|
|
|
|
exit(0)
|
|
|
|
return cfg_file
|
2018-12-02 22:09:55 -06:00
|
|
|
|
2018-12-07 01:03:17 -06:00
|
|
|
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
@main.command()
|
|
|
|
def sync():
|
2018-12-07 01:03:17 -06:00
|
|
|
cfg_file = get_config_file()
|
|
|
|
fd = open(cfdg_file);
|
|
|
|
parse_cfg_file = yaml.safe_load(fd)
|
|
|
|
services = parse_cfg_file['services']
|
|
|
|
for service in services:
|
|
|
|
try:
|
|
|
|
ServiceClass = importfile('{}/apps/{}.py'.format(PROJ_DIR, service['name'])
|
|
|
|
ServiceInstance = ServiceClass(**service)
|
|
|
|
ServiceInstance.sync()
|
|
|
|
except ErrorDuringImport as e:
|
|
|
|
print(e)
|
|
|
|
exit(1)
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|