From 367aefbede20a50f0f127930724c789a7cecebf7 Mon Sep 17 00:00:00 2001 From: Bence Balogh Date: Tue, 18 Jul 2023 15:51:54 +0200 Subject: [PATCH] imgtool: Add write to file option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dávid Házi Signed-off-by: Bence Balogh Change-Id: I6028955be5cbcd20d49ef2126dce8d4636b824a6 --- scripts/imgtool/keys/general.py | 19 +++++++++++++++++-- scripts/imgtool/main.py | 14 ++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/scripts/imgtool/keys/general.py b/scripts/imgtool/keys/general.py index 033a70f0..0393a9a5 100644 --- a/scripts/imgtool/keys/general.py +++ b/scripts/imgtool/keys/general.py @@ -8,7 +8,18 @@ AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" 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(header, end='', file=file) for count, b in enumerate(encoded_bytes): @@ -39,7 +50,11 @@ class KeyClass(object): file=file) def emit_public_pem(self, file=sys.stdout): - print(str(self.get_public_pem(), 'utf-8'), file=file, end='') + 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='') + else: + print(str(self.get_public_pem(), 'utf-8'), file=sys.stdout, end='') def emit_private(self, minimal, format, file=sys.stdout): self._emit( diff --git a/scripts/imgtool/main.py b/scripts/imgtool/main.py index eba557fc..53557075 100755 --- a/scripts/imgtool/main.py +++ b/scripts/imgtool/main.py @@ -128,8 +128,11 @@ def keygen(type, key, password): type=click.Choice(valid_encodings), help='Valid encodings: {}'.format(', '.join(valid_encodings))) @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') -def getpub(key, encoding, lang): +def getpub(key, encoding, lang, output): if encoding and lang: raise click.UsageError('Please use only one of `--encoding/-e` ' 'or `--lang/-l`') @@ -138,14 +141,17 @@ def getpub(key, encoding, lang): # `default=valid_encodings[0]` should be added to `-e` param. lang = valid_langs[0] key = load_key(key) + + if not output: + output = sys.stdout if key is None: print("Invalid passphrase") elif lang == 'c' or encoding == 'lang-c': - key.emit_c_public() + key.emit_c_public(file=output) elif lang == 'rust' or encoding == 'lang-rust': - key.emit_rust_public() + key.emit_rust_public(file=output) elif encoding == 'pem': - key.emit_public_pem() + key.emit_public_pem(file=output) else: raise click.UsageError()