From eeef570f7bf2bb396e91849f9635c3486264a692 Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Thu, 10 Oct 2024 15:30:51 +0200 Subject: [PATCH] ports/psoc6/mp_custom/fs.py: Added logger and fixed ls line end split. Signed-off-by: enriquezgarc --- tests/ports/psoc6/mp_custom/fs.py | 36 ++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/ports/psoc6/mp_custom/fs.py b/tests/ports/psoc6/mp_custom/fs.py index 84c31c56575f3..59f910201038f 100755 --- a/tests/ports/psoc6/mp_custom/fs.py +++ b/tests/ports/psoc6/mp_custom/fs.py @@ -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] @@ -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 @@ -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 @@ -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 @@ -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: @@ -96,11 +114,16 @@ 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 = [] @@ -108,11 +131,14 @@ def rm_files_if_exist(files): 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.") @@ -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) @@ -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