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, see <http://www.gnu.org/licenses/>.
21 ######################################################################
23 import re
, string
, sys
, commands
25 HEADER
= """######################################################################
27 ## autogenerated file of smb.conf parameters
28 ## generate_parm_table <..../param/loadparm.c>
30 ## Copyright (C) Gerald Carter 2004.
32 ## This program is free software; you can redistribute it and/or modify
33 ## it under the terms of the GNU General Public License as published by
34 ## the Free Software Foundation; either version 3 of the License, or
35 ## (at your option) any later version.
37 ## This program is distributed in the hope that it will be useful,
38 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
39 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 ## GNU General Public License for more details.
42 ## You should have received a copy of the GNU General Public License
43 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
45 ######################################################################
47 from SambaParm import SambaParmString, SambaParmBool, SambaParmBoolRev
49 ## boolean defines for parm_table
55 FOOTER
= """##### end of smbparm.y ##########################################
56 #################################################################"""
58 TESTPARM
= "/usr/bin/testparm"
60 ## fields in Samba's parameter table
71 'P_BOOL' : 'SambaParmBool',
72 'P_BOOLREV' : 'SambaParmBoolRev',
73 'P_STRING' : 'SambaParmString',
74 'P_USTRING' : 'SambaParmString',
75 'P_GSTRING' : 'SambaParmString',
76 'P_LIST' : 'SambaParmString',
77 'P_ENUM' : 'SambaParmString',
78 'P_CHAR' : 'SambaParmString',
79 'P_OCTAL' : 'SambaParmString',
80 'P_INTEGER' : 'SambaParmString',
83 ######################################################################
85 ######################################################################
87 ## First thing is to build the dictionary of parmeter names ##
88 ## based on the output from testparm ##
90 cmd
= "/usr/bin/testparm -s -v /dev/null"
91 ( status
, testparm_output
) = commands
.getstatusoutput( cmd
)
93 sys
.stderr
.write( "Failed to execute testparm!\n%s\n" % testparm_output
)
96 ## break the output into a list ##
98 lines
= string
.split( testparm_output
, "\n" )
100 ## loop through list -- parameters in testparm output have ##
101 ## whitespace at the beginning of the line ##
103 pattern
= re
.compile( "^\s+" )
104 for input_str
in lines
:
105 if not pattern
.search( input_str
):
107 input_str
= string
.strip( input_str
)
108 parts
= string
.split( input_str
, "=" )
109 parts
[0] = string
.strip( parts
[0] )
110 parts
[1] = string
.strip( parts
[1] )
111 key
= string
.upper( string
.join(string
.split(parts
[0]), "") )
112 new
= parts
[1].replace('\\', '\\\\')
113 def_values
[key
] = new
115 ## open loadparm.c and get the entire list of parameters ##
116 ## including synonums ##
118 if len(sys
.argv
) != 2:
119 print "Usage: %s <.../param/loadparm.c>" % ( sys
.argv
[0] )
123 fconfig
= open( sys
.argv
[1], "r" )
125 print "%s does not exist!" % sys
.argv
[1]
128 ## Loop through loadparm.c -- all parameters are either ##
129 ## P_LOCAL or P_GLOBAL ##
132 pattern
= re
.compile( '{".*P_[GL]' )
134 input_str
= fconfig
.readline()
135 if len(input_str
) == 0 :
137 input_str
= string
.strip(input_str
)
139 ## see if we have a patch for a parameter definition ##
142 if pattern
.search( input_str
) :
144 ## strip the surrounding '{.*},' ##
146 input_str
= input_str
[1:-2]
147 parm
= string
.split(input_str
, ",")
149 ## strip the ""'s and upper case ##
151 name
= (string
.strip(parm
[displayName
])[1:-1])
152 key
= string
.upper( string
.join(string
.split(name
), "") )
153 var_name
= string
.strip( parm
[variable
] )
156 ## try to catch synonyms -- if the parameter was not reported ##
157 ## by testparm, then save it and come back after we will out ##
158 ## the variable list ##
160 if not def_values
.has_key( key
):
161 synonyms
.append( input_str
)
165 var_table
[var_name
] = key
167 parmType
= string
.strip(parm
[type])
169 parm_table
[key
] = [ name
, string
.strip(parm
[type]), string
.strip(parm
[scope
]), def_values
[key
] ]
171 ## Deal with any synonyms ##
173 for input_str
in synonyms
:
174 parm
= string
.split(input_str
, ",")
175 name
= (string
.strip(parm
[displayName
])[1:-1])
176 key
= string
.upper( string
.join(string
.split(name
), "") )
177 var_name
= string
.strip( parm
[variable
] )
179 ## if there's no pre-existing key, then testparm doesn't know about it
180 if not var_table
.has_key( var_name
):
184 parm_table
[key
] = parm_table
[var_table
[var_name
]][:]
185 # parm_table[key][1] = parm[1]
186 parm_table
[key
][1] = string
.strip(parm
[1])
189 ## print out smbparm.py ##
193 smbparm
= open ( "smbparm.py", "w" )
195 print "Cannot write to smbparm.py"
198 smbparm
.write( HEADER
)
199 smbparm
.write( "parm_table = {\n" )
201 for x
in parm_table
.keys():
203 smbparm
.write("\t%-25s: (\"%s\", %s, %s, \"%s\"),\n" % ( key
, parm_table
[x
][0],
204 obj_table
[parm_table
[x
][1]], parm_table
[x
][2], parm_table
[x
][3] ))
206 smbparm
.write( "}\n" )
208 smbparm
.write( FOOTER
)
209 smbparm
.write( "\n" )
215 ## cut-n-paste area ##
218 for x
in parm_table
.keys():
219 if def_values
.has_key( x
):
220 parm_table
[x
].append( def_values
[x
] )
222 parm_table
[x
].append( "" )