Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 1 | # 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 Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 17 | import os |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame] | 18 | import traceback |
Scott Baker | de2f7ff | 2018-12-19 16:00:09 -0800 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 20 | |
| 21 | CWD = OUTPUT_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 22 | SWAGGER_DOCS_DIR = os.path.abspath(CWD + '/../swagger/specs') |
Matteo Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 23 | REPO_DIR = os.path.abspath(CWD + "/../../../") |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 24 | |
| 25 | class Args: |
| 26 | pass |
| 27 | |
Zack Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 28 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 29 | def 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 Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 35 | print("Generating swagger docs for %s" % (xproto)) |
Scott Baker | de2f7ff | 2018-12-19 16:00:09 -0800 | [diff] [blame] | 36 | args = XOSProcessorArgs() |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 37 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 44 | XOSProcessor.process(args) |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame] | 45 | except Exception: |
Zack Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 46 | print("ERROR: Couldn't generate swagger specs") |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame] | 47 | traceback.print_exc() |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 48 | |
Zack Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 49 | |
Matteo Scandolo | b8a4b12 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 50 | def 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 Baker | 73cb2c6 | 2019-06-04 15:40:16 -0700 | [diff] [blame] | 55 | 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 Baker | 0b04606 | 2019-06-04 18:01:18 -0700 | [diff] [blame] | 61 | if "xos-tosca" in item: |
| 62 | # don't generate docs for xos-tosca's unit tests |
| 63 | continue |
Matteo Scandolo | b8a4b12 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 64 | 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 Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 67 | elif os.path.isfile(item_abs_path) and ".xproto" in item_abs_path: |
Matteo Scandolo | b8a4b12 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 68 | files.append(item_abs_path) |
| 69 | |
Zack Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 70 | protos = sorted([f for f in files if "xproto" in f]) |
Scott Baker | f755532 | 2019-06-05 10:03:03 -0700 | [diff] [blame] | 71 | |
| 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 Scandolo | b8a4b12 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 84 | |
| 85 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 86 | if __name__ == '__main__': |
Zack Williams | d6fc750 | 2020-02-24 16:12:59 -0700 | [diff] [blame] | 87 | protos = get_xproto_recursively(REPO_DIR) |
| 88 | generate_swagger_docs(protos) |