Change in pysim[master]: pySim-shell: add filesystem exporter

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Mar 27 18:22:28 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23210 )

Change subject: pySim-shell: add filesystem exporter
......................................................................

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
---
M pySim-shell.py
1 file changed, 58 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  daniel: Looks good to me, but someone else must approve



diff --git a/pySim-shell.py b/pySim-shell.py
index 8e222f3..5f4624c 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -170,6 +170,64 @@
 		"""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')

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23210
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I82f3ce92cd91a5ed3c4884d62f6b22e9589c8a49
Gerrit-Change-Number: 23210
Gerrit-PatchSet: 12
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <dwillmann at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-CC: dexter <pmaier at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210327/0971c7b0/attachment.htm>


More information about the gerrit-log mailing list