acfd6d0b4e9ca25997492cfbf0b87293ebd1d613
[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 foramt", required=True)
58     parser_export.add_argument('-c', "--recipes", dest="recipes", metavar="paths", nargs='+', help="The path of recipe")
59     parser_export.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the outdir path", default=os.getcwd())
60     # create the parser for the 'create' command
61     parser_create = subparsers.add_parser('create', help='create an image for tizen')
62     parser_create.add_argument('-r', "--input", dest="input", metavar="data", nargs='+', help="Input data files (kickstart/recipes)", required=True)
63     parser_create.add_argument('-o', "--outdir", dest="outdir", action="store", help="The result file is distributed in the output path", default=os.getcwd())
64     
65     parser_start = subparsers.add_parser('start', help='start the tic-core demon on system. port 59001 is used by default ')
66     parser_start.add_argument('-p', "--port", dest="port", action="store", help="port number", default=59001)
67     
68     return parser
69
70 def main(argv):
71     logger = logging.getLogger('tic')
72     try:
73         # Setup argument parser
74         parser = create_parser()
75         # Process arguments
76         args = parser.parse_args(argv[1:])
77         
78         if args.subparser_name == 'analyze':
79             view_data = command.analyze(args.repos, args.recipes)
80             output_dir=os.path.abspath(os.path.expanduser(args.outdir))
81             file.write(os.path.join(output_dir, 'viewdata.json'), json.dumps(view_data))
82         elif args.subparser_name == 'export':
83             output=command.exports(args.format, args.recipes, None, args.outdir)
84             logger.info("export the %s file: %s", args.format, output)
85         elif args.subparser_name == 'create':
86             logger.info('create command is yet available')
87             pass
88         elif args.subparser_name == 'start':
89             tic_server.start(args.port)
90         return 0
91     except KeyboardInterrupt:
92         ### handle keyboard interrupt ###
93         return 0
94     except error.TICError as err:
95         logger.error(err)
96     except Exception as ex:
97         logger.error(ex)
98         return 2
99     
100 if __name__ == "__main__":
101     log.setup('tic')
102     sys.exit(main(sys.argv))
103
104