blob: 77c74bfb26e92c252dee3e0cdb35e72d400ba094 [file] [log] [blame]
Matteo Scandolo431781c2017-09-06 15:33:07 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
Scott Bakerde2f7ff2018-12-19 16:00:09 -080016from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo431781c2017-09-06 15:33:07 -070017
18CWD = OUTPUT_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
19SWAGGER_DOCS_DIR = os.path.abspath(CWD + '/../swagger/specs')
20ORCHESTRATION_DIR = os.path.abspath(CWD + "/../../../")
21SERVICE_DIR = os.path.abspath(ORCHESTRATION_DIR + "/xos_services")
Matteo Scandolof2713372018-02-21 09:38:27 -080022PROFILE_DIR = os.path.abspath(ORCHESTRATION_DIR + "/profiles")
Matteo Scandolo431781c2017-09-06 15:33:07 -070023
24XOS_XPROTO = os.path.abspath(CWD + "/../../xos/core/models/core.xproto")
25
26class Args:
27 pass
28
29def generate_swagger_docs(xproto):
30
31 # if not os.path.isfile(xproto):
32 # print "ERROR: Couldn't find xproto file for %s at: %s" % (service, xproto)
33 # return
34
35 print "Generating swagger docs for %s" % (xproto)
Scott Bakerde2f7ff2018-12-19 16:00:09 -080036 args = XOSProcessorArgs()
Matteo Scandolo431781c2017-09-06 15:33:07 -070037 args.files = xproto
38 args.target = 'swagger.xtarget'
39 args.output = SWAGGER_DOCS_DIR
40 args.write_to_file = "single"
41 args.dest_file = "swagger.yaml"
42 args.quiet = False
43 try:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080044 XOSProcessor.process(args)
Matteo Scandolo431781c2017-09-06 15:33:07 -070045 except Exception, e:
46 print "ERROR: Couldn't generate swagger specs"
47 print e
48
Matteo Scandolob8a4b122018-01-19 13:36:22 -080049def get_xproto_recursively(root):
50 files = []
51 items = os.listdir(root)
52 # iterate over the content of the folder excluding hidden items
53 for item in [i for i in items if i[0] is not "."]:
54 item_abs_path = os.path.abspath(root + "/" + item)
55 if os.path.isdir(item_abs_path):
56 files = files + get_xproto_recursively(item_abs_path)
57 elif os.path.isfile(item_abs_path):
58 files.append(item_abs_path)
59
60 return [f for f in files if "xproto" in f]
61
62
Matteo Scandolo431781c2017-09-06 15:33:07 -070063def main():
64
Matteo Scandolo431781c2017-09-06 15:33:07 -070065 protos = [XOS_XPROTO]
66
Matteo Scandolob8a4b122018-01-19 13:36:22 -080067 service_protos = get_xproto_recursively(SERVICE_DIR)
68
Matteo Scandolof2713372018-02-21 09:38:27 -080069 profile_protos = get_xproto_recursively(PROFILE_DIR)
70
71 generate_swagger_docs(protos + service_protos + profile_protos)
Matteo Scandolo431781c2017-09-06 15:33:07 -070072
73
74if __name__ == '__main__':
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080075 main()