Commit b14cf2b0 authored by Julien David's avatar Julien David
Browse files

Merge branch '003-db-generator' into 'master'

WIP adding get_generator

See merge request !6
parents 9b877120 2865752f
Pipeline #1016 passed with stage
in 13 seconds
softwares/server/__pycache__/__init__.cpython-36.pyc
softwares/server/__pycache__/server.cpython-36.pyc
softwares/server/.vscode/settings.json
softwares/database/__init__.py
softwares/database/.vscode/settings.json
......@@ -8,17 +6,8 @@ softwares/.vscode/settings.json
tests/.vscode/settings.json
.vscode/settings.json
softwares/__init__.py
softwares/__pycache__/__init__.cpython-38.pyc
softwares/server/__pycache__/__init__.cpython-38.pyc
softwares/server/__pycache__/server.cpython-38.pyc
tests/__init__.py
tests/.vscode/settings.json
tests/.vscode/settings.json
tests/__pycache__/test_secret.cpython-38-pytest-6.0.2.pyc
tests/.vscode/settings.json
tests/.vscode/settings.json
tests/__pycache__/__init__.cpython-38.pyc
tests/__pycache__/test_query_valid.cpython-38-pytest-6.0.2.pyc
tests/__pycache__/test_mail.cpython-38-pytest-6.0.2.pyc
tests/.vscode/*
softwares/mailer/__init__.py
softwares/server/__init__.py
*/__pycache__/*
\ No newline at end of file
import mysql.connector
from get_docker_secret import get_docker_secret
import os
import random
# Exception thrown when no password has been defined to connect to the database
class NoPasswordDefineError(Exception):
......@@ -26,6 +27,7 @@ def get_password():
return password
return __rdos_password__
# Connect to the local MYSQL database on lipn-rdos
def database_connection():
mydb = mysql.connector.connect(
......@@ -76,7 +78,7 @@ def apply_for_job(database, job_id: str):
return mycursor.rowcount
# Returns a dictionnary containing each generator's parameters as a key. The value associated to each key is "is this parameter's value written in the comand line or in a separated file
# Returns a dictionary containing each generator's parameters as a key. The value associated to each key is "is this parameter's value written in the comand line or in a separated file
# in which case the value is the filename.
def getParameters(generators: dict):
database = database_connection()
......@@ -97,3 +99,56 @@ def getParameters(generators: dict):
database.disconnect()
return (parameters_basefile, parameters_default)
# Returns a dictionary containing all database generators existing in database
# The result returned contains every generators in database with parameters and default values for each field
def get_generators():
database = database_connection()
generators = {}
sql_get_new_job = "SELECT generators.tool, name, basefile, defaultValue from parameters inner join generators on parameters.idGenerator=generators.id"
mycursor = database.cursor(buffered=False)
mycursor.execute(sql_get_new_job)
myresult = mycursor.fetchall()
mycursor.close()
for result in myresult:
generators[result[0]] = [result]
# Insert in database query send from client and returns insert status
def db_insert(query: dict):
if(query is not None):
database = database_connection()
if(db_job_check(query) != 0):
query['id'] = str(random.getrandbits(64))
try:
values = query.values()
cols = query.keys()
sql_new_job_row = "INSERT INTO jobs (%s) VALUES ('%s');" % (", ".join(cols), "','".join(values))
mycursor = database.cursor(buffered=True)
mycursor.execute(sql_new_job_row)
print("Record inserteds in DB :", query.values)
database.commit()
mycursor.close()
database.close()
except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err))
# Checks existance of query ID in database and returns the status
def db_job_check(query: dict):
if (query is not None):
db = database_connection()
try:
ret = 0
val = query['id']
sql_new_job_row = "SELECT * FROM jobs WHERE ID=('%s') LIMIT 1;" % (val)
mycursor = db.cursor(buffered=True)
mycursor.execute(sql_new_job_row)
db.commit()
ret = mycursor.rowcount
mycursor.close()
db.close()
return ret
except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err))
......@@ -10,7 +10,7 @@ sys.path.append(os.path.realpath('../softwares/'))
__RDOS_Dict__ = {}
# tools List
__RDOS_Tool__ = []
__RDOS_Tool__ = {}
class RdosServer:
......@@ -115,3 +115,29 @@ def verif_param(data: dict):
raise Exception("Query Doesn't match")
else:
raise Exception("Error Database")
# Function db_generators returns database generators
# Function takes a dictionary request from client side
# Function returns all database generators or raises an error if request doesn't match
def db_generators(req: dict):
match = {"request": "generators"}
if(req == match):
s = db.get_generators()
return s
else:
raise Exception("Error Database")
# Function db_insert send a query to insert in database by socket
# Function db_insert takes a socket and dictionary for input
# Function db_insert returns a string if query send
def db_insert(s: socket, req: dict):
if(req is not None):
if (verif_param(req) == req):
ins = db.insert_query(req)
s.send(bytes(json.dumps(ins), "utf-8"))
print("Query send")
else:
raise Exception("Query Doesn't match")
raise Exception("Invalid Query")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment