def _downloadCfs(self, cfsurl, **kwargs):
        """Get daya from channel finder web service

        :param cfsurl:
        :param kwargs:
        :return:
        """
        keep_prpts = kwargs.pop('keep', None)
        converter  = kwargs.pop('converter', {})
        from channelfinder import ChannelFinderClient
        cf = ChannelFinderClient(BaseURL = cfsurl)
        if len(kwargs) == 0:
            chs = cf.find(name='*')
        else:
            #print kwargs
            chs = cf.find(**kwargs)
        if not chs:
            return

        if keep_prpts is None:
            # use all possible property names
            keep_prpts = [p.Name for p in cf.getAllProperties()]
            
        #print "# include properties", properties
        for ch in chs:
            # keep only known properties
            prptdict = ch.getProperties()
            # prpts is known part from prptdict, otherwise empty dict
            if prptdict is not None:
                prpts = dict([v for v in prptdict.iteritems()])
                # convert the data type
            else:
                prpts = None
            # the empty tags could be None
            if self.use_unicode:
                self.results.append([unicode(ch.Name),
                                  dict([(unicode(k), unicode(v))
                                        for k,v in prpts.iteritems()]),
                                  [unicode(v) for v in ch.getTags()]])
            else:
                self.results.append([ch.Name.encode('ascii'),
                                  dict([(k.encode('ascii'), v.encode('ascii'))
                                        for k,v in prpts.iteritems()]),
                                  [v.encode('ascii') for v in ch.getTags()]])
            if self.results[-1][1]:
                for k in converter:
                    self.results[-1][1][k] = converter[k](prpts[k])
            # warn if hostName or iocName does not present
            if self.host_name not in self.results[-1][1]:
                _logger.warn("no 'hostName' for {0}".format(self.results[-1]))
            if self.ioc_name not in self.results[-1][1]:
                _logger.warn("no 'iocName' for {0}".format(self.results[-1]))

            del prptdict
Exemple #2
0
def initCfs(fpv, sep=","):
    dat = {}
    for line in open(fpv, 'r').readlines():
        #pv, hdl, name, idx, fam, fld
        rec = [v.strip() for v in line.split(sep)]
        pv, prpt = rec[0], rec[1:]
        dat.setdefault(pv, [])
        dat[pv].append(tuple(rec[1:]))

    import conf
    cfinput = {
        'BaseURL': cfsurl,
        'username': conf.username,
        'password': conf.password
    }
    cf = ChannelFinderClient(**cfinput)
    prpts = [p.Name for p in cf.getAllProperties()]
    for p in ["elemName", "elemIndex", "elemType", "elemField"]:
        if p in prpts: continue
        # property owner is cf-asd
        logging.info("add new property '%s'" % p)
        cf.set(property=Property(p, "cf-asd"))

    for pv,prptsets in dat.items():
        if len(prptsets) == 1:
            hdl, name, idx, fam, fld = prptsets[0]
            #cf.update(channel=Channel(pv, "cf-update", properties=[
            #    Property("elemHandle", "cf-asd", hdl),
            #    Property("elemName",   "cf-asd", name),
            #    Property("elemIndex",  "cf-asd", idx),
            #    Property("elemType",   "cf-asd", fam),
            #    Property("elemField",  "cf-asd", fld)]))
            cf.update(property=Property("elemHandle", "cf-asd", hdl),
                      channelName=pv)
            cf.update(property=Property("elemName",   "cf-asd", name),
                      channelName=pv)
            cf.update(property=Property("elemIndex",  "cf-asd", idx),
                      channelName=pv)
            cf.update(property=Property("elemType",   "cf-asd", fam),
                      channelName=pv)
            cf.update(property=Property("elemField",  "cf-asd", fld),
                      channelName=pv)
            logging.info("Done with %s" % pv)
Exemple #3
0
def initCfs(fpv, sep=","):
    dat = {}
    for line in open(fpv, 'r').readlines():
        #pv, hdl, name, idx, fam, fld
        rec = [v.strip() for v in line.split(sep)]
        pv, prpt = rec[0], rec[1:]
        dat.setdefault(pv, [])
        dat[pv].append(tuple(rec[1:]))

    import conf
    cfinput = {
        'BaseURL': cfsurl,
        'username': conf.username,
        'password': conf.password
    }
    cf = ChannelFinderClient(**cfinput)
    prpts = [p.Name for p in cf.getAllProperties()]
    for p in ["elemName", "elemIndex", "elemType", "elemField"]:
        if p in prpts: continue
        # property owner is cf-asd
        logging.info("add new property '%s'" % p)
        cf.set(property=Property(p, "cf-asd"))

    for pv, prptsets in dat.items():
        if len(prptsets) == 1:
            hdl, name, idx, fam, fld = prptsets[0]
            #cf.update(channel=Channel(pv, "cf-update", properties=[
            #    Property("elemHandle", "cf-asd", hdl),
            #    Property("elemName",   "cf-asd", name),
            #    Property("elemIndex",  "cf-asd", idx),
            #    Property("elemType",   "cf-asd", fam),
            #    Property("elemField",  "cf-asd", fld)]))
            cf.update(property=Property("elemHandle", "cf-asd", hdl),
                      channelName=pv)
            cf.update(property=Property("elemName", "cf-asd", name),
                      channelName=pv)
            cf.update(property=Property("elemIndex", "cf-asd", idx),
                      channelName=pv)
            cf.update(property=Property("elemType", "cf-asd", fam),
                      channelName=pv)
            cf.update(property=Property("elemField", "cf-asd", fld),
                      channelName=pv)
            logging.info("Done with %s" % pv)
