Skip to content

Commit

Permalink
refactored to only handle database prep
Browse files Browse the repository at this point in the history
  • Loading branch information
bikegeek committed Jul 23, 2023
1 parent 0a8dca8 commit 31595e1
Showing 1 changed file with 19 additions and 70 deletions.
89 changes: 19 additions & 70 deletions METdbLoad/sql/scripts/db_prep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'''
Creates the METviewer database to store MET output.
Creates the METviewer database to store MET output. Requires a YAML configuration
file (data_loading_config.yaml) with relevant database information (i.e. username,
password, etc.).
'''
import os.path
import subprocess
Expand All @@ -13,7 +15,7 @@


@dataclass
class DatabaseLoadingInfo:
class :
'''
Data class for keeping the relevant information for loading the
METviewer database.
Expand All @@ -24,18 +26,10 @@ class DatabaseLoadingInfo:
password: str
host_name: str
port_number: int
group: str
schema_path: str
data_dir: str
xml_spec_file: str
load_stat: bool
load_mode: bool
load_mtd: bool
load_mpr: bool
load_orank: bool
config_file_dir: str

def __init__(self, config_obj: dict, config_file_dir:str):
def __init__(self, config_obj: dict, config_file_dir: str):
'''

Args:
Expand All @@ -48,55 +42,9 @@ def __init__(self, config_obj: dict, config_file_dir:str):
self.password = config_obj['password']
self.host_name = config_obj['host']
self.port_number = config_obj['port']
self.group = config_obj['group']
self.schema_path = config_obj['schema_location']
self.data_dir = config_obj['data_dir']
self.xml_spec_file = config_obj['xml_specification']
self.load_stat = config_obj['load_stat']
self.load_mode = config_obj['load_mode']
self.load_mtd = config_obj['load_mtd']
self.load_mpr = config_obj['load_mpr']
self.load_orank = config_obj['load_orank']
self.description = config_obj['description']
self.config_file_dir = config_file_dir


def update_spec_file(self):
'''
Edit the XML specification file to reflect the settings in the
YAML configuration file.
'''

# Assign the host with the host and port assigned in the YAML config file
import xml.etree.ElementTree as et
tree = et.parse(self.xml_spec_file)
root = tree.getroot()

for host in root.iter('host'):
host.text = self.host_name + ":" + str(self.port_number)

for dbname in root.iter('database'):
dbname.text = self.db_name

for user in root.iter('user'):
user.text = self.user_name

for password in root.iter('password'):
password.text = self.password

for data_folder in root.iter('folder_tmpl'):
data_folder.text = self.data_dir

for group in root.iter('group'):
group.text = self.group

for desc in root.iter('description'):
desc.text = self.description

tree.write(os.path.join(self.config_file_dir, 'load_met.xml'))



def create_database(self):
'''
Create the commands to create the database.
Expand All @@ -105,9 +53,9 @@ def create_database(self):
'''
# Command to create the database, set up permissions, and load the schema.
uname_pass_list = [ '-u', self.user_name, ' -p', self.password, ' -e ']
uname_pass_list = ['-u', self.user_name, ' -p', self.password, ' -e ']
uname_pass = ''.join(uname_pass_list)
create_list = [ "'create database ", self.db_name, "'"]
create_list = ["'create database ", self.db_name, "'"]
create_str = ''.join(create_list)
create_cmd = uname_pass + create_str

Expand All @@ -133,10 +81,9 @@ def create_database(self):
pass

try:
create_db = subprocess.check_output(['mysql', uname_pass, create_str,
self.db_name])
db_permissions = subprocess.checkoutput(['mysql', uname_pass, create_str,
self.db_name])
create_db = subprocess.check_output(['mysql', create_cmd])
db_permissions = subprocess.checkoutput(['mysql', perms_cmd])
db_schema = subprocess.checkoutput(['mysql', schema_cmd])
except subprocess.CalledProcessError:
logging.error('Error in executing mysql commands')

Expand All @@ -148,20 +95,20 @@ def delete_database(self):
'''

# Command to delete the database
uname_pass_list = ['-u', self.user_name, ' -p' , self.password, ' -e ']
uname_pass_list = ['-u', self.user_name, ' -p', self.password, ' -e ']
uname_pass = ''.join(uname_pass_list)
drop_list = ["'drop database ", self.db_name, "'" ]
drop_list = ["'drop database ", self.db_name, "'"]
drop_str = ''.join(drop_list)
drop_cmd = uname_pass + drop_str
logging.debug(f'Drop database command: {drop_cmd}')

try:
_ = subprocess.check_output(['mysql', uname_pass, drop_str,
self.db_name])
_ = subprocess.check_output(['mysql', drop_cmd])

except subprocess.CalledProcessError:
logging.error('Error in executing mysql commands')


if __name__ == "__main__":

# Create a parser
Expand All @@ -180,17 +127,19 @@ def delete_database(self):

action_requested = str(action).lower()
logging.debug(f'Action requested: {action_requested}')
logging.debug(f'Config file to use: {str(config_file)}')
logging.debug(f'YAML Config file to use: {str(config_file)}')
config_file_dir = os.path.dirname(config_file)
logging.debug(f'Directory of config file: {config_file_dir}')

with open(config_file, 'r') as cf:
db_config_info = yaml.safe_load(cf)
db_loader = DatabaseLoadingInfo(db_config_info, config_file_dir)
db_loader = DatabaseInfo(db_config_info, config_file_dir)
if action_requested == 'create':
db_loader.update_spec_file()
db_loader.create_database()
elif action_requested == 'delete':
db_loader.delete_database()
else:
logging.warning(f'{action_requested} is not a supported option.')
logging.warning(
f'{action_requested} is not a supported option. Only "create" and '
f'"delete" are supported options.')

0 comments on commit 31595e1

Please sign in to comment.