/release/src/router/samba3/examples/scripts/shares/python/generate_parm_table.py

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