[TIC-CORE] support cli commands
[archive/20170607/tools/tic-core.git] / tools / tic-core
1 #!/usr/bin/python
2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3 #
4 # Contact: 
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
6
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # Contributors:
20 # - S-Core Co., Ltd
21
22 from argparse import ArgumentParser
23 from argparse import RawDescriptionHelpFormatter
24 import os
25 import sys
26 import json
27 import logging
28 from tic import command
29 from tic.utils import log
30 from tic.utils import file
31 from tic.utils import error
32 from tic.server import tic_server
33
34 __version__ = 0.1
35 __date__ = '2016-11-07'
36 __updated__ = '2016-11-07'
37
38 def create_parser():
39     program_name = os.path.basename(sys.argv[0])
40     program_version = "%s" % __version__
41     program_version_message = '%s %s' % (program_name, program_version)
42     program_shortdesc = 'tic-core is a tool for analyzing package dependencies and generating Image' #__import__('__main__').__doc__
43     program_epilog = "Try 'tic-core SUBCOMMAND --help' for help on a specific sub-command."
44     
45     parser = ArgumentParser(description=program_shortdesc, epilog=program_epilog,
46                             formatter_class=RawDescriptionHelpFormatter)
47     parser.add_argument('-v', '--version', action='version', version=program_version_message)
48     subparsers = parser.add_subparsers(dest='subparser_name', help='sub-command help')
49         
50     # create the parser for the 'analyze' command
51     parser_analyze = subparsers.add_parser('analyze', help='analyze install-dependency of packages')
52     parser_analyze.add_argument('-r', "--repos", dest="repos", metavar="urls", nargs='+', help="The URL of repository [default: %(default)s]")
53     parser_analyze.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="The path of recipe")
54     parser_analyze.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the outdir path", default=os.getcwd())
55     # create the parser for the 'export' command
56     parser_export = subparsers.add_parser('export', help='export files')
57     parser_export.add_argument('-f', "--format", dest="format", metavar="recipe/ks", help="exports file format", required=True)
58     parser_export.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="recipe files")
59     parser_export.add_argument('-o', "--outdir", dest="outdir", action="store", help="export file output directory", default=os.getcwd())
60     # create the parser for the 'create' command
61     parser_create = subparsers.add_parser('createimage', help='create an image for tizen')
62     parser_create.add_argument('-c', "--recipes", dest="recipes", metavar="recipes", nargs='+', help="recipe files to be used for image creation")
63     parser_create.add_argument('-k', "--ks", dest="kickstart", metavar="kickstart", help="ks file to be used for image creation")
64     parser_create.add_argument('-o', "--outdir", dest="outdir", action="store", help="image output directory", default=os.getcwd())
65     
66     parser_start = subparsers.add_parser('start', help='start the tic-core demon on system. port 59001 is used by default ')
67     parser_start.add_argument('-p', "--port", dest="port", action="store", help="port number", default=59001)
68     
69     return parser
70
71 def main(argv):
72     logger = logging.getLogger('tic')
73     try:
74         # Setup argument parser
75         parser = create_parser()
76         # Process arguments
77         args = parser.parse_args(argv[1:])
78         
79         if args.subparser_name == 'analyze':
80             view_data = command.analyze(args.repos, args.recipes)
81             output_dir=os.path.abspath(os.path.expanduser(args.outdir))
82             file.write(os.path.join(output_dir, 'viewdata.json'), json.dumps(view_data))
83         elif args.subparser_name == 'export':
84             #TODO Temporary code(should be deleted)
85             recipes={'name':'default'}
86             packages=['attr', 'filesystem']
87             output=command.exports(args.format, recipes, packages, args.outdir)
88             logger.info("export the %s file: %s", args.format, output)
89         elif args.subparser_name == 'createimage':
90             if args.recipes or args.kickstart:
91                 command.createimage(args.recipes, args.kickstart, args.outdir)
92             else:
93                 logger.info('kickstart or recipes file is required')
94         elif args.subparser_name == 'start':
95             tic_server.start(args.port)
96         return 0
97     except KeyboardInterrupt:
98         ### handle keyboard interrupt ###
99         return 0
100     except error.TICError as err:
101         logger.error(err)
102     except Exception as ex:
103         logger.error(ex)
104         return 2
105     
106 if __name__ == "__main__":
107     log.setup('tic')
108     sys.exit(main(sys.argv))
109
110