File: main.py

package info (click to toggle)
libvirt 11.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 209,020 kB
  • sloc: ansic: 535,831; xml: 321,783; python: 11,974; perl: 2,626; sh: 2,185; makefile: 448; javascript: 126; cpp: 22
file content (88 lines) | stat: -rwxr-xr-x 2,824 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import os
import sys

from rpcgen.parser import XDRParser
from rpcgen.generator import (
    XDRTypeDeclarationGenerator,
    XDRTypeImplementationGenerator,
    XDRMarshallDeclarationGenerator,
    XDRMarshallImplementationGenerator,
)


def parse_cli():
    parser = argparse.ArgumentParser("RPC code generator")
    parser.add_argument(
        "-m",
        "--mode",
        choices=["header", "source", "repr"],
        help="Output generation mode",
    )
    parser.add_argument(
        "-r", "--header", default=[], action="append", help="Extra headers to include"
    )
    parser.add_argument("input", default="-", nargs="?", help="XDR input protocol file")
    parser.add_argument("output", default="-", nargs="?", help="Generated output file")

    return parser.parse_args()


def main():
    args = parse_cli()

    infp = sys.stdin
    outfp = sys.stdout
    if args.input != "-":
        infp = open(args.input, "r")
    if args.output != "-":
        # the old genprotocol.pl wrapper would make the
        # output files mode 0444, which will prevent us
        # from writing directly do them. Explicitly
        # unlinking first gets rid of any old possibly
        # read-only copy
        #
        # We can delete this in a few years, once we
        # know users won't have a previously generated
        # readonly copy lieing around.
        try:
            os.unlink(args.output)
        except Exception:
            pass
        outfp = open(args.output, "w")

    parser = XDRParser(infp)
    spec = parser.parse()

    if args.mode == "header":
        print("/* This file is auto-generated from %s */\n" % args.input, file=outfp)
        print("#include <rpc/rpc.h>", file=outfp)
        print('#include "internal.h"', file=outfp)
        for h in args.header:
            print('#include "%s"' % h, file=outfp)
        print("", file=outfp)
        print("#pragma once\n", file=outfp)
        generator = XDRTypeDeclarationGenerator(spec)
        print(generator.visit(), file=outfp)
        generator = XDRMarshallDeclarationGenerator(spec)
        print(generator.visit(), file=outfp)
    elif args.mode == "source":
        print("/* This file is auto-generated from %s */\n" % args.input, file=outfp)
        print("#include <config.h>", file=outfp)
        for h in args.header:
            print('#include "%s"' % h, file=outfp)
        print("", file=outfp)
        generator = XDRTypeImplementationGenerator(spec)
        print(generator.visit(), file=outfp)
        generator = XDRMarshallImplementationGenerator(spec)
        print(generator.visit(), file=outfp)
    elif args.mode == "repr":
        print(spec, file=outfp)
    else:
        pass  # Just validates XDR input syntax


main()