From: Gui Chen Date: Mon, 13 Aug 2012 07:25:26 +0000 (+0800) Subject: better handling to check arguments before cmd running X-Git-Tag: 0.15~67 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=96b70b026f99ca2c8e3ff41c3ea304f9e6922d26;p=tools%2Fmic.git better handling to check arguments before cmd running Signed-off-by: Gui Chen --- diff --git a/mic/creator.py b/mic/creator.py index d88d7bd..5aa7ab3 100644 --- a/mic/creator.py +++ b/mic/creator.py @@ -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() diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index 5988b73..9d5e7a5 100644 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -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'] diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index d62b20c..0023023 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -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 diff --git a/plugins/imager/liveusb_plugin.py b/plugins/imager/liveusb_plugin.py index 97fc0bd..1e18100 100644 --- a/plugins/imager/liveusb_plugin.py +++ b/plugins/imager/liveusb_plugin.py @@ -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 diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index f1e8fc8..9986992 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -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'] diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 47f6512..89c1f76 100644 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -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']