def out_translate(platform, file_names, out_file, module_name, translator_args, incremental): do_translate = False # flag for incremental translate mode if translator_args.get('list_imports', None): do_translate = True # see if we can skip this module elif incremental: # if we are in incremental translate mode # check for any files that need built for file_name in file_names: if is_modified(file_name, out_file): if platform is not None: platform = "[%s] " % platform else: platform = '' print("Translating file %s:" % platform, file_name) do_translate = True break if not incremental or do_translate: pydir = os.path.abspath(os.path.dirname(__file__)) if not 'PYJS_SYSPATH' in os.environ: os.environ['PYJS_SYSPATH'] = sys.path[0] opts = ["--module-name", module_name, "-o"] if sys.platform == 'win32': opts.append(out_file) shell = False else: file_names = map(lambda x: x.replace(" ", r"\ "), file_names) opts.append(out_file.replace(" ", r"\ ")) shell = True opts += get_translator_opts(translator_args) + file_names opts = [pyjs.PYTHON] + [os.path.join(pydir, translate_cmd) ] + translate_cmd_opts + opts pyjscompile_cmd = '"%s"' % '" "'.join(opts) proc = subprocess.Popen(pyjscompile_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell, cwd=pydir, env=os.environ) stdout_value, ret = proc.communicate('')[0], proc.returncode if ret: raise translator.TranslationError( 'general fail in translator process') if translator_args.get('list_imports', None): print("List Imports %s:" % platform, file_names) print(stdout_value) return [], [] deps, js_libs = parse_outfile(out_file) # use this to create dependencies for Makefiles. maybe. #print "translate", out_file, deps, js_libs, stdout_value return deps, js_libs
def __call__(self): try: self.visited_modules = {} self.done = {} self.dependencies = {} self.visit_start() for platform in [None] + self.platforms: self.visit_start_platform(platform) old_path = self.path self.path = [BUILTIN_PATH, PYLIB_PATH, PYJAMASLIB_PATH] self.visit_modules([builtin_module], platform) self.path = old_path self.visit_modules(self.modules, platform) if not self.list_imports: self.visit_end_platform(platform) if not self.list_imports: self.visit_end() except translator.TranslationError(e): raise
def __init__(self, compiler, module_name, module_file_name, src, mod, output, dynamic=0, findFile=None, **kw): self.compiler = compiler self.ast = compiler.ast self.js_module_name = self.jsname("variable", module_name) if module_name: self.module_prefix = "$module." else: self.module_prefix = "" self.module_name = module_name src = src.replace("\r\n", "\n") src = src.replace("\n\r", "\n") src = src.replace("\r", "\n") self.src = src.split("\n") self.output = output self.dynamic = dynamic self.findFile = findFile self.set_compile_options(kw) self.future_division = False self.imported_modules = [] self.imported_js = [] self.is_class_definition = False self.local_prefix = None self.track_lines = {} self.stacksize_depth = 0 self.option_stack = [] self.lookup_stack = [{}] self.indent_level = 0 self.__unique_ids__ = {} self.try_depth = -1 self.is_generator = False self.generator_states = [] self.state_max_depth = len(self.generator_states) self.constant_int = {} self.constant_long = {} self.top_level = True translator.PYJSLIB_BUILTIN_MAPPING['__file__'] = "'%s'" % module_file_name self.import_context = "null" self.w(self.indent() + "runinteractive(function (ctxt) {") self.w(self.spacing() + "var $module = ctxt;") self.w(self.spacing() + "var __name__ = ctxt.__name__;") self.w(self.spacing() + "var %s = ctxt;" % self.js_module_name) if self.attribute_checking and not module_name in ['sys', 'pyjslib']: attribute_checking = True self.w( self.indent() + 'try {') else: attribute_checking = False save_output = self.output self.output = StringIO() mod.lineno = 1 self.track_lineno(mod, True) for child in mod.node: self.has_js_return = False self.has_yield = False self.is_generator = False self.track_lineno(child) assert self.top_level if isinstance(child, self.ast.Function): self._function(child, None) elif isinstance(child, self.ast.Class): self._class(child) elif isinstance(child, self.ast.Import): self._import(child, None, True) elif isinstance(child, self.ast.From): self._from(child, None, True) elif isinstance(child, self.ast.Discard): self._discard(child, None) elif isinstance(child, self.ast.Assign): self._assign(child, None) elif isinstance(child, self.ast.AugAssign): self._augassign(child, None) elif isinstance(child, self.ast.If): self._if(child, None) elif isinstance(child, self.ast.For): self._for(child, None) elif isinstance(child, self.ast.While): self._while(child, None) elif isinstance(child, self.ast.Subscript): self._subscript_stmt(child, None) elif isinstance(child, self.ast.Global): self._global(child, None) elif isinstance(child, self.ast.Printnl): self._print(child, None) elif isinstance(child, self.ast.Print): self._print(child, None) elif isinstance(child, self.ast.TryExcept): self._tryExcept(child, None) elif isinstance(child, self.ast.TryFinally): self._tryFinally(child, None) elif isinstance(child, self.ast.Raise): self._raise(child, None) elif isinstance(child, self.ast.Stmt): self._stmt(child, None, True) elif isinstance(child, self.ast.With): self._with(child, None) elif isinstance(child, self.ast.AssAttr): self._assattr(child, None) elif isinstance(child, self.ast.AssName): self._assname(child, None) elif isinstance(child, self.ast.AssTuple): for node in child.nodes: self._stmt(node, None) elif isinstance(child, self.ast.Slice): self.w( self.spacing() + self._slice(child, None)) else: raise translator.TranslationError( "unsupported type (in __init__)", child, self.module_name) captured_output = self.output.getvalue() self.output = save_output if self.source_tracking and self.store_source: for l in self.track_lines.keys(): self.w( self.spacing() + '''%s.__track_lines__[%d] = "%s";''' % (self.js_module_name, l, self.track_lines[l].replace('"', '\"')), translate=False) self.w( self.local_js_vars_decl([])) if captured_output.find("@CONSTANT_DECLARATION@") >= 0: captured_output = captured_output.replace("@CONSTANT_DECLARATION@", self.constant_decl()) else: self.w( self.constant_decl()) if captured_output.find("@ATTRIB_REMAP_DECLARATION@") >= 0: captured_output = captured_output.replace("@ATTRIB_REMAP_DECLARATION@", self.attrib_remap_decl()) self.w( captured_output, False) if attribute_checking: self.w( self.dedent() + "} catch ($pyjs_attr_err) {throw @{{_errorMapping}}($pyjs_attr_err);};") self.w( self.dedent() + "}, __main__);")