1 | #!/usr/bin/env python |
---|
2 | # SPDX-License-Identifier: BSD-2-Clause |
---|
3 | """ Generates files of the modules from the specification. """ |
---|
4 | |
---|
5 | # Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de) |
---|
6 | # |
---|
7 | # Redistribution and use in source and binary forms, with or without |
---|
8 | # modification, are permitted provided that the following conditions |
---|
9 | # are met: |
---|
10 | # 1. Redistributions of source code must retain the above copyright |
---|
11 | # notice, this list of conditions and the following disclaimer. |
---|
12 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | # notice, this list of conditions and the following disclaimer in the |
---|
14 | # documentation and/or other materials provided with the distribution. |
---|
15 | # |
---|
16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | # POSSIBILITY OF SUCH DAMAGE. |
---|
27 | |
---|
28 | import argparse |
---|
29 | import difflib |
---|
30 | import sys |
---|
31 | |
---|
32 | import rtemsspec |
---|
33 | |
---|
34 | |
---|
35 | def _diff(obj: rtemsspec.content.Content, path: str) -> None: |
---|
36 | from_file = f"a/{path}" |
---|
37 | to_file = f"b/{path}" |
---|
38 | try: |
---|
39 | file_lines = open(path).read().splitlines() |
---|
40 | except FileNotFoundError: |
---|
41 | file_lines = [] |
---|
42 | diff_lines = list( |
---|
43 | difflib.unified_diff(file_lines, |
---|
44 | str(obj).splitlines(), |
---|
45 | fromfile=from_file, |
---|
46 | tofile=to_file, |
---|
47 | n=3, |
---|
48 | lineterm="")) |
---|
49 | if diff_lines: |
---|
50 | print(f"diff -u {from_file} {to_file}") |
---|
51 | print("\n".join(diff_lines)) |
---|
52 | |
---|
53 | |
---|
54 | def main() -> None: |
---|
55 | """ Generates files of the modules from the specification. """ |
---|
56 | parser = argparse.ArgumentParser() |
---|
57 | parser.add_argument("-u", |
---|
58 | "--diff", |
---|
59 | action="store_true", |
---|
60 | help="print the unified difference from the original" |
---|
61 | " file content to the new generated content") |
---|
62 | parser.add_argument("targets", |
---|
63 | metavar="TARGET", |
---|
64 | nargs="*", |
---|
65 | help="a target file of a specification item") |
---|
66 | args = parser.parse_args(sys.argv[1:]) |
---|
67 | if args.diff: |
---|
68 | rtemsspec.content.Content.write = _diff # type: ignore |
---|
69 | config = rtemsspec.util.load_config("config.yml") |
---|
70 | item_cache = rtemsspec.items.ItemCache(config["spec"]) |
---|
71 | rtemsspec.validation.generate(config["validation"], item_cache, |
---|
72 | args.targets) |
---|
73 | |
---|
74 | if not args.targets: |
---|
75 | rtemsspec.interface.generate(config["interface"], item_cache) |
---|
76 | rtemsspec.applconfig.generate(config["appl-config"], item_cache) |
---|
77 | rtemsspec.specdoc.document(config["spec-documentation"], item_cache) |
---|
78 | rtemsspec.glossary.generate(config["glossary"], item_cache) |
---|
79 | rtemsspec.interfacedoc.generate(config["interface-documentation"], |
---|
80 | item_cache) |
---|
81 | |
---|
82 | |
---|
83 | if __name__ == "__main__": |
---|
84 | main() |
---|