<p>laforge <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/pysim/+/23210">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  daniel: Looks good to me, but someone else must approve

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pySim-shell: add filesystem exporter<br><br>add a new command "export" that can either export indidividual files or<br>a whole directory tree. The command will generate a script that contains<br>update_binary and update_record commands along with the file contents.<br>This script can later be used to restore multiple files at once.<br><br>Change-Id: I82f3ce92cd91a5ed3c4884d62f6b22e9589c8a49<br>Related: OS#4963<br>---<br>M pySim-shell.py<br>1 file changed, 58 insertions(+), 0 deletions(-)<br><br></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 8e222f3..5f4624c 100755</span><br><span>--- a/pySim-shell.py</span><br><span>+++ b/pySim-shell.py</span><br><span>@@ -170,6 +170,64 @@</span><br><span>                """Display a filesystem-tree with all selectable files"""</span><br><span>              self.walk()</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+       def export(self, filename, context):</span><br><span style="color: hsl(120, 100%, 40%);">+          context['COUNT'] += 1</span><br><span style="color: hsl(120, 100%, 40%);">+         path_list = self._cmd.rs.selected_file.fully_qualified_path(True)</span><br><span style="color: hsl(120, 100%, 40%);">+             path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+              self._cmd.poutput("#" * 80)</span><br><span style="color: hsl(120, 100%, 40%);">+         file_str = '/'.join(path_list) + "/" + str(filename) + " " * 80</span><br><span style="color: hsl(120, 100%, 40%);">+           self._cmd.poutput("# " + file_str[0:77] + "#")</span><br><span style="color: hsl(120, 100%, 40%);">+            self._cmd.poutput("#" * 80)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               self._cmd.poutput("# directory: %s (%s)" % ('/'.join(path_list), '/'.join(path_list_fid)))</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(filename, self._cmd)</span><br><span style="color: hsl(120, 100%, 40%);">+                    path_list = self._cmd.rs.selected_file.fully_qualified_path(True)</span><br><span style="color: hsl(120, 100%, 40%);">+                     path_list_fid = self._cmd.rs.selected_file.fully_qualified_path(False)</span><br><span style="color: hsl(120, 100%, 40%);">+                        self._cmd.poutput("# file: %s (%s)" % (path_list[-1], path_list_fid[-1]))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                 fd = fcp_dec['file_descriptor']</span><br><span style="color: hsl(120, 100%, 40%);">+                       structure = fd['structure']</span><br><span style="color: hsl(120, 100%, 40%);">+                   self._cmd.poutput("# structure: %s" % str(structure))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                     for f in path_list:</span><br><span style="color: hsl(120, 100%, 40%);">+                           self._cmd.poutput("select " + str(f))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                     if structure == 'transparent':</span><br><span style="color: hsl(120, 100%, 40%);">+                                result = self._cmd.rs.read_binary()</span><br><span style="color: hsl(120, 100%, 40%);">+                           self._cmd.poutput("update_binary " + str(result[0]))</span><br><span style="color: hsl(120, 100%, 40%);">+                        if structure == 'cyclic' or structure == 'linear_fixed':</span><br><span style="color: hsl(120, 100%, 40%);">+                              num_of_rec = fd['num_of_rec']</span><br><span style="color: hsl(120, 100%, 40%);">+                         for r in range(1, num_of_rec + 1):</span><br><span style="color: hsl(120, 100%, 40%);">+                                    result = self._cmd.rs.read_record(r)</span><br><span style="color: hsl(120, 100%, 40%);">+                                  self._cmd.poutput("update_record %d %s" % (r, str(result[0])))</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%);">+              except Exception as e:</span><br><span style="color: hsl(120, 100%, 40%);">+                        bad_file_str = '/'.join(path_list) + "/" + str(filename) + ", " + str(e)</span><br><span style="color: hsl(120, 100%, 40%);">+                  self._cmd.poutput("# bad file: %s" % bad_file_str)</span><br><span style="color: hsl(120, 100%, 40%);">+                  context['ERR'] += 1</span><br><span style="color: hsl(120, 100%, 40%);">+                   context['BAD'].append(bad_file_str)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+         self._cmd.poutput("#")</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    export_parser = argparse.ArgumentParser()</span><br><span style="color: hsl(120, 100%, 40%);">+     export_parser.add_argument('--filename', type=str, default=None, help='only export specific file')</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  @cmd2.with_argparser(export_parser)</span><br><span style="color: hsl(120, 100%, 40%);">+   def do_export(self, opts):</span><br><span style="color: hsl(120, 100%, 40%);">+            """Export files to script that can be imported back later"""</span><br><span style="color: hsl(120, 100%, 40%);">+            context = {'ERR':0, 'COUNT':0, 'BAD':[]}</span><br><span style="color: hsl(120, 100%, 40%);">+              if opts.filename:</span><br><span style="color: hsl(120, 100%, 40%);">+                     self.export(opts.filename, context)</span><br><span style="color: hsl(120, 100%, 40%);">+           else:</span><br><span style="color: hsl(120, 100%, 40%);">+                 self.walk(0, self.export, context)</span><br><span style="color: hsl(120, 100%, 40%);">+            self._cmd.poutput("# total files visited: %u" % context['COUNT'])</span><br><span style="color: hsl(120, 100%, 40%);">+           self._cmd.poutput("# bad files:           %u" % context['ERR'])</span><br><span style="color: hsl(120, 100%, 40%);">+             for b in context['BAD']:</span><br><span style="color: hsl(120, 100%, 40%);">+                      self._cmd.poutput("#  " + b)</span><br><span style="color: hsl(120, 100%, 40%);">+                if context['ERR']:</span><br><span style="color: hsl(120, 100%, 40%);">+                    raise RuntimeError("unable to export %i file(s)" % context['ERR'])</span><br><span> </span><br><span> </span><br><span> @with_default_category('USIM Commands')</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/pysim/+/23210">change 23210</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/+/23210"/><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: I82f3ce92cd91a5ed3c4884d62f6b22e9589c8a49 </div>
<div style="display:none"> Gerrit-Change-Number: 23210 </div>
<div style="display:none"> Gerrit-PatchSet: 12 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: daniel <dwillmann@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-CC: dexter <pmaier@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>