generatePythonCode 3.4 KB

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