Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import os
2import sys
3import subprocess
4import tempfile
5import importlib
6import click
7import yaml
8from logging import getLogger, basicConfig, DEBUG
10import lotrpc.sunrpc.parse
11import lotrpc.sunrpc.rpcgen
14log = getLogger(__name__)
17@click.group(invoke_without_command=True)
18@click.pass_context
19def cli(ctx):
20 if ctx.invoked_subcommand is None:
21 print(ctx.get_help())
24@cli.command()
25@click.argument("file", type=click.File('r'))
26@click.option("--verbose/--no-verbose", default=False)
27def lex(file, verbose):
28 if verbose:
29 basicConfig(level=DEBUG)
30 for token in lotrpc.sunrpc.parse.get_lexer(file):
31 log.info("token %s", token)
34def parse_file(file, cpp, defs, verbose):
35 defs = yaml.load(defs, Loader=yaml.FullLoader)
36 if cpp is not None:
37 with subprocess.Popen(["cpp"], stdin=subprocess.PIPE,
38 stdout=subprocess.PIPE) as p:
39 p.stdin.write(file.read().encode('utf-8'))
40 p.stdin.close()
41 file = p.stdout
42 return lotrpc.sunrpc.parse.parse_file(
43 p.stdout, debug=verbose, defines=defs)
45 return lotrpc.sunrpc.parse.parse_file(file, debug=verbose, defines=defs)
48defdef = "{LM_MAXSTRLEN: 1024, MAXNAMELEN: 1025, MAXNETNAMELEN: 255}"
51@cli.command()
52@click.option("--cpp/--no-cpp", default=False)
53@click.option("--defs", default=defdef)
54@click.option("--verbose/--no-verbose", default=False)
55@click.argument("file", type=click.File('r'))
56def parse(file, cpp, defs, verbose):
57 if verbose:
58 basicConfig(level=DEBUG)
59 result = parse_file(file, cpp, defs, verbose)
60 print(yaml.dump(result))
63@cli.command()
64@click.option("--cpp/--no-cpp", default=False)
65@click.option("--defs", default=defdef)
66@click.option("--template", default=None, type=click.File('r'))
67@click.option("--verbose/--no-verbose", default=False)
68@click.argument("file", type=click.File('r'))
69def rpcgen(file, cpp, defs, template, verbose):
70 if verbose:
71 basicConfig(level=DEBUG)
72 data = parse_file(file, cpp, defs, verbose)
73 tmpl = None
74 if template is not None:
75 tmpl = template.read()
76 res = lotrpc.sunrpc.rpcgen.generate_proto(data, tmpl)
77 print(res)
80@cli.command()
81@click.option("--cpp/--no-cpp", default=False)
82@click.option("--defs", default=defdef)
83@click.option("--template", default=None, type=click.File('r'))
84@click.option("--verbose/--no-verbose", default=False)
85@click.argument("file", type=click.File('r'))
86def rpcgen_autopep(file, cpp, defs, template, verbose):
87 if verbose:
88 basicConfig(level=DEBUG)
89 data = parse_file(file, cpp, defs, verbose)
90 tmpl = None
91 if template is not None:
92 tmpl = template.read()
93 res = lotrpc.sunrpc.rpcgen.generate_proto(data, tmpl)
94 with tempfile.NamedTemporaryFile(suffix=".py", mode="w") as tf:
95 tf.write(res)
96 with subprocess.Popen(["autopep8", "--diff", tf.name], stdin=subprocess.DEVNULL) as p:
97 p.wait()
100@cli.command()
101@click.option("--cpp/--no-cpp", default=False)
102@click.option("--defs", default=defdef)
103@click.option("--template", default=None, type=click.File('r'))
104@click.option("--verbose/--no-verbose", default=False)
105@click.argument("file", type=click.File('r'))
106def rpcgen_help(file, cpp, defs, template, verbose):
107 if verbose:
108 basicConfig(level=DEBUG)
109 data = parse_file(file, cpp, defs, verbose)
110 tmpl = None
111 if template is not None:
112 tmpl = template.read()
113 with tempfile.TemporaryDirectory() as tmpd:
114 with open(os.path.join(tmpd, "mymodule.py"), "w") as ofp:
115 res = lotrpc.sunrpc.rpcgen.generate_proto(data, tmpl)
116 ofp.write(res)
117 sys.path.append(tmpd)
118 mod = importlib.import_module("mymodule")
119 help(mod)
122if __name__ == '__main__':
123 cli()