[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[rhel6-branch 06/10] Improvements to losetup-stub



Related: rhbz#706099
---
 command-stubs/losetup-stub |   74 ++++++++++++++++++++++++++------------------
 1 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/command-stubs/losetup-stub b/command-stubs/losetup-stub
index bf186a1..af9bd51 100755
--- a/command-stubs/losetup-stub
+++ b/command-stubs/losetup-stub
@@ -2,7 +2,7 @@
 #
 # losetup-stub
 #
-# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
+# Copyright (C) 2007, 2011  Red Hat, Inc.  All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -18,39 +18,53 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+from optparse import OptionParser
 import os
 import sys
 
-# for testing
-if (os.path.exists('isys')):
-    sys.path.append('isys')
-
 sys.path.append('/usr/lib/anaconda')
-
 import isys
-from sys import argv
+
 
 def usage():
-    print "usage: losetup [-d] /dev/loopN [image]"
-    sys.exit(1)
-
-if len(argv) < 3:
-    usage()
-
-if argv[1] == "-d" and len(argv[2]) > 4 and argv[2][-5:-1] == "loop":
-    try:
-        isys.unlosetup(argv[2])
-    except SystemError, (errno, msg):
-        print msg
-        sys.exit (1)
-    sys.exit(0)
-
-if len(argv[1]) > 4 and argv[1][-5:-1] == "loop":
-    try:
-        isys.losetup(argv[1], argv[2])
-    except SystemError, (errno, msg):
-        print msg
-        sys.exit (1)
-    sys.exit(0)
-
-usage()
+    return 'Usage: %prog [-d] <loopdev> [file]'
+
+
+def main(prog, args):
+
+    def err(msg):
+        sys.stderr.write('%s: %s\n' % (os.path.basename(prog), msg))
+        sys.exit(1)
+
+    parser = OptionParser(usage=usage())
+    parser.add_option('-d', '--detach', action='store_true', default=False)
+
+    opts, args = parser.parse_args(args)
+
+    if opts.detach:
+        if not args:
+            err('missing operand')
+
+        for loopdev in args:
+            try:
+                isys.unlosetup(loopdev)
+            except SystemError as e:
+                err(e)
+
+    else:
+        try:
+            loopdev, image = args
+        except ValueError:
+            if len(args) > 2:
+                err("extra operand '%s'" % args[2])
+            else:
+                err('missing operand')
+        else:
+            try:
+                isys.losetup(loopdev, image)
+            except SystemError as e:
+                err(e)
+
+
+if __name__ == '__main__':
+    main(prog=sys.argv[0], args=sys.argv[1:])
-- 
1.7.3.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]