blob: f22d1c2e81001593317621e9c5ef06e479be95c6 [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
Zack Williamsd6fc7502020-02-24 16:12:59 -070015from __future__ import print_function
16
Matteo Scandolo431781c2017-09-06 15:33:07 -070017import os
Zack Williams67142162019-03-06 14:01:32 -070018import traceback
Scott Bakerde2f7ff2018-12-19 16:00:09 -080019from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo431781c2017-09-06 15:33:07 -070020
21CWD = OUTPUT_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
22SWAGGER_DOCS_DIR = os.path.abspath(CWD + '/../swagger/specs')
Matteo Scandolo64514092019-02-20 10:47:48 -080023REPO_DIR = os.path.abspath(CWD + "/../../../")
Matteo Scandolo431781c2017-09-06 15:33:07 -070024
25class Args:
26 pass
27
Zack Williamsd6fc7502020-02-24 16:12:59 -070028
Matteo Scandolo431781c2017-09-06 15:33:07 -070029def 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
Zack Williamsd6fc7502020-02-24 16:12:59 -070035 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)
Zack Williams67142162019-03-06 14:01:32 -070045 except Exception:
Zack Williamsd6fc7502020-02-24 16:12:59 -070046 print("ERROR: Couldn't generate swagger specs")
Zack Williams67142162019-03-06 14:01:32 -070047 traceback.print_exc()
Matteo Scandolo431781c2017-09-06 15:33:07 -070048
Zack Williamsd6fc7502020-02-24 16:12:59 -070049
Matteo Scandolob8a4b122018-01-19 13:36:22 -080050def get_xproto_recursively(root):
51 files = []
52 items = os.listdir(root)
53 # iterate over the content of the folder excluding hidden items
54 for item in [i for i in items if i[0] is not "."]:
Scott Baker73cb2c62019-06-04 15:40:16 -070055 if ("venv" in item) or ("virtualenv" in item):
56 # avoid recursing through virtual env directories
57 continue
58 if "xos-genx-tests" in item:
59 # don't generate docs for xosgenx's unit tests
60 continue
Scott Baker0b046062019-06-04 18:01:18 -070061 if "xos-tosca" in item:
62 # don't generate docs for xos-tosca's unit tests
63 continue
Matteo Scandolob8a4b122018-01-19 13:36:22 -080064 item_abs_path = os.path.abspath(root + "/" + item)
65 if os.path.isdir(item_abs_path):
66 files = files + get_xproto_recursively(item_abs_path)
Matteo Scandolo64514092019-02-20 10:47:48 -080067 elif os.path.isfile(item_abs_path) and ".xproto" in item_abs_path:
Matteo Scandolob8a4b122018-01-19 13:36:22 -080068 files.append(item_abs_path)
69
Zack Williamsd6fc7502020-02-24 16:12:59 -070070 protos = sorted([f for f in files if "xproto" in f])
Scott Bakerf7555322019-06-05 10:03:03 -070071
72 # remove the core xproto...
73 core_proto = None
74 for proto in protos[:]:
75 if "core.xproto" in proto:
76 protos.remove(proto)
77 core_proto = proto
78
79 # ... and put it at the front of the list
80 if core_proto:
81 protos = [core_proto] + protos
82
83 return protos
Matteo Scandolob8a4b122018-01-19 13:36:22 -080084
85
Matteo Scandolo431781c2017-09-06 15:33:07 -070086if __name__ == '__main__':
Zack Williamsd6fc7502020-02-24 16:12:59 -070087 protos = get_xproto_recursively(REPO_DIR)
88 generate_swagger_docs(protos)