PythonPackageInit.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. //
  3. // PythonPackageInit.pp
  4. //
  5. // This file is imported by Template.*.pp for a toplevel directory
  6. // that defines PYTHON_PACKAGE, to create the appropriate __init__.py
  7. // file for the toplevel of the package.
  8. //
  9. // Make sure that every subdirectory of src that contains an
  10. // __init__.py file also contains a Sources.pp file.
  11. #define initpys $[wildcard $[TOPDIR]/src/*/__init__.py]
  12. #foreach file $[initpys]
  13. #define dirname $[dir $[file]]
  14. #if $[not $[wildcard $[dirname]/Sources.pp]]
  15. #print Directory $[notdir $[standardize $[dirname]]] doesn't have a Sources.pp file!
  16. #endif
  17. #end file
  18. #output __init__.py
  19. # This file is generated by ppremake. Do not edit it directly; edit
  20. # dtool/pptempl/PythonPackageInit.pp instead if you need to make changes.
  21. """This file defines the path to the Python files within this package.
  22. There are two cases:
  23. (1) This is a source tree being run interactively by a developer, in
  24. which case the Python files are found in package/src/*/*.py. This
  25. case also breaks down into two sub-cases: (1a) we are using the
  26. ctattach tools, in which case we should look for the files in the
  27. actual source directory according to the ctattach variables, or
  28. (1b) we are not using the ctattach tools, in which case the files
  29. are right where we expect them to be.
  30. (2) This is an installed tree being run by an end-user, in which case
  31. the Python files are found in package/*/*.py. In this case, this
  32. file doesn't really need to be installed; an empty __init__.py
  33. file to define the package would serve just as well. But the file
  34. is crafted so that it will do no harm if it is installed.
  35. """
  36. package = '$[upcase $[PACKAGE]]'
  37. import os
  38. import sys
  39. def deCygwinify(path):
  40. if os.name in ['nt'] and path[0] == '/':
  41. # On Windows, we may need to convert from a Cygwin-style path
  42. # to a native Windows path.
  43. # Check for a case like /i/ or /p/: this converts
  44. # to i:/ or p:/.
  45. dirs = path.split('/')
  46. if len(dirs) > 2 and len(dirs[1]) == 1:
  47. path = '%s:\%s' % (dirs[1], '\\'.join(dirs[2:]))
  48. else:
  49. # Otherwise, prepend $PANDA_ROOT and flip the slashes.
  50. pandaRoot = os.getenv('PANDA_ROOT')
  51. if pandaRoot:
  52. path = os.path.normpath(pandaRoot + path)
  53. return path
  54. if os.getenv('CTPROJS'):
  55. # Ok, this is case (1a): we are using the ctattach tools, and
  56. # therefore will expect to find the source files in
  57. # $(package)/src/*/*.py. Completely replace the search path with
  58. # this path.
  59. tree = os.getenv(package)
  60. if not tree:
  61. raise StandardError, 'CTPROJS is defined, but you are not attached to %s!' % (package)
  62. tree = deCygwinify(tree)
  63. __path__[0] = os.path.join(tree, 'src')
  64. # Also make sure we import direct.showbase.FindCtaPaths, so we'll
  65. # be able to find all of the other attached projects.
  66. if package != 'DIRECT':
  67. tree = os.getenv('DIRECT')
  68. if not tree:
  69. raise StandardError,'CTPROJS is defined, but you are not attached to DIRECT!'
  70. tree = deCygwinify(tree)
  71. parent, base = os.path.split(tree)
  72. if parent not in sys.path:
  73. sys.path.append(parent)
  74. import direct.showbase.FindCtaPaths
  75. else:
  76. # We are not using the ctattach tools.
  77. srcDir = os.path.join(__path__[0], 'src')
  78. if os.path.isdir(srcDir):
  79. # The source directory exists; therefore, we are in case (1b).
  80. __path__[0] = srcDir
  81. else:
  82. # The source directory does not exist, so we must be in case
  83. # (2). Leave well enough alone.
  84. pass
  85. #end __init__.py