samba_dnsupdate: make rodc_dns_update() more robust against timing problems
[samba.git] / source4 / scripting / bin / gen_error_common.py
1 #!/usr/bin/env python3
2
3 #
4 # Unix SMB/CIFS implementation.
5 #
6 # Utility methods for generating error codes from a file.
7 #
8 # Copyright (C) Noel Power <noel.power@suse.com> 2014
9 # Copyright (C) Catalyst IT Ltd. 2017
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24
25 # error data model
26 class ErrorDef:
27     def __init__(self):
28         self.err_code = None
29         self.err_define = None
30         self.err_string = ""
31         self.linenum = ""
32
33 def escapeString( input ):
34     output = input.replace('"','\\"')
35     output = output.replace("\\<","\\\\<")
36     output = output.replace('\t',"")
37     return output
38
39 # Parse error descriptions from a file which is the content
40 # of an HTML table.
41 # The file must be formatted as:
42 # [error code hex]
43 # [error name short]
44 # [error description]
45 # Blank lines are allowed and errors do not have to have a
46 # description.
47 # Returns a list of ErrorDef objects.
48 def parseErrorDescriptions( file_contents, isWinError, transformErrorFunction ):
49     errors = []
50     count = 0
51     for line in file_contents:
52         if line == None or line == '\t' or line == "" or line == '\n':
53             continue
54         content = line.strip().split(None,1)
55         # start new error definition ?
56         if line.startswith("0x"):
57             newError = ErrorDef()
58             newError.err_code = int(content[0],0)
59             # escape the usual suspects
60             if len(content) > 1:
61                 newError.err_string = escapeString(content[1])
62             newError.linenum = count
63             newError.isWinError = isWinError
64             errors.append(newError)
65         else:
66             if len(errors) == 0:
67                 continue
68             err = errors[-1]
69             if err.err_define == None:
70                 err.err_define = transformErrorFunction(content[0])
71             else:
72                 if len(content) > 0:
73                     desc =  escapeString(line.strip())
74                     if len(desc):
75                         if err.err_string == "":
76                             err.err_string = desc
77                         else:
78                             err.err_string = err.err_string + " " + desc
79             count = count + 1
80     print("parsed %d lines generated %d error definitions"%(count,len(errors)))
81     return errors
82