generatePythonCode 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/local/bin/python
  2. import getopt
  3. import sys
  4. import os
  5. import FFIConstants
  6. # Define a help string for the user
  7. helpString ="""
  8. generatePythonCode [opts] -i libtool libcode1 libcode2 ...
  9. Generates Python code for the C++ libraries listed.
  10. Example:
  11. generatePythonCode -v -d $DIRECT/lib/py -e $DIRECT/src/all/python -i libdtool libpandaexpress libpanda libdirect
  12. Options:
  13. -h print this message
  14. -v verbose
  15. -d dir directory to write output code
  16. -e dir directory to pull extension code from
  17. -i lib interrogate library
  18. -O no C++ comments or assertion statements
  19. """
  20. # Initialize variables
  21. outputDir = ''
  22. extensionsDir = ''
  23. interrogateLib = ''
  24. codeLibs = []
  25. # Extract the args the user passed in
  26. try:
  27. opts, pargs = getopt.getopt(sys.argv[1:], 'hvOd:e:i:')
  28. except Exception, e:
  29. # User passed in a bad option, print the error and the help, then exit
  30. print e
  31. print helpString
  32. sys.exit()
  33. if len(opts)==0:
  34. print helpString
  35. sys.exit()
  36. # Store the option values into our variables
  37. for opt in opts:
  38. flag, value = opt
  39. if (flag == '-h'):
  40. print helpString
  41. sys.exit()
  42. elif (flag == '-v'):
  43. FFIConstants.notify.setVerbose(1)
  44. elif (flag == '-d'):
  45. outputDir = value
  46. elif (flag == '-e'):
  47. extensionsDir = value
  48. elif (flag == '-i'):
  49. interrogateLib = value
  50. elif (flag == '-O'):
  51. FFIConstants.wantComments = 0
  52. FFIConstants.wantTypeChecking = 0
  53. else:
  54. FFIConstants.notify.error('illegal option: ' + flag)
  55. # Store the program arguments into the codeLibs
  56. codeLibs = pargs
  57. # Now do some error checking and verbose output
  58. if (not interrogateLib):
  59. FFIConstants.notify.error('You must specify an interrogate library (-i lib)')
  60. else:
  61. FFIConstants.notify.info('Setting interrogate library to: ' + interrogateLib)
  62. FFIConstants.InterrogateModuleName = interrogateLib
  63. if (not outputDir):
  64. FFIConstants.notify.info('Setting output directory to current directory')
  65. outputDir = '.'
  66. elif (not os.path.exists(outputDir)):
  67. FFIConstants.notify.error('Directory does not exists: ' + outputDir)
  68. else:
  69. FFIConstants.notify.info('Setting output directory to: ' + outputDir)
  70. if (not extensionsDir):
  71. FFIConstants.notify.info('Setting extensions directory to current directory')
  72. extensionsDir = '.'
  73. elif (not os.path.exists(extensionsDir)):
  74. FFIConstants.notify.error('Directory does not exists: ' + extensionsDir)
  75. else:
  76. FFIConstants.notify.info('Setting extensions directory to: ' + extensionsDir)
  77. if (not codeLibs):
  78. FFIConstants.notify.error('You must specify one or more libraries to generate code from')
  79. else:
  80. FFIConstants.notify.info('Generating code for: ' + `codeLibs`)
  81. FFIConstants.CodeModuleNameList = codeLibs
  82. # Ok, now we can start generating code
  83. import FFIInterrogateDatabase
  84. db = FFIInterrogateDatabase.FFIInterrogateDatabase()
  85. db.updateBindings()
  86. db.generateCode(outputDir, extensionsDir)