/examples/scripts/shares/python/generate_parm_table.py

https://bitbucket.org/knarf/samba · Python · 222 lines · 126 code · 46 blank · 50 comment · 18 complexity · 62658a93a6508c4d7b017cdb2b044fec MD5 · raw file

  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 3 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, see <http://www.gnu.org/licenses/>.
  20. ##
  21. ######################################################################
  22. import re, string, sys, commands
  23. HEADER = """######################################################################
  24. ##
  25. ## autogenerated file of smb.conf parameters
  26. ## generate_parm_table <..../param/loadparm.c>
  27. ##
  28. ## Copyright (C) Gerald Carter 2004.
  29. ##
  30. ## This program is free software; you can redistribute it and/or modify
  31. ## it under the terms of the GNU General Public License as published by
  32. ## the Free Software Foundation; either version 3 of the License, or
  33. ## (at your option) any later version.
  34. ##
  35. ## This program is distributed in the hope that it will be useful,
  36. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. ## GNU General Public License for more details.
  39. ##
  40. ## You should have received a copy of the GNU General Public License
  41. ## along with this program; if not, see <http://www.gnu.org/licenses/>.
  42. ##
  43. ######################################################################
  44. from SambaParm import SambaParmString, SambaParmBool, SambaParmBoolRev
  45. ## boolean defines for parm_table
  46. P_LOCAL = 0
  47. P_GLOBAL = 1
  48. """
  49. FOOTER = """##### end of smbparm.y ##########################################
  50. #################################################################"""
  51. TESTPARM = "/usr/bin/testparm"
  52. ## fields in Samba's parameter table
  53. displayName = 0
  54. type = 1
  55. scope = 2
  56. variable = 3
  57. flags = 6
  58. parm_table = {}
  59. var_table = {}
  60. def_values = {}
  61. obj_table = {
  62. 'P_BOOL' : 'SambaParmBool',
  63. 'P_BOOLREV' : 'SambaParmBoolRev',
  64. 'P_STRING' : 'SambaParmString',
  65. 'P_USTRING' : 'SambaParmString',
  66. 'P_GSTRING' : 'SambaParmString',
  67. 'P_LIST' : 'SambaParmString',
  68. 'P_ENUM' : 'SambaParmString',
  69. 'P_CHAR' : 'SambaParmString',
  70. 'P_OCTAL' : 'SambaParmString',
  71. 'P_INTEGER' : 'SambaParmString',
  72. }
  73. ######################################################################
  74. ## BEGIN MAIN CODE ##
  75. ######################################################################
  76. ## First thing is to build the dictionary of parmeter names ##
  77. ## based on the output from testparm ##
  78. cmd = "/usr/bin/testparm -s -v /dev/null"
  79. ( status, testparm_output ) = commands.getstatusoutput( cmd )
  80. if status:
  81. sys.stderr.write( "Failed to execute testparm!\n%s\n" % testparm_output )
  82. ## break the output into a list ##
  83. lines = string.split( testparm_output, "\n" )
  84. ## loop through list -- parameters in testparm output have ##
  85. ## whitespace at the beginning of the line ##
  86. pattern = re.compile( "^\s+" )
  87. for input_str in lines:
  88. if not pattern.search( input_str ):
  89. continue
  90. input_str = string.strip( input_str )
  91. parts = string.split( input_str, "=" )
  92. parts[0] = string.strip( parts[0] )
  93. parts[1] = string.strip( parts[1] )
  94. key = string.upper( string.join(string.split(parts[0]), "") )
  95. new = parts[1].replace('\\', '\\\\')
  96. def_values[key] = new
  97. ## open loadparm.c and get the entire list of parameters ##
  98. ## including synonums ##
  99. if len(sys.argv) != 2:
  100. print "Usage: %s <.../param/loadparm.c>" % ( sys.argv[0] )
  101. sys.exit( 1 )
  102. try:
  103. fconfig = open( sys.argv[1], "r" )
  104. except IOError:
  105. print "%s does not exist!" % sys.argv[1]
  106. sys.exit (1)
  107. ## Loop through loadparm.c -- all parameters are either ##
  108. ## P_LOCAL or P_GLOBAL ##
  109. synonyms = []
  110. pattern = re.compile( '{".*P_[GL]' )
  111. while True:
  112. input_str= fconfig.readline()
  113. if len(input_str) == 0 :
  114. break
  115. input_str= string.strip(input_str)
  116. ## see if we have a patch for a parameter definition ##
  117. parm = []
  118. if pattern.search( input_str) :
  119. ## strip the surrounding '{.*},' ##
  120. input_str= input_str[1:-2]
  121. parm = string.split(input_str, ",")
  122. ## strip the ""'s and upper case ##
  123. name = (string.strip(parm[displayName])[1:-1])
  124. key = string.upper( string.join(string.split(name), "") )
  125. var_name = string.strip( parm[variable] )
  126. ## try to catch synonyms -- if the parameter was not reported ##
  127. ## by testparm, then save it and come back after we will out ##
  128. ## the variable list ##
  129. if not def_values.has_key( key ):
  130. synonyms.append( input_str)
  131. continue
  132. var_table[var_name] = key
  133. parmType = string.strip(parm[type])
  134. parm_table[key] = [ name , string.strip(parm[type]), string.strip(parm[scope]), def_values[key] ]
  135. ## Deal with any synonyms ##
  136. for input_str in synonyms:
  137. parm = string.split(input_str, ",")
  138. name = (string.strip(parm[displayName])[1:-1])
  139. key = string.upper( string.join(string.split(name), "") )
  140. var_name = string.strip( parm[variable] )
  141. ## if there's no pre-existing key, then testparm doesn't know about it
  142. if not var_table.has_key( var_name ):
  143. continue
  144. ## just make a copy
  145. parm_table[key] = parm_table[var_table[var_name]][:]
  146. # parm_table[key][1] = parm[1]
  147. parm_table[key][1] = string.strip(parm[1])
  148. ## ##
  149. ## print out smbparm.py ##
  150. ## ##
  151. try:
  152. smbparm = open ( "smbparm.py", "w" )
  153. except IOError:
  154. print "Cannot write to smbparm.py"
  155. sys.exit( 1 )
  156. smbparm.write( HEADER )
  157. smbparm.write( "parm_table = {\n" )
  158. for x in parm_table.keys():
  159. key = "\"%s\"" % x
  160. smbparm.write("\t%-25s: (\"%s\", %s, %s, \"%s\"),\n" % ( key, parm_table[x][0],
  161. obj_table[parm_table[x][1]], parm_table[x][2], parm_table[x][3] ))
  162. smbparm.write( "}\n" )
  163. smbparm.write( FOOTER )
  164. smbparm.write( "\n" )
  165. sys.exit(0)
  166. ## ##
  167. ## cut-n-paste area ##
  168. ## ##
  169. for x in parm_table.keys():
  170. if def_values.has_key( x ):
  171. parm_table[x].append( def_values[x] )
  172. else:
  173. parm_table[x].append( "" )