eggcacher.py 3.0 KB

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