Source code for ecpi.scripts.ecpi_conf_tool

#!/usr/bin/env python3
# -*-coding:utf-8 -*

import os.path as osp
import os
import argparse
import shutil
import sys
from ecpi.common import workspace_dirs

conf_posix_env = """#!/usr/bin/bash
export ECPI_ROOT=%s
export ECPI_PATH=%s
export ECPI_VER=%s
export ECPI_HOME=%s
export ECPI_LVL=%s
export ECPI_USER=%s
export ECPI_TOKEN=%s

#######################################################
#  Function for initializing an ECPI_HOME directory
#######################################################
ecpi_init(){
RC=.ect/ecpirc; 
if [ -f $RC -a -z $1 ]; 
then 
    source $RC; 
else 
    ecpi_conf_tool $1 && source $RC ; 
fi;}
"""
# FIXME: Write the right ecpi_init script for windows
conf_nt_env = """
set ECPI_ROOT=%s
set ECPI_PATH=%s
set ECPI_VER=%s
set ECPI_HOME=%s
set ECPI_LVL=%s
set ECPI_USER=%s
set ECPI_TOKEN=%s

;######################################################
;  Function for initializing an ECPI_HOME directory
;######################################################
function ecpi_init(){
RC=.ect/ecpi.bat; 
if [ -f $RC -a -z $1 ]; 
then 
    . $RC; 
else 
    ecpi_conf_tool $1 && . $RC ; 
fi;}
"""


[docs]def add_args(): """Retrieve command line options.""" parser = argparse.ArgumentParser( add_help=True, usage="ecpi_conf_tool [options]", description="""Configures the current directory as ECPI_HOME. It recovers the following information: 1) the Python environment where *eclairs-gp* will be installed (ECPI_ROOT), 2) the current working directory (cwd) which will becomes the ECPI_HOME, and 3) username and token at gitlab.cea.fr for module development (if member of the ecpi's developers team. """, ) parser.add_argument( "--conf", "-c", action="store_true", required=False, help="Configures the environment sourcing files with the ECPI information" "recovered from the system. No local folders will be modified.", ) parser.add_argument( "--update", "-u", action="store_true", required=False, help="Recreates the environment sourcing files with the information of " "the *eclairs-gp* package.", ) parser.add_argument( "--show", "-s", action="store_true", required=False, help="Show the actual configuration of the current working directory and" "the loaded environment variables.", ) parser.add_argument( "--reconf", "-r", action="store_true", required=False, help="Recreates the full environment. It will erase ecpi's main workspace folders" ) return parser.parse_args()
[docs]def config_ecpi_user(): username = input("Please, write your gitlab.cea.fr username (login): ") val = input(f"Your answer is {username}. It is correct ? ([y]/n) : ") if val.lower() not in ['', 'y', 'o', 'yes', 'oui']: print(f'\n >> Your answer was [{val}]. Exiting without changes...') return None return username
[docs]def config_ecpi_token(): ecpi_token = input("Please, write your gitlab.cea.fr personal (r/w) token : ") val = input(f"Your token is < {ecpi_token} >. It is correct ? ([y]/n) : ") if val.lower() not in ['', 'y', 'o', 'yes', 'oui']: print(f'\n >> Your answer was [{val}]. Exiting without changes...') return None return ecpi_token
[docs]def show_welcome_message(): msg = f"""This program will configure the current directory as the localhome for the ECLAIRs Pipeline ($ECPI_HOME={os.getcwd()}). It will create the following folders: - OUTPUT - DATA_TESTS - DATA_PROCESS that will be used for the eclairs-gp pipeline for local processing. It will create a configuration file for your environment. You can activate it with the command: prompt>$ source .ect/ecpirc """ print(msg) val = input(" Do you want to continue (y/n)? : ") if val.lower() not in ['y', 'o', 'yes', 'oui']: print(f'\n >> Your answer was [{val}]. Exiting without changes...') return False return True
[docs]def create_local_folders(init=False): if init: ecpi_home = os.getcwd() else: ecpi_home = os.getenv('ECPI_HOME') if not ecpi_home: print(f'Error: your local configuration is buggy. Try creating a new local folder') return False for subdir in workspace_dirs.values(): os.makedirs(osp.join(ecpi_home, subdir), exist_ok=True) return True
[docs]def check_config(): ecpi_home_config_dir = osp.join(os.getcwd(), '.ect') config_file = osp.join(ecpi_home_config_dir, 'ecpirc') if osp.isdir(ecpi_home_config_dir): if osp.isfile(config_file): with open(config_file, 'r') as conf: print('Warning: the current folder is already an ECPI_HOME folder with config:') print(conf.read()) print('\n if you want to recreate the configuration file use \'ecpi_init [-c|-r]\'') print('Please, check the help') return False return True
[docs]def show_config(): config_filename = osp.join('.ect', 'ecpirc') if not osp.isfile(config_filename): print('The current folder is not an ECPI_HOME folder. Nothing to show!') return False with open(config_filename, 'r') as conf: print(f" The content of your configuration file is: \n{conf.read()} \n") print('Looking for active environment variables...') l_env = os.environ empty = True for key in l_env: if key.find('ECPI') >= 0: print(f"Variable {key} found with value: {l_env[key]}") empty = False if empty: print("None ECPI environment variables active. Done!") return True
[docs]def set_config(): l_sep = osp.sep full = __file__.split(l_sep) # ecpi_root = osp.join(l_sep.join(full[:full.index('ecpi')])) ecpi_root = osp.join(l_sep.join(full[:-3])) ecpi_home_config_dir = osp.join(os.getcwd(), '.ect') # ecpi_path = '' if os.name.lower() is 'posix': ecpi_path = osp.join(l_sep.join(full[:-6]), 'bin') elif os.name.lower() is 'nt': # Python environment's structure is different in Windows ecpi_path = l_sep.join(full[:-5]) else: print(f'At this point {os.name} is not supported. I will try to configure as *posix*.') ecpi_path = osp.join(l_sep.join(full[:-6]), 'bin') ecpi_home = os.getcwd() print(f'Testing home configuration {ecpi_home_config_dir}') if osp.exists(ecpi_home_config_dir): print(f'{os.getcwd()} is already configured as a local ECLAIRs-gp pipeline working directory') return False ecpi_user = config_ecpi_user() ecpi_token = config_ecpi_token() print(ecpi_user, ecpi_token) # In this version, username and token is mandatory if ecpi_user is None or ecpi_token is None: return False if os.name.lower() is 'nt': local_conf = conf_nt_env % (ecpi_root, ecpi_path, 'v01', ecpi_home, 1, ecpi_user, ecpi_token) else: local_conf = conf_posix_env % (ecpi_root, ecpi_path, 'v01', ecpi_home, 1, ecpi_user, ecpi_token) os.makedirs(ecpi_home_config_dir) with open(osp.join(ecpi_home_config_dir, 'ecpirc'), 'w') as conf: conf.writelines(local_conf) return True
# main function for an entry-point must be without arguments # arguments must be added with the add_args function
[docs]def main(): args = add_args() ecpi_home_conf_dir = osp.join(os.getcwd(), '.ect') config_file = osp.join(ecpi_home_conf_dir, 'ecpirc') # TODO: refactors the main part for gitlab users (handle username and token) if args.conf: if osp.isdir(ecpi_home_conf_dir) or osp.isfile(config_file): shutil.rmtree(ecpi_home_conf_dir) print('--> Configuring your ECPI environment !') set_config() sys.exit(0) else: print('You are not in an ECPI_HOME folder. Skipping configuration!') sys.exit(1) if args.reconf: if osp.isdir(ecpi_home_conf_dir) or osp.isfile(config_file): shutil.rmtree(ecpi_home_conf_dir) for subdir in workspace_dirs.values(): if osp.isdir(subdir): shutil.rmtree(osp.join(os.getcwd(), subdir)) print('--> Configuring your ECPI environment !') set_config() create_local_folders(init=True) sys.exit(0) else: print('You are not in an ECPI_HOME folder. Skipping configuration!') sys.exit(1) if args.update: if not osp.isdir(ecpi_home_conf_dir) and not osp.isfile(config_file): print('You are not in an ECPI_HOME folder. Skipping configuration!') sys.exit(1) shutil.rmtree(ecpi_home_conf_dir) print('--> Updating your ECPI environment !') set_config() sys.exit(0) if args.show: if not show_config(): print('--> There is something wrong with your ECPI environment!') sys.exit(1) sys.exit(0) if check_config(): if not show_welcome_message(): sys.exit(1) print('--> Configuring your ECPI environment !') set_config() create_local_folders(init=True) sys.exit(0)
if __name__ == '__main__': main()