Example #1
0
def read_from_ports(raw_paths, options, tree=None):
    event = threading.Event()

    mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
    mm.load_mods_and_poas(options.modules)
    if options.verbose:
        print >>sys.stderr, \
                'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)
    if options.timeout == -1:
        max = options.max
        if options.verbose:
            print >> sys.stderr, 'Will run {0} times.'.format(max)
    else:
        max = -1
        if options.verbose:
            print >> sys.stderr, 'Will stop after {0}s'.format(options.timeout)

    targets = port_types.parse_targets(raw_paths)
    if not tree:
        paths = [t[0] for t in targets]
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
    port_specs = port_types.make_port_specs(targets, mm, tree)
    port_types.require_all_input(port_specs)
    if options.verbose:
        print >>sys.stderr, \
                'Port specifications: {0}'.format([str(p) for p in port_specs])

    comp_name, mgr = comp_mgmt.make_comp('rtprint_reader',
                                         tree,
                                         rtprint_comp.Reader,
                                         port_specs,
                                         event=event,
                                         rate=options.rate,
                                         max=max)
    if options.verbose:
        print >> sys.stderr, 'Created component {0}'.format(comp_name)
    comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
    comp_mgmt.connect(comp, port_specs, tree)
    comp_mgmt.activate(comp)
    try:
        if options.timeout != -1:
            event.wait(options.timeout)
            comp_mgmt.disconnect(comp)
            comp_mgmt.deactivate(comp)
        elif options.max > -1:
            event.wait()
            comp_mgmt.disconnect(comp)
            comp_mgmt.deactivate(comp)
        else:
            while True:
                raw_input()
            # The manager will catch the Ctrl-C and shut down itself, so don't
            # disconnect/deactivate the component
    except KeyboardInterrupt:
        pass
    except EOFError:
        pass
    tree.give_away_orb()
    del tree
    comp_mgmt.shutdown(mgr)
Example #2
0
def display_info(options):
    if not options.filename:
        raise rts_exceptions.NoLogFileNameError

    if options.logger == 'simpkl':
        l_type = simpkl_log.SimplePickleLog
    elif options.logger == 'text':
        raise rts_exceptions.UnsupportedLogTypeError('text', 'inspection')
    else:
        raise rts_exceptions.BadLogTypeError(options.logger)

    mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
    mm.load_mods_and_poas(options.modules)
    if options.verbose:
        print >>sys.stderr, \
                'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)

    statinfo = os.stat(options.filename)
    size = statinfo.st_size
    if size > 1024 * 1024 * 1024:  # GiB
        size_str = '{0:.2f}GiB ({1}B)'.format(size / (1024.0 * 1024 * 1024),
                                              size)
    elif size > 1024 * 1024:  # MiB
        size_str = '{0:.2f}MiB ({1}B)'.format(size / (1024.0 * 1024), size)
    elif size > 1024:  # KiB
        size_str = '{0:.2f}KiB ({1}B)'.format(size / 1024.0, size)
    else:
        size_str = '{0}B'.format(size)
    log = l_type(filename=options.filename, mode='r', verbose=options.verbose)

    start_time, port_specs = log.metadata
    start_time_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                   time.localtime(start_time))
    first_ind, first_time = log.start
    first_time_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                   time.localtime(first_time.float))
    end_ind, end_time = log.end
    end_time_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                 time.localtime(end_time.float))

    print 'Name: {0}'.format(options.filename)
    print 'Size: ' + size_str
    print 'Start time: {0} ({1})'.format(start_time_str, start_time)
    print 'First entry time: {0} ({1})'.format(first_time_str, first_time)
    print 'End time: {0} ({1})'.format(end_time_str, end_time)
    print 'Number of entries: {0}'.format(end_ind + 1)
    for ii, p in enumerate(port_specs):
        print 'Channel {0}'.format(ii + 1)
        print '  Name: {0}'.format(p.name)
        print '  Data type: {0} ({1})'.format(p.type_name, p.type)
        print '  Sources:'
        for r in p.raw:
            print '    {0}'.format(r)
