Newer
Older
#!/usr/bin/python
# -*- coding: utf-8 -*-
import yaml
import logging
import sys

Arthur Le Bars
committed
import os
import subprocess
def parse_config(config_file):
"""

Arthur Le Bars
committed
Parse a config file containing users and password used by the different services (tripal, galaxy, chado, ...)

Arthur Le Bars
committed
:param config_file:
:return:
"""
config_variables = {}
logging.debug("Using config: %s" % os.path.abspath(config_file))
try:
with open(config_file, 'r') as stream:
yaml_dict = yaml.safe_load(stream)
for k, v in yaml_dict.items():
for k2, v2 in v.items():
config_variables[k2] = v2 # Add a key:value pair to variables for replacement in the compose template file
except FileNotFoundError:
logging.critical("The config file specified doesn't exist (%s)" % config_file)
sys.exit()
except OSError:
logging.critical("The config file specified cannot be read (%s)" % config_file)
sys.exit()
return config_variables
def parse_input(input_file):
"""
Parse the yml input file to extract data to create the SpeciesData objects
Return a list of dictionaries. Each dictionary contains data tied to a species
:param input_file:
:return:
"""
parsed_sp_dict_list = []

Arthur Le Bars
committed
try:
with open(input_file, 'r') as stream:
try:
yaml_dict = yaml.safe_load(stream)
for k, v in yaml_dict.items():
parsed_sp_dict_list.append(v)

Arthur Le Bars
committed
except yaml.YAMLError as err:
logging.critical("Input file is not in YAML format")
sys.exit(err)

Arthur Le Bars
committed
except FileNotFoundError:
logging.critical("The specified input file doesn't exist (%s)" % input_file)
sys.exit()
except OSError:
logging.critical("The specified input file cannot be read (%s)" % input_file)
sys.exit()

Arthur Le Bars
committed

Arthur Le Bars
committed
"""
Separate a list between empty items and non empty items.
Return a dict with 2 keys: empty values (items) and non empty values (items)
:param li:
:return:
"""
filtered_dict = {"empty": [], "not_empty": []}

Arthur Le Bars
committed
filtered_dict["empty"].append(i)

Arthur Le Bars
committed
filtered_dict["not_empty"].append(i)
return filtered_dict

Arthur Le Bars
committed
def check_galaxy_state(genus_lowercase, species, script_dir):

Arthur Le Bars
committed
"""
Read the logs of the galaxy container for the current species to check if the service is "ready"
:param genus_lowercase:
:param species:

Arthur Le Bars
committed
:param script_dir:

Arthur Le Bars
committed
:return:
"""

Arthur Le Bars
committed
# Run supervisorctl status in the galaxy container via serexec
galaxy_logs = subprocess.run(["%s/serexec" % script_dir, "{0}_{1}_galaxy".format(genus_lowercase, species),
"supervisorctl", "status", "galaxy:"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Arthur Le Bars
committed
if "galaxy:galaxy_web RUNNING" in str(galaxy_logs.stdout) \
and "galaxy:handler0 RUNNING" in str(galaxy_logs.stdout) \
and "galaxy:handler1 RUNNING" in str(galaxy_logs.stdout):

Arthur Le Bars
committed
return 1
else:
return 0
def get_species_history_id(instance, full_name):
"""
Set and return the current species history id in its galaxy instance
:param instance:
:param full_name:
:return:
"""
histories = instance.histories.get_histories(name=str(full_name))
history_id = histories[0]["id"]
show_history = instance.histories.show_history(history_id=history_id)
return [history_id, show_history]

Arthur Le Bars
committed
def get_species_to_deploy(sp_dict_list):
"""
Find and return which species (i.e genus species) are not duplicated in the input dictionary used by gga scripts
Returns a list of species (directories) for which to deploy the stack
This aims to reduce the number of deployments for a single species by removing this step from the gga_init
loop iterating over input species
:param sp_dict_list:
:return:
"""
to_deploy_li = []
for sp in sp_dict_list:
for k, v in sp.items():
sp_gspecies = ""
for k2, v2 in v.items():
if k2 == "genus":

Arthur Le Bars
committed
sp_gspecies = sp_gspecies.lower() + v2

Arthur Le Bars
committed
elif k2 == "species":

Arthur Le Bars
committed
sp_gspecies = sp_gspecies.lower() + "_" + v2

Arthur Le Bars
committed
if sp_gspecies not in to_deploy_li and sp_gspecies != "":
to_deploy_li.append(sp_gspecies)

Arthur Le Bars
committed

Arthur Le Bars
committed
return to_deploy_li

Arthur Le Bars
committed
def write_metadata(metadata_file, metadata_dict):
"""
:param metadata_file:
:param metadata_dict:
:return:
"""
ret = 0
return metadata_file, metadata_dict, ret