imgtool: Add write to file option

Signed-off-by: Dávid Házi <david.hazi@arm.com>
Signed-off-by: Bence Balogh <bence.balogh@arm.com>
Change-Id: I6028955be5cbcd20d49ef2126dce8d4636b824a6
This commit is contained in:
Bence Balogh 2023-07-18 15:51:54 +02:00 committed by Dávid Vincze
parent 99613c672f
commit 367aefbede
2 changed files with 27 additions and 6 deletions

View File

@ -8,7 +8,18 @@ AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
class KeyClass(object): class KeyClass(object):
def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, len_format=None): def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout,
len_format=None):
if file and file is not sys.stdout:
with open(file, 'w') as file:
self._emit_to_output(header, trailer, encoded_bytes, indent,
file, len_format)
else:
self._emit_to_output(header, trailer, encoded_bytes, indent,
sys.stdout, len_format)
def _emit_to_output(self, header, trailer, encoded_bytes, indent, file,
len_format):
print(AUTOGEN_MESSAGE, file=file) print(AUTOGEN_MESSAGE, file=file)
print(header, end='', file=file) print(header, end='', file=file)
for count, b in enumerate(encoded_bytes): for count, b in enumerate(encoded_bytes):
@ -39,7 +50,11 @@ class KeyClass(object):
file=file) file=file)
def emit_public_pem(self, file=sys.stdout): def emit_public_pem(self, file=sys.stdout):
if file and file is not sys.stdout:
with open(file, 'w') as file:
print(str(self.get_public_pem(), 'utf-8'), file=file, end='') print(str(self.get_public_pem(), 'utf-8'), file=file, end='')
else:
print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='')
def emit_private(self, minimal, format, file=sys.stdout): def emit_private(self, minimal, format, file=sys.stdout):
self._emit( self._emit(

View File

@ -128,8 +128,11 @@ def keygen(type, key, password):
type=click.Choice(valid_encodings), type=click.Choice(valid_encodings),
help='Valid encodings: {}'.format(', '.join(valid_encodings))) help='Valid encodings: {}'.format(', '.join(valid_encodings)))
@click.option('-k', '--key', metavar='filename', required=True) @click.option('-k', '--key', metavar='filename', required=True)
@click.option('-o', '--output', metavar='output', required=False,
help='Specify the output file\'s name. \
The stdout is used if it is not provided.')
@click.command(help='Dump public key from keypair') @click.command(help='Dump public key from keypair')
def getpub(key, encoding, lang): def getpub(key, encoding, lang, output):
if encoding and lang: if encoding and lang:
raise click.UsageError('Please use only one of `--encoding/-e` ' raise click.UsageError('Please use only one of `--encoding/-e` '
'or `--lang/-l`') 'or `--lang/-l`')
@ -138,14 +141,17 @@ def getpub(key, encoding, lang):
# `default=valid_encodings[0]` should be added to `-e` param. # `default=valid_encodings[0]` should be added to `-e` param.
lang = valid_langs[0] lang = valid_langs[0]
key = load_key(key) key = load_key(key)
if not output:
output = sys.stdout
if key is None: if key is None:
print("Invalid passphrase") print("Invalid passphrase")
elif lang == 'c' or encoding == 'lang-c': elif lang == 'c' or encoding == 'lang-c':
key.emit_c_public() key.emit_c_public(file=output)
elif lang == 'rust' or encoding == 'lang-rust': elif lang == 'rust' or encoding == 'lang-rust':
key.emit_rust_public() key.emit_rust_public(file=output)
elif encoding == 'pem': elif encoding == 'pem':
key.emit_public_pem() key.emit_public_pem(file=output)
else: else:
raise click.UsageError() raise click.UsageError()