initial import code into git
[platform/upstream/mic.git] / tools / mic-image-create
1 #!/usr/bin/python -t
2
3 import sys, os, os.path, string
4 import micng.utils.argparse as argparse
5 import micng.configmgr as configmgr
6 import micng.pluginmgr as pluginmgr
7
8 class Creator(object):
9     name = 'create'
10
11     def __init__(self):
12         self.configmgr = configmgr.getConfigMgr()
13         self.pluginmgr = pluginmgr.PluginMgr()
14         self.pluginmgr.loadPlugins()
15         self.plugincmds = self.pluginmgr.getPluginByCateg('imager')
16
17     def main(self, argv=None):
18 #        import pdb
19 #        pdb.set_trace()
20         if os.getuid() != 0:
21             print "Please run the program as root"
22             return 0
23         prog = os.path.basename(sys.argv[0])
24         parser = argparse.ArgumentParser(
25                   usage='%s [COMMONOPT] <subcommand> [SUBOPT] ARGS' % prog,
26                   ) 
27         parser.add_argument('-k', '--cache', dest='cache', help='cache diretory')
28         parser.add_argument('-o', '--outdir', dest='outdir', help='output diretory')
29         parser.add_argument('-t', '--tmpdir', dest='tmpdir', help='temp diretory')
30
31
32         subparsers = parser.add_subparsers(title='subcommands')
33         for subcmd, klass in self.plugincmds:
34             subcmd_help = 'create ' + subcmd + ' image'
35             subcmd_parser = subparsers.add_parser(
36                               subcmd, 
37                               usage=prog+' [COMMONOPT] '+subcmd+'  [SUBOPT] ARGS',
38                               help=subcmd_help
39                               )
40             if hasattr(klass, 'do_options'):
41                 add_subopt = getattr(klass, 'do_options')
42                 add_subopt(subcmd_parser)
43             if hasattr(klass, 'do_create'):
44                 do_create = getattr(klass, 'do_create')
45                 subcmd_parser.set_defaults(func=do_create)
46
47         if not argv:
48             parser.print_help()
49             return True
50
51         args = parser.parse_args(argv)
52         if args.outdir:
53             self.configmgr.setProperty('outdir', args.outdir)
54         if args.tmpdir:
55             self.configmgr.setProperty('tmpdir', args.tmpdir)
56         if args.cache:
57             self.configmgr.setProperty('cache', args.cache)
58 #        print 'outdir', self.configmgr.getProperty('outdir')
59 #        print 'tmpdir', self.configmgr.getProperty('tmpdir')
60 #        print 'cache', self.configmgr.getProperty('cache')
61         args.func(args)
62         return True
63
64 if __name__ == "__main__":
65     create = Creator()
66     ret = create.main(sys.argv[1:])
67     sys.exit(ret)