8c24c8d0cecda62f8bfc78813c3d58d9d6ee353c
[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 from tic.config import configmgr
34
35 __version__ = 0.1
36 __date__ = '2016-11-07'
37 __updated__ = '2016-11-07'
38
39 def create_parser():
40     program_name = os.path.basename(sys.argv[0])
41     program_version = "%s" % __version__
42     program_version_message = '%s %s' % (program_name, program_version)
43     program_shortdesc = 'tic-core is a tool for analyzing package dependencies and generating Image' #__import__('__main__').__doc__
44     program_epilog = "Try 'tic-core SUBCOMMAND --help' for help on a specific sub-command."
45     
46     parser = ArgumentParser(description=program_shortdesc, epilog=program_epilog,
47                             formatter_class=RawDescriptionHelpFormatter)
48     parser.add_argument('-v', '--version', action='version', version=program_version_message)
49     subparsers = parser.add_subparsers(dest='subparser_name', help='sub-command help')
50         
51     # create the parser for the 'analyze' command
52     parser_analyze = subparsers.add_parser('analyze', help='analyze install-dependency of packages')
53     parser_analyze.add_argument('-r', "--repos", dest="repos", metavar="urls", nargs='+', help="The URL of repository [default: %(default)s]")
54     parser_analyze.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="The path of recipe")
55     parser_analyze.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the outdir path", default=os.getcwd())
56     # create the parser for the 'export' command
57     parser_export = subparsers.add_parser('export', help='export files')
58     parser_export.add_argument('-f', "--format", dest="format", metavar="recipe/ks", help="exports file format", required=True)
59     parser_export.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="recipe files")
60     parser_export.add_argument('-o', "--outdir", dest="outdir", action="store", help="export file output directory", default=os.getcwd())
61     # create the parser for the 'create' command
62     parser_create = subparsers.add_parser('createimage', help='create an image for tizen')
63     parser_create.add_argument('-c', "--recipes", dest="recipes", metavar="recipes", nargs='+', help="recipe files to be used for image creation")
64     parser_create.add_argument('-k', "--ks", dest="kickstart", metavar="kickstart", help="ks file to be used for image creation")
65     parser_create.add_argument('-o', "--outdir", dest="outdir", action="store", help="image output directory", default=os.getcwd())
66     
67     parser_start = subparsers.add_parser('start', help='start the tic-core demon on system.')
68     parser_start.add_argument('-p', "--port", dest="port", action="store", help="port number")
69     
70     return parser
71
72 def main(argv):
73     logger = logging.getLogger('tic')
74     try:
75         # Setup argument parser
76         parser = create_parser()
77         # Process arguments
78         args = parser.parse_args(argv[1:])
79         
80         if args.subparser_name == 'analyze':
81             view_data = command.analyze(args.repos, args.recipes)
82             output_dir=os.path.abspath(os.path.expanduser(args.outdir))
83             file.write(os.path.join(output_dir, 'viewdata.json'), json.dumps(view_data))
84         elif args.subparser_name == 'export':
85             #TODO Temporary code(should be deleted)
86             recipes={'name':'default'}
87             packages=['attr', 'filesystem']
88             output=command.exports(args.format, recipes, packages, args.outdir)
89             logger.info("export the %s file: %s", args.format, output)
90         elif args.subparser_name == 'createimage':
91             if args.recipes or args.kickstart:
92                 command.createimage(args.recipes, args.kickstart, args.outdir)
93             else:
94                 logger.info('kickstart or recipes file is required')
95         elif args.subparser_name == 'start':
96             if not args.port:
97                 args.port = configmgr.server['port']
98             tic_server.start(args.port)
99         return 0
100     except KeyboardInterrupt:
101         ### handle keyboard interrupt ###
102         return 0
103     except error.TICError as err:
104         logger.error(err)
105     except Exception as ex:
106         logger.error(ex)
107         return 2
108     
109 if __name__ == "__main__":
110     log.setup('tic')
111     sys.exit(main(sys.argv))
112
113