2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
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
8 # http://floralicense.org/license/
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.
20 from flask import Flask
21 from flask import Response
22 from flask.globals import request
23 from flask.helpers import url_for
27 from tic import command
28 from tic.utils import error
35 <title>TIC-Core web server</title>
36 <h1> TIC-Core web server is working<h1>
39 @app.route('/analysis', methods=['POST'])
42 logger = logging.getLogger(__name__)
43 logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data))
44 target_info = json.loads(request.data)
45 view_data = command.analyze(target_info.get('recipes'))
46 #view_data = command.analyze(None)
47 resp = makeresponse(view_data, None)
48 except error.TICError as err:
50 resp = makeresponse(str(err), err)
51 except ValueError as ve:
53 resp = makeresponse(str(ve), ve)
54 except Exception as ex:
56 resp = makeresponse(str(ex), ex)
60 @app.route('/imports', methods=['POST'])
63 logger = logging.getLogger(__name__)
64 logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data))
65 repo_info = json.loads(request.data)
66 view_data = command.imports(repo_info.get('recipes'))
67 resp = makeresponse(view_data, None)
68 except error.TICError as err:
70 resp = makeresponse(str(err), err)
71 except Exception as ex:
73 resp = makeresponse(str(ex), ex)
77 @app.route('/exports', methods=['POST'])
80 logger = logging.getLogger(__name__)
81 logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data))
82 exportInfo = json.loads(request.data)
83 export_type = request.args.get('format')
84 output = command.exports(export_type, exportInfo.get('recipes'), exportInfo.get('packages'), exportInfo.get('outdir'), exportInfo.get('filename'))
85 resp = makeresponse(output, None)
86 except error.TICError as err:
88 resp = makeresponse(str(err), err)
89 except ValueError as ve:
91 resp = makeresponse(str(ve), ve)
92 except Exception as ex:
94 resp = makeresponse(str(ex), ex)
98 def start(port_num=8082):
99 # cryptographic random generator
100 app.secret_key = os.urandom(24)
102 with app.test_request_context():
103 print(url_for('index'))
104 print(url_for('analysis'))
105 print(url_for('imports'))
106 print(url_for('exports'))
107 if isinstance(port_num, (str, unicode)):
108 port_num = int(port_num)
109 app.run(host='0.0.0.0', threaded=True, port=port_num)
111 def makeresponse(data, err):
114 if isinstance(err, error.TICError) or isinstance(err, ValueError):
115 status = 400 # Bad Request
116 elif isinstance(err, Exception):
117 status = 500 # Internal Server Error
118 data='Internal Server Error'
119 res_body = json.dumps(ResultInfo('false', None, data)._asdict())
121 res_body = json.dumps(ResultInfo('true', data, None)._asdict())
123 resp = Response(status=status,
124 mimetype='application/json',
128 ResultInfoType = collections.namedtuple('ResultInfo', 'result, data, message')
129 def ResultInfo(result='false', data=None, message=None):
130 return ResultInfoType(result, data, message)
132 if __name__ == '__main__':