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 | |
| 15 | import os |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame^] | 16 | import traceback |
Scott Baker | de2f7ff | 2018-12-19 16:00:09 -0800 | [diff] [blame] | 17 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 18 | |
| 19 | CWD = OUTPUT_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 20 | SWAGGER_DOCS_DIR = os.path.abspath(CWD + '/../swagger/specs') |
Matteo Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 21 | REPO_DIR = os.path.abspath(CWD + "/../../../") |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 22 | |
| 23 | class Args: |
| 24 | pass |
| 25 | |
| 26 | def 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 Baker | de2f7ff | 2018-12-19 16:00:09 -0800 | [diff] [blame] | 33 | args = XOSProcessorArgs() |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 34 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 41 | XOSProcessor.process(args) |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame^] | 42 | except Exception: |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 43 | print "ERROR: Couldn't generate swagger specs" |
Zack Williams | 6714216 | 2019-03-06 14:01:32 -0700 | [diff] [blame^] | 44 | traceback.print_exc() |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 45 | |
Matteo Scandolo | b8a4b12 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 46 | def 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 "."]: |
| 51 | item_abs_path = os.path.abspath(root + "/" + item) |
| 52 | if os.path.isdir(item_abs_path): |
| 53 | files = files + get_xproto_recursively(item_abs_path) |
Matteo Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 54 | 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] | 55 | files.append(item_abs_path) |
| 56 | |
| 57 | return [f for f in files if "xproto" in f] |
| 58 | |
| 59 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 60 | def main(): |
| 61 | |
Matteo Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 62 | protos = get_xproto_recursively(REPO_DIR) |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 63 | |
Matteo Scandolo | 6451409 | 2019-02-20 10:47:48 -0800 | [diff] [blame] | 64 | generate_swagger_docs(protos) |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 68 | main() |