[TIC-CORE] Initial Import
[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 from tic import command
28 from tic.utils import file
29 from tic.utils import error
30 from tic.server import tic_server
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('analysis', help='analyze install-dependency of packages')
50     parser_analyze.add_argument('-r', "--repo", dest="repos", metavar="urls", nargs='+', help="The URL of repository [default: %(default)s]", required=True)
51     parser_analyze.add_argument('-o', "--output", dest="output", action="store", help="The result file is distributed in the output path", default=os.getcwd())
52     
53     parser_download = subparsers.add_parser('download', help='download repodata from repositories')
54     parser_download.add_argument('-r', "--repo", dest="repos", metavar="urls", nargs='+', help="The URL of repository [default: %(default)s]", required=True)
55     parser_download.add_argument('-o', "--output", dest="output", action="store", help="The result file is distributed in the output path [default: %(default)s]")
56     
57     parser_start = subparsers.add_parser('start', help='start the tic-core demon on system. port 59001 is used by default ')
58     parser_start.add_argument('-p', "--port", dest="port", action="store", help="port number", default=59001)
59     
60     return parser
61
62 def main(argv):
63     try:
64         # Setup argument parser
65         parser = create_parser()
66         # Process arguments
67         args = parser.parse_args(argv[1:])
68         
69         if args.subparser_name == 'analysis':
70             view_data = command.analyze(args.repos, None)
71             file.write(args.output, json.dumps(view_data))
72         elif args.subparser_name == 'download':
73             pass
74         elif args.subparser_name == 'start':
75             tic_server.start(args.port)
76         else:
77             print('Nothing')
78         return 0
79     
80     except KeyboardInterrupt:
81         ### handle keyboard interrupt ###
82         return 0
83     except error.TICError as err:
84         print(err)
85     except Exception as e:
86         print(e)
87         return 2
88     
89 if __name__ == "__main__":
90     sys.exit(main(sys.argv))
91
92