2 ######################################################################
4 ## Generate parameter dictionary from param/loadparm.c
6 ## Copyright (C) Gerald Carter 2004.
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 3 of the License, or
11 ## (at your option) any later version.
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 ######################################################################
24 import re, string, sys, commands
26 HEADER = """######################################################################
28 ## autogenerated file of smb.conf parameters
29 ## generate_parm_table <..../param/loadparm.c>
31 ## Copyright (C) Gerald Carter 2004.
33 ## This program is free software; you can redistribute it and/or modify
34 ## it under the terms of the GNU General Public License as published by
35 ## the Free Software Foundation; either version 3 of the License, or
36 ## (at your option) any later version.
38 ## This program is distributed in the hope that it will be useful,
39 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
40 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 ## GNU General Public License for more details.
43 ## You should have received a copy of the GNU General Public License
44 ## along with this program; if not, write to the Free Software
45 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 ######################################################################
49 from SambaParm import SambaParmString, SambaParmBool, SambaParmBoolRev
51 ## boolean defines for parm_table
57 FOOTER = """##### end of smbparm.y ##########################################
58 #################################################################"""
60 TESTPARM = "/usr/bin/testparm"
62 ## fields in Samba's parameter table
73 'P_BOOL' : 'SambaParmBool',
74 'P_BOOLREV' : 'SambaParmBoolRev',
75 'P_STRING' : 'SambaParmString',
76 'P_USTRING' : 'SambaParmString',
77 'P_GSTRING' : 'SambaParmString',
78 'P_LIST' : 'SambaParmString',
79 'P_ENUM' : 'SambaParmString',
80 'P_CHAR' : 'SambaParmString',
81 'P_OCTAL' : 'SambaParmString',
82 'P_INTEGER' : 'SambaParmString',
85 ######################################################################
87 ######################################################################
89 ## First thing is to build the dictionary of parmeter names ##
90 ## based on the output from testparm ##
92 cmd = "/usr/bin/testparm -s -v /dev/null"
93 ( status, testparm_output ) = commands.getstatusoutput( cmd )
95 sys.stderr.write( "Failed to execute testparm!\n%s\n" % testparm_output )
98 ## break the output into a list ##
100 lines = string.split( testparm_output, "\n" )
102 ## loop through list -- parameters in testparm output have ##
103 ## whitespace at the beginning of the line ##
105 pattern = re.compile( "^\s+" )
106 for input_str in lines:
107 if not pattern.search( input_str ):
109 input_str = string.strip( input_str )
110 parts = string.split( input_str, "=" )
111 parts[0] = string.strip( parts[0] )
112 parts[1] = string.strip( parts[1] )
113 key = string.upper( string.join(string.split(parts[0]), "") )
114 new = parts[1].replace('\\', '\\\\')
115 def_values[key] = new
117 ## open loadparm.c and get the entire list of parameters ##
118 ## including synonums ##
120 if len(sys.argv) != 2:
121 print "Usage: %s <.../param/loadparm.c>" % ( sys.argv[0] )
125 fconfig = open( sys.argv[1], "r" )
127 print "%s does not exist!" % sys.argv[1]
130 ## Loop through loadparm.c -- all parameters are either ##
131 ## P_LOCAL or P_GLOBAL ##
134 pattern = re.compile( '{".*P_[GL]' )
136 input_str= fconfig.readline()
137 if len(input_str) == 0 :
139 input_str= string.strip(input_str)
141 ## see if we have a patch for a parameter definition ##
144 if pattern.search( input_str) :
146 ## strip the surrounding '{.*},' ##
148 input_str= input_str[1:-2]
149 parm = string.split(input_str, ",")
151 ## strip the ""'s and upper case ##
153 name = (string.strip(parm[displayName])[1:-1])
154 key = string.upper( string.join(string.split(name), "") )
155 var_name = string.strip( parm[variable] )
158 ## try to catch synonyms -- if the parameter was not reported ##
159 ## by testparm, then save it and come back after we will out ##
160 ## the variable list ##
162 if not def_values.has_key( key ):
163 synonyms.append( input_str)
167 var_table[var_name] = key
169 parmType = string.strip(parm[type])
171 parm_table[key] = [ name , string.strip(parm[type]), string.strip(parm[scope]), def_values[key] ]
173 ## Deal with any synonyms ##
175 for input_str in synonyms:
176 parm = string.split(input_str, ",")
177 name = (string.strip(parm[displayName])[1:-1])
178 key = string.upper( string.join(string.split(name), "") )
179 var_name = string.strip( parm[variable] )
181 ## if there's no pre-existing key, then testparm doesn't know about it
182 if not var_table.has_key( var_name ):
186 parm_table[key] = parm_table[var_table[var_name]][:]
187 # parm_table[key][1] = parm[1]
188 parm_table[key][1] = string.strip(parm[1])
191 ## print out smbparm.py ##
195 smbparm = open ( "smbparm.py", "w" )
197 print "Cannot write to smbparm.py"
200 smbparm.write( HEADER )
201 smbparm.write( "parm_table = {\n" )
203 for x in parm_table.keys():
205 smbparm.write("\t%-25s: (\"%s\", %s, %s, \"%s\"),\n" % ( key, parm_table[x][0],
206 obj_table[parm_table[x][1]], parm_table[x][2], parm_table[x][3] ))
208 smbparm.write( "}\n" )
210 smbparm.write( FOOTER )
211 smbparm.write( "\n" )
217 ## cut-n-paste area ##
220 for x in parm_table.keys():
221 if def_values.has_key( x ):
222 parm_table[x].append( def_values[x] )
224 parm_table[x].append( "" )