def createAndCompile(self, source): fd, sourcefile = tempfile.mkstemp() os.close(fd) os.remove(sourcefile) sourcefile = sourcefile.replace('-', '_') if PY2: fd = open('%s.tmpl' % sourcefile, 'w') else: fd = open('%s.tmpl' % sourcefile, 'w', encoding='utf-8') fd.write(source) fd.close() wrap = CheetahWrapper.CheetahWrapper() wrap.main([ 'cheetah', 'compile', '--encoding=utf-8', '--settings=encoding="utf-8"', '--quiet', '--nobackup', sourcefile ]) module_name = os.path.split(sourcefile)[1] module = load_module_from_file(module_name, module_name, sourcefile + '.py') template = getattr(module, module_name) os.remove('%s.tmpl' % sourcefile) for sourcefile_py in glob('%s.py*' % sourcefile): # *.py[co] os.remove(sourcefile_py) __pycache__ = os.path.join(os.path.dirname(sourcefile), '__pycache__') if os.path.exists(__pycache__): # PY3 rmtree(__pycache__) return template
def getmod(self, nm): stuff = self.map.get(nm) if stuff: fnm = stuff[0] return load_module_from_file(nm, nm, fnm) return None
def getmod(self, nm, getsuffixes=get_suffixes, loadco=marshal.loads, newmod=new_module): pth = _os_path_join(self.path, nm) possibles = [(pth, 0, None)] if pathIsDir(pth): possibles.insert(0, (_os_path_join(pth, '__init__'), 1, pth)) py = pyc = None for pth, ispkg, pkgpth in possibles: for ext, mode, typ in getsuffixes(): attempt = pth + ext try: st = _os_stat(attempt) except Exception: pass else: if typ == 3: # imp.C_EXTENSION return load_module_from_file(nm, nm, attempt) elif typ == 1: # imp.PY_SOURCE py = (attempt, st) else: pyc = (attempt, st) if py or pyc: break if py is None and pyc is None: return None while True: if pyc is None or py and pyc[1][8] < py[1][8]: try: with open(py[0], 'r') as py_code_file: py_code = py_code_file.read() co = compile(py_code + '\n', py[0], 'exec') try: py_compile.compile(py[0]) except IOError: pass __file__ = py[0] break except SyntaxError as e: print("Invalid syntax in %s" % py[0]) print(e.args) raise elif pyc: with open(pyc[0], 'rb') as pyc_file: stuff = pyc_file.read() try: co = loadco(stuff[8:]) __file__ = pyc[0] break except (ValueError, EOFError): pyc = None else: return None mod = newmod(nm) mod.__file__ = __file__ if ispkg: mod.__path__ = [pkgpth] subimporter = PathImportDirector(mod.__path__) mod.__importsub__ = subimporter.getmod mod.__co__ = co return mod