Exemple #4
0
class CFProcessor(service.Service):
    def __init__(self, name, conf):
        _log.info("CF_INIT %s", name)
        self.name, self.conf = name, conf
        self.channel_dict = defaultdict(list)
        self.iocs = dict()
        self.client = None
        self.currentTime = getCurrentTime
        self.lock = DeferredLock()

    def startService(self):
        service.Service.startService(self)
        self.running = 1
        _log.info("CF_START")

        if self.client is None:  # For setting up mock test client
            """
            Using the default python cf-client.  The url, username, and
            password are provided by the channelfinder._conf module.
            """
            from channelfinder import ChannelFinderClient
            self.client = ChannelFinderClient()
            try:
                cf_props = [
                    prop['name'] for prop in self.client.getAllProperties()
                ]

                reqd_props = {
                    'hostName', 'iocName', 'pvStatus', 'time', 'iocid'
                }

                wl = self.conf.get('infotags', list())
                whitelist = [s.strip(', ') for s in wl.split()] \
                    if wl else wl
                # Are any required properties not already present on CF?
                properties = reqd_props - set(cf_props)
                # Are any whitelisted properties not already present on CF?
                # If so, add them too.
                properties.update(set(whitelist) - set(cf_props))

                owner = self.conf.get('username', 'cfstore')
                for prop in properties:
                    self.client.set(property={u'name': prop, u'owner': owner})

                self.whitelist = set(whitelist)
                _log.debug('WHITELIST = {}'.format(self.whitelist))
            except ConnectionError:
                _log.exception("Cannot connect to Channelfinder service")
                raise
            else:
                self.clean_service()

    def stopService(self):
        service.Service.stopService(self)
        # Set channels to inactive and close connection to client
        self.running = 0
        self.clean_service()
        _log.info("CF_STOP")

    @defer.inlineCallbacks
    def commit(self, transaction_record):
        yield self.lock.acquire()
        try:
            yield deferToThread(self.__commit__, transaction_record)
        finally:
            self.lock.release()

    def __commit__(self, TR):
        _log.debug("CF_COMMIT %s", TR.infos.items())
        """
        a dictionary with a list of records with their associated property info  
        pvInfo 
        {rid: { "pvName":"recordName",
                "infoProperties":{propName:value, ...}}}
        """

        iocName = TR.infos.get('IOCNAME') or TR.src.port
        hostName = TR.infos.get('HOSTNAME') or TR.src.host
        owner = TR.infos.get('ENGINEER') or TR.infos.get(
            'CF_USERNAME') or self.conf.get('username', 'cfstore')
        time = self.currentTime()

        pvInfo = {}
        for rid, (rname, rtype) in TR.addrec.items():
            pvInfo[rid] = {"pvName": rname}
        for rid, (recinfos) in TR.recinfos.items():
            # find intersection of these sets
            recinfo_wl = [p for p in self.whitelist if p in recinfos.keys()]
            if recinfo_wl:
                pvInfo[rid]['infoProperties'] = list()
                for infotag in recinfo_wl:
                    _log.debug('INFOTAG = {}'.format(infotag))
                    property = {
                        u'name': infotag,
                        u'owner': owner,
                        u'value': recinfos[infotag]
                    }
                    pvInfo[rid]['infoProperties'].append(property)
        _log.debug(pvInfo)

        pvNames = [info["pvName"] for rid, (info) in pvInfo.items()]

        delrec = list(TR.delrec)
        _log.info("DELETED records " + str(delrec))

        host = TR.src.host
        port = TR.src.port
        """The unique identifier for a particular IOC"""
        iocid = host + ":" + str(port)
        _log.info("CF_COMMIT: " + iocid)

        if TR.initial:
            """Add IOC to source list """
            self.iocs[iocid] = {
                "iocname": iocName,
                "hostname": hostName,
                "owner": owner,
                "time": time,
                "channelcount": 0
            }
        if not TR.connected:
            delrec.extend(self.channel_dict.keys())
        for pv in pvNames:
            self.channel_dict[pv].append(
                iocid)  # add iocname to pvName in dict
            self.iocs[iocid]["channelcount"] += 1
        for pv in delrec:
            if iocid in self.channel_dict[pv]:
                self.channel_dict[pv].remove(iocid)
                if iocid in self.iocs:
                    self.iocs[iocid]["channelcount"] -= 1
                if self.iocs[iocid]['channelcount'] == 0:
                    self.iocs.pop(iocid, None)
                elif self.iocs[iocid]['channelcount'] < 0:
                    _log.error("channel count negative!")
                if len(self.channel_dict[pv]
                       ) <= 0:  # case: channel has no more iocs
                    del self.channel_dict[pv]
        poll(__updateCF__, self.client, pvInfo, delrec, self.channel_dict,
             self.iocs, hostName, iocName, iocid, owner, time)
        dict_to_file(self.channel_dict, self.iocs, self.conf)

    def clean_service(self):
        """
        Marks all channels as "Inactive" until the recsync server is back up
        """
        sleep = 1
        retry_limit = 5
        owner = self.conf.get('username', 'cfstore')
        while 1:
            try:
                _log.debug("Cleaning service...")
                channels = self.client.findByArgs([('pvStatus', 'Active')])
                if channels is not None:
                    new_channels = []
                    for ch in channels or []:
                        new_channels.append(ch[u'name'])
                    if len(new_channels) > 0:
                        self.client.update(property={
                            u'name': 'pvStatus',
                            u'owner': owner,
                            u'value': "Inactive"
                        },
                                           channelNames=new_channels)
                    _log.debug("Service clean.")
                    return
            except RequestException:
                _log.exception("cleaning failed, retrying: ")

            time.sleep(min(60, sleep))
            sleep *= 1.5
            if self.running == 0 and sleep >= retry_limit:
                _log.debug("Abandoning clean.")
                return
