Exemple #1
0
    def _compile(self, filename, source):
        """ Compiles the Python source code to a code object and
        attempts to write it to an appropriate .pyc file. """
        
        if source and source[-1] != '\n':
            source = source + '\n'
        code = __builtin__.compile(source, filename.cStr(), 'exec')

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(pycExtension)
        try:
            f = open(pycFilename, 'wb')
        except IOError:
            pass
        else:
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', self.timestamp))
            f.write(marshal.dumps(code))
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()

        return code
Exemple #2
0
    def _compile(self, filename, source):
        """ Compiles the Python source code to a code object and
        attempts to write it to an appropriate .pyc file. """

        if source and source[-1] != '\n':
            source = source + '\n'
        code = __builtin__.compile(source, filename.cStr(), 'exec')

        # try to cache the compiled code
        pycFilename = Filename(filename)
        pycFilename.setExtension(pycExtension)
        try:
            f = open(pycFilename, 'wb')
        except IOError:
            pass
        else:
            f.write('\0\0\0\0')
            f.write(struct.pack('<I', self.timestamp))
            f.write(marshal.dumps(code))
            f.flush()
            f.seek(0, 0)
            f.write(imp.get_magic())
            f.close()

        return code
Exemple #3
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not. """

        if self.fileType == FTPythonCompiled:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            return None

        elif self.fileType == FTCompiledModule:
            return None

        # It's a .py file (or an __init__.py file; same thing).  Read
        # the .pyc file if it is available and current; otherwise read
        # the .py file and compile it.
        pycFilename = Filename(self.filename)
        pycFilename.setExtension(pycExtension)
        pycVfile = vfs.getFile(pycFilename, False)
        t_pyc = None
        if pycVfile:
            t_pyc = pycVfile.getTimestamp()

        code = None
        if t_pyc and t_pyc >= self.timestamp:
            code = self._loadPyc(pycVfile, self.timestamp)

        if not code:
            source = self._read_source()
            filename = Filename(self.filename)
            filename.setExtension('py')
            code = self._compile(filename, source)

        return code
Exemple #4
0
 def _read_source(self):
     """ Returns the Python source for this file, if it is
     available, or None if it is not. """
     
     if self.fileType == FTPythonCompiled or \
        self.fileType == FTCompiledModule:
         return None
     
     filename = Filename(self.filename)
     filename.setExtension('py')
     try:
         file = open(filename, 'rU')
     except IOError:
         return None
     return file.read()
Exemple #5
0
    def _read_source(self):
        """ Returns the Python source for this file, if it is
        available, or None if it is not. """

        if self.fileType == FTPythonCompiled or \
           self.fileType == FTCompiledModule:
            return None

        filename = Filename(self.filename)
        filename.setExtension('py')
        try:
            file = open(filename, 'rU')
        except IOError:
            return None
        return file.read()
Exemple #6
0
    def _import_compiled_module(self, fullname):
        """ Loads the compiled C/C++ shared object as a Python module,
        and returns it. """

        vfile = vfs.getFile(self.filename, False)

        # We can only import a compiled module if it already exists on
        # disk.  This means if it's a truly virtual file that has no
        # on-disk equivalent, we have to write it to a temporary file
        # first.
        if hasattr(vfile, 'getMount') and \
           isinstance(vfile.getMount(), VirtualFileMountSystem):
            # It's a real file.
            filename = self.filename
        else:
            # It's a virtual file.  Dump it.
            filename = Filename.temporary('', self.filename.getBasenameWoExtension(),
                                          '.' + self.filename.getExtension(),
                                          type = Filename.TDso)
            filename.setExtension(self.filename.getExtension())
            fin = open(vfile, 'rb')
            fout = open(filename, 'wb')
            data = fin.read(4096)
            while data:
                fout.write(data)
                data = fin.read(4096)
            fin.close()
            fout.close()

        module = imp.load_module(fullname, None, filename.toOsSpecific(),
                                 self.desc)
        module.__file__ = self.filename.cStr()
        return module
Exemple #7
0
    def _import_compiled_module(self, fullname):
        """ Loads the compiled C/C++ shared object as a Python module,
        and returns it. """

        vfile = vfs.getFile(self.filename, False)

        # We can only import a compiled module if it already exists on
        # disk.  This means if it's a truly virtual file that has no
        # on-disk equivalent, we have to write it to a temporary file
        # first.
        if hasattr(vfile, 'getMount') and \
           isinstance(vfile.getMount(), VirtualFileMountSystem):
            # It's a real file.
            filename = self.filename
        else:
            # It's a virtual file.  Dump it.
            filename = Filename.temporary(
                '',
                self.filename.getBasenameWoExtension(),
                '.' + self.filename.getExtension(),
                type=Filename.TDso)
            filename.setExtension(self.filename.getExtension())
            fin = open(vfile, 'rb')
            fout = open(filename, 'wb')
            data = fin.read(4096)
            while data:
                fout.write(data)
                data = fin.read(4096)
            fin.close()
            fout.close()

        module = imp.load_module(fullname, None, filename.toOsSpecific(),
                                 self.desc)
        module.__file__ = self.filename.cStr()
        return module
Exemple #8
0
 def loadParticleConfig(self, file):
     #Start of the code from steam.ptf
     self.p.cleanup()
     self.p = ParticleEffect()
     self.p.loadConfig(Filename(file))
     #Sets particles to birth relative to the teapot, but to render at toplevel
     self.p.start(self.t)
     self.p.setPos(3.000, 0.000, 2.250)
Exemple #9
0
    def _read_code(self):
        """ Returns the Python compiled code object for this file, if
        it is available, or None if it is not. """

        if self.fileType == FTPythonCompiled:
            # It's a pyc file; just read it directly.
            pycVfile = vfs.getFile(self.filename, False)
            if pycVfile:
                return self._loadPyc(pycVfile, None)
            return None

        elif self.fileType == FTCompiledModule:
            return None

        # It's a .py file (or an __init__.py file; same thing).  Read
        # the .pyc file if it is available and current; otherwise read
        # the .py file and compile it.
        pycFilename = Filename(self.filename)
        pycFilename.setExtension(pycExtension)
        pycVfile = vfs.getFile(pycFilename, False)
        t_pyc = None
        if pycVfile:
            t_pyc = pycVfile.getTimestamp()

        code = None
        if t_pyc and t_pyc >= self.timestamp:
            code = self._loadPyc(pycVfile, self.timestamp)

        if not code:
            source = self._read_source()
            filename = Filename(self.filename)
            filename.setExtension('py')
            code = self._compile(filename, source)

        return code
Exemple #10
0
    def readFavIcon(self):
        vfs = VirtualFileSystem.getGlobalPtr()
        filename = Filename('favicon.ico')

        searchPath = DSearchPath()
        searchPath.appendDirectory(Filename('.'))
        searchPath.appendDirectory(Filename('etc'))
        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('$DIRECT/src/http')))
        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('direct/src/http')))
        searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars('direct/http')))
        found = vfs.resolveFilename(filename,searchPath)
        if not found:
            raise "Couldn't find direct/http/favicon.ico"

        return vfs.readFile(filename, 1)
Exemple #11
0
 def __init__(self, path):
     self.dir_path = Filename.fromOsSpecific(path)
Exemple #12
0
 def getdata(self, path):
     path = Filename(self.dir_path, Filename.fromOsSpecific(path))
     f = open(path, 'rb')
     return f.read()
Exemple #13
0
    def find_module(self, fullname):
        basename = fullname.split('.')[-1]
        path = Filename(self.dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource)

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        filename = Filename(path)
        filename.setExtension(pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled)

        # Look for a compiled C/C++ module.
        for desc in imp.get_suffixes():
            if desc[2] != imp.C_EXTENSION:
                continue

            filename = Filename(path)
            filename.setExtension(desc[0][1:])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(self,
                                 vfile,
                                 filename,
                                 FTCompiledModule,
                                 desc=desc)

        # Finally, consider a package, i.e. a directory containing
        # __init__.py.
        filename = Filename(path, '__init__.py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self,
                             vfile,
                             filename,
                             FTPythonSource,
                             packagePath=path)
        filename = Filename(path, '__init__.' + pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self,
                             vfile,
                             filename,
                             FTPythonCompiled,
                             packagePath=path)

        return None
Exemple #14
0
 def __init__(self, path):
     self.dir_path = Filename.fromOsSpecific(path)
Exemple #15
0
    def find_module(self, fullname):
        basename = fullname.split('.')[-1]
        path = Filename(self.dir_path, basename)

        # First, look for Python files.
        filename = Filename(path)
        filename.setExtension('py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource)

        # If there's no .py file, but there's a .pyc file, load that
        # anyway.
        filename = Filename(path)
        filename.setExtension(pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled)

        # Look for a compiled C/C++ module.
        for desc in imp.get_suffixes():
            if desc[2] != imp.C_EXTENSION:
                continue
            
            filename = Filename(path)
            filename.setExtension(desc[0][1:])
            vfile = vfs.getFile(filename, True)
            if vfile:
                return VFSLoader(self, vfile, filename, FTCompiledModule,
                                 desc = desc)
        

        # Finally, consider a package, i.e. a directory containing
        # __init__.py.
        filename = Filename(path, '__init__.py')
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonSource,
                             packagePath = path)
        filename = Filename(path, '__init__.' + pycExtension)
        vfile = vfs.getFile(filename, True)
        if vfile:
            return VFSLoader(self, vfile, filename, FTPythonCompiled,
                             packagePath = path)

        return None
Exemple #16
0
 def getdata(self, path):
     path = Filename(self.dir_path, Filename.fromOsSpecific(path))
     f = open(path, 'rb')
     return f.read()
Exemple #17
0
from panda3d.pandac import PointLight, AmbientLight
from panda3d.pandac import LightRampAttrib, AuxBitplaneAttrib
from panda3d.pandac import CardMaker
from panda3d.pandac import Shader, Texture
from panda3d.pandac import Point3,Vec4,Vec3
from panda3d.direct.task.Task import Task
from panda3d.direct.actor.Actor import Actor
from panda3d.direct.gui.OnscreenText import OnscreenText
from panda3d.direct.showbase.DirectObject import DirectObject
from panda3d.direct.showbase.BufferViewer import BufferViewer
from panda3d.direct.filter.CommonFilters import CommonFilters
import sys,os

# Figure out what directory this program is in.
MYDIR=os.path.abspath(sys.path[0])
MYDIR=Filename.fromOsSpecific(MYDIR).getFullpath()

font = loader.loadFont("cmss12")

# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1), font = font,
                        pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1), font = font,
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

class ToonMaker(DirectObject):
    def __init__(self):
Exemple #18
0
 def loadShader(self, name):
     fn = os.path.join(PANDA_SHADER_PATH, name)
     fn = Filename.fromOsSpecific(fn)
     fn.makeTrueCase()
     return Shader.load(fn)
Exemple #19
0
def runPackedApp(args):
    if not args:
        raise ArgumentError, "No Panda app specified.  Use:\npython RunAppMF.py app.mf"

    vfs = VirtualFileSystem.getGlobalPtr()

    fname = Filename.fromOsSpecific(args[0])
    if not vfs.exists(fname):
        raise ArgumentError, "No such file: %s" % (args[0])

    mf = Multifile()
    if not mf.openRead(fname):
        raise ArgumentError, "Not a Panda Multifile: %s" % (args[0])

    # Clear *all* the mount points, including "/", so that we no
    # longer access the disk directly.
    vfs.unmountAll()

    # Mount the Multifile under /mf, by convention, and make that our
    # "current directory".
    vfs.mount(mf, MultifileRoot, vfs.MFReadOnly)
    vfs.chdir(MultifileRoot)

    # Make sure the directories on our standard Python path are mounted
    # read-only, so we can still load Python.
    for dirname in sys.path:
        vfs.mount(dirname, dirname, vfs.MFReadOnly)

    # Also mount some standard directories read-write (temporary and
    # app-data directories).
    tdir = Filename.temporary('', '')
    for dirname in set([ tdir.getDirname(),
                         Filename.getTempDirectory().cStr(),
                         Filename.getUserAppdataDirectory().cStr(),
                         Filename.getCommonAppdataDirectory().cStr() ]):
        vfs.mount(dirname, dirname, 0)

    # Now set up Python to import this stuff.
    VFSImporter.register()
    sys.path = [ MultifileRoot ] + sys.path

    # Put our root directory on the model-path and prc-path, too.
    getModelPath().prependDirectory(MultifileRoot)

    # Load the implicit App.prc file.
    loadPrcFileData(AppPrcFilename, AppPrc)

    # Load any prc files in the root.  We have to load them
    # explicitly, since the ConfigPageManager can't directly look
    # inside the vfs.
    for f in vfs.scanDirectory(MultifileRoot):
        if f.getFilename().getExtension() == 'prc':
            data = f.readFile(True)
            loadPrcFileData(f.getFilename().cStr(), data)

    # Replace the builtin open and file symbols so user code will get
    # our versions by default, which can open and read files out of
    # the multifile.
    __builtin__.file = file.file
    __builtin__.open = file.open
    os.listdir = file.listdir
    os.walk = file.walk

    import main
    if hasattr(main, 'main') and callable(main.main):
        main.main()
Exemple #20
0
from panda3d.pandac import PointLight, AmbientLight
from panda3d.pandac import LightRampAttrib, AuxBitplaneAttrib
from panda3d.pandac import CardMaker
from panda3d.pandac import Shader, Texture
from panda3d.pandac import Point3, Vec4, Vec3
from panda3d.direct.task.Task import Task
from panda3d.direct.actor.Actor import Actor
from panda3d.direct.gui.OnscreenText import OnscreenText
from panda3d.direct.showbase.DirectObject import DirectObject
from panda3d.direct.showbase.BufferViewer import BufferViewer
from panda3d.direct.filter.CommonFilters import CommonFilters
import sys, os

# Figure out what directory this program is in.
MYDIR = os.path.abspath(sys.path[0])
MYDIR = Filename.fromOsSpecific(MYDIR).getFullpath()

font = loader.loadFont("cmss12")


# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg,
                        style=1,
                        fg=(1, 1, 1, 1),
                        font=font,
                        pos=(-1.3, pos),
                        align=TextNode.ALeft,
                        scale=.05)

Exemple #21
0
 def loadShader(self, name):
     fn = os.path.join(PANDA_SHADER_PATH, name)
     fn = Filename.fromOsSpecific(fn)
     fn.makeTrueCase()
     return Shader.load(fn)