def startJob(self, body): project = json.loads(body) os.mkdir(self.job_dir) # sprit_id = project['sprite_idx'] block_id = project['block_idx'] # Write uploaded program xml = os.path.join(self.job_dir, 'job.xml') with open(xml, 'w') as file: file.write(project['project'].encode('utf-8')) # Parse and write python program p = parser.parses(project['project'].encode('utf-8')) ctx = p.create_context() file_ast = p.to_ast(ctx, 'main_%s' % block_id) code = astor.to_source(file_ast) program = os.path.join(self.job_dir, 'job.py') with open(program, 'w') as file: file.write(code) self.job_process = JobProcess(self, self.id) reactor.spawnProcess( self.job_process, sys.executable, [sys.executable, program], env=os.environ)
def test_parse(self): snap_stdlib.cleanReport() if self.script: document = self.xml.format( script=self.script, block="".join(self.blocks) or self.block) else: document = self.xml parser = snap_parser.parses(document) ctx = parser.create_context() script = parser.to_ast(ctx) ast.fix_missing_locations(script) try: code = compile(script, '<string>', 'exec') module = imp.new_module(__name__ + '.compiled_block') exec code in module.__dict__ module.main_0() if self.report: self.assertEqual( module.stdlib._report, self.report, "%s != %s\ncode::\n\n%s" % (module.stdlib._report, self.report, astor.to_source(script))) if self.vars: self.assertEqual( module._globals, self.vars, "%s != %s\ncode::\n\n%s" % (module._globals, self.vars, astor.to_source(script))) except: print "Generated AST object\n", ast.dump(script) parsed = ast.parse(astor.to_source(script)) print "Parsed AST object\n", ast.dump(parsed) print "Script\n", astor.to_source(script) raise self.assertTrue(len(parser.stack) == 0, parser.stack)