blob: 4e80984e7b2daa264ff982568ce4ebfab9fdad55 [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
16from xosgenx.generator import XOSGenerator
17
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")
22
23XOS_XPROTO = os.path.abspath(CWD + "/../../xos/core/models/core.xproto")
24
25class Args:
26 pass
27
28def generate_swagger_docs(xproto):
29
30 # if not os.path.isfile(xproto):
31 # print "ERROR: Couldn't find xproto file for %s at: %s" % (service, xproto)
32 # return
33
34 print "Generating swagger docs for %s" % (xproto)
35 args = Args()
36 args.files = xproto
37 args.target = 'swagger.xtarget'
38 args.output = SWAGGER_DOCS_DIR
39 args.write_to_file = "single"
40 args.dest_file = "swagger.yaml"
41 args.quiet = False
42 try:
43 XOSGenerator.generate(args)
44 except Exception, e:
45 print "ERROR: Couldn't generate swagger specs"
46 print e
47
Matteo Scandolob8a4b122018-01-19 13:36:22 -080048def get_xproto_recursively(root):
49 files = []
50 items = os.listdir(root)
51 # iterate over the content of the folder excluding hidden items
52 for item in [i for i in items if i[0] is not "."]:
53 item_abs_path = os.path.abspath(root + "/" + item)
54 if os.path.isdir(item_abs_path):
55 files = files + get_xproto_recursively(item_abs_path)
56 elif os.path.isfile(item_abs_path):
57 files.append(item_abs_path)
58
59 return [f for f in files if "xproto" in f]
60
61
Matteo Scandolo431781c2017-09-06 15:33:07 -070062def main():
63
Matteo Scandolo431781c2017-09-06 15:33:07 -070064 protos = [XOS_XPROTO]
65
Matteo Scandolob8a4b122018-01-19 13:36:22 -080066 service_protos = get_xproto_recursively(SERVICE_DIR)
67
68 generate_swagger_docs(protos + service_protos)
Matteo Scandolo431781c2017-09-06 15:33:07 -070069
70
71if __name__ == '__main__':
72 main()