Skip to content

Commit

Permalink
ports/psoc6/mp_custom/fs.py: Added logger and fixed ls line end split.
Browse files Browse the repository at this point in the history
Signed-off-by: enriquezgarc <enriquezgarcia.external@infineon.com>
  • Loading branch information
jaenrig-ifx committed Oct 10, 2024
1 parent 80ccac1 commit eeef570
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tests/ports/psoc6/mp_custom/fs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import subprocess
import sys
import os
import logging

logger = logging.getLogger("fs")
logging.basicConfig(format="%(levelname)s: %(message)s", encoding="utf-8", level=logging.WARNING)

device = sys.argv[1]
test_type = sys.argv[2]
Expand All @@ -24,6 +28,8 @@ def set_mpr_run_script(mem_type):
if mem_type == "sd":
mpr_run_script = f"run {test_script_dir}/fs_mount_sd.py"

logger.debug(f'Set setup script command to: "{mpr_run_script}"')


def set_remote_dir_path(mem_type):
# Set test directory path based on the memory type
Expand All @@ -33,6 +39,8 @@ def set_remote_dir_path(mem_type):
else:
remote_directory_path = "/"

logger.debug(f'Set remote path to : "{mpr_run_script}"')


def get_test_input_files(test_type):
# The "basic" test uses only the small file
Expand All @@ -57,27 +65,35 @@ def get_test_name(test_type, mem_type):
elif mem_type == "flash":
mem_type_suffix = ""

logger.debug(f"Get tests name : fs_{test_type}{mem_type_suffix}.py")

return f"fs_{test_type}{mem_type_suffix}.py"


def ls_files(files):
logger.debug(f"ls_files input: {files}")
# It will return an array with the file size found in the remote directory
# If -1, the file was not found
mpr_ls = f"{mpr_connect} {mpr_run_script} fs ls {remote_directory_path}"
logger.debug(f"ls_files command: {mpr_ls}")
output = subprocess.run(f"{mpr_ls}", shell=True, capture_output=True)

files_result = []
lines = output.stdout.decode().split("\r\n")
lines = output.stdout.decode().split("\n")
logger.debug(f"ls_files output lines: {lines}")

for file in files:
file_size = -1
for line in lines:
line = line.split()
logger.debug(f"ls_files current processed line: {line}")
if file in line:
file_size = line[0]
logger.debug(f"ls_files found file: {file} with size {file_size}")

files_result.append(file_size)

logger.debug(f"ls_files result: {files_result}")
return files_result


Expand All @@ -87,6 +103,8 @@ def rm_files(files):
# ../tools/mpremote/mpremote.py connect /dev/ttyACM0 fs rm /test_fs_medium_file.txt + fs rm /test_fs_medium_file.txt
mpr_rm = f"{mpr_connect} {mpr_run_script} fs rm "

logger.debug(f"rm_files command: {mpr_rm}")

rm_sub_cmd = ""
last_file = files[-1]
for file in files:
Expand All @@ -96,23 +114,31 @@ def rm_files(files):

rm_sub_cmd += f"fs rm {remote_directory_path}{file}{append_cmd_operand}"

subprocess.run(f"{mpr_connect} {mpr_run_script} {rm_sub_cmd}", shell=True, capture_output=True)
mpr_rm_cmd = f"{mpr_connect} {mpr_run_script} {rm_sub_cmd}"

logger.debug(f"rm_files command: {mpr_rm_cmd}")

subprocess.run(f"{mpr_rm_cmd}", shell=True, capture_output=True)


def rm_files_if_exist(files):
matches = ls_files(files)
logger.debug(f"Files (to be removed) found: {matches}")

# Take only the files that which sizes are not -1 (the existing files in the remote directory)
existing_files = []
for i in range(len(matches)):
if matches[i] != -1:
existing_files.append(files[i])

logger.debug(f"Existing (to be removed) files: {existing_files}")

# Remove any found input files in the remote directory
if existing_files != []:
print(f"Removing existing files...")
rm_files(existing_files)
if ls_files(files) == [-1 for _ in range(len(files))]:
matches = ls_files(files)
if matches == [-1 for _ in range(len(files))]:
print(f"Existing files removed.")


Expand All @@ -129,6 +155,8 @@ def copy_files(input_cp_files):

cp_cmd = f"{mpr_connect} {mpr_run_script} {cp_sub_cmd}"

logger.debug(f"cp_files command: {cp_cmd}")

subprocess.run(cp_cmd, shell=True, capture_output=True)


Expand All @@ -137,6 +165,8 @@ def validate_test(files, file_sizes):
# in the remote directory with the expected file sizes
found_sizes = ls_files(files)

logger.debug(f"Found sizes: {found_sizes}")

if found_sizes != file_sizes:
msg = "fail"
exit_code = 1
Expand Down

0 comments on commit eeef570

Please sign in to comment.