blob: ffe36e7f969a2178075dca401b15d0ba411646b1 [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')
Matteo Scandolo64514092019-02-20 10:47:48 -080020REPO_DIR = os.path.abspath(CWD + "/../../../")
Matteo Scandolo431781c2017-09-06 15:33:07 -070021
22class Args:
23 pass
24
25def generate_swagger_docs(xproto):
26
27 # if not os.path.isfile(xproto):
28 # print "ERROR: Couldn't find xproto file for %s at: %s" % (service, xproto)
29 # return
30
31 print "Generating swagger docs for %s" % (xproto)
Scott Bakerde2f7ff2018-12-19 16:00:09 -080032 args = XOSProcessorArgs()
Matteo Scandolo431781c2017-09-06 15:33:07 -070033 args.files = xproto
34 args.target = 'swagger.xtarget'
35 args.output = SWAGGER_DOCS_DIR
36 args.write_to_file = "single"
37 args.dest_file = "swagger.yaml"
38 args.quiet = False
39 try:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080040 XOSProcessor.process(args)
Matteo Scandolo431781c2017-09-06 15:33:07 -070041 except Exception, e:
42 print "ERROR: Couldn't generate swagger specs"
43 print e
44
Matteo Scandolob8a4b122018-01-19 13:36:22 -080045def get_xproto_recursively(root):
46 files = []
47 items = os.listdir(root)
48 # iterate over the content of the folder excluding hidden items
49 for item in [i for i in items if i[0] is not "."]:
50 item_abs_path = os.path.abspath(root + "/" + item)
51 if os.path.isdir(item_abs_path):
52 files = files + get_xproto_recursively(item_abs_path)
Matteo Scandolo64514092019-02-20 10:47:48 -080053 elif os.path.isfile(item_abs_path) and ".xproto" in item_abs_path:
Matteo Scandolob8a4b122018-01-19 13:36:22 -080054 files.append(item_abs_path)
55
56 return [f for f in files if "xproto" in f]
57
58
Matteo Scandolo431781c2017-09-06 15:33:07 -070059def main():
60
Matteo Scandolo64514092019-02-20 10:47:48 -080061 protos = get_xproto_recursively(REPO_DIR)
Matteo Scandolo431781c2017-09-06 15:33:07 -070062
Matteo Scandolo64514092019-02-20 10:47:48 -080063 generate_swagger_docs(protos)
Matteo Scandolo431781c2017-09-06 15:33:07 -070064
65
66if __name__ == '__main__':
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080067 main()