def check_source_code(source_code): res = '' errors = [] # aeval = asteval.Interpreter() # try: # aeval.parse(source_code) # except Exception as e: # msg = check_ast_errors(aeval.error) # print 'ast:', msg # errors.append(msg) tmp_file = NamedTemporaryFile() try: tmp_file.write(source_code) tmp_file.flush() sp = Popen(['pylint', '--errors-only', '--msg-template="{line}: {msg} ({symbol})"', '--disable=E0602', tmp_file.name], stdout=PIPE, stderr=PIPE) out, err = sp.communicate() # from pylint import epylint as lint # cmd = "%s --msg-template='{line:3d},{column:2d}: {msg} ({symbol})'" % (tmp_file.name,) # print cmd # (stdout, stderr) = lint.py_run(cmd, True) # out = stdout.read() # err = stderr.read() if out: # errors.append(out) errors.append('\n'.join(out.split('\n')[1:])) # Remove first line of stdout if err: errors.append(err) finally: tmp_file.close() if not errors: # aeval = asteval.Interpreter() # res = aeval(source_code) importCode(source_code, 'player') # wrapper_path = os.path.join(os.path.dirname(__file__), 'eval_wrapper.py') # sp = Popen(['python', wrapper_path, '-c', source_code], stdout=PIPE) # res = sp.communicate()[0] # sp = Popen(['python', '-c', source_code], stdout=PIPE) # if 'decide' not in aeval.symtable or not isinstance(aeval.symtable['decide'], asteval.asteval.Procedure): # errors.append('The `decide(context)` function must be implemented\n') # elif len(aeval.symtable['decide'].argnames) != 1: # errors.append('The `decide(context)` function must accept a context argument\n') output = { 'success': not bool(errors), 'output': res, 'errors': ''.join(errors) } return output
def _r(self, args): """ esegue un processo senza fornirgli input e restituisce stdout+stderr e l'exit code. """ p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) out, err = p.communicate() if p.returncode != 0: print "ERROR executing:", " ".join(args) return out, p.returncode
def checkIsSVNRepository(env=None, path=None): if env is None: env = os.environ.copy() env["LC_MESSAGES"] = "C" args = ["svn", "info"] if path is not None: args.append(path) svninfo = Popen(args, stdout=PIPE, env=env) svninfo.wait() return not bool(svninfo.returncode)
def executeCommand(arg, shell=False): env = os.environ.copy() env["LC_MESSAGES"] = "C" execution = Popen(arg, stdout=PIPE, env=env, shell=shell) execution.wait() if execution.returncode: return None resp = "" for L in execution.stdout: L = L.strip() resp += L + "\n" return resp.strip()
def getRelativePath(): # Usa svn info per estrarre il path relativo alla root del repositorio in # cui si trova il programma. env = os.environ.copy() env["LC_MESSAGES"] = "C" info = {} # Prova a cercare le informazioni con svn, se non è un repository svn prova # con git svn if not checkIsSVNRepository(env): shell = sys.platform.startswith("win") svninfo = Popen(["git", "svn", "info"], stdout=PIPE, env=env, shell=shell) else: svninfo = Popen(["svn", "info"], stdout=PIPE, env=env) svninfo.wait() for L in svninfo.stdout: L = L.strip() if L: k, v = L.split(": ", 1) info[k] = v assert info["URL"].startswith(info["Repository Root"]) return info["URL"][len(info["Repository Root"]) :]