blob: 7a01bc7919ba583a685371531504cfb09fae0bac [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
Zack Williams67142162019-03-06 14:01:32 -070016import traceback
Scott Bakerde2f7ff2018-12-19 16:00:09 -080017from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo431781c2017-09-06 15:33:07 -070018
19CWD = OUTPUT_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
20SWAGGER_DOCS_DIR = os.path.abspath(CWD + '/../swagger/specs')
Matteo Scandolo64514092019-02-20 10:47:48 -080021REPO_DIR = os.path.abspath(CWD + "/../../../")
Matteo Scandolo431781c2017-09-06 15:33:07 -070022
23class Args:
24 pass
25
26def generate_swagger_docs(xproto):
27
28 # if not os.path.isfile(xproto):
29 # print "ERROR: Couldn't find xproto file for %s at: %s" % (service, xproto)
30 # return
31
32 print "Generating swagger docs for %s" % (xproto)
Scott Bakerde2f7ff2018-12-19 16:00:09 -080033 args = XOSProcessorArgs()
Matteo Scandolo431781c2017-09-06 15:33:07 -070034 args.files = xproto
35 args.target = 'swagger.xtarget'
36 args.output = SWAGGER_DOCS_DIR
37 args.write_to_file = "single"
38 args.dest_file = "swagger.yaml"
39 args.quiet = False
40 try:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080041 XOSProcessor.process(args)
Zack Williams67142162019-03-06 14:01:32 -070042 except Exception:
Matteo Scandolo431781c2017-09-06 15:33:07 -070043 print "ERROR: Couldn't generate swagger specs"
Zack Williams67142162019-03-06 14:01:32 -070044 traceback.print_exc()
Matteo Scandolo431781c2017-09-06 15:33:07 -070045
Matteo Scandolob8a4b122018-01-19 13:36:22 -080046def get_xproto_recursively(root):
47 files = []
48 items = os.listdir(root)
49 # iterate over the content of the folder excluding hidden items
50 for item in [i for i in items if i[0] is not "."]:
Scott Baker73cb2c62019-06-04 15:40:16 -070051 if ("venv" in item) or ("virtualenv" in item):
52 # avoid recursing through virtual env directories
53 continue
54 if "xos-genx-tests" in item:
55 # don't generate docs for xosgenx's unit tests
56 continue
Scott Baker0b046062019-06-04 18:01:18 -070057 if "xos-tosca" in item:
58 # don't generate docs for xos-tosca's unit tests
59 continue
Matteo Scandolob8a4b122018-01-19 13:36:22 -080060 item_abs_path = os.path.abspath(root + "/" + item)
61 if os.path.isdir(item_abs_path):
62 files = files + get_xproto_recursively(item_abs_path)
Matteo Scandolo64514092019-02-20 10:47:48 -080063 elif os.path.isfile(item_abs_path) and ".xproto" in item_abs_path:
Matteo Scandolob8a4b122018-01-19 13:36:22 -080064 files.append(item_abs_path)
65
Scott Bakerf7555322019-06-05 10:03:03 -070066 protos = [f for f in files if "xproto" in f]
67
68 protos = sorted(protos)
69
70 # remove the core xproto...
71 core_proto = None
72 for proto in protos[:]:
73 if "core.xproto" in proto:
74 protos.remove(proto)
75 core_proto = proto
76
77 # ... and put it at the front of the list
78 if core_proto:
79 protos = [core_proto] + protos
80
81 return protos
Matteo Scandolob8a4b122018-01-19 13:36:22 -080082
83
Matteo Scandolo431781c2017-09-06 15:33:07 -070084def main():
85
Matteo Scandolo64514092019-02-20 10:47:48 -080086 protos = get_xproto_recursively(REPO_DIR)
Matteo Scandolo431781c2017-09-06 15:33:07 -070087
Matteo Scandolo64514092019-02-20 10:47:48 -080088 generate_swagger_docs(protos)
Matteo Scandolo431781c2017-09-06 15:33:07 -070089
90
91if __name__ == '__main__':
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080092 main()