imgtool: update a few errors to use click's

Click has better UI for exceptions, so instead of throwing a backtrace,
allow it to print a nicer error message.

Signed-off-by: Fabio Utzig <utzig@apache.org>
This commit is contained in:
Fabio Utzig 2020-01-15 11:37:51 -03:00 committed by Fabio Utzig
parent 9a492d5e87
commit 1f50892096
2 changed files with 21 additions and 12 deletions

View File

@ -156,6 +156,7 @@ class Image():
def load(self, path): def load(self, path):
"""Load an image from a given file""" """Load an image from a given file"""
ext = os.path.splitext(path)[1][1:].lower() ext = os.path.splitext(path)[1][1:].lower()
try:
if ext == INTEL_HEX_EXT: if ext == INTEL_HEX_EXT:
ih = IntelHex(path) ih = IntelHex(path)
self.payload = ih.tobinarray() self.payload = ih.tobinarray()
@ -163,6 +164,8 @@ class Image():
else: else:
with open(path, 'rb') as f: with open(path, 'rb') as f:
self.payload = f.read() self.payload = f.read()
except FileNotFoundError:
raise click.UsageError("Input file not found")
# Add the image header if needed. # Add the image header if needed.
if self.pad_header and self.header_size > 0: if self.pad_header and self.header_size > 0:
@ -180,8 +183,8 @@ class Image():
if ext == INTEL_HEX_EXT: if ext == INTEL_HEX_EXT:
# input was in binary format, but HEX needs to know the base addr # input was in binary format, but HEX needs to know the base addr
if self.base_addr is None and hex_addr is None: if self.base_addr is None and hex_addr is None:
raise Exception("No address exists in input file neither was " raise click.UsageError("No address exists in input file "
"it provided by user") "neither was it provided by user")
h = IntelHex() h = IntelHex()
if hex_addr is not None: if hex_addr is not None:
self.base_addr = hex_addr self.base_addr = hex_addr
@ -381,7 +384,8 @@ class Image():
return MAX_ALIGN * 2 + magic_size return MAX_ALIGN * 2 + magic_size
else: else:
if write_size not in set([1, 2, 4, 8]): if write_size not in set([1, 2, 4, 8]):
raise Exception("Invalid alignment: {}".format(write_size)) raise click.BadParameter("Invalid alignment: {}".format(
write_size))
m = DEFAULT_MAX_SECTORS if max_sectors is None else max_sectors m = DEFAULT_MAX_SECTORS if max_sectors is None else max_sectors
trailer = m * 3 * write_size # status area trailer = m * 3 * write_size # status area
if enckey is not None: if enckey is not None:

View File

@ -22,6 +22,7 @@ import imgtool.keys as keys
import sys import sys
from imgtool import image, imgtool_version from imgtool import image, imgtool_version
from imgtool.version import decode_version from imgtool.version import decode_version
from .keys import RSAUsageError, ECDSAUsageError, Ed25519UsageError
def gen_rsa2048(keyfile, passwd): def gen_rsa2048(keyfile, passwd):
@ -116,7 +117,10 @@ def getpriv(key, minimal):
key = load_key(key) key = load_key(key)
if key is None: if key is None:
print("Invalid passphrase") print("Invalid passphrase")
try:
key.emit_private(minimal) key.emit_private(minimal)
except (RSAUsageError, ECDSAUsageError, Ed25519UsageError) as e:
raise click.UsageError(e)
@click.argument('imgfile') @click.argument('imgfile')
@ -254,7 +258,8 @@ def sign(key, align, version, header_size, pad_header, slot_size, pad,
or (isinstance(key, keys.RSA) and or (isinstance(key, keys.RSA) and
not isinstance(enckey, keys.RSAPublic))): not isinstance(enckey, keys.RSAPublic))):
# FIXME # FIXME
raise Exception("Signing and encryption must use the same type of key") raise click.UsageError("Signing and encryption must use the same "
"type of key")
img.create(key, enckey, dependencies) img.create(key, enckey, dependencies)
img.save(outfile, hex_addr) img.save(outfile, hex_addr)