pySim-shell: add filesystem exporter

add a new command "export" that can either export indidividual files or
a whole directory tree. The command will generate a script that contains
update_binary and update_record commands along with the file contents.
This script can later be used to restore multiple files at once.

Change-Id: I82f3ce92cd91a5ed3c4884d62f6b22e9589c8a49
Related: OS#4963
This commit is contained in:
Philipp Maier 2021-02-25 17:06:18 +01:00
parent f62866f4d3
commit 24f7bd3ab5
1 changed files with 58 additions and 0 deletions

View File

@ -170,6 +170,64 @@ class Iso7816Commands(CommandSet):
"""Display a filesystem-tree with all selectable files"""
self.walk()
def export(self, filename, context):
context['COUNT'] += 1
path_list = self._cmd.rs.selected_file.fully_qualified_path(True)
path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)
self._cmd.poutput("#" * 80)
file_str = '/'.join(path_list) + "/" + str(filename) + " " * 80
self._cmd.poutput("# " + file_str[0:77] + "#")
self._cmd.poutput("#" * 80)
self._cmd.poutput("# directory: %s (%s)" % ('/'.join(path_list), '/'.join(path_list_fid)))
try:
fcp_dec = self._cmd.rs.select(filename, self._cmd)
path_list = self._cmd.rs.selected_file.fully_qualified_path(True)
path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)
self._cmd.poutput("# file: %s (%s)" % (path_list[-1], path_list_fid[-1]))
fd = fcp_dec['file_descriptor']
structure = fd['structure']
self._cmd.poutput("# structure: %s" % str(structure))
for f in path_list:
self._cmd.poutput("select " + str(f))
if structure == 'transparent':
result = self._cmd.rs.read_binary()
self._cmd.poutput("update_binary " + str(result[0]))
if structure == 'cyclic' or structure == 'linear_fixed':
num_of_rec = fd['num_of_rec']
for r in range(1, num_of_rec + 1):
result = self._cmd.rs.read_record(r)
self._cmd.poutput("update_record %d %s" % (r, str(result[0])))
fcp_dec = self._cmd.rs.select("..", self._cmd)
except Exception as e:
bad_file_str = '/'.join(path_list) + "/" + str(filename) + ", " + str(e)
self._cmd.poutput("# bad file: %s" % bad_file_str)
context['ERR'] += 1
context['BAD'].append(bad_file_str)
self._cmd.poutput("#")
export_parser = argparse.ArgumentParser()
export_parser.add_argument('--filename', type=str, default=None, help='only export specific file')
@cmd2.with_argparser(export_parser)
def do_export(self, opts):
"""Export files to script that can be imported back later"""
context = {'ERR':0, 'COUNT':0, 'BAD':[]}
if opts.filename:
self.export(opts.filename, context)
else:
self.walk(0, self.export, context)
self._cmd.poutput("# total files visited: %u" % context['COUNT'])
self._cmd.poutput("# bad files: %u" % context['ERR'])
for b in context['BAD']:
self._cmd.poutput("# " + b)
if context['ERR']:
raise RuntimeError("unable to export %i file(s)" % context['ERR'])
@with_default_category('USIM Commands')