Exemple #5
0
class CFProcessor(service.Service):
    def __init__(self, name, conf):
        _log.info("CF_INIT %s", name)
        self.name, self.conf = name, conf
        self.channel_dict = defaultdict(list)
        self.iocs = dict()
        self.client = None
        self.currentTime = getCurrentTime
        self.lock = DeferredLock()

    def startService(self):
        service.Service.startService(self)
        # Returning a Deferred is not supported by startService(),
        # so instead attempt to acquire the lock synchonously!
        d = self.lock.acquire()
        if not d.called:
            d.cancel()
            service.Service.stopService(self)
            raise RuntimeError(
                'Failed to acquired CF Processor lock for service start')

        try:
            self._startServiceWithLock()
        except:
            service.Service.stopService(self)
            raise
        finally:
            self.lock.release()

    def _startServiceWithLock(self):
        _log.info("CF_START")

        if self.client is None:  # For setting up mock test client
            """
            Using the default python cf-client.  The url, username, and
            password are provided by the channelfinder._conf module.
            """
            from channelfinder import ChannelFinderClient
            self.client = ChannelFinderClient()
            try:
                cf_props = [
                    prop['name'] for prop in self.client.getAllProperties()
                ]
                if (self.conf.get('alias', 'default') == 'on'):
                    reqd_props = {
                        'hostName', 'iocName', 'pvStatus', 'time', 'iocid',
                        'alias'
                    }
                else:
                    reqd_props = {
                        'hostName', 'iocName', 'pvStatus', 'time', 'iocid'
                    }
                wl = self.conf.get('infotags', list())
                whitelist = [s.strip(', ') for s in wl.split()] \
                    if wl else wl
                # Are any required properties not already present on CF?
                properties = reqd_props - set(cf_props)
                # Are any whitelisted properties not already present on CF?
                # If so, add them too.
                properties.update(set(whitelist) - set(cf_props))

                owner = self.conf.get('username', 'cfstore')
                for prop in properties:
                    self.client.set(property={u'name': prop, u'owner': owner})

                self.whitelist = set(whitelist)
                _log.debug('WHITELIST = {}'.format(self.whitelist))
            except ConnectionError:
                _log.exception("Cannot connect to Channelfinder service")
                raise
            else:
                if self.conf.getboolean('cleanOnStart', True):
                    self.clean_service()

    def stopService(self):
        service.Service.stopService(self)
        return self.lock.run(self._stopServiceWithLock)

    def _stopServiceWithLock(self):
        # Set channels to inactive and close connection to client
        if self.conf.getboolean('cleanOnStop', True):
            self.clean_service()
        _log.info("CF_STOP")

    # @defer.inlineCallbacks # Twisted v16 does not support cancellation!
    def commit(self, transaction_record):
        return self.lock.run(self._commitWithLock, transaction_record)

    def _commitWithLock(self, TR):
        self.cancelled = False

        t = deferToThread(self._commitWithThread, TR)

        def cancelCommit(d):
            self.cancelled = True
            d.callback(None)

        d = defer.Deferred(cancelCommit)

        def waitForThread(_ignored):
            if self.cancelled:
                return t

        d.addCallback(waitForThread)

        def chainError(err):
            if not err.check(defer.CancelledError):
                _log.error("CF_COMMIT FAILURE: %s", err)
            if self.cancelled:
                if not err.check(defer.CancelledError):
                    raise defer.CancelledError()
                return err
            else:
                d.callback(None)

        def chainResult(_ignored):
            if self.cancelled:
                raise defer.CancelledError()
            else:
                d.callback(None)

        t.addCallbacks(chainResult, chainError)
        return d

    def _commitWithThread(self, TR):
        if not self.running:
            raise defer.CancelledError(
                'CF Processor is not running (TR: %s:%s)', TR.src.host,
                TR.src.port)

        _log.info("CF_COMMIT: %s", TR)
        """
        a dictionary with a list of records with their associated property info  
        pvInfo 
        {rid: { "pvName":"recordName",
                "infoProperties":{propName:value, ...}}}
        """

        host = TR.src.host
        port = TR.src.port
        iocName = TR.infos.get('IOCNAME') or TR.src.port
        hostName = TR.infos.get('HOSTNAME') or TR.src.host
        owner = TR.infos.get('ENGINEER') or TR.infos.get(
            'CF_USERNAME') or self.conf.get('username', 'cfstore')
        time = self.currentTime()
        """The unique identifier for a particular IOC"""
        iocid = host + ":" + str(port)

        pvInfo = {}
        for rid, (rname, rtype) in TR.addrec.items():
            pvInfo[rid] = {"pvName": rname}
        for rid, (recinfos) in TR.recinfos.items():
            # find intersection of these sets
            if rid not in pvInfo:
                _log.warn('IOC: %s: PV not found for recinfo with RID: %s',
                          iocid, rid)
                continue
            recinfo_wl = [p for p in self.whitelist if p in recinfos.keys()]
            if recinfo_wl:
                pvInfo[rid]['infoProperties'] = list()
                for infotag in recinfo_wl:
                    property = {
                        u'name': infotag,
                        u'owner': owner,
                        u'value': recinfos[infotag]
                    }
                    pvInfo[rid]['infoProperties'].append(property)
        for rid, alias in TR.aliases.items():
            if rid not in pvInfo:
                _log.warn('IOC: %s: PV not found for alias with RID: %s',
                          iocid, rid)
                continue
            pvInfo[rid]['aliases'] = alias

        delrec = list(TR.delrec)
        _log.debug("Delete records: %s", delrec)

        pvInfoByName = {}
        for rid, (info) in pvInfo.items():
            if info["pvName"] in pvInfoByName:
                _log.warn(
                    "Commit contains multiple records with PV name: %s (%s)",
                    pv, iocid)
                continue
            pvInfoByName[info["pvName"]] = info
            _log.debug("Add record: %s: %s", rid, info)

        if TR.initial:
            """Add IOC to source list """
            self.iocs[iocid] = {
                "iocname": iocName,
                "hostname": hostName,
                "owner": owner,
                "time": time,
                "channelcount": 0
            }
        if not TR.connected:
            delrec.extend(self.channel_dict.keys())
        for pv in pvInfoByName.keys():
            self.channel_dict[pv].append(
                iocid)  # add iocname to pvName in dict
            self.iocs[iocid]["channelcount"] += 1
            """In case, alias exists"""
            if (self.conf.get('alias', 'default' == 'on')):
                if pv in pvInfoByName and "aliases" in pvInfoByName[pv]:
                    for a in pvInfoByName[pv]["aliases"]:
                        self.channel_dict[a].append(
                            iocid)  # add iocname to pvName in dict
                        self.iocs[iocid]["channelcount"] += 1
        for pv in delrec:
            if iocid in self.channel_dict[pv]:
                self.channel_dict[pv].remove(iocid)
                if iocid in self.iocs:
                    self.iocs[iocid]["channelcount"] -= 1
                if self.iocs[iocid]['channelcount'] == 0:
                    self.iocs.pop(iocid, None)
                elif self.iocs[iocid]['channelcount'] < 0:
                    _log.error("Channel count negative: %s", iocid)
                if len(self.channel_dict[pv]
                       ) <= 0:  # case: channel has no more iocs
                    del self.channel_dict[pv]
                """In case, alias exists"""
                if (self.conf.get('alias', 'default' == 'on')):
                    if pv in pvInfoByName and "aliases" in pvInfoByName[pv]:
                        for a in pvInfoByName[pv]["aliases"]:
                            self.channel_dict[a].remove(iocid)
                            if iocid in self.iocs:
                                self.iocs[iocid]["channelcount"] -= 1
                            if self.iocs[iocid]['channelcount'] == 0:
                                self.iocs.pop(iocid, None)
                            elif self.iocs[iocid]['channelcount'] < 0:
                                _log.error("Channel count negative: %s", iocid)
                            if len(self.channel_dict[a]
                                   ) <= 0:  # case: channel has no more iocs
                                del self.channel_dict[a]
        poll(__updateCF__, self, pvInfoByName, delrec, hostName, iocName,
             iocid, owner, time)
        dict_to_file(self.channel_dict, self.iocs, self.conf)

    def clean_service(self):
        """
        Marks all channels as "Inactive" until the recsync server is back up
        """
        sleep = 1
        retry_limit = 5
        owner = self.conf.get('username', 'cfstore')
        while 1:
            try:
                _log.info("CF Clean Started")
                channels = self.client.findByArgs(
                    prepareFindArgs(self.conf, [('pvStatus', 'Active')]))
                if channels is not None:
                    new_channels = []
                    for ch in channels or []:
                        new_channels.append(ch[u'name'])
                    _log.info("Total channels to update: %s",
                              len(new_channels))
                    while len(new_channels) > 0:
                        _log.debug(
                            'Update "pvStatus" property to "Inactive" for %s channels',
                            min(len(new_channels), 10000))
                        self.client.update(property={
                            u'name': 'pvStatus',
                            u'owner': owner,
                            u'value': "Inactive"
                        },
                                           channelNames=new_channels[:10000])
                        new_channels = new_channels[10000:]
                    _log.info("CF Clean Completed")
                    return
            except RequestException as e:
                _log.error("Clean service failed: %s", e)

            _log.info("Clean service retry in %s seconds", min(60, sleep))
            time.sleep(min(60, sleep))
            sleep *= 1.5
            if self.running == 0 and sleep >= retry_limit:
                _log.info("Abandoning clean after %s seconds", retry_limit)
                return
Exemple #6
0
def cfs_append_from_sqlite(fname, update_only):
    sq = ap.chanfinder.ChannelFinderAgent()
    sq.loadSqlite(fname)

    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']

    allpvs = []
    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    prpt_data, tag_data = {}, {}
    # the data body
    for pv, prpts, tags in sq.rows:
        if not pv: continue
        if pv in allpvs: continue
        if pv.find("SR:") != 0: continue
        logging.info("updating '{0}'".format(pv))

        allpvs.append(pv)
        prpt_list, tag_list = [], []
        for k, v in prpts.items():
            if k not in [
                    "elemIndex", "system", "elemType", "elemHandle",
                    "elemName", "elemField"
            ]:
                continue
            prpt_data.setdefault((k, v), [])
            prpt_data[(k, v)].append(pv)
        for tag in tags:
            if not tag.startswith("aphla."): continue
            tag_data.setdefault(tag, [])
            tag_data[tag].append(pv)
            #tag_list.append(Tag(r.strip()), tag_owner)
            logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner))
            #addPvTag(cf, pv, tag, tag_owner)

    errpvs = []
    for pv in allpvs:
        chs = cf.find(name=pv)
        if not chs:
            errpvs.append(pv)
            print("PV '%s' does not exist" % pv)
            continue
        elif len(chs) != 1:
            print("Find two results for pv=%s" % pv)
            continue
        prpts = chs[0].getProperties()
        if not prpts: continue
        for prpt, val in prpts.items():
            pvlst = prpt_data.get((prpt, val), [])
            if not pvlst: continue
            try:
                j = pvlst.index(pv)
                prpt_data[(prpt, val)].pop(j)
            except:
                # the existing data is not in the update list, skip
                pass

    #if errpvs:
    #    #raise RuntimeError("PVs '{0}' are missing".format(errpvs))
    #    print

    logging.warn("{0} does not exist in DB".format(errpvs))

    for k, v in prpt_data.items():
        vf = [pv for pv in v if pv not in errpvs]
        if not vf:
            logging.info("no valid PVs for {0}".format(k))
            continue
        updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf)
        logging.info("add property {0} for pvs {1}".format(k, vf))
    for k, v in tag_data.items():
        vf = [pv for pv in v if pv not in errpvs]
        addTagPvs(cf, k, vf, tag_owner)
        logging.info("add tag {0} for pvs {1}".format(k, vf))
