eggcacher.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ##############################################################################
  2. #
  3. # eggcacher
  4. #
  5. # EggCacher searches a directory for egg files, and loads
  6. # them all into the model-cache. This is used as part of the
  7. # panda installation process.
  8. #
  9. ##############################################################################
  10. import os
  11. import sys
  12. import gc
  13. from panda3d.core import *
  14. class EggCacher:
  15. def __init__(self, args):
  16. maindir = Filename.fromOsSpecific(os.getcwd()).getFullpath()
  17. ExecutionEnvironment.setEnvironmentVariable("MAIN_DIR", maindir)
  18. self.bamcache = BamCache.getGlobalPtr()
  19. self.pandaloader = Loader()
  20. self.loaderopts = LoaderOptions(LoaderOptions.LF_no_ram_cache)
  21. if not self.bamcache.getActive():
  22. print("The model cache is not currently active.")
  23. print("You must set a model-cache-dir in your config file.")
  24. sys.exit(1)
  25. self.parseArgs(args)
  26. files = self.scanPaths(self.paths)
  27. self.processFiles(files)
  28. def parseArgs(self, args):
  29. self.concise = 0
  30. self.pzkeep = 0
  31. while len(args) > 0:
  32. if args[0] == "--concise":
  33. self.concise = 1
  34. args = args[1:]
  35. elif args[0] == "--pzkeep":
  36. self.pzkeep = 1
  37. args = args[1:]
  38. else:
  39. break
  40. if len(args) < 1:
  41. print("Usage: eggcacher options file-or-directory")
  42. sys.exit(1)
  43. self.paths = args
  44. def scanPath(self, eggs, path):
  45. if not os.path.exists(path):
  46. print("No such file or directory: " + path)
  47. return
  48. if os.path.isdir(path):
  49. for f in os.listdir(path):
  50. self.scanPath(eggs, os.path.join(path,f))
  51. return
  52. if path.endswith(".egg"):
  53. size = os.path.getsize(path)
  54. eggs.append((path,size))
  55. return
  56. if path.endswith(".egg.pz") or path.endswith(".egg.gz"):
  57. size = os.path.getsize(path)
  58. if self.pzkeep:
  59. eggs.append((path, size))
  60. else:
  61. eggs.append((path[:-3], size))
  62. def scanPaths(self, paths):
  63. eggs = []
  64. for path in paths:
  65. #abs = os.path.abspath(path)
  66. self.scanPath(eggs, path)
  67. return eggs
  68. def processFiles(self, files):
  69. total = 0
  70. for (path, size) in files:
  71. total += size
  72. progress = 0
  73. for (path, size) in files:
  74. fn = Filename.fromOsSpecific(path)
  75. cached = self.bamcache.lookup(fn, "bam")
  76. percent = (progress * 100) / total
  77. report = path
  78. if self.concise:
  79. report = os.path.basename(report)
  80. print("Preprocessing Models %2d%% %s" % (percent, report))
  81. sys.stdout.flush()
  82. if cached and not cached.hasData():
  83. self.pandaloader.loadSync(fn, self.loaderopts)
  84. gc.collect()
  85. ModelPool.releaseAllModels()
  86. TexturePool.releaseAllTextures()
  87. progress += size
  88. def main(args=None):
  89. if args is None:
  90. args = sys.argv[1:]
  91. cacher = EggCacher(args)
  92. return 0
  93. if __name__ == '__main__':
  94. sys.exit(main())