示例#1
0
def query( monitor_changes = True ):
    """
    Start a new LRS context, and collect all devices
    :param monitor_changes: If True, devices will update dynamically as they are removed/added
    """
    global rs
    if not rs:
        return
    #
    # Before we can start a context and query devices, we need to enable all the ports
    # on the acroname, if any:
    if acroname:
        acroname.connect()  # MAY THROW!
        acroname.enable_ports( sleep_on_change = 5 )  # make sure all connected!
    #
    # Get all devices, and store by serial-number
    global _device_by_sn, _context, _port_to_sn
    _context = rs.context()
    _device_by_sn = dict()
    try:
        log.d( 'discovering devices ...' )
        log.debug_indent()
        for dev in _context.query_devices():
            if dev.is_update_device():
                sn = dev.get_info( rs.camera_info.firmware_update_id )
            else:
                sn = dev.get_info( rs.camera_info.serial_number )
            _device_by_sn[sn] = Device( sn, dev )
            log.d( '...', dev )
    finally:
        log.debug_unindent()
    #
    if monitor_changes:
        _context.set_devices_changed_callback( _device_change_callback )
示例#2
0
def find_includes(filepath, filelist=set()):
    """
    Recursively searches a .cpp file for #include directives and returns
    a set of all of them.
    :return: a list of all includes found
    """
    filedir = os.path.dirname(filepath)
    try:
        log.debug_indent()
        for include_line in file.grep(r'^\s*#\s*include\s+("(.*)"|<(.*)>)\s*$',
                                      filepath):
            m = include_line['match']
            index = include_line['index']
            include = find_include(
                m.group(2), filedir) or find_include_in_dirs(
                    m.group(2)) or find_include_in_dirs(m.group(3))
            if include:
                if include in filelist:
                    log.d(m.group(0), '->', include, '(already processed)')
                else:
                    log.d(m.group(0), '->', include)
                    filelist.add(include)
                    filelist = find_includes(include, filelist)
            else:
                log.d('not found:', m.group(0))
    finally:
        log.debug_unindent()
    return filelist
示例#3
0
def query(monitor_changes=True):
    """
    Start a new LRS context, and collect all devices
    :param monitor_changes: If True, devices will update dynamically as they are removed/added
    """
    global rs
    if not rs:
        return
    #
    # Before we can start a context and query devices, we need to enable all the ports
    # on the acroname, if any:
    if acroname:
        if not acroname.hub:
            acroname.connect()  # MAY THROW!
            acroname.enable_ports(
                sleep_on_change=5)  # make sure all connected!
    #
    # Get all devices, and store by serial-number
    global _device_by_sn, _context, _port_to_sn
    _context = rs.context()
    _device_by_sn = dict()
    try:
        log.d('discovering devices ...')
        log.debug_indent()
        for retry in range(3):
            try:
                devices = _context.query_devices()
                break
            except RuntimeError as e:
                log.d('FAILED to query devices:', e)
                if retry > 1:
                    log.e('FAILED to query devices', retry + 1, 'times!')
                    raise
                else:
                    time.sleep(1)
        for dev in devices:
            # The FW update ID is always available, it seems, and is the ASIC serial number
            # whereas the Serial Number is the OPTIC serial number and is only available in
            # non-recovery devices. So we use the former...
            sn = dev.get_info(rs.camera_info.firmware_update_id)
            device = Device(sn, dev)
            _device_by_sn[sn] = device
            log.d(
                '... port {}:'.format(device.port is None and '?'
                                      or device.port), sn, dev)
    finally:
        log.debug_unindent()
    #
    if monitor_changes:
        _context.set_devices_changed_callback(_device_change_callback)
示例#4
0
def run(cmd, stdout=None, timeout=200, append=False):
    """
    Wrapper function for subprocess.run.
    If the child process times out or ends with a non-zero exit status an exception is raised!

    :param cmd: the command and argument for the child process, as a list
    :param stdout: path of file to direct the output of the process to (None to disable)
    :param timeout: number of seconds to give the process before forcefully ending it (None to disable)
    :param append: if True and stdout is not None, the log of the test will be appended to the file instead of
                   overwriting it
    :return: the output written by the child, if stdout is None -- otherwise N/A
    """
    log.d('running:', cmd)
    handle = None
    start_time = time.time()
    try:
        log.debug_indent()
        if stdout and stdout != subprocess.PIPE:
            if append:
                handle = open(stdout, "a")
                handle.write(
                    "\n---------------------------------------------------------------------------------\n\n"
                )
                handle.flush()
            else:
                handle = open(stdout, "w")
            stdout = handle
        rv = subprocess.run(cmd,
                            stdout=stdout,
                            stderr=subprocess.STDOUT,
                            universal_newlines=True,
                            timeout=timeout,
                            check=True)
        result = rv.stdout
        if not result:
            result = []
        else:
            result = result.split('\n')
        return result
    finally:
        if handle:
            handle.close()
        log.debug_unindent()
        run_time = time.time() - start_time
        log.d("test took", run_time, "seconds")
示例#5
0
def discover():
    """
    Return all Acroname module specs in a list. Raise NoneFoundError if one is not found!
    """

    log.d('discovering Acroname modules ...')
    # see https://acroname.com/reference/_modules/brainstem/module.html#Module.discoverAndConnect
    try:
        log.debug_indent()
        specs = brainstem.discover.findAllModules(brainstem.link.Spec.USB)
        if not specs:
            raise NoneFoundError()
        for spec in specs:
            log.d('...', spec)
    finally:
        log.debug_unindent()

    return specs
示例#6
0
def log_settings_differences( data ):
    global depth_sensor, sd
    depth_sensor.set_option(rs.option.visual_preset, int(rs.l500_visual_preset.low_ambient_light))
    actual_data = str( sd.serialize_json() )
    data_dict = json_to_dict( data )
    actual_data_dict = json_to_dict( actual_data )
    log.debug_indent()
    try:
        # logging the differences in the settings between the expected and the actual values
        for key in actual_data_dict.keys():
            if key not in data_dict:
                log.d( "New setting added to json:", key)
            elif "Visual Preset" in key or "Temperature" in key or "temperature" in key:
                # the line regarding the visual preset will always be different because we load 1 from data but set it to
                # 3 for low ambient. Also all lines regarding temperatures depend on the camera and don't affect the preset
                continue
            elif data_dict[ key ] != actual_data_dict[ key ]:
                log.d( key, "was expected to have value of", data_dict[ key ],
                       "but actually had value of", actual_data_dict[ key ])
    finally:
        log.debug_unindent()
示例#7
0
def subprocess_run(cmd, stdout = None):
    log.d( 'running:', cmd )
    handle = None
    try:
        log.debug_indent()
        if stdout  and  stdout != subprocess.PIPE:
            handle = open( stdout, "w" )
            stdout = handle
        rv = subprocess.run( cmd,
                             stdout = stdout,
                             stderr = subprocess.STDOUT,
                             universal_newlines = True,
                             check = True)
        result = rv.stdout
        if not result:
            result = []
        else:
            result = result.split( '\n' )
        return result
    finally:
        if handle:
            handle.close()
        log.debug_unindent()
示例#8
0
def process_cpp(dir, builddir):
    global regex, required_tags, list_only, available_tags, tests_and_tags
    found = []
    shareds = []
    statics = []
    if regex:
        pattern = re.compile(regex)
    log.d('looking for C++ files in:', dir)
    for f in file.find(dir, '(^|/)test-.*\.cpp$'):
        testdir = os.path.splitext(f)[
            0]  # "log/internal/test-all"  <-  "log/internal/test-all.cpp"
        testparent = os.path.dirname(testdir)  # "log/internal"
        # We need the project name unique: keep the path but make it nicer:
        if testparent:
            testname = 'test-' + testparent.replace(
                '/', '-') + '-' + os.path.basename(testdir)[
                    5:]  # "test-log-internal-all"
        else:
            testname = testdir  # no parent folder so we get "test-all"

        if regex and not pattern.search(testname):
            continue

        log.d('... found:', f)
        log.debug_indent()
        try:
            if required_tags or list_tags:
                config = libci.TestConfigFromCpp(dir + os.sep + f, context)
                if not all(tag in config.tags for tag in required_tags):
                    continue
                available_tags.update(config.tags)
                if list_tests:
                    tests_and_tags[testname] = config.tags

            if testname not in tests_and_tags:
                tests_and_tags[testname] = None

            # Build the list of files we want in the project:
            # At a minimum, we have the original file, plus any common files
            filelist = [dir + '/' + f, '${ELPP_FILES}', '${CATCH_FILES}']
            # Add any "" includes specified in the .cpp that we can find
            includes = find_includes(dir + '/' + f)
            # Add any files explicitly listed in the .cpp itself, like this:
            #         //#cmake:add-file <filename>
            # Any files listed are relative to $dir
            shared = False
            static = False
            custom_main = False
            for cmake_directive in file.grep('^//#cmake:\s*', dir + '/' + f):
                m = cmake_directive['match']
                index = cmake_directive['index']
                cmd, *rest = cmake_directive['line'][m.end():].split()
                if cmd == 'add-file':
                    for additional_file in rest:
                        files = additional_file
                        if not os.path.isabs(additional_file):
                            files = dir + '/' + testparent + '/' + additional_file
                        files = glob(files)
                        if not files:
                            log.e(f + '+' + str(index) + ': no files match "' +
                                  additional_file + '"')
                        for abs_file in files:
                            abs_file = os.path.normpath(abs_file)
                            abs_file = abs_file.replace('\\', '/')
                            if not os.path.exists(abs_file):
                                log.e(f + '+' + str(index) +
                                      ': file not found "' + additional_file +
                                      '"')
                            log.d('add file:', abs_file)
                            filelist.append(abs_file)
                            if (os.path.splitext(abs_file)[0] == 'cpp'):
                                # Add any "" includes specified in the .cpp that we can find
                                includes |= find_includes(abs_file)
                elif cmd == 'static!':
                    if len(rest):
                        log.e(f + '+' + str(index) +
                              ': unexpected arguments past \'' + cmd + '\'')
                    elif shared:
                        log.e(f + '+' + str(index) + ': \'' + cmd +
                              '\' mutually exclusive with \'shared!\'')
                    else:
                        log.d('static!')
                        static = True
                elif cmd == 'shared!':
                    if len(rest):
                        log.e(f + '+' + str(index) +
                              ': unexpected arguments past \'' + cmd + '\'')
                    elif static:
                        log.e(f + '+' + str(index) + ': \'' + cmd +
                              '\' mutually exclusive with \'static!\'')
                    else:
                        log.d('shared!')
                        shared = True
                elif cmd == 'custom-main':
                    custom_main = True
                else:
                    log.e(
                        f + '+' + str(index) + ': unknown cmd \'' + cmd +
                        '\' (should be \'add-file\', \'static!\', or \'shared!\')'
                    )
            for include in includes:
                filelist.append(include)

            # 'cmake:custom-main' indicates that the test is defining its own main() function.
            # If not specified we use a default main() which lives in its own .cpp:
            if not custom_main:
                filelist.append(root +
                                "/unit-tests/unit-test-default-main.cpp")

            if list_only:
                continue

            # Each CMakeLists.txt sits in its own directory
            os.makedirs(builddir + '/' + testdir,
                        exist_ok=True)  # "build/log/internal/test-all"
            generate_cmake(builddir, testdir, testname, filelist, custom_main)
            if static:
                statics.append(testdir)
            elif shared:
                shareds.append(testdir)
            else:
                found.append(testdir)
        finally:
            log.debug_unindent()
    return found, shareds, statics
示例#9
0
def map_unknown_ports():
    """
    Fill in unknown ports in devices by enabling one port at a time, finding out which device
    is there.
    """
    if not acroname:
        return
    global _device_by_sn
    devices_with_unknown_ports = [
        device for device in _device_by_sn.values() if device.port is None
    ]
    if not devices_with_unknown_ports:
        return
    #
    ports = acroname.ports()
    known_ports = [
        device.port for device in _device_by_sn.values()
        if device.port is not None
    ]
    unknown_ports = [port for port in ports if port not in known_ports]
    try:
        log.d('mapping unknown ports', unknown_ports, '...')
        log.debug_indent()
        #log.d( "active ports:", ports )
        #log.d( "- known ports:", known_ports )
        #log.d( "= unknown ports:", unknown_ports )
        #
        for known_port in known_ports:
            if known_port not in ports:
                log.e("A device was found on port", known_port,
                      "but the port is not reported as used by Acroname!")
        #
        if len(unknown_ports) == 1:
            device = devices_with_unknown_ports[0]
            log.d('... port', unknown_ports[0], 'has', device.handle)
            device._port = unknown_ports[0]
            return
        #
        acroname.disable_ports(ports)
        wait_until_all_ports_disabled()
        #
        # Enable one port at a time to try and find what device is connected to it
        n_identified_ports = 0
        for port in unknown_ports:
            #
            log.d('enabling port', port)
            acroname.enable_ports([port], disable_other_ports=True)
            sn = None
            for retry in range(5):
                if len(enabled()) == 1:
                    sn = list(enabled())[0]
                    break
                time.sleep(1)
            if not sn:
                log.d('could not recognize device in port', port)
            else:
                device = _device_by_sn.get(sn)
                if device:
                    log.d('... port', port, 'has', device.handle)
                    device._port = port
                    n_identified_ports += 1
                    if len(devices_with_unknown_ports) == n_identified_ports:
                        #log.d( 'no more devices; stopping' )
                        break
                else:
                    log.w("Device with serial number", sn, "was found in port",
                          port, "but was not in context")
            acroname.disable_ports([port])
            wait_until_all_ports_disabled()
    finally:
        log.debug_unindent()
示例#10
0
# Run all tests
if pyrs:
    sys.path.append( pyrs_path )
from rspy import devices
devices.query()
#
# Under Travis, we'll have no devices and no acroname
skip_live_tests = len(devices.all()) == 0  and  not devices.acroname
#
log.reset_errors()
for test in prioritize_tests( get_tests() ):
    #
    log.d( 'found', test.name, '...' )
    try:
        log.debug_indent()
        test.debug_dump()
        #
        if tag and tag not in test.config.tags:
            log.d( 'does not fit --tag:', test.tags )
            continue
        #
        if not test.is_live():
            test_wrapper( test )
            continue
        #
        if skip_live_tests:
            log.w( test.name + ':', 'is live and there are no cameras; skipping' )
            continue
        #
        for configuration, serial_numbers in devices_by_test_config( test ):