Exemple #7
0
def cfs_append_from_csv2(rec_list, update_only):
    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']
    import csv
    rd = csv.reader(rec_list)

    allpvs = []
    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    prpt_data, tag_data = {}, {}
    # the data body
    for s in rd:
        if not s: continue
        if not s[0].strip(): continue
        if s[0].strip().startswith('#'): continue

        pv = s[0].strip()
        logging.info("updating '{0}'".format(pv))
        #cf.update(property=Property('elemType', PRPTOWNER, 'QUAD'),
        #          channelNames = [pv])
        #chs = cf.find(name=pv)
        #sys.exit(0)
        #logging.info("{0} and {1}".format(chs[0].Name, type(chs[0].Name)))
        #logging.info("{0} and {1}".format(pv, type(pv)))

        allpvs.append(pv)
        prpt_list, tag_list = [], []
        for r in s[1:]:
            if r.find('=') > 0:
                prpt, val = [v.strip() for v in r.split('=')]
                prpt_data.setdefault((prpt, val), [])
                prpt_data[(prpt, val)].append(pv)
            else:
                # it is a tag
                tag = r.strip()
                if not tag: continue
                tag_data.setdefault(tag, [])
                tag_data[tag].append(pv)
                #tag_list.append(Tag(r.strip()), tag_owner)
                logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner))
                #addPvTag(cf, pv, tag, tag_owner)

    errpvs = []
    for pv in allpvs:
        chs = cf.find(name=pv)
        if not chs:
            errpvs.append(pv)
            print("PV '%s' does not exist" % pv)
            continue
        elif len(chs) != 1:
            print("Find two results for pv=%s" % pv)
            continue
        for prpt, val in chs[0].getProperties().items():
            pvlst = prpt_data.get((prpt, val), [])
            if not pvlst: continue
            try:
                j = pvlst.index(pv)
                prpt_data[(prpt, val)].pop(j)
            except:
                # the existing data is not in the update list, skip
                pass

    #if errpvs:
    #    #raise RuntimeError("PVs '{0}' are missing".format(errpvs))
    #    print

    logging.warn("{0} does not exist in DB".format(errpvs))

    for k, v in prpt_data.items():
        vf = [pv for pv in v if pv not in errpvs]
        if not vf:
            logging.info("no valid PVs for {0}".format(k))
            continue
        updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf)
        logging.info("add property {0} for pvs {1}".format(k, vf))
    for k, v in tag_data.items():
        vf = [pv for pv in v if pv not in errpvs]
        addTagPvs(cf, k, vf, tag_owner)
        logging.info("add tag {0} for pvs {1}".format(k, vf))
Exemple #8
0
def cfs_append_from_csv1(rec_list, update_only):
    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']
    import csv
    rd = csv.reader(rec_list)
    # header line
    header = rd.next()
    # lower case of header
    hlow = [s.lower() for s in header]
    # number of headers, pv + properties
    nheader = len(header)
    # the index of PV, properties and tags
    ipv = hlow.index('pv')
    # the code did not rely on it, but it is a good practice
    if ipv != 0:
        raise RuntimeError("the first column should be pv")

    iprpt, itags = [], []
    for i, h in enumerate(header):
        if i == ipv: continue
        # if the header is empty, it is a tag
        if len(h.strip()) == 0:
            itags.append(i)
        else:
            iprpt.append(i)

    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    tags = {}
    # the data body
    for s in rd:
        prpts = [Property(header[i], prpt_owner, s[i]) for i in iprpt if s[i]]
        # itags could be empty if we put all tags in the end columns
        for i in itags + range(nheader, len(s)):
            rec = tags.setdefault(s[i].strip(), [])
            rec.append(s[ipv].strip())

        #print s[ipv], prpts, tags
        ch = cf.find(name=s[ipv])
        if ch is None:
            logging.warning("pv {0} does not exist".format(s[ipv]))
        elif len(ch) > 1:
            logging.warning("pv {0} is not unique ({1})".format(
                s[ipv], len(ch)))
        else:
            for p in prpts:
                #continue
                if p.Name in ignore_prpts: continue
                #if p.Name != 'symmetry': continue
                logging.info("updating '{0}' with property, {1}={2}".format(
                    s[ipv], p.Name, p.Value))
                cf.update(channelName=s[ipv], property=p)

    logging.info("finished updating properties")
    for t, pvs in tags.items():
        if not hasTag(cf, t): cf.set(tag=Tag(t, tag_owner))
        if 'V:1-SR-BI{BETA}X-I' in pvs: continue
        if 'V:1-SR-BI{BETA}Y-I' in pvs: continue
        try:
            cf.update(tag=Tag(t, tag_owner), channelNames=pvs)
        except:
            print(t, pvs)
            raise

        logging.info("update '{0}' for {1} pvs".format(t, len(pvs)))
    logging.info("finished updating tags")
Exemple #9
0
    def downloadCfs(self, cfsurl, **kwargs):
        """
        downloads data from channel finder service.
        
        :param cfsurl: the URL of channel finder service.
        :type cfsurl: str
        :param keep: if present, it only downloads specified properties.
        :type keep: list
        :param converter: convert properties from string to other format.
        :type converter: dict

        :Example:

            >>> prpt_list = ['elemName', 'sEnd']
            >>> conv_dict = {'sEnd', float}
            >>> downloadCfs(URL, keep = prpt_list, converter = conv_dict)
            >>> downloadCfs(URL, property=[('hostName', 'virtac2')])
            >>> downloadCfs(URL, property=[('hostName', 'virtac')], tagName='aphla.*')

        The channel finder client API provides *property* and *tagName* as
        keywords parameters. 
        """
        keep_prpts = kwargs.pop('keep', None)
        converter = kwargs.pop('converter', {})
        self.source = cfsurl

        from channelfinder import ChannelFinderClient
        cf = ChannelFinderClient(BaseURL=cfsurl)
        if len(kwargs) == 0:
            chs = cf.find(name='*')
        else:
            #print kwargs
            chs = cf.find(**kwargs)
        if not chs: return

        if keep_prpts is None:
            # use all possible property names
            keep_prpts = [p.Name for p in cf.getAllProperties()]

        #print "# include properties", properties
        for ch in chs:
            # keep only known properties
            prptdict = ch.getProperties()
            # prpts is known part from prptdict, otherwise empty dict
            if prptdict is not None:
                prpts = dict([v for v in prptdict.iteritems()])
                # convert the data type
            else:
                prpts = None
            # the empty tags could be None
            if self.use_unicode:
                self.rows.append([
                    unicode(ch.Name),
                    dict([(unicode(k), unicode(v))
                          for k, v in prpts.iteritems()]),
                    [unicode(v) for v in ch.getTags()]
                ])
            else:
                self.rows.append([
                    ch.Name.encode('ascii'),
                    dict([(k.encode('ascii'), v.encode('ascii'))
                          for k, v in prpts.iteritems()]),
                    [v.encode('ascii') for v in ch.getTags()]
                ])
            if self.rows[-1][1]:
                for k in converter:
                    self.rows[-1][1][k] = converter[k](prpts[k])
            # warn if hostName or iocName does not present
            if "hostName" not in self.rows[-1][1]:
                _logger.warn("no 'hostName' for {0}".format(self.rows[-1]))
            if "iocName" not in self.rows[-1][1]:
                _logger.warn("no 'iocName' for {0}".format(self.rows[-1]))

            del prptdict
