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:
parent
99613c672f
commit
367aefbede
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue