def _require_c(filename, options): def _get(opt, k, defval): if hasattr(opt, k): return getattr(opt, k) return defval tool = _get(options, 'c_tool', 'PyInline') library_dirs = _get(options, 'libdirs', None) libraries = _get(options, 'libs', None) includes = _get(options, 'includes', None) if tool == 'PyInline': extras = {} if library_dirs: extras['library_dirs'] = library_dirs if libraries: extras['libraries'] = libraries if includes: if os.name == 'posix': os.environ['GCC_INCLUDE_DIR'] = ':'.join(includes) else: os.environ['GCC_INCLUDE_DIR'] = ';'.join(includes) mod = PyV8.JSClass() data = _file_data(filename) if data: PyInline.build(code=data, targetmodule=mod, language="C", **extras) return mod else: raise RequireError, "Unable to locate or read %s" % filename elif tool == 'ctypesgen': cmd = "ctypesgen.py %s " % filename if library_dirs: for x in library_dirs: cmd += " --libdir=%s " % x if libraries: for x in libraries: cmd += " --library=%s " % x if includes: for x in includes: cmd += " --includedir=%s " % x modname = filename.split(os.sep)[-1].split('.')[0] modfile = CTYPES_MODULES_PATH + "/%s.py" % modname cmd += " -o %s " % modfile logging.info(cmd) os.system(cmd) m = __import__(modname) return m
def unittest(): options = PyV8.JSClass() """ try: m = require( "../../test/data/bad" , options ) except: print "got expected error", sys.exc_info() options.language = "python" m = require("../../test/data/junk.py", options ) assert 11 == m.pyfunc(10) m = require("os", options) assert '.' == m.curdir options.language = "c" m = require("../../test/data/test.c",options) print m.my_add( 10, 20.0 ) print "JsCode:", print JsCode """ """
def _require_js(filename, options): # check search path for global RequirePath if filename[-3:] != ".js": filename += ".js" if filename[0] == '/': # Then this is an absolute path .. pathname = filename data = _file_data(pathname) if not data: raise RequireError, \ "Path %s either does not exist or not readable" % pathname else: data = None # search for filename within predefined search path for path in RequirePath: pathname = path + filename data = _file_data(pathname) if data: break # terminate search if data: # evaluate module and move variables/functions to a # object named the same as the file without the extension. # So if fubar.js contains foo(), then we define # a global object fubar and add foo as a member # like this: fubar.foo # local context used as a sandbox for evaluating modules. with PyV8.JSContext(jsapi.HandlerAPI()) as context: # inject 'module' object into javascript. context.locals.module = { 'exports': {}, 'id': filename, 'filename': filename, 'loaded': False, 'parent': None, 'children': [] } prev_namespace = set(dir(context.locals)) try: # execute javascript code in module. context.eval(data) except: et, ev, e_tb = sys.exc_info() msg = "[%s]\n\t %s" % (pathname, ev) raise RequireError, msg mod = PyV8.JSClass() if len(context.locals.module.exports) > 0: for (n, v) in dict(context.locals.module.exports).items(): setattr(mod, n, v) else: curr_namespace = set(dir(context.locals)) for n in list(curr_namespace - prev_namespace): v = getattr(context.locals, n) setattr(mod, n, v) delattr(context.locals, n) return mod else: msgfmt = "Unable to find %s or was not readable in any search path %s" msg = msgfmt % (filename, str(RequirePath)) raise RequireError, msg