Exemple #10
0
    def downloadCfs(self, cfsurl, **kwargs):
        """
        downloads data from channel finder service.
        
        :param cfsurl: the URL of channel finder service.
        :type cfsurl: str
        :param keep: if present, it only downloads specified properties.
        :type keep: list
        :param converter: convert properties from string to other format.
        :type converter: dict

        :Example:

            >>> prpt_list = ['elemName', 'sEnd']
            >>> conv_dict = {'sEnd', float}
            >>> downloadCfs(URL, keep = prpt_list, converter = conv_dict)
            >>> downloadCfs(URL, property=[('hostName', 'virtac2')])
            >>> downloadCfs(URL, property=[('hostName', 'virtac')], tagName='aphla.*')

        The channel finder client API provides *property* and *tagName* as
        keywords parameters. 
        """
        keep_prpts = kwargs.pop('keep', None)
        converter  = kwargs.pop('converter', {})
        self.source = cfsurl

        from channelfinder import ChannelFinderClient
        cf = ChannelFinderClient(BaseURL = cfsurl)
        if len(kwargs) == 0:
            chs = cf.find(name='*')
        else:
            #print kwargs
            chs = cf.find(**kwargs)
        if not chs: return

        if keep_prpts is None:
            # use all possible property names
            keep_prpts = [p.Name for p in cf.getAllProperties()]
            
        #print "# include properties", properties
        for ch in chs:
            # keep only known properties
            prptdict = ch.getProperties()
            # prpts is known part from prptdict, otherwise empty dict
            if prptdict is not None:
                prpts = dict([v for v in prptdict.iteritems()])
                # convert the data type
            else:
                prpts = None
            # the empty tags could be None
            if self.use_unicode:
                self.rows.append([unicode(ch.Name), 
                                  dict([(unicode(k), unicode(v))
                                        for k,v in prpts.iteritems()]),
                                  [unicode(v) for v in ch.getTags()]])
            else:
                self.rows.append([ch.Name.encode('ascii'), 
                                  dict([(k.encode('ascii'), v.encode('ascii'))
                                        for k,v in prpts.iteritems()]),
                                  [v.encode('ascii') for v in ch.getTags()]])
            if self.rows[-1][1]:
                for k in converter:
                    self.rows[-1][1][k] = converter[k](prpts[k])
            # warn if hostName or iocName does not present
            if "hostName" not in self.rows[-1][1]:
                _logger.warn("no 'hostName' for {0}".format(self.rows[-1]))
            if "iocName" not in self.rows[-1][1]:
                _logger.warn("no 'iocName' for {0}".format(self.rows[-1]))

            del prptdict
Exemple #11
0
class CFProcessor(service.Service):
    def __init__(self, name, conf):
        _log.info("CF_INIT %s", name)
        self.name, self.conf = name, conf
        self.channel_dict = defaultdict(list)
        self.iocs = dict()
        self.client = None
        self.currentTime = getCurrentTime
        self.lock = DeferredLock()

    def startService(self):
        service.Service.startService(self)
        self.running = 1
        _log.info("CF_START")

        if self.client is None:  # For setting up mock test client
            """
            Using the default python cf-client.  The url, username, and
            password are provided by the channelfinder._conf module.
            """
            from channelfinder import ChannelFinderClient
            self.client = ChannelFinderClient()
            try:
                cf_props = [prop['name'] for prop in self.client.getAllProperties()]
                if (self.conf.get('alias', 'default') == 'on'):
                    reqd_props = {'hostName', 'iocName', 'pvStatus', 'time', 'iocid', 'alias'}
                else:
                    reqd_props = {'hostName', 'iocName', 'pvStatus', 'time', 'iocid'}
                wl = self.conf.get('infotags', list())
                whitelist = [s.strip(', ') for s in wl.split()] \
                    if wl else wl
                # Are any required properties not already present on CF?
                properties = reqd_props - set(cf_props)
                # Are any whitelisted properties not already present on CF?
                # If so, add them too.
                properties.update(set(whitelist) - set(cf_props))

                owner = self.conf.get('username', 'cfstore')
                for prop in properties:
                    self.client.set(property={u'name': prop, u'owner': owner})

                self.whitelist = set(whitelist)
                _log.debug('WHITELIST = {}'.format(self.whitelist))
            except ConnectionError:
                _log.exception("Cannot connect to Channelfinder service")
                raise
            else:
                self.clean_service()

    def stopService(self):
        service.Service.stopService(self)
        # Set channels to inactive and close connection to client
        self.running = 0
        self.clean_service()
        _log.info("CF_STOP")

    @defer.inlineCallbacks
    def commit(self, transaction_record):
        yield self.lock.acquire()
        try:
            yield deferToThread(self.__commit__, transaction_record)
        finally:
            self.lock.release()

    def __commit__(self, TR):
        _log.debug("CF_COMMIT %s", TR.infos.items())
        """
        a dictionary with a list of records with their associated property info  
        pvInfo 
        {rid: { "pvName":"recordName",
                "infoProperties":{propName:value, ...}}}
        """

        iocName = TR.infos.get('IOCNAME') or TR.src.port
        hostName = TR.infos.get('HOSTNAME') or TR.src.host
        owner = TR.infos.get('ENGINEER') or TR.infos.get('CF_USERNAME') or self.conf.get('username', 'cfstore')
        time = self.currentTime()

        pvInfo = {}
        for rid, (rname, rtype) in TR.addrec.items():
            pvInfo[rid] = {"pvName": rname}
        for rid, (recinfos) in TR.recinfos.items():
            # find intersection of these sets
            recinfo_wl = [p for p in self.whitelist if p in recinfos.keys()]
            if recinfo_wl:
                pvInfo[rid]['infoProperties'] = list()
                for infotag in recinfo_wl:
                    _log.debug('INFOTAG = {}'.format(infotag))
                    property = {u'name': infotag, u'owner': owner,
                                u'value': recinfos[infotag]}
                    pvInfo[rid]['infoProperties'].append(property)
        for rid, alias in TR.aliases.items():
            pvInfo[rid]['aliases'] = alias
        _log.debug(pvInfo)

        pvNames = [info["pvName"] for rid, (info) in pvInfo.items()]

        delrec = list(TR.delrec)
        _log.info("DELETED records " + str(delrec))

        host = TR.src.host
        port = TR.src.port

        """The unique identifier for a particular IOC"""
        iocid = host + ":" + str(port)
        _log.info("CF_COMMIT: " + iocid)

        if TR.initial:
            """Add IOC to source list """
            self.iocs[iocid] = {"iocname": iocName, "hostname": hostName, "owner": owner, "time": time,
                                "channelcount": 0}
        if not TR.connected:
            delrec.extend(self.channel_dict.keys())
        for pv in pvNames:
            self.channel_dict[pv].append(iocid)  # add iocname to pvName in dict
            self.iocs[iocid]["channelcount"] += 1
            """In case, alias exists"""
            if (self.conf.get('alias', 'default' == 'on')):
                al = [info["aliases"] for rid, (info) in pvInfo.items() if info["pvName"] == pv and "aliases" in info ]
                if len(al) == 1:
                    ali = al[0]
                    for a in ali:
                        self.channel_dict[a].append(iocid)  # add iocname to pvName in dict
                        self.iocs[iocid]["channelcount"] += 1
        for pv in delrec:
            if iocid in self.channel_dict[pv]:
                self.channel_dict[pv].remove(iocid)
                if iocid in self.iocs:
                    self.iocs[iocid]["channelcount"] -= 1
                if self.iocs[iocid]['channelcount'] == 0:
                    self.iocs.pop(iocid, None)
                elif self.iocs[iocid]['channelcount'] < 0:
                    _log.error("channel count negative!")
                if len(self.channel_dict[pv]) <= 0:  # case: channel has no more iocs
                    del self.channel_dict[pv]
                """In case, alias exists"""
                if (self.conf.get('alias', 'default' == 'on')):
                    al = [info["aliases"] for rid, (info) in pvInfo.items() if info["pvName"] == pv and "aliases" in info ]
                    if len(al) == 1:
                        ali = al[0]
                        for a in ali:
                            self.channel_dict[a].remove(iocid)
                            if iocid in self.iocs:
                                self.iocs[iocid]["channelcount"] -= 1
                            if self.iocs[iocid]['channelcount'] == 0:
                                self.iocs.pop(iocid, None)
                            elif self.iocs[iocid]['channelcount'] < 0:
                                _log.error("channel count negative!")
                            if len(self.channel_dict[a]) <= 0:  # case: channel has no more iocs
                                del self.channel_dict[a]
        poll(__updateCF__, self.client, pvInfo, delrec, self.channel_dict, self.iocs, self.conf, hostName, iocName, iocid,
             owner, time)
        dict_to_file(self.channel_dict, self.iocs, self.conf)

    def clean_service(self):
        """
        Marks all channels as "Inactive" until the recsync server is back up
        """
        sleep = 1
        retry_limit = 5
        owner = self.conf.get('username', 'cfstore')
        while 1:
            try:
                _log.debug("Cleaning service...")
                channels = self.client.findByArgs([('pvStatus', 'Active')])
                if channels is not None:
                    new_channels = []
                    for ch in channels or []:
                        new_channels.append(ch[u'name'])
                    if len(new_channels) > 0:
                        self.client.update(property={u'name': 'pvStatus', u'owner': owner, u'value': "Inactive"},
                                           channelNames=new_channels)
                    _log.debug("Service clean.")
                    return
            except RequestException:
                _log.exception("cleaning failed, retrying: ")

            time.sleep(min(60, sleep))
            sleep *= 1.5
            if self.running == 0 and sleep >= retry_limit:
                _log.debug("Abandoning clean.")
                return
Exemple #12
0
def cfs_append_from_sqlite(fname, update_only):
    sq = ap.chanfinder.ChannelFinderAgent()
    sq.loadSqlite(fname)

    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags  = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']

    allpvs = []
    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    prpt_data, tag_data = {}, {}
    # the data body
    for pv, prpts, tags in sq.rows:
        if not pv: continue
        if pv in allpvs: continue
        if pv.find("SR:") != 0: continue
        logging.info("updating '{0}'".format(pv))
        
        allpvs.append(pv)
        prpt_list, tag_list = [], []
        for k,v in prpts.items():
            if k not in ["elemIndex", "system", "elemType", "elemHandle",
                         "elemName", "elemField"]: continue
            prpt_data.setdefault((k, v), [])
            prpt_data[(k, v)].append(pv)
        for tag in tags:
            if not tag.startswith("aphla."): continue
            tag_data.setdefault(tag, [])
            tag_data[tag].append(pv)
            #tag_list.append(Tag(r.strip()), tag_owner)
            logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner))
            #addPvTag(cf, pv, tag, tag_owner)
        
    errpvs = []
    for pv in allpvs:
        chs = cf.find(name=pv)
        if not chs:
            errpvs.append(pv)
            print "PV '%s' does not exist" % pv
            continue
        elif len(chs) != 1:
            print "Find two results for pv=%s" % pv
            continue
        prpts = chs[0].getProperties()
        if not prpts: continue
        for prpt,val in prpts.items():
            pvlst = prpt_data.get((prpt, val), [])
            if not pvlst: continue
            try:
                j = pvlst.index(pv)
                prpt_data[(prpt,val)].pop(j)
            except:
                # the existing data is not in the update list, skip
                pass

    #if errpvs: 
    #    #raise RuntimeError("PVs '{0}' are missing".format(errpvs))
    #    print 

    logging.warn("{0} does not exist in DB".format(errpvs))

    for k,v in prpt_data.iteritems():
        vf = [pv for pv in v if pv not in errpvs]
        if not vf: 
            logging.info("no valid PVs for {0}".format(k))
            continue
        updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf)
        logging.info("add property {0} for pvs {1}".format(k, vf))
    for k,v in tag_data.iteritems():
        vf = [pv for pv in v if pv not in errpvs]
        addTagPvs(cf, k, vf, tag_owner)
        logging.info("add tag {0} for pvs {1}".format(k, vf))
Exemple #13
0
def cfs_append_from_csv2(rec_list, update_only):
    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags  = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']
    import csv
    rd = csv.reader(rec_list)

    allpvs = []
    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    prpt_data, tag_data = {}, {}
    # the data body
    for s in rd:
        if not s: continue
        if not s[0].strip(): continue
        if s[0].strip().startswith('#'): continue
        
        pv = s[0].strip()
        logging.info("updating '{0}'".format(pv))
        #cf.update(property=Property('elemType', PRPTOWNER, 'QUAD'),
        #          channelNames = [pv])
        #chs = cf.find(name=pv)
        #sys.exit(0)
        #logging.info("{0} and {1}".format(chs[0].Name, type(chs[0].Name)))
        #logging.info("{0} and {1}".format(pv, type(pv)))

        allpvs.append(pv)
        prpt_list, tag_list = [], []
        for r in s[1:]:
            if r.find('=') > 0:
                prpt, val = [v.strip() for v in r.split('=')]
                prpt_data.setdefault((prpt, val), [])
                prpt_data[(prpt, val)].append(pv)
            else:
                # it is a tag
                tag = r.strip()
                if not tag: continue
                tag_data.setdefault(tag, [])
                tag_data[tag].append(pv)
                #tag_list.append(Tag(r.strip()), tag_owner)
                logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner))
                #addPvTag(cf, pv, tag, tag_owner)
        
    errpvs = []
    for pv in allpvs:
        chs = cf.find(name=pv)
        if not chs:
            errpvs.append(pv)
            print "PV '%s' does not exist" % pv
            continue
        elif len(chs) != 1:
            print "Find two results for pv=%s" % pv
            continue
        for prpt,val in chs[0].getProperties().items():
            pvlst = prpt_data.get((prpt, val), [])
            if not pvlst: continue
            try:
                j = pvlst.index(pv)
                prpt_data[(prpt,val)].pop(j)
            except:
                # the existing data is not in the update list, skip
                pass

    #if errpvs: 
    #    #raise RuntimeError("PVs '{0}' are missing".format(errpvs))
    #    print 

    logging.warn("{0} does not exist in DB".format(errpvs))

    for k,v in prpt_data.iteritems():
        vf = [pv for pv in v if pv not in errpvs]
        if not vf: 
            logging.info("no valid PVs for {0}".format(k))
            continue
        updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf)
        logging.info("add property {0} for pvs {1}".format(k, vf))
    for k,v in tag_data.iteritems():
        vf = [pv for pv in v if pv not in errpvs]
        addTagPvs(cf, k, vf, tag_owner)
        logging.info("add tag {0} for pvs {1}".format(k, vf))
Exemple #14
0
def cfs_append_from_csv1(rec_list, update_only):
    cf = ChannelFinderClient(**cfinput)
    all_prpts = [p.Name for p in cf.getAllProperties()]
    all_tags  = [t.Name for t in cf.getAllTags()]
    ignore_prpts = ['hostName', 'iocName']
    import csv
    rd = csv.reader(rec_list)
    # header line
    header = rd.next()
    # lower case of header
    hlow = [s.lower() for s in header]
    # number of headers, pv + properties
    nheader = len(header)
    # the index of PV, properties and tags
    ipv = hlow.index('pv')
    # the code did not rely on it, but it is a good practice
    if ipv != 0:
        raise RuntimeError("the first column should be pv")

    iprpt, itags = [], []
    for i, h in enumerate(header):
        if i == ipv: continue
        # if the header is empty, it is a tag
        if len(h.strip()) == 0: 
            itags.append(i)
        else:
            iprpt.append(i)

    tag_owner = OWNER
    prpt_owner = PRPTOWNER
    tags = {}
    # the data body
    for s in rd:
        prpts = [Property(header[i], prpt_owner, s[i]) for i in iprpt if s[i]]
        # itags could be empty if we put all tags in the end columns
        for i in itags + range(nheader, len(s)):
            rec = tags.setdefault(s[i].strip(), [])
            rec.append(s[ipv].strip())

        #print s[ipv], prpts, tags
        ch = cf.find(name=s[ipv])
        if ch is None:
            logging.warning("pv {0} does not exist".format(s[ipv]))
        elif len(ch) > 1:
            logging.warning("pv {0} is not unique ({1})".format(s[ipv], len(ch)))
        else:
            for p in prpts:
                #continue
                if p.Name in ignore_prpts: continue
                #if p.Name != 'symmetry': continue
                logging.info("updating '{0}' with property, {1}={2}".format(
                        s[ipv], p.Name, p.Value))
                cf.update(channelName=s[ipv], property=p)

    logging.info("finished updating properties")
    for t,pvs in tags.iteritems():
        if not hasTag(cf, t): cf.set(tag=Tag(t, tag_owner))
        if 'V:1-SR-BI{BETA}X-I' in pvs: continue
        if 'V:1-SR-BI{BETA}Y-I' in pvs: continue
        try:
            cf.update(tag=Tag(t, tag_owner), channelNames=pvs)
        except:
            print t, pvs
            raise

        logging.info("update '{0}' for {1} pvs".format(t, len(pvs)))
    logging.info("finished updating tags")