File 0029-tcm_dump-Implement-file-option.patch of Package lio-utils

From 215ca5050fe994e6617fe0e1e80036f8c0f1e2e0 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Thu, 15 May 2014 10:11:12 +0200
Subject: tcm_dump: Implement '--file' option

Implement a '--file' option to dump the script into a file.
This is required for systemd integration as the systemd unit
syntax doesn't support redirection.

References: bnc#876881

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 tcm-py/tcm_dump.py | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/tcm-py/tcm_dump.py b/tcm-py/tcm_dump.py
index 9e182b1..27644e4 100755
--- a/tcm-py/tcm_dump.py
+++ b/tcm-py/tcm_dump.py
@@ -264,6 +264,37 @@ def tcm_backup_to_file(option, opt_str, value, parser):
 	back.close()
 	return backup_file
 
+def tcm_dump_to_file(option, opt_str, value, parser):
+	dump_file = str(value)
+
+	op = "tcm_dump --stdout"
+	p = sub.Popen(op, shell=True, stdout=sub.PIPE).stdout
+	if not p:
+		print "Unable to dump Target_Core_Mod/ConfigFS running state"
+		sys.exit(1)
+
+	try:
+		back = open(dump_file, 'w+')
+	except IOError:
+		print "cannot open " + dump_file
+		p.close()
+		sys.exit(1)
+	else:
+		line = p.readline()
+		while line:
+			print >>back, line.rstrip()
+			line = p.readline()
+
+		back.close()
+
+	p.close()
+	op = "chmod 755 " + dump_file
+	ret = os.system(op)
+	if ret:
+		print "Unable to mark " + dump_file + " as executable"
+
+	return dump_file
+
 def tcm_full_backup(option, opt_str, value, parser):
 	overwrite = str(value)
 
@@ -332,6 +363,8 @@ def main():
 		help="Dump running Target_Core_Mod/ConfigFS syntax to STDOUT")
 	parser.add_option("--t", "--tofile", action="callback", callback=tcm_backup_to_file, nargs=1,
 		type="string", dest="DATE_TIME", help="Backup running Target_Core_Mod/ConfigFS syntax to /etc/target/backup/tcm_backup-<DATE_TIME>.sh")
+        parser.add_option("--f", "--file", action="callback", callback=tcm_dump_to_file, nargs=1,
+		type="string", dest="DUMP_FILE", help="Save running Target_Core_Mod/ConfigFS syntax to <DUMP_FILE>")
 
 	(options, args) = parser.parse_args()
 	if len(sys.argv) == 1:
-- 
1.7.12.4