susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 3 | # |
| 4 | # Copyright (c) 2015, Colorado State University. |
| 5 | # |
| 6 | # This file is part of ndn-atmos. |
| 7 | # |
| 8 | # ndn-atmos is free software: you can redistribute it and/or modify it under the |
| 9 | # terms of the GNU Lesser General Public License as published by the Free Software |
| 10 | # Foundation, either version 3 of the License, or (at your option) any later version. |
| 11 | # |
| 12 | # ndn-atmos is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 14 | # PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 15 | # |
| 16 | # You should have received copies of the GNU General Public License and GNU Lesser |
| 17 | # General Public License along with ndn-atmos, e.g., in COPYING.md file. If not, see |
| 18 | # <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | # See AUTHORS.md for complete list of ndn-atmos authors and contributors. |
| 21 | |
| 22 | '''This module is for inserting NDN names in underlying database''' |
| 23 | |
| 24 | import mysql.connector as sql |
| 25 | from mysql.connector import errorcode |
| 26 | import sys |
| 27 | import getpass |
| 28 | import hashlib |
| 29 | from ndn_cmmap_translators import atmos_translator |
| 30 | |
| 31 | def createTables(cursor): |
| 32 | TABLES = {} |
| 33 | TABLES['cmip5'] = ( |
| 34 | "CREATE TABLE `cmip5` (" |
| 35 | " `id` int(100) AUTO_INCREMENT NOT NULL," |
| 36 | " `sha256` varchar(64) UNIQUE NOT NULL," |
| 37 | " `name` varchar(1000) NOT NULL," |
| 38 | " `activity` varchar(100) NOT NULL," |
| 39 | " `product` varchar(100) NOT NULL," |
| 40 | " `organization` varchar(100) NOT NULL," |
| 41 | " `model` varchar(100) NOT NULL," |
| 42 | " `experiment` varchar(100) NOT NULL," |
| 43 | " `frequency` varchar(100) NOT NULL," |
| 44 | " `modeling_realm` varchar(100) NOT NULL," |
| 45 | " `variable_name` varchar(100) NOT NULL," |
| 46 | " `ensemble` varchar(100) NOT NULL," |
| 47 | " `time` varchar(100) NOT NULL," |
| 48 | " PRIMARY KEY (`id`)" |
| 49 | ") ENGINE=InnoDB") |
| 50 | |
| 51 | #check if tables exist, if not create them |
| 52 | #create tables per schema in the wiki |
| 53 | for tableName, query in TABLES.items(): |
| 54 | try: |
| 55 | print("Creating table {}: ".format(tableName)) |
| 56 | cursor.execute(query) |
| 57 | print("Created table {}: ".format(tableName)) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 58 | return True |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 59 | except sql.Error as err: |
| 60 | if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: |
| 61 | print("Table already exists: %s " %(tableName)) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 62 | return True |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 63 | else: |
| 64 | print("Failed to create table: %s" %(err.msg)) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 65 | return False |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 66 | |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 67 | def insertName(cursor, name): |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 68 | if __debug__: |
| 69 | print("Name to insert %s " %(name)) |
| 70 | |
| 71 | #hashvalue is needed since name is too long for primary key (must be <767 bytes) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 72 | hashValue = hashlib.sha256(str(name).encode('utf-8')).hexdigest() |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 73 | if __debug__: |
| 74 | print("Hash of name %s " %(hashValue)) |
| 75 | |
| 76 | ##NOTE:must use %s to prevent sql injection in all sql queries |
| 77 | splitName = list(filter(None, name.split("/"))) |
| 78 | splitName.insert(0, hashValue) |
| 79 | splitName.insert(0, name) |
| 80 | splitName = tuple(splitName) |
| 81 | if __debug__: |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 82 | print("Name to insert in database %s" %(splitName,)) |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 83 | |
| 84 | addRecord = ("INSERT INTO cmip5 " |
| 85 | "(name, sha256, activity, product, organization, model, experiment, frequency,\ |
| 86 | modeling_realm, variable_name, ensemble, time) " |
| 87 | "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") |
| 88 | |
| 89 | try: |
| 90 | cursor.execute(addRecord, splitName) |
| 91 | print("Inserted record %s" %(name)) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 92 | return True |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 93 | except sql.Error as err: |
susmit shannigrahi | 851e88b | 2015-06-08 15:33:29 -0600 | [diff] [blame] | 94 | print("Error inserting name %s, \nError:%s" %(name, err.msg)) |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 95 | return False |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 96 | |
| 97 | if __name__ == '__main__': |
| 98 | datafilePath = input("File/directory to translate: ") |
| 99 | configFilepath = input("Schema file path for the above file/directory: ") |
| 100 | |
| 101 | #do the translation, the library should provide error messages, if any |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 102 | ndnNames = atmos_translator.argsForTranslation(datafilePath, configFilepath) |
susmit shannigrahi | 851e88b | 2015-06-08 15:33:29 -0600 | [diff] [blame] | 103 | if ndnNames is False: |
| 104 | print("Error parsing config file, exiting") |
| 105 | sys.exit(-1) |
| 106 | |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 107 | if len(ndnNames) == 0: |
| 108 | print("No name returned from the translator, exiting.") |
| 109 | sys.exit(-1) |
| 110 | if __debug__: |
| 111 | print("Returned NDN names: %s" %(ndnNames)) |
| 112 | |
| 113 | #open connection to db |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 114 | dbHost = input("Database Host?") |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 115 | dbName = input("Database to store NDN names?") |
| 116 | dbUsername = input("Database username?") |
| 117 | dbPasswd = getpass.getpass("Database password?") |
| 118 | |
| 119 | try: |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 120 | con = sql.connect(user=dbUsername, database=dbName, password=dbPasswd, host=dbHost) |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 121 | cursor = con.cursor() |
| 122 | if __debug__: |
| 123 | print("successfully connected to database") |
| 124 | |
| 125 | #if tables do not exist, create tables |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 126 | #if table already exists, or creation successful, continue |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 127 | if createTables(cursor): |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 128 | for ndnName in ndnNames: |
| 129 | #get list of files and insert into database |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 130 | resInsertnames = insertName(cursor, ndnName) |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 131 | if __debug__: |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 132 | print("Return Code is %s for inserting name: %s" %(resInsertnames, ndnName)) |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 133 | con.commit() |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 134 | else: |
| 135 | print("Error creating tables, exiting.") |
| 136 | sys.exit(-1) |
| 137 | |
susmit shannigrahi | 21009e3 | 2015-03-26 17:00:32 -0600 | [diff] [blame] | 138 | except sql.Error as err: |
| 139 | if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: |
| 140 | print("Incorrect username or password") |
| 141 | elif err.errno == errorcode.ER_BAD_DB_ERROR: |
| 142 | print("Database does not exist") |
| 143 | else: |
| 144 | print("Error connecting to Database: %s" %(err.msg)) |
| 145 | finally: |
| 146 | con.close() |
susmit shannigrahi | ec5a9fd | 2015-05-15 12:59:37 -0600 | [diff] [blame] | 147 | |