Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
MMirbach committed Mar 15, 2021
1 parent 1ee5469 commit ce25c34
Show file tree
Hide file tree
Showing 5 changed files with 7,994 additions and 157 deletions.
107 changes: 107 additions & 0 deletions unit-tests/py/rspy/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2021 Intel Corporation. All Rights Reserved.

import os, re, platform, subprocess, sys

sys.path = list()
sys.path.append( '' ) # directs Python to search modules in the current directory first
sys.path.append( os.path.dirname( sys.executable ))
sys.path.append( os.path.join( os.path.dirname( sys.executable ), 'DLLs' ))
sys.path.append( os.path.join( os.path.dirname( sys.executable ), 'lib' ))
# Add our py/ module directory
current_dir = os.path.dirname( os.path.abspath( __file__ ))
sys.path.append( os.path.dirname( current_dir ) )

from rspy import log

# get os and directories for future use
# NOTE: WSL will read as 'Linux' but the build is Windows-based!
system = platform.system()
if system == 'Linux' and "microsoft" not in platform.uname()[3].lower():
linux = True
else:
linux = False

def filesin( root ):
# Yield all files found in root, using relative names ('root/a' would be yielded as 'a')
for (path,subdirs,leafs) in os.walk( root ):
for leaf in leafs:
# We have to stick to Unix conventions because CMake on Windows is fubar...
yield os.path.relpath( path + '/' + leaf, root ).replace( '\\', '/' )

def find( dir, mask ):
pattern = re.compile( mask )
for leaf in filesin( dir ):
if pattern.search( leaf ):
#log.d(leaf)
yield leaf

def is_executable(path_to_test):
global linux
if linux:
return os.access(path_to_test, os.X_OK)
else:
return path_to_test.endswith('.exe')

# wrapper function for subprocess.run
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()

def remove_newlines (lines):
for line in lines:
if line[-1] == '\n':
line = line[:-1] # excluding the endline
yield line

def grep_( pattern, lines, context ):
index = 0
matches = 0
for line in lines:
index = index + 1
match = pattern.search( line )
if match:
context['index'] = index
context['line'] = line
context['match'] = match
yield context
matches = matches + 1
if matches:
del context['index']
del context['line']
del context['match']

def grep( expr, *args ):
#log.d( f'grep {expr} {args}' )
pattern = re.compile( expr )
context = dict()
for filename in args:
context['filename'] = filename
with open( filename, errors = 'ignore' ) as file:
for line in grep_( pattern, remove_newlines( file ), context ):
yield line

def cat( filename ):
with open( filename, errors = 'ignore' ) as file:
for line in remove_newlines( file ):
log.out( line )
33 changes: 33 additions & 0 deletions unit-tests/py/rspy/repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2021 Intel Corporation. All Rights Reserved.

import os

# this script is located in librealsense/unit-tests/py/rspy, so one directory up is the main repository
source = os.path.dirname( os.path.dirname( os.path.dirname( os.path.dirname( os.path.abspath( __file__ )))))

def get_bundled_fw_version( product_line ):
"""
:param product_line: product line of a given device (ex. L500, D400)
:return: the bundled FW version for this device
"""
# common/fw/firmware-version.h contains the bundled FW versions for all product lines
fw_versions_file = os.path.join(librealsense, 'common', 'fw', 'firmware-version.h')
if not os.path.isfile(fw_versions_file):
log.e("Expected to find a file containing FW versions at", fw_versions_file, ", but the file was not found")
sys.exit(1)

fw_versions = open(fw_versions_file, 'r')
for line in fw_versions:
words = line.split()
if len( words ) == 0 or words[0] != "#define":
continue
if product_line[0:2] in words[1]: # the file contains FW versions for L5XX or D4XX devices, so we only look at the first 2 characters
fw_versions.close()
return words[2][1:-1] # remove "" from beginning and end of FW version
fw_versions.close()
return None

def pretty_fw_version( fw_version_as_string ):
""" return a version with zeros removed """
return '.'.join( [str(int(c)) for c in fw_version_as_string.split( '.' )] )
Loading

0 comments on commit ce25c34

Please sign in to comment.