Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016 Comcast Cable Communications Management, LLC. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # Looks for preprocessor macros with struct initializers and siphons them |
| 17 | # off into another file for later parsing; ostensibly to generate |
| 18 | # documentation from struct initializer data. |
| 19 | |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 20 | import argparse |
| 21 | import logging |
| 22 | import os |
| 23 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 24 | import siphon |
| 25 | |
| 26 | DEFAULT_LOGFILE = None |
Nathan Skrzypczak | 1e167a4 | 2022-03-25 12:06:51 +0100 | [diff] [blame] | 27 | DEFAULT_LOGLEVEL = "warning" |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 28 | DEFAULT_OUTPUT = "build-root/docs/siphons" |
| 29 | DEFAULT_PREFIX = os.getcwd() |
| 30 | |
| 31 | ap = argparse.ArgumentParser() |
| 32 | ap.add_argument("--log-file", default=DEFAULT_LOGFILE, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 33 | help="Log file [%s]" % DEFAULT_LOGFILE) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 34 | ap.add_argument("--log-level", default=DEFAULT_LOGLEVEL, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 35 | choices=["debug", "info", "warning", "error", "critical"], |
| 36 | help="Logging level [%s]" % DEFAULT_LOGLEVEL) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 37 | |
| 38 | ap.add_argument("--output", '-o', metavar="directory", default=DEFAULT_OUTPUT, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 39 | help="Output directory for .siphon files [%s]" % |
| 40 | DEFAULT_OUTPUT) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 41 | ap.add_argument("--input-prefix", metavar="path", default=DEFAULT_PREFIX, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 42 | help="Prefix to strip from input pathnames [%s]" % |
| 43 | DEFAULT_PREFIX) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 44 | ap.add_argument("input", nargs='+', metavar="input_file", |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 45 | help="Input C source files") |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 46 | args = ap.parse_args() |
| 47 | |
| 48 | logging.basicConfig(filename=args.log_file, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 49 | level=getattr(logging, args.log_level.upper(), None)) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 50 | log = logging.getLogger("siphon_generate") |
| 51 | |
| 52 | |
| 53 | generate = siphon.generate.Generate(output_directory=args.output, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 54 | input_prefix=args.input_prefix) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 55 | |
| 56 | # Pre-process file names in case they indicate a file with |
| 57 | # a list of files |
| 58 | files = [] |
| 59 | for filename in args.input: |
| 60 | if filename.startswith('@'): |
| 61 | with open(filename[1:], 'r') as fp: |
| 62 | lines = fp.readlines() |
| 63 | for line in lines: |
| 64 | file = line.strip() |
| 65 | if file not in files: |
| 66 | files.append(file) |
| 67 | lines = None |
| 68 | else: |
| 69 | if filename not in files: |
| 70 | files.append(filename) |
| 71 | |
| 72 | # Iterate all the input files we've been given |
| 73 | for filename in files: |
| 74 | generate.parse(filename) |
| 75 | |
| 76 | # Write the extracted data |
| 77 | generate.deliver() |
| 78 | |
| 79 | # All done |