blob: 3c3bb6f56cc824627ff316d50c8e324b135d83a1 [file] [log] [blame]
#!/usr/bin/env python3
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
#
# Copyright (c) 2015, Colorado State University.
#
# This file is part of ndn-atmos.
#
# ndn-atmos is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# ndn-atmos is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received copies of the GNU General Public License and GNU Lesser
# General Public License along with ndn-atmos, e.g., in COPYING.md file. If not, see
# <http://www.gnu.org/licenses/>.
#
# See AUTHORS.md for complete list of ndn-atmos authors and contributors.
'''This module is for inserting NDN names in underlying database'''
import mysql.connector as sql
from mysql.connector import errorcode
import sys
import getpass
import hashlib
from ndn_cmmap_translators import atmos_translator
def createTables(cursor):
TABLES = {}
TABLES['cmip5'] = (
"CREATE TABLE `cmip5` ("
" `id` int(100) AUTO_INCREMENT NOT NULL,"
" `sha256` varchar(64) UNIQUE NOT NULL,"
" `name` varchar(1000) NOT NULL,"
" `activity` varchar(100) NOT NULL,"
" `product` varchar(100) NOT NULL,"
" `organization` varchar(100) NOT NULL,"
" `model` varchar(100) NOT NULL,"
" `experiment` varchar(100) NOT NULL,"
" `frequency` varchar(100) NOT NULL,"
" `modeling_realm` varchar(100) NOT NULL,"
" `variable_name` varchar(100) NOT NULL,"
" `ensemble` varchar(100) NOT NULL,"
" `time` varchar(100) NOT NULL,"
" PRIMARY KEY (`id`)"
") ENGINE=InnoDB")
#check if tables exist, if not create them
#create tables per schema in the wiki
for tableName, query in TABLES.items():
try:
print("Creating table {}: ".format(tableName))
cursor.execute(query)
print("Created table {}: ".format(tableName))
return True
except sql.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("Table already exists: %s " %(tableName))
return True
else:
print("Failed to create table: %s" %(err.msg))
return False
def insertName(cursor, name):
if __debug__:
print("Name to insert %s " %(name))
#hashvalue is needed since name is too long for primary key (must be <767 bytes)
hashValue = hashlib.sha256(str(name).encode('utf-8')).hexdigest()
if __debug__:
print("Hash of name %s " %(hashValue))
##NOTE:must use %s to prevent sql injection in all sql queries
splitName = list(filter(None, name.split("/")))
splitName.insert(0, hashValue)
splitName.insert(0, name)
splitName = tuple(splitName)
if __debug__:
print("Name to insert in database %s" %(splitName,))
addRecord = ("INSERT INTO cmip5 "
"(name, sha256, activity, product, organization, model, experiment, frequency,\
modeling_realm, variable_name, ensemble, time) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
try:
cursor.execute(addRecord, splitName)
print("Inserted record %s" %(name))
return True
except sql.Error as err:
print("Error inserting name %s, \nError:%s" %(name, err.msg))
return False
if __name__ == '__main__':
datafilePath = input("File/directory to translate: ")
configFilepath = input("Schema file path for the above file/directory: ")
#do the translation, the library should provide error messages, if any
ndnNames = atmos_translator.argsForTranslation(datafilePath, configFilepath)
if ndnNames is False:
print("Error parsing config file, exiting")
sys.exit(-1)
if len(ndnNames) == 0:
print("No name returned from the translator, exiting.")
sys.exit(-1)
if __debug__:
print("Returned NDN names: %s" %(ndnNames))
#open connection to db
dbHost = input("Database Host?")
dbName = input("Database to store NDN names?")
dbUsername = input("Database username?")
dbPasswd = getpass.getpass("Database password?")
try:
con = sql.connect(user=dbUsername, database=dbName, password=dbPasswd, host=dbHost)
cursor = con.cursor()
if __debug__:
print("successfully connected to database")
#if tables do not exist, create tables
#if table already exists, or creation successful, continue
if createTables(cursor):
for ndnName in ndnNames:
#get list of files and insert into database
resInsertnames = insertName(cursor, ndnName)
if __debug__:
print("Return Code is %s for inserting name: %s" %(resInsertnames, ndnName))
con.commit()
else:
print("Error creating tables, exiting.")
sys.exit(-1)
except sql.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Incorrect username or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print("Error connecting to Database: %s" %(err.msg))
finally:
con.close()