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