Example #3
0
def record_log(raw_paths, options, tree=None):
    event = threading.Event()

    if options.end is not None and options.end < 0:
        raise rts_exceptions.BadEndPointError
    if options.end is None and options.index:
        print >>sys.stderr, '{0}: WARNING: --index has no effect without '\
                '--end'.format(os.path.basename(sys.argv[0]))

    mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
    mm.load_mods_and_poas(options.modules)
    if options.verbose:
        print >>sys.stderr, \
                'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)

    if options.timeout is not None:
        print >> sys.stderr, 'Recording for {0}s.'.format(options.timeout)
    else:
        if options.end is not None:
            if options.index:
                print >> sys.stderr, 'Recording {0} entries.'.format(
                    int(options.end))
            else:
                end_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                        time.localtime(options.end))
                print >> sys.stderr, 'Recording until {0} ({1}).'.format(
                    end_str, options.end)

    if options.logger == 'simpkl':
        l_type = simpkl_log.SimplePickleLog
    elif options.logger == 'text':
        l_type = text_log.TextLog
    else:
        raise rts_exceptions.BadLogTypeError(options.logger)

    sources = port_types.parse_targets(raw_paths)
    if not tree:
        paths = [s[0] for s in sources]
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
    port_specs = port_types.make_port_specs(sources, mm, tree)
    port_types.require_all_input(port_specs)
    if options.verbose:
        print >>sys.stderr, \
                'Port specifications: {0}'.format([str(p) for p in port_specs])

    if options.end is None:
        end = -1  # Send -1 as the default
    else:
        end = options.end
    comp_name, mgr = comp_mgmt.make_comp('rtlog_recorder',
                                         tree,
                                         rtlog_comps.Recorder,
                                         port_specs,
                                         event=event,
                                         logger_type=l_type,
                                         filename=options.filename,
                                         lims_are_ind=options.index,
                                         end=end,
                                         verbose=options.verbose,
                                         rate=options.exec_rate)
    if options.verbose:
        print >> sys.stderr, 'Created component {0}'.format(comp_name)
    try:
        comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
        comp_mgmt.connect(comp, port_specs, tree)
        comp_mgmt.activate(comp)
    except Exception, e:
        #comp_mgmt.shutdown(mgr)
        raise e
Example #4
0
def play_log(raw_paths, options, tree=None):
    event = threading.Event()

    if not options.filename:
        raise rts_exceptions.NoLogFileNameError
    if options.start is not None and options.start < 0:
        raise rts_exceptions.BadStartPointError
    if options.end is not None and options.end < 0:
        raise rts_exceptions.BadEndPointError
    if options.end is None and options.start is None and options.index:
        print >>sys.stderr, '{0}: WARNING: --index has no effect without '\
                '--start or --end'.format(os.path.basename(sys.argv[0]))

    mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
    mm.load_mods_and_poas(options.modules)
    if options.verbose:
        print >>sys.stderr, \
                'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)

    if options.timeout is not None:
        print >> sys.stderr, 'Playing for {0}s.'.format(options.timeout)
    else:
        if options.end is not None:
            if options.start is not None:
                if options.index:
                    print >>sys.stderr, 'Playing from entry {0} to entry '\
                            '{1}.'.format(int(options.start), int(options.end))
                else:
                    start_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                              time.localtime(options.start))
                    end_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                            time.localtime(options.end))
                    print >>sys.stderr, 'Playing from {0} ({1}) until {2} '\
                            '({3}).'.format(start_str, options.start, end_str,
                                    options.end)
            else:
                if options.index:
                    print >> sys.stderr, 'Playing {0} entries.'.format(
                        int(options.end))
                else:
                    end_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                            time.localtime(options.end))
                    print >> sys.stderr, 'Playing until {0} ({1}).'.format(
                        end_str, options.end)
        elif options.start is not None:
            if options.index:
                print >> sys.stderr, 'Playing from entry {0}.'.format(
                    int(options.start))
            else:
                start_str = time.strftime('%Y-%m-%d %H:%M:%S',
                                          time.localtime(options.start))
                print >> sys.stderr, 'Playing from {0} ({1}).'.format(
                    start_str, options.start)

    if options.logger == 'simpkl':
        l_type = simpkl_log.SimplePickleLog
    elif options.logger == 'text':
        raise rts_exceptions.UnsupportedLogTypeError('text', 'playback')
    else:
        raise rts_exceptions.BadLogTypeError(options.logger)

    targets = port_types.parse_targets(raw_paths)
    if not tree:
        paths = [t[0] for t in targets]
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
    port_specs = port_types.make_port_specs(targets, mm, tree)
    if options.verbose:
        print >>sys.stderr, \
                'Port specifications: {0}'.format([str(p) for p in port_specs])
    port_types.require_all_output(port_specs)

    if options.start is None:
        start = 0  # Send 0 as the default
    else:
        start = options.start
    if options.end is None:
        end = -1  # Send -1 as the default
    else:
        end = options.end
    comp_name, mgr = comp_mgmt.make_comp('rtlog_player',
                                         tree,
                                         rtlog_comps.Player,
                                         port_specs,
                                         event=event,
                                         logger_type=l_type,
                                         filename=options.filename,
                                         lims_are_ind=options.index,
                                         start=start,
                                         end=end,
                                         scale_rate=options.rate,
                                         abs_times=options.abs_times,
                                         ignore_times=options.ig_times,
                                         verbose=options.verbose,
                                         rate=options.exec_rate)
    if options.verbose:
        print >> sys.stderr, 'Created component {0}'.format(comp_name)
    comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
    comp_mgmt.connect(comp, port_specs, tree)
    comp_mgmt.activate(comp)
    try:
        if options.timeout is not None:
            event.wait(options.timeout)
            comp_mgmt.disconnect(comp)
            try:
                comp_mgmt.deactivate(comp)
            except rts_exceptions.DeactivateError:
                # Don't care about this because the component will be shut down
                # soon anyway
                pass
        #elif options.end is not None:
        else:
            event.wait()
            comp_mgmt.disconnect(comp)
            try:
                comp_mgmt.deactivate(comp)
            except rts_exceptions.DeactivateError:
                # Don't care about this because the component will be shut down
                # soon anyway
                pass
        #else:
        #while True:
        #raw_input()
        # The manager will catch the Ctrl-C and shut down itself, so don't
        # disconnect/deactivate the component
    except KeyboardInterrupt:
        pass
    except EOFError:
        pass
    tree.give_away_orb()
    del tree
    comp_mgmt.shutdown(mgr)
