<p>dexter has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/pysim/+/23686">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pySim-shell: tree/export: catch errors during DF selection<br><br>When a DF is selected, then the method walk() does not catch the<br>related execption, which causes the command to stop immediately. Lets<br>also catch those exceptions and generate appropriate error messages<br>during export.<br><br>Change-Id: I24ccf64965e7b0756c3db77eb2084b22c357a570<br>Related: OS#4963<br>---<br>M pySim-shell.py<br>1 file changed, 31 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/86/23686/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/pySim-shell.py b/pySim-shell.py</span><br><span>index 3059f3f..29138cc 100755</span><br><span>--- a/pySim-shell.py</span><br><span>+++ b/pySim-shell.py</span><br><span>@@ -176,10 +176,26 @@</span><br><span> output_str += " " + str(files[f].fid)</span><br><span> output_str += " " + str(files[f].desc)</span><br><span> self._cmd.poutput(output_str)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if isinstance(files[f], CardDF):</span><br><span style="color: hsl(0, 100%, 40%);">- fcp_dec = self._cmd.rs.select(f, self._cmd)</span><br><span style="color: hsl(0, 100%, 40%);">- self.walk(indent + 1, action, context)</span><br><span style="color: hsl(0, 100%, 40%);">- fcp_dec = self._cmd.rs.select("..", self._cmd)</span><br><span style="color: hsl(120, 100%, 40%);">+ skip_df=False</span><br><span style="color: hsl(120, 100%, 40%);">+ try:</span><br><span style="color: hsl(120, 100%, 40%);">+ fcp_dec = self._cmd.rs.select(f, self._cmd)</span><br><span style="color: hsl(120, 100%, 40%);">+ except Exception as e:</span><br><span style="color: hsl(120, 100%, 40%);">+ skip_df=True</span><br><span style="color: hsl(120, 100%, 40%);">+ df = self._cmd.rs.selected_file</span><br><span style="color: hsl(120, 100%, 40%);">+ df_path_list = df.fully_qualified_path(True)</span><br><span style="color: hsl(120, 100%, 40%);">+ df_skip_reason_str = '/'.join(df_path_list) + "/" + str(f) + ", " + str(e)</span><br><span style="color: hsl(120, 100%, 40%);">+ if context:</span><br><span style="color: hsl(120, 100%, 40%);">+ context['DF_SKIP'] += 1</span><br><span style="color: hsl(120, 100%, 40%);">+ context['DF_SKIP_REASON'].append(df_skip_reason_str)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ # If the DF was skipped, we never have entered the directory</span><br><span style="color: hsl(120, 100%, 40%);">+ # below, so we must not move up.</span><br><span style="color: hsl(120, 100%, 40%);">+ if skip_df == False:</span><br><span style="color: hsl(120, 100%, 40%);">+ self.walk(indent + 1, action, context)</span><br><span style="color: hsl(120, 100%, 40%);">+ fcp_dec = self._cmd.rs.select("..", self._cmd)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> elif action:</span><br><span> df_before_action = self._cmd.rs.selected_file</span><br><span> action(f, context)</span><br><span>@@ -251,7 +267,7 @@</span><br><span> @cmd2.with_argparser(export_parser)</span><br><span> def do_export(self, opts):</span><br><span> """Export files to script that can be imported back later"""</span><br><span style="color: hsl(0, 100%, 40%);">- context = {'ERR':0, 'COUNT':0, 'BAD':[]}</span><br><span style="color: hsl(120, 100%, 40%);">+ context = {'ERR':0, 'COUNT':0, 'BAD':[], 'DF_SKIP':0, 'DF_SKIP_REASON':[]}</span><br><span> if opts.filename:</span><br><span> self.export(opts.filename, context)</span><br><span> else:</span><br><span>@@ -260,8 +276,17 @@</span><br><span> self._cmd.poutput("# bad files: %u" % context['ERR'])</span><br><span> for b in context['BAD']:</span><br><span> self._cmd.poutput("# " + b)</span><br><span style="color: hsl(0, 100%, 40%);">- if context['ERR']:</span><br><span style="color: hsl(0, 100%, 40%);">- raise RuntimeError("unable to export %i file(s)" % context['ERR'])</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ self._cmd.poutput("# skipped dedicated files(s): %u" % context['DF_SKIP'])</span><br><span style="color: hsl(120, 100%, 40%);">+ for b in context['DF_SKIP_REASON']:</span><br><span style="color: hsl(120, 100%, 40%);">+ self._cmd.poutput("# " + b)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if context['ERR'] and context['DF_SKIP']:</span><br><span style="color: hsl(120, 100%, 40%);">+ raise RuntimeError("unable to export %i elementry file(s) and %i dedicated file(s)" % (context['ERR'], context['DF_SKIP']))</span><br><span style="color: hsl(120, 100%, 40%);">+ elif context['ERR']:</span><br><span style="color: hsl(120, 100%, 40%);">+ raise RuntimeError("unable to export %i elementry file(s)" % context['ERR'])</span><br><span style="color: hsl(120, 100%, 40%);">+ elif context['DF_SKIP']:</span><br><span style="color: hsl(120, 100%, 40%);">+ raise RuntimeError("unable to export %i dedicated files(s)" % context['ERR'])</span><br><span> </span><br><span> </span><br><span> @with_default_category('ISO7816 Commands')</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/pysim/+/23686">change 23686</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/pysim/+/23686"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: pysim </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I24ccf64965e7b0756c3db77eb2084b22c357a570 </div>
<div style="display:none"> Gerrit-Change-Number: 23686 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: dexter <pmaier@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>