From f408a400394c2170dfdfadff3f1b3dff1256786a Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Fri, 9 Apr 2021 21:16:12 +0200 Subject: pySim-shell: tree/export: catch errors during DF selection When a DF is selected, then the method walk() does not catch the related execption, which causes the command to stop immediately. Lets also catch those exceptions and generate appropriate error messages during export. Change-Id: I24ccf64965e7b0756c3db77eb2084b22c357a570 Related: OS#4963 --- pySim-shell.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/pySim-shell.py b/pySim-shell.py index 3059f3f..29138cc 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -176,10 +176,26 @@ class PySimCommands(CommandSet): output_str += " " + str(files[f].fid) output_str += " " + str(files[f].desc) self._cmd.poutput(output_str) + if isinstance(files[f], CardDF): - fcp_dec = self._cmd.rs.select(f, self._cmd) - self.walk(indent + 1, action, context) - fcp_dec = self._cmd.rs.select("..", self._cmd) + skip_df=False + try: + fcp_dec = self._cmd.rs.select(f, self._cmd) + except Exception as e: + skip_df=True + df = self._cmd.rs.selected_file + df_path_list = df.fully_qualified_path(True) + df_skip_reason_str = '/'.join(df_path_list) + "/" + str(f) + ", " + str(e) + if context: + context['DF_SKIP'] += 1 + context['DF_SKIP_REASON'].append(df_skip_reason_str) + + # If the DF was skipped, we never have entered the directory + # below, so we must not move up. + if skip_df == False: + self.walk(indent + 1, action, context) + fcp_dec = self._cmd.rs.select("..", self._cmd) + elif action: df_before_action = self._cmd.rs.selected_file action(f, context) @@ -251,7 +267,7 @@ class PySimCommands(CommandSet): @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':[]} + context = {'ERR':0, 'COUNT':0, 'BAD':[], 'DF_SKIP':0, 'DF_SKIP_REASON':[]} if opts.filename: self.export(opts.filename, context) else: @@ -260,8 +276,17 @@ class PySimCommands(CommandSet): 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']) + + self._cmd.poutput("# skipped dedicated files(s): %u" % context['DF_SKIP']) + for b in context['DF_SKIP_REASON']: + self._cmd.poutput("# " + b) + + if context['ERR'] and context['DF_SKIP']: + raise RuntimeError("unable to export %i elementry file(s) and %i dedicated file(s)" % (context['ERR'], context['DF_SKIP'])) + elif context['ERR']: + raise RuntimeError("unable to export %i elementry file(s)" % context['ERR']) + elif context['DF_SKIP']: + raise RuntimeError("unable to export %i dedicated files(s)" % context['ERR']) @with_default_category('ISO7816 Commands') -- cgit v1.2.3