Example #5
0
def write_to_ports(raw_paths, options, tree=None):
    event = threading.Event()

    mm = modmgr.ModuleMgr(verbose=options.verbose, paths=options.paths)
    mm.load_mods_and_poas(options.modules)
    if options.verbose:
        print >>sys.stderr, \
                'Pre-loaded modules: {0}'.format(mm.loaded_mod_names)

    if options.const:
        val = mm.evaluate(options.const)
        if options.verbose:
            print >>sys.stderr, 'Evaluated value to {0}'.format(val)
    else:
        if options.verbose:
            print >>sys.stderr, 'Reading values from stdin.'

    if options.timeout == -1:
        max = options.max
        if options.verbose:
            print >>sys.stderr, 'Will run {0} times.'.format(max)
    else:
        max = -1
        if options.verbose:
            print >>sys.stderr, 'Will stop after {0}s'.format(options.timeout)

    targets = port_types.parse_targets(raw_paths)
    if not tree:
        paths = [t[0] for t in targets]
        tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
    port_specs = port_types.make_port_specs(targets, mm, tree)
    port_types.require_all_output(port_specs)
    if options.verbose:
        print >>sys.stderr, \
                'Port specifications: {0}'.format([str(p) for p in port_specs])

    if options.const:
        comp_name, mgr = comp_mgmt.make_comp('rtinject_writer', tree,
                rtinject_comp.Writer, port_specs, event=event, rate=options.rate,
                max=max, val=val)
    else:
        buffer = []
        mutex = threading.RLock()
        comp_name, mgr = comp_mgmt.make_comp('rtinject_writer', tree,
                rtinject_comp.StdinWriter, port_specs, event=event,
                rate=options.rate, max=max, buf=buffer, mutex=mutex)
    if options.verbose:
        print >>sys.stderr, 'Created component {0}'.format(comp_name)
    comp = comp_mgmt.find_comp_in_mgr(comp_name, mgr)
    comp_mgmt.connect(comp, port_specs, tree)
    comp_mgmt.activate(comp)
    if options.const:
        try:
            if options.timeout != -1:
                event.wait(options.timeout)
            elif options.max > -1:
                event.wait()
            else:
                raw_input()
        except KeyboardInterrupt:
            pass
        except EOFError:
            pass
    else:
        # Read stdin until we receive max number of values or Ctrl-C is hit
        val_cnt = 0
        try:
            while val_cnt < max or max < 0:
                l = sys.stdin.readline()
                if not l:
                    break
                if l[0] == '#':
                    continue
                val = mm.evaluate(l)
                with mutex:
                    buffer.append(val)
                val_cnt += 1
        except KeyboardInterrupt:
            pass
        # Wait until the buffer has been cleared
        while True:
            with mutex:
                if not buffer:
                    break
    comp_mgmt.disconnect(comp)
    comp_mgmt.deactivate(comp)
    tree.give_away_orb()
    del tree
    comp_mgmt.shutdown(mgr)