param: auto generate param_functions.c at build time
[sfrench/samba-autobuild/.git] / script / generate_param.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2014 Catalyst.Net Ltd
3 #
4 # Auto generate param_functions.c
5 #
6 #   ** NOTE! The following LGPL license applies to the ldb
7 #   ** library. This does NOT imply that all of Samba is released
8 #   ** under the LGPL
9 #
10 #  This library is free software; you can redistribute it and/or
11 #  modify it under the terms of the GNU Lesser General Public
12 #  License as published by the Free Software Foundation; either
13 #  version 3 of the License, or (at your option) any later version.
14 #
15 #  This library is distributed in the hope that it will be useful,
16 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 #  Lesser General Public License for more details.
19 #
20 #  You should have received a copy of the GNU Lesser General Public
21 #  License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #
23
24 import errno
25 import os
26 import re
27 import subprocess
28 import xml.etree.ElementTree as ET
29 import sys
30 import optparse
31
32 # parse command line arguments
33 parser = optparse.OptionParser()
34 parser.add_option("-f", "--file", dest="filename",
35                   help="input file", metavar="FILE")
36 parser.add_option("-o", "--output", dest="output",
37                   help='output file', metavar="FILE")
38 parser.add_option("--mode", type="choice", metavar="<FUNCTIONS>",
39                  choices=["FUNCTIONS"], default="FUNCTIONS")
40
41 (options, args) = parser.parse_args()
42
43 if options.filename is None:
44     parser.error("No input file specified")
45 if options.output is None:
46     parser.error("No output file specified")
47
48 def iterate_all(path):
49     """Iterate and yield all the parameters. 
50
51     :param path: path to parameters xml file
52     """
53
54     try:
55         p = open(path, 'r')
56     except IOError, e:
57         raise Exception("Error opening parameters file")
58     out = p.read()
59
60     # parse the parameters xml file
61     root = ET.fromstring(out)
62     for parameter in root:
63         name = parameter.attrib.get("name")
64         param_type = parameter.attrib.get("type")
65         context = parameter.attrib.get("context")
66         func = parameter.attrib.get("function")
67         synonym = parameter.attrib.get("synonym")
68         removed = parameter.attrib.get("removed")
69         generated = parameter.attrib.get("generated_function")
70         if synonym == "1" or removed == "1" or generated == "0":
71             continue
72         constant = parameter.attrib.get("constant")
73         parm = parameter.attrib.get("parm")
74         if name is None or param_type is None or context is None:
75             raise Exception("Error parsing parameter: " + name)
76         if func is None:
77             func = name.replace(" ", "_").lower()
78         yield {'name': name,
79                'type': param_type,
80                'context': context,
81                'function': func,
82                'constant': (constant == '1'),
83                'parm': (parm == '1')}
84
85 # map doc attributes to a section of the generated function
86 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
87 param_type_dict = {"boolean": "_BOOL", "list": "_LIST", "string": "_STRING",
88                    "integer": "_INTEGER", "enum": "_INTEGER", "char" : "_CHAR",
89                    "boolean-auto": "_INTEGER"}
90
91 f = open(options.output, 'w')
92
93 try:
94     for parameter in iterate_all(options.filename):
95         # filter out parameteric options
96         if ':' in parameter['name']:
97             continue
98         output_string = "FN"
99         temp = context_dict.get(parameter['context'])
100         if temp is None: 
101             raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
102         output_string += temp
103         if parameter['constant']:
104             output_string += "_CONST"
105         if parameter['parm']: 
106             output_string += "_PARM"
107         temp = param_type_dict.get(parameter['type'])
108         if temp is None:
109             raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
110         output_string += temp
111         f.write(output_string + "(" + parameter['function'] +", " + parameter['function'] + ')\n')
112 finally:
113     f.close()