Statistics
| Branch: | Tag: | Revision:

root / autotools / build-rpc @ 26d502d0

History | View | Annotate | Download (4.8 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script to generate RPC code.
23

    
24
"""
25

    
26
# pylint: disable=C0103
27
# [C0103] Invalid name
28

    
29
import sys
30
import re
31
import itertools
32
import textwrap
33
from cStringIO import StringIO
34

    
35
from ganeti import utils
36
from ganeti import compat
37
from ganeti import build
38

    
39

    
40
_SINGLE = "single-node"
41
_MULTI = "multi-node"
42

    
43

    
44
def _WritePreamble(sw):
45
  """Writes a preamble for the RPC wrapper output.
46

    
47
  """
48
  sw.Write("# This code is automatically generated at build time.")
49
  sw.Write("# Do not modify manually.")
50
  sw.Write("")
51
  sw.Write("\"\"\"Automatically generated RPC client wrappers.")
52
  sw.Write("")
53
  sw.Write("\"\"\"")
54
  sw.Write("")
55
  sw.Write("from ganeti import rpc_defs")
56
  sw.Write("")
57

    
58

    
59
def _WrapCode(line):
60
  """Wraps Python code.
61

    
62
  """
63
  return textwrap.wrap(line, width=70, expand_tabs=False,
64
                       fix_sentence_endings=False, break_long_words=False,
65
                       replace_whitespace=True,
66
                       subsequent_indent=utils.ShellWriter.INDENT_STR)
67

    
68

    
69
def _WriteDocstring(sw, name, timeout, kind, args, desc):
70
  """Writes a docstring for an RPC wrapper.
71

    
72
  """
73
  sw.Write("\"\"\"Wrapper for RPC call '%s'", name)
74
  sw.Write("")
75
  if desc:
76
    sw.Write(desc)
77
    sw.Write("")
78

    
79
  note = ["This is a %s call" % kind]
80
  if timeout:
81
    note.append(" with a timeout of %s" % utils.FormatSeconds(timeout))
82
  sw.Write("@note: %s", "".join(note))
83

    
84
  if kind == _SINGLE:
85
    sw.Write("@type node: string")
86
    sw.Write("@param node: Node name")
87
  else:
88
    sw.Write("@type node_list: list of string")
89
    sw.Write("@param node_list: List of node names")
90

    
91
  if args:
92
    for (argname, _, argtext) in args:
93
      if argtext:
94
        docline = "@param %s: %s" % (argname, argtext)
95
        for line in _WrapCode(docline):
96
          sw.Write(line)
97
  sw.Write("")
98
  sw.Write("\"\"\"")
99

    
100

    
101
def _WriteBaseClass(sw, clsname, calls):
102
  """Write RPC wrapper class.
103

    
104
  """
105
  sw.Write("")
106
  sw.Write("class %s(object):", clsname)
107
  sw.IncIndent()
108
  try:
109
    sw.Write("# E1101: Non-existent members")
110
    sw.Write("# R0904: Too many public methods")
111
    sw.Write("# pylint: disable=E1101,R0904")
112

    
113
    if not calls:
114
      sw.Write("pass")
115
      return
116

    
117
    sw.Write("_CALLS = rpc_defs.CALLS[%r]", clsname)
118
    sw.Write("")
119

    
120
    for (name, kind, timeout, args, _, desc) in calls:
121
      funcargs = ["self"]
122

    
123
      if kind == _SINGLE:
124
        funcargs.append("node")
125
      elif kind == _MULTI:
126
        funcargs.append("node_list")
127
      else:
128
        raise Exception("Unknown kind '%s'" % kind)
129

    
130
      funcargs.extend(map(compat.fst, args))
131

    
132
      assert "read_timeout" not in funcargs
133
      funcargs.append("read_timeout=%s" % timeout)
134

    
135
      funcargs.append("_def=_CALLS[%r]" % name)
136

    
137
      funcdef = "def call_%s(%s):" % (name, utils.CommaJoin(funcargs))
138
      for line in _WrapCode(funcdef):
139
        sw.Write(line)
140

    
141
      sw.IncIndent()
142
      try:
143
        _WriteDocstring(sw, name, timeout, kind, args, desc)
144

    
145
        buf = StringIO()
146
        buf.write("return ")
147

    
148
        # In case line gets too long and is wrapped in a bad spot
149
        buf.write("( ")
150

    
151
        buf.write("self._Call(_def, ")
152
        if kind == _SINGLE:
153
          buf.write("[node]")
154
        else:
155
          buf.write("node_list")
156

    
157
        buf.write(", read_timeout, [%s])" %
158
                  # Function arguments
159
                  utils.CommaJoin(map(compat.fst, args)))
160

    
161
        if kind == _SINGLE:
162
          buf.write("[node]")
163
        buf.write(")")
164

    
165
        for line in _WrapCode(buf.getvalue()):
166
          sw.Write(line)
167
      finally:
168
        sw.DecIndent()
169
      sw.Write("")
170
  finally:
171
    sw.DecIndent()
172

    
173

    
174
def main():
175
  """Main function.
176

    
177
  """
178
  buf = StringIO()
179
  sw = utils.ShellWriter(buf)
180

    
181
  _WritePreamble(sw)
182

    
183
  for filename in sys.argv[1:]:
184
    sw.Write("# Definitions from '%s'", filename)
185

    
186
    module = build.LoadModule(filename)
187

    
188
    # Call types are re-defined in definitions file to avoid imports. Verify
189
    # here to ensure they're equal to local constants.
190
    assert module.SINGLE == _SINGLE
191
    assert module.MULTI == _MULTI
192

    
193
    for (clsname, calls) in module.CALLS.items():
194
      _WriteBaseClass(sw, clsname, calls.values())
195

    
196
  print buf.getvalue()
197

    
198

    
199
if __name__ == "__main__":
200
  main()