Skip to content
tango_mysql_dump 2.54 KiB
Newer Older
# -*- coding: utf-8 -*-
#
# This file is part of the bliss project
#
# Copyright (c) 2016 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.

import argparse
import MySQLdb
from bliss.config import static

Vincent Michel's avatar
Vincent Michel committed

def main(host=None, user=None, passwd=None):
Vincent Michel's avatar
Vincent Michel committed
    conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db="tango")
    cursor = conn.cursor()
    cursor.execute('select name,server,class from device where ior like "ior:%"')
    server2nodes = {}
    device2nodes = {}
Vincent Michel's avatar
Vincent Michel committed
    for name, server, klass in cursor:
        if name.startswith("dserver"):
            continue

        node = server2nodes.get(server)
        if node is None:
Vincent Michel's avatar
Vincent Michel committed
            node = static.Node(
                config, filename="tango/%s.yml" % server.replace("/", "_")
            )
            exe_name, personal = server.split("/")
            node["server"] = exe_name
            node["personal_name"] = personal
            server2nodes[server] = node
Vincent Michel's avatar
Vincent Michel committed

        device_node = static.Node(config, parent=node)
        device_node["tango_name"] = name
        device_node["class"] = klass
        device2nodes[name] = device_node
Vincent Michel's avatar
Vincent Michel committed
        device_list = node.get("device")
Vincent Michel's avatar
Vincent Michel committed
            node["device"] = [device_node]
        else:
            device_list.append(device_node)
Vincent Michel's avatar
Vincent Michel committed
    # properties
Vincent Michel's avatar
Vincent Michel committed
    cursor.execute(
        "select device,name,value from property_device order by device,count"
    )
    for device, name, value in cursor:
        device_node = device2nodes.get(device)
        if device_node is None:
            continue
Vincent Michel's avatar
Vincent Michel committed
        properties = device_node.get("properties")
Vincent Michel's avatar
Vincent Michel committed
            properties = static.Node(config, parent=device_node)
            device_node["properties"] = properties

        values = properties.get(name)
        if values is None:
            properties[name] = value
        else:
Vincent Michel's avatar
Vincent Michel committed
            if isinstance(values, list):
Vincent Michel's avatar
Vincent Michel committed
                properties[name] = [values, value]

Vincent Michel's avatar
Vincent Michel committed
    for node in list(server2nodes.values()):
Vincent Michel's avatar
Vincent Michel committed


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
Vincent Michel's avatar
Vincent Michel committed
    parser.add_argument("--host", dest="host", help="host where mysql is running")
    parser.add_argument("--user", dest="user", help="mysql user")
    parser.add_argument("--passwd", dest="passwd", help="mysql password")
    options = parser.parse_args()
Vincent Michel's avatar
Vincent Michel committed
    main(host=options.host, user=options.user, passwd=options.passwd)