2020-08-03 19:09:16 +02:00
|
|
|
import pandas as pd
|
|
|
|
import configparser
|
2020-08-18 20:18:30 +02:00
|
|
|
import subprocess
|
2020-08-03 19:09:16 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
# read database credentials
|
|
|
|
group = snakemake.params["group"]
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(snakemake.input["db_credentials"])
|
|
|
|
|
|
|
|
# bash command to create table and restore tables from sql file
|
2020-08-18 20:18:30 +02:00
|
|
|
checkdb_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " -e use " + config[group]["database"]
|
2020-08-03 19:09:16 +02:00
|
|
|
create_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " -e \"CREATE DATABASE IF NOT EXISTS " + config[group]["database"] + ";\""
|
2020-08-18 20:32:26 +02:00
|
|
|
restore_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " " + config[group]["database"] + " < data/external/rapids_example.sql"
|
2020-08-03 19:09:16 +02:00
|
|
|
|
|
|
|
try:
|
2020-08-18 23:39:25 +02:00
|
|
|
print("Checking if " + config[group]["database"] + " database exists")
|
|
|
|
subprocess.run(checkdb_cmd.split(), check = True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
2020-08-18 20:18:30 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2020-08-18 23:39:25 +02:00
|
|
|
print(config[group]["database"] + " database does not exist")
|
|
|
|
print("Creating " + config[group]["database"] + " database")
|
2020-08-03 19:09:16 +02:00
|
|
|
os.system(create_cmd)
|
2020-08-18 23:39:25 +02:00
|
|
|
print(config[group]["database"] + " database created")
|
|
|
|
print("Restoring rapids_example.sql")
|
2020-08-03 19:09:16 +02:00
|
|
|
os.system(restore_cmd)
|
2020-08-18 23:39:25 +02:00
|
|
|
print("rapids_example.sql restored in " + config[group]["database"] + " database")
|
2020-08-18 20:18:30 +02:00
|
|
|
else:
|
2020-08-18 23:39:25 +02:00
|
|
|
raise ValueError(config[group]["database"] + " DB already exists")
|