73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2019. Tobias Kurze
|
|
|
|
import hashlib
|
|
import logging
|
|
from datetime import datetime
|
|
from json import dumps
|
|
from pprint import pprint
|
|
|
|
from sqlalchemy import and_
|
|
|
|
from backend import db
|
|
from backend.models.recorder_model import RecorderModel, RecorderCommand
|
|
from backend.recorder_adapters import get_defined_recorder_adapters
|
|
|
|
|
|
def calculate_md5_checksum(string_to_md5_sum: str):
|
|
return hashlib.md5(string_to_md5_sum.encode('utf-8')).hexdigest()
|
|
|
|
|
|
def create_recorder_commands_for_recorder_adapter(command_definitions: dict, recorder_model: RecorderModel):
|
|
existing_recorder_commands = RecorderCommand.query.filter(
|
|
and_(RecorderCommand.name.in_(command_definitions.keys())),
|
|
RecorderCommand.recorder_model == recorder_model)
|
|
existing_commands = set()
|
|
for existing_command in existing_recorder_commands:
|
|
existing_commands.add(existing_command.name)
|
|
args = command_definitions.get(existing_command.name)
|
|
if dumps(existing_command.parameters) != dumps(args):
|
|
logging.warning(
|
|
"The function definition {} collides with an existing definition of the same name "
|
|
"but different parameters!".format(
|
|
existing_command.name))
|
|
existing_command.last_time_modified = datetime.utcnow()
|
|
existing_command.parameters = args
|
|
db.session.commit()
|
|
|
|
for c_d in set(command_definitions.keys()) - existing_commands:
|
|
# print(c_d)
|
|
args = command_definitions.get(c_d)
|
|
# create new recorder command(s)
|
|
r_c = RecorderCommand(name=c_d, parameters=args, recorder_model=recorder_model)
|
|
db.session.add(r_c)
|
|
db.session.commit()
|
|
|
|
|
|
def update_recorder_models_database():
|
|
r_as = get_defined_recorder_adapters()
|
|
for r_a in r_as:
|
|
r_m = RecorderModel.get_by_adapter_id(r_a["id"])
|
|
model_checksum = calculate_md5_checksum(dumps(r_a["commands"]))
|
|
if r_m is None:
|
|
r_m = RecorderModel(record_adapter_id=r_a["id"], model_name=r_a["name"], checksum=model_checksum,
|
|
requires_user=r_a.get('requires_user', None),
|
|
requires_password=r_a.get('requires_password', None))
|
|
db.session.add(r_m)
|
|
db.session.flush()
|
|
db.session.refresh(r_m)
|
|
else:
|
|
if not model_checksum == r_m.checksum:
|
|
r_m.model_name = r_a["name"]
|
|
r_m.model_name = r_a["name"]
|
|
r_m.checksum = model_checksum
|
|
create_recorder_commands_for_recorder_adapter(r_a["commands"], r_m)
|
|
db.session.commit()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
db.create_all()
|
|
update_recorder_models_database()
|