From 41513443cb3d431c5e1522d3d43e293ea67fc6a0 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 18 Feb 2022 21:55:17 +0800 Subject: [PATCH] espefuse: Handle errors from script files for execute_scripts cmd Closes https://github.com/espressif/esp-idf/issues/8420 --- espressif/efuse/esp32/operations.py | 9 +++++++-- espressif/efuse/esp32c2/operations.py | 9 +++++++-- espressif/efuse/esp32c3/operations.py | 9 +++++++-- espressif/efuse/esp32h2beta1/operations.py | 9 +++++++-- espressif/efuse/esp32s2/operations.py | 9 +++++++-- espressif/efuse/esp32s3/operations.py | 9 +++++++-- espressif/efuse/esp32s3beta2/operations.py | 9 +++++++-- 7 files changed, 49 insertions(+), 14 deletions(-) diff --git a/espressif/efuse/esp32/operations.py b/espressif/efuse/esp32/operations.py index 931ccc5fa..dd65592f2 100644 --- a/espressif/efuse/esp32/operations.py +++ b/espressif/efuse/esp32/operations.py @@ -9,6 +9,7 @@ import argparse import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -220,7 +221,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -245,7 +250,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32c2/operations.py b/espressif/efuse/esp32c2/operations.py index 9c1589517..b58533337 100644 --- a/espressif/efuse/esp32c2/operations.py +++ b/espressif/efuse/esp32c2/operations.py @@ -9,6 +9,7 @@ import argparse import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -210,7 +211,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -235,7 +240,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32c3/operations.py b/espressif/efuse/esp32c3/operations.py index 007235b60..116ae4710 100644 --- a/espressif/efuse/esp32c3/operations.py +++ b/espressif/efuse/esp32c3/operations.py @@ -9,6 +9,7 @@ import argparse import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -236,7 +237,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -261,7 +266,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32h2beta1/operations.py b/espressif/efuse/esp32h2beta1/operations.py index 125375c60..30bcd99bc 100644 --- a/espressif/efuse/esp32h2beta1/operations.py +++ b/espressif/efuse/esp32h2beta1/operations.py @@ -9,6 +9,7 @@ import argparse import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -237,7 +238,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -262,7 +267,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32s2/operations.py b/espressif/efuse/esp32s2/operations.py index ffa0e3165..b07e8e9ab 100644 --- a/espressif/efuse/esp32s2/operations.py +++ b/espressif/efuse/esp32s2/operations.py @@ -10,6 +10,7 @@ import argparse import io import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -340,7 +341,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -365,7 +370,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32s3/operations.py b/espressif/efuse/esp32s3/operations.py index e8821adbc..ac3ee7385 100644 --- a/espressif/efuse/esp32s3/operations.py +++ b/espressif/efuse/esp32s3/operations.py @@ -10,6 +10,7 @@ import argparse import io import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -339,7 +340,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -364,7 +369,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: diff --git a/espressif/efuse/esp32s3beta2/operations.py b/espressif/efuse/esp32s3beta2/operations.py index d4b1fcef1..e924061eb 100644 --- a/espressif/efuse/esp32s3beta2/operations.py +++ b/espressif/efuse/esp32s3beta2/operations.py @@ -9,6 +9,7 @@ import argparse import os # noqa: F401. It is used in IDF scripts +import traceback import espsecure @@ -274,7 +275,11 @@ def espefuse(esp, efuses, args, command): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='operation') add_commands(subparsers, efuses) - cmd_line_args = parser.parse_args(command.split()) + try: + cmd_line_args = parser.parse_args(command.split()) + except SystemExit: + traceback.print_stack() + raise esptool.FatalError('"{}" - incorrect command'.format(command)) if cmd_line_args.operation == 'execute_scripts': configfiles = cmd_line_args.configfiles index = cmd_line_args.index @@ -299,7 +304,7 @@ def execute_scripts(esp, efuses, args): for file in scripts: with open(file.name, 'r') as file: - exec(file.read()) + exec(compile(file.read(), file.name, 'exec')) if args.debug: for block in efuses.blocks: