Update the mic doc
[platform/upstream/mic.git] / tools / mic
1 #!/usr/bin/env python
2
3 #Copyright (c) 2011 Intel, Inc.
4 #
5 #This program is free software; you can redistribute it and/or modify it
6 #under the terms of the GNU General Public License as published by the Free
7 #Software Foundation; version 2 of the License
8 #
9 #This program is distributed in the hope that it will be useful, but
10 #WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #
18 # pylint: disable-msg=E0611, E1101, R0201
19 # E0611: no name in module, some attributes are set during running, so ignore it
20 # E1101: %s %r has no %r member, some attributes are set during running,
21 #        so ignore it
22 # R0201: Method could be a function
23
24 """
25  This mudule is entry for mic.
26  It defines a class named MicCmd inheriting Cmdln, and supplies interfaces like
27  'create, chroot, convert' and also some parameters for command 'mic'.
28 """
29 import os
30 import sys
31 import errno
32
33 from argparse import ArgumentParser, SUPPRESS
34
35 from mic import msger, __version__ as VERSION
36 from mic.utils import misc, errors
37 from mic.conf import configmgr
38 from mic.plugin import pluginmgr
39 from mic.helpformat import MICHelpFormatter, subparser
40     
41
42 @subparser
43 def chroot_parser(parser):
44     """chroot into an image
45
46     Examples:
47         mic chroot platform.img
48         mic chroot platform.img ls
49     """
50     parser.add_argument('imagefile', help='Path of image file')
51     parser.add_argument('-s', '--saveto', action = 'store', dest = 'saveto', default = None,
52                         help = "Save the unpacked image to specified dir")
53     parser.add_argument('-c', '--cmd', dest = 'cmd', default = None,
54                         help = "command which will be executed in chroot environment")
55     parser.set_defaults(alias="ch")
56     return parser
57     
58 @subparser
59 def create_parser(parser):
60     """create an image
61     Examples:
62       $ mic -d -v create auto handset_blackbay.ks
63       $ mic -d -v cr loop handset_blackbay.ks --logfile=mic.log
64     """
65
66     parent_parser = ArgumentParser(add_help=False)
67     parent_parser.add_argument('ksfile', help='Path of ksfile');
68     parent_parser.add_argument('--logfile', dest='logfile', default=None,
69                                help='Path of logfile')
70     parent_parser.add_argument('-c', '--config', dest='config', default=None,
71                                help='Specify config file for mic')                
72     parent_parser.add_argument('-k', '--cachedir', action='store',
73                                dest='cachedir', default=None,
74                                help='Cache directory to store the downloaded')
75     parent_parser.add_argument('-o', '--outdir', action='store', dest='outdir', 
76                                default=None, help='Output directory')
77     parent_parser.add_argument('-A', '--arch', dest='arch', default=None,
78                                help='Specify repo architecture')
79     parent_parser.add_argument('--release', dest='release', default=None, metavar='RID',
80                                help='Generate a release of RID with all necessary'
81                                ' files, when @BUILD_ID@ is contained in '
82                                'kickstart file, it will be replaced by RID')
83     parent_parser.add_argument("--record-pkgs", dest="record_pkgs", default=None,
84                                help='Record the info of installed packages, '
85                                'multiple values can be specified which '
86                                'joined by ",", valid values: "name", '
87                                '"content", "license", "vcs"')
88     parent_parser.add_argument('--pkgmgr', dest='pkgmgr', default=None,
89                                help='Specify backend package manager')
90     parent_parser.add_argument('--local-pkgs-path', dest='local_pkgs_path', default=None,
91                                help='Path for local pkgs(rpms) to be installed')
92     parent_parser.add_argument('--runtime', dest='runtime', default=None,
93                                help='Specify runtime mode, avaiable: bootstrap')
94     # --taring-to is alias to --pack-to
95     parent_parser.add_argument('--taring-to', dest='pack_to', default=None,
96                                help=SUPPRESS)
97     parent_parser.add_argument('--pack-to', dest='pack_to', default=None,
98                                help='Pack the images together into the specified'
99                                     ' achive, extension supported: .zip, .tar, '
100                                     '.tar.gz, .tar.bz2, etc. by default, .tar '
101                                     'will be used')
102     parent_parser.add_argument('--copy-kernel', action='store_true', dest='copy_kernel',
103                                help='Copy kernel files from image /boot directory'
104                                     ' to the image output directory.')
105     parent_parser.add_argument('--install-pkgs', action='store', dest='install_pkgs', default=None,
106                                help='Specify what type of packages to be installed,'
107                                     ' valid: source, debuginfo, debugsource')
108     parent_parser.add_argument('--check-pkgs', action='store', dest='check_pkgs', default=[],
109                                help='Check if given packages would be installed, '
110                                     'packages should be separated by comma')
111     parent_parser.add_argument('--tmpfs', action='store_true', dest='enabletmpfs',
112                                help='Setup tmpdir as tmpfs to accelerate, experimental'
113                                     ' feature, use it if you have more than 4G memory')
114     parent_parser.add_argument('--repourl', action='append', dest='repourl', default=[],
115                                help=SUPPRESS)
116     parent_parser.add_argument('-R', '--repo', action='append',
117                                dest='repo', default=[],
118                                help=SUPPRESS)
119     parent_parser.add_argument('--ignore-ksrepo', action='store_true',
120                                dest='ignore_ksrepo', default=False,
121                                help=SUPPRESS)
122     parent_parser.add_argument('--strict-mode', action='store_true',
123                                dest='strict_mode', default=False,
124                                help='Abort creation of image, if there are some errors'
125                                     ' during rpm installation. ')
126     parser.set_defaults(alias="cr")
127
128     subparsers  = parser.add_subparsers(title='Subcommands', dest='subcommand')
129     auto_parser = subparsers.add_parser('auto', parents=[parent_parser], help='auto detect image type from magic header')
130     
131     fs_parser   = subparsers.add_parser('fs', parents=[parent_parser], 
132                                         help='create fs image')
133     fs_parser.add_argument("--include-src", dest = "include_src",action = "store_true",
134                            default = False, help = "Generate a image with source rpms included")
135                   
136     loop_parser = subparsers.add_parser('loop', parents=[parent_parser], help='create loop image')
137
138     loop_parser.add_argument("--compress-disk-image", dest="compress_image",
139                              choices=("gz", "bz2", "lzo"), default=None,
140                              help="Same with --compress-image")
141     # alias to compress-image for compatibility
142     loop_parser.add_argument("--compress-image", dest="compress_image",
143                              choices=("gz", "bz2", "lzo"), default=None,
144                              help="Compress all loop images with 'gz' or 'bz2' or 'lzo',"
145                                   "Note: if you want to use 'lzo', package 'lzop' is needed to"
146                                   "be installed manually.")
147     loop_parser.add_argument("--shrink", action='store_true', default=False,
148                   help="Whether to shrink loop images to minimal size")
149                   
150     qcow_parser = subparsers.add_parser('qcow', parents=[parent_parser], help='create qcow image')
151
152     return parser
153
154 def main(argv):
155     """Script entry point."""
156     
157     def print_version():
158         """log name, verion, hostname"""
159         
160         name = 'mic'
161         msger.raw("%s %s (%s)" % (name,
162                                   VERSION,
163                                   misc.get_hostname_distro_str()))
164                                   
165     def has_parameter(arg, arglist):
166         """
167         Helper function.
168         Check if argument requires parameter by analyzing
169         its action. Parameter is required only for 'store' and 'append' actions
170         """
171         if arg.startswith('-'):
172             for args in arglist:
173                 if arg in (args['short'], args['long']):
174                     if args.get('action') in (None, 'store', 'append'):
175                         return True
176                     return False
177
178     # Create top level parser
179     epilog = "Try 'mic SUBCOMMAND --help' for help on a specific subcommand."
180     description = "mic - the Image Creation tool"
181     parser = ArgumentParser(description=description, epilog=epilog,
182                             formatter_class=MICHelpFormatter)
183
184     # List of global arguments
185     # The main purpose of this structure is to contain arguments
186     # of add_argument. This is used to do aliasing properly
187     # (see code under the comment 'replace aliases with real commands')
188     global_args = [{'short': '-V', 'long': '--version', 'action': 'version',
189                     'version': '%(prog)s ' + VERSION},
190                    {'short': '-d', 'long': '--debug', 'action': 'store_true',
191                     'help': 'debug output'},
192                    {'short': '-v', 'long': '--verbose', 'action': 'store_true',
193                     'help': 'verbose output'},
194                    {'short': '-i', 'long': '--interactive', 'action': 'store_true',
195                     'dest': 'interactive', 'default': 'True', 'help': 'interactive output'}, 
196                    {'short': '', 'long': '--non-interactive', 'action': 'store_false',
197                     'dest': 'interactive', 'default': 'True', 'help': 'non-interactive output'}, 
198                   ]
199
200     for args in global_args:
201         parser_kwargs = {}
202         for key in ('action', 'help', 'version', 'default', 'dest'):
203             if key in args:
204                 parser_kwargs[key] = args[key]
205         
206         if args['short'] is '':
207             parser.add_argument(args['long'], **parser_kwargs)
208         else:
209             parser.add_argument(args['short'], args['long'], **parser_kwargs)
210
211     # hacked by the request of cmdln lovers
212     parser.format_usage = parser.format_help
213
214     # Create parsers for subcommands
215     subparsers = parser.add_subparsers(title='subcommands')
216
217     # collect aliases
218     aliases = {}
219     for name, obj in globals().iteritems():
220         if name.endswith('_parser') and callable(obj):
221             aliases[obj(subparsers).get_default('alias')] = name.split('_')[0]
222
223     # replace aliases with real commands
224     for i, arg in enumerate(argv[1:]):
225         if not arg.startswith('-'):
226             # argv[i] is previous argument to arg
227             if not has_parameter(argv[i], global_args) and arg in aliases:
228                 argv[i+1] = aliases[arg]
229                 break
230     
231     # Parse arguments
232     args = parser.parse_args(argv[1:])
233
234     if args.interactive:
235         msger.enable_interactive()
236     else:
237         msger.disable_interactive()
238
239     if args.verbose:
240         msger.set_loglevel('VERBOSE')
241
242     if args.debug:
243         try:
244             import rpm
245             rpm.setVerbosity(rpm.RPMLOG_NOTICE)
246         except ImportError:
247             pass
248
249         msger.set_loglevel('DEBUG')
250
251     print_version()
252
253     # Import target module and call 'main' from it
254     module = __import__("mic.%s" % args.module, fromlist=[args.module])
255     return module.main(parser, args, argv[1:])
256
257     
258 if __name__ == "__main__":
259     try:
260         sys.exit(main(sys.argv))
261     except KeyboardInterrupt:
262         msger.error('\n^C catched, program aborted.')
263     except IOError as ioerr:
264         # catch 'no space left' exception, etc
265         if ioerr.errno == errno.ENOSPC:
266             msger.error('\nNo space left on device')
267         raise
268     except errors.Usage as usage:
269         msger.error(str(usage))
270     except errors.Abort as  msg:
271         msger.info(str(msg))
272     except errors.CreatorError as err:
273         if msger.get_loglevel() == 'DEBUG':
274             import traceback
275             msger.error(traceback.format_exc())
276         else:
277             msger.error(str(err))