better handling to check arguments before cmd running
authorGui Chen <gui.chen@intel.com>
Mon, 13 Aug 2012 07:25:26 +0000 (15:25 +0800)
committerGui Chen <gui.chen@intel.com>
Mon, 13 Aug 2012 07:25:26 +0000 (15:25 +0800)
Signed-off-by: Gui Chen <gui.chen@inte-.com>
mic/creator.py
plugins/imager/fs_plugin.py
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py

index d88d7bd..5aa7ab3 100644 (file)
@@ -240,11 +240,24 @@ class Creator(cmdln.Cmdln):
 
         self.postoptparse()
 
-        if os.geteuid() != 0 and args[0] != 'help':
-            msger.error('root permission is required to continue, abort')
-
         return self.cmd(args)
 
+    def precmd(self, argv): # check arguments before cmd
+        if argv[0] == 'help' or argv[0] == '?':
+            return argv
+        if len(argv) == 1:
+            return ['help', argv[0]]
+        elif len(argv) > 2:
+            raise errors.Usage("Extra arguments given")
+
+        if not os.path.exists(argv[1]):
+            raise errors.CreatorError("Can't find file: %s" % argv[1])
+
+        if os.geteuid() != 0:
+            raise msger.error("Root permission is required, abort")
+
+        return argv
+
     def do_auto(self, subcmd, opts, *args):
         """${cmd_name}: auto detect image type from magic header
 
@@ -275,16 +288,6 @@ class Creator(cmdln.Cmdln):
 
             return None
 
-        if not args:
-            self.do_help(['help', subcmd])
-            return None
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
-        if not os.path.exists(args[0]):
-            raise errors.CreatorError("Can't find the file: %s" % args[0])
-
         with open(args[0], 'r') as rf:
             first_line = rf.readline()
 
index 5988b73..9d5e7a5 100644 (file)
@@ -43,18 +43,9 @@ class FsPlugin(ImagerPlugin):
         ${cmd_option_list}
         """
 
-        if not args:
-            raise errors.Usage("need one argument as the path of ks file")
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
         creatoropts = configmgr.create
         ksconf = args[0]
 
-        if not os.path.exists(ksconf):
-            raise errors.CreatorError("Can't find the file: %s" % ksconf)
-
         recording_pkgs = []
         if len(creatoropts['record_pkgs']) > 0:
             recording_pkgs = creatoropts['record_pkgs']
index d62b20c..0023023 100644 (file)
@@ -39,18 +39,9 @@ class LiveCDPlugin(ImagerPlugin):
         ${cmd_option_list}
         """
 
-        if not args:
-            raise errors.Usage("need one argument as the path of ks file")
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
         creatoropts = configmgr.create
         ksconf = args[0]
 
-        if not os.path.exists(ksconf):
-            raise errors.CreatorError("Can't find the file: %s" % ksconf)
-
         if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
             msger.warning('livecd cannot support arm images, Quit')
             return
index 97fc0bd..1e18100 100644 (file)
@@ -41,18 +41,9 @@ class LiveUSBPlugin(ImagerPlugin):
         ${cmd_option_list}
         """
 
-        if not args:
-            raise errors.Usage("need one argument as the path of ks file")
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
         creatoropts = configmgr.create
         ksconf = args[0]
 
-        if not os.path.exists(ksconf):
-            raise errors.CreatorError("Can't find the file: %s" % ksconf)
-
         if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
             msger.warning('liveusb cannot support arm images, Quit')
             return
index f1e8fc8..9986992 100644 (file)
@@ -48,18 +48,9 @@ class LoopPlugin(ImagerPlugin):
         ${cmd_option_list}
         """
 
-        if not args:
-            raise errors.Usage("need one argument as the path of ks file")
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
         creatoropts = configmgr.create
         ksconf = args[0]
 
-        if not os.path.exists(ksconf):
-            raise errors.CreatorError("Can't find the file: %s" % ksconf)
-
         recording_pkgs = []
         if len(creatoropts['record_pkgs']) > 0:
             recording_pkgs = creatoropts['record_pkgs']
index 47f6512..89c1f76 100644 (file)
@@ -48,18 +48,9 @@ class RawPlugin(ImagerPlugin):
         ${cmd_option_list}
         """
 
-        if not args:
-            raise errors.Usage("need one argument as the path of ks file")
-
-        if len(args) != 1:
-            raise errors.Usage("Extra arguments given")
-
         creatoropts = configmgr.create
         ksconf = args[0]
 
-        if not os.path.exists(ksconf):
-            raise errors.CreatorError("Can't find the file: %s" % ksconf)
-
         recording_pkgs = []
         if len(creatoropts['record_pkgs']) > 0:
             recording_pkgs = creatoropts['record_pkgs']