~ubuntu-branches/ubuntu/lucid/samba/lucid-proposed

« back to all changes in this revision

Viewing changes to examples/scripts/python/generate_parm_table.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-07-21 17:53:23 UTC
  • mfrom: (0.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050721175323-m3oh6aoigywohfnq
Tags: 3.0.14a-6ubuntu1
Resynchronise with Debian, resolving merge conflicts (#12360)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
######################################################################
 
3
##
 
4
##  Generate parameter dictionary from param/loadparm.c
 
5
##
 
6
##  Copyright (C) Gerald Carter                2004.
 
7
##
 
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 2 of the License, or
 
11
##  (at your option) any later version.
 
12
##
 
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.
 
17
##
 
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.
 
21
##
 
22
######################################################################
 
23
 
 
24
import re, string, sys, commands
 
25
 
 
26
HEADER = """######################################################################
 
27
##
 
28
## autogenerated file of smb.conf parameters
 
29
## generate_parm_table <..../param/loadparm.c>
 
30
##
 
31
##  Copyright (C) Gerald Carter                2004.
 
32
##
 
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 2 of the License, or
 
36
##  (at your option) any later version.
 
37
##
 
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.
 
42
##
 
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.
 
46
##
 
47
######################################################################
 
48
 
 
49
from SambaParm import SambaParmString, SambaParmBool, SambaParmBoolRev
 
50
 
 
51
## boolean defines for parm_table
 
52
P_LOCAL    = 0
 
53
P_GLOBAL   = 1
 
54
 
 
55
"""
 
56
 
 
57
FOOTER = """##### end of smbparm.y ##########################################
 
58
#################################################################"""
 
59
 
 
60
TESTPARM = "/opt/src/samba-cvs/samba-3.0/source/bin/testparm"
 
61
 
 
62
## fields in Samba's parameter table
 
63
displayName = 0
 
64
type        = 1
 
65
scope       = 2
 
66
variable    = 3
 
67
flags       = 6
 
68
 
 
69
parm_table  = {}
 
70
var_table   = {}
 
71
def_values  = {}
 
72
obj_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',
 
83
}
 
84
 
 
85
######################################################################
 
86
##                        BEGIN MAIN CODE                           ##
 
87
######################################################################
 
88
 
 
89
## First thing is to build the dictionary of parmeter names  ##
 
90
## based on the output from testparm                         ##
 
91
 
 
92
cmd = "/opt/samba/bin/testparm -s -v /dev/null"
 
93
( status, testparm_output ) = commands.getstatusoutput( cmd )
 
94
if status:
 
95
        sys.stderr.write( "Failed to execute testparm!\n%s\n" % testparm_output )
 
96
 
 
97
 
 
98
## break the output into a list ##
 
99
 
 
100
lines = string.split( testparm_output, "\n" )
 
101
 
 
102
## loop through list -- parameters in testparm output have ##
 
103
## whitespace at the beginning of the line                 ##
 
104
 
 
105
pattern = re.compile( "^\s+" )
 
106
for input_str in lines:
 
107
        if not pattern.search( input_str ):
 
108
                continue
 
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
        def_values[key] = parts[1]
 
115
 
 
116
## open loadparm.c and get the entire list of parameters ##
 
117
## including synonums                                    ##
 
118
                
 
119
if len(sys.argv) != 2:
 
120
        print "Usage: %s <.../param/loadparm.c>" % ( sys.argv[0] )
 
121
        sys.exit( 1 )
 
122
        
 
123
try:
 
124
        fconfig = open( sys.argv[1], "r" )
 
125
except IOError:
 
126
        print "%s does not exist!" % sys.argv[1]
 
127
        sys.exit (1)
 
128
 
 
129
## Loop through loadparm.c --  all parameters are either ##
 
130
## P_LOCAL or P_GLOBAL                                   ##
 
131
 
 
132
synonyms = []
 
133
pattern = re.compile( '{".*P_[GL]' )
 
134
while True:
 
135
        input_str= fconfig.readline()
 
136
        if  len(input_str) == 0 :
 
137
                break
 
138
        input_str= string.strip(input_str)
 
139
        
 
140
        ## see if we have a patch for a parameter definition ##
 
141
        
 
142
        parm = []
 
143
        if pattern.search( input_str) :
 
144
        
 
145
                ## strip the surrounding '{.*},' ##
 
146
                
 
147
                input_str= input_str[1:-2]
 
148
                parm = string.split(input_str, ",")
 
149
                
 
150
                ## strip the ""'s and upper case ##
 
151
                
 
152
                name = (string.strip(parm[displayName])[1:-1])
 
153
                key = string.upper( string.join(string.split(name), "") )
 
154
                var_name = string.strip( parm[variable] )
 
155
                
 
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                                          ##
 
159
                
 
160
                if not def_values.has_key( key ):
 
161
                        synonyms.append( input_str)
 
162
                        continue
 
163
                
 
164
                        
 
165
                var_table[var_name] = key 
 
166
                
 
167
                parmType = string.strip(parm[type])
 
168
                
 
169
                parm_table[key] = [ name , string.strip(parm[type]), string.strip(parm[scope]), def_values[key] ]
 
170
 
 
171
## Deal with any synonyms ##
 
172
 
 
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] )
 
178
                
 
179
                ## if there's no pre-existing key, then testparm doesn't know about it
 
180
                if not var_table.has_key( var_name ):
 
181
                        continue
 
182
                        
 
183
                ## just make a copy
 
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])
 
187
 
 
188
##                      ##      
 
189
## print out smbparm.py ##
 
190
##                      ##      
 
191
 
 
192
try:
 
193
        smbparm = open ( "smbparm.py", "w" )
 
194
except IOError:
 
195
        print "Cannot write to smbparm.py"
 
196
        sys.exit( 1 )
 
197
        
 
198
smbparm.write( HEADER )
 
199
smbparm.write( "parm_table = {\n" )
 
200
 
 
201
for x in parm_table.keys():
 
202
        key = "\"%s\"" % x
 
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] ))
 
205
 
 
206
smbparm.write( "}\n" )
 
207
 
 
208
smbparm.write( FOOTER )
 
209
smbparm.write( "\n" )
 
210
 
 
211
sys.exit(0)
 
212
 
 
213
 
 
214
##                  ##
 
215
## cut-n-paste area ##
 
216
##                  ##
 
217
 
 
218
for x in parm_table.keys():
 
219
        if def_values.has_key( x ):
 
220
                parm_table[x].append( def_values[x] )
 
221
        else:
 
222
                parm_table[x].append( "" )