s4/scripting/bin: open unicode files with utf8 encoding and write unicode string
[herb/samba-autobuild/.git] / source4 / scripting / bin / gen_werror.py
1 #!/usr/bin/env python3
2
3 #
4 # Unix SMB/CIFS implementation.
5 #
6 # WERROR error definition generation
7 #
8 # Copyright (C) Catalyst.Net Ltd. 2017
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program 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
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 from __future__ import unicode_literals
24 # this file is a bin script and was not imported by any other modules
25 # so it should be fine to enable unicode string for python2
26
27 import sys, os.path, io, string
28 from gen_error_common import parseErrorDescriptions, ErrorDef
29
30 def generateHeaderFile(out_file, errors):
31     out_file.write("/*\n")
32     out_file.write(" * Descriptions for errors generated from\n")
33     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
34     out_file.write(" */\n\n")
35     out_file.write("#ifndef _WERR_GEN_H\n")
36     out_file.write("#define _WERR_GEN_H\n")
37     for err in errors:
38         line = "#define %s W_ERROR(%s)\n" % (err.err_define, hex(err.err_code))
39         out_file.write(line)
40     out_file.write("\n#endif /* _WERR_GEN_H */\n")
41
42 def generateSourceFile(out_file, errors):
43     out_file.write("#include \"werror.h\"\n")
44
45     out_file.write("/*\n")
46     out_file.write(" * Names for errors generated from\n")
47     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
48     out_file.write(" */\n")
49
50     out_file.write("static const struct werror_code_struct dos_errs[] = \n")
51     out_file.write("{\n")
52     for err in errors:
53         out_file.write("\t{ \"%s\", %s },\n" % (err.err_define, err.err_define))
54     out_file.write("{ 0, W_ERROR(0) }\n")
55     out_file.write("};\n")
56
57     out_file.write("\n/*\n")
58     out_file.write(" * Descriptions for errors generated from\n")
59     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
60     out_file.write(" */\n")
61
62     out_file.write("static const struct werror_str_struct dos_err_strs[] = \n")
63     out_file.write("{\n")
64     for err in errors:
65         # Account for the possibility that some errors may not have descriptions
66         if err.err_string == "":
67             continue
68         out_file.write("\t{ %s, \"%s\" },\n"%(err.err_define, err.err_string))
69     out_file.write("\t{ W_ERROR(0), 0 }\n")
70     out_file.write("};")
71
72 def generatePythonFile(out_file, errors):
73     out_file.write("/*\n")
74     out_file.write(" * Errors generated from\n")
75     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
76     out_file.write(" */\n")
77     out_file.write("#include <Python.h>\n")
78     out_file.write("#include \"python/py3compat.h\"\n")
79     out_file.write("#include \"includes.h\"\n\n")
80     out_file.write("static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)\n");
81     out_file.write("{\n");
82     out_file.write("\tif (v > LONG_MAX) {\n");
83     out_file.write("\t\treturn PyLong_FromUnsignedLongLong(v);\n");
84     out_file.write("\t} else {\n");
85     out_file.write("\t\treturn PyInt_FromLong(v);\n");
86     out_file.write("\t}\n");
87     out_file.write("}\n\n");
88     # This is needed to avoid a missing prototype error from the C
89     # compiler. There is never a prototype for this function, it is a
90     # module loaded by python with dlopen() and found with dlsym().
91     out_file.write("static struct PyModuleDef moduledef = {\n")
92     out_file.write("\tPyModuleDef_HEAD_INIT,\n")
93     out_file.write("\t.m_name = \"werror\",\n")
94     out_file.write("\t.m_doc = \"WERROR defines\",\n")
95     out_file.write("\t.m_size = -1,\n")
96     out_file.write("};\n\n")
97     out_file.write("MODULE_INIT_FUNC(werror)\n")
98     out_file.write("{\n")
99     out_file.write("\tPyObject *m;\n\n")
100     out_file.write("\tm = PyModule_Create(&moduledef);\n");
101     out_file.write("\tif (m == NULL)\n");
102     out_file.write("\t\treturn NULL;\n\n");
103     for err in errors:
104         line = """\tPyModule_AddObject(m, \"%s\",
105                   \t\tndr_PyLong_FromUnsignedLongLong(W_ERROR_V(%s)));\n""" % (err.err_define, err.err_define)
106         out_file.write(line)
107     out_file.write("\n");
108     out_file.write("\treturn m;\n");
109     out_file.write("}\n");
110
111 def transformErrorName( error_name ):
112     if error_name.startswith("WERR_"):
113         error_name = error_name.replace("WERR_", "", 1)
114     elif error_name.startswith("ERROR_"):
115         error_name = error_name.replace("ERROR_", "", 1)
116     return "WERR_" + error_name.upper()
117
118 # Script to generate files werror_gen.h, doserr_gen.c and
119 # py_werror.c.
120 #
121 # These files contain generated definitions for WERRs and
122 # their descriptions/names.
123 #
124 # This script takes four inputs:
125 # [1]: The name of the text file which is the content of an HTML table
126 #      (e.g. the one found at https://msdn.microsoft.com/en-us/library/cc231199.aspx)
127 #      copied and pasted.
128 # [2]: [[output werror_gen.h]]
129 # [3]: [[output doserr_gen.c]]
130 # [4]: [[output py_werror.c]]
131 def main():
132     if len(sys.argv) == 5:
133         input_file_name = sys.argv[1]
134         gen_headerfile_name = sys.argv[2]
135         gen_sourcefile_name = sys.argv[3]
136         gen_pythonfile_name = sys.argv[4]
137     else:
138         print("usage: %s winerrorfile headerfile sourcefile pythonfile" % sys.argv[0])
139         sys.exit()
140
141     input_file = io.open(input_file_name, "rt", encoding='utf8')
142     errors = parseErrorDescriptions(input_file, True, transformErrorName)
143     input_file.close()
144
145     print("writing new header file: %s" % gen_headerfile_name)
146     out_file = io.open(gen_headerfile_name, "wt", encoding='utf8')
147     generateHeaderFile(out_file, errors)
148     out_file.close()
149     print("writing new source file: %s" % gen_sourcefile_name)
150     out_file = io.open(gen_sourcefile_name, "wt", encoding='utf8')
151     generateSourceFile(out_file, errors)
152     out_file.close()
153     print("writing new python file: %s" % gen_pythonfile_name)
154     out_file = io.open(gen_pythonfile_name, "wt", encoding='utf8')
155     generatePythonFile(out_file, errors)
156     out_file.close()
157
158 if __name__ == '__main__':
159
160     main()