def join_check(user_id, first_name, last_name, username): if was_here(user_id) is not True: try: write(user_id=user_id, first_name=first_name, last_name=last_name, user_name=username) return True except Exception as identifier: return identifier
def make (): print 'Creating {} source file for problem {}'.format(settings.langexts[globals.getext()], globals.getprob()) template = helper.read(os.path.join(globals.templatedir, globals.getmode() + '.' + globals.getext())) if template is None: print '{}.{} template file not found (add it to templates folder)'.format(globals.getmode(), globals.getext()) return False helper.write(os.path.join(globals.working, globals.getsrc()), pattern.convert(template)) print 'Source file created: ' + globals.getsrc() return True
def parse(self): n = reader.readParse(self.file) print(n.grammer) first = create_first.First(n.grammer) helper.write(first.first_rules, "Firsts") follow = create_follow.Follow(first.rules_by_non_terminal, first.first_rules) helper.write(follow.follow_rules, "Follows") pt = parsing_table.ParsingTable(first.first_rules, follow.follow_rules, first.rules_by_non_terminal) helper.write_parsing_table(pt.parsing_table, pt.expressions)
def get(self, param): if not self.logged(): return error("You not logged") try: if type(param) is int: return write(self._data.get(param)) elif type(param) is unicode: return write(self._data.get(param)) else: return write(self._data.get(session["email"])) except: return error("Invalid request")
def addmsg(msg, system=True): logstr = helper.read(globals.logfile) if logstr is None: return False lines = logstr.split('\n') time = (datetime.datetime.now() - datetime.datetime.strptime(lines[1].split('started ')[1], formatstr)) timestr = '{:d}:{:02d}'.format(time.seconds / 3600, (time.seconds % 3600) / 60) logstr += '> {:>5} - {:>4}: {}'.format(timestr, 'pcu' if system else 'user', msg) if not msg.endswith('\n'): logstr += '\n' helper.write(globals.logfile, logstr) return True
def login(self): if self.logged(): return write("Well done") email = request.values.get("email") password = request.values.get("password") if email is not None and \ password is not None and \ self._data.valid(email, sha512(password)): session["email"] = email return write("Well done") else: return error("Incorrect login or password")
def download(self,url,keyword): numtries = 0 while True: numtries += 1 if numtries >= 2: #get new ip if one exists self.getNewIp() numtries = 1 curtime = time.time() diftime = curtime - self.iptime; #if (self.iptime != 0) and (diftime < WAITTIME): #wait 5 to 6 seconds in total, before using the same proxy # gevent.sleep(random.uniform(WAITTIME-diftime,WAITTIME+1-diftime)) try: r = bytearray() text = "" with Timeout(TIMEOUT,False): #just use server ip instead of having to search for a working proxy ip #to do: fix this... if self.workLoad == 1: r = requests.get(url) else: r = requests.get(url, proxies={"http":self.curip}) text = r.text if not (len(text)): raise Exception("too slow") if "You have exceeded the maximum allowed page request rate for this website." in text: raise Exception("maximum allowed requests") #4xx or 5xx response error raised r.raise_for_status() #sometimes using proxies causes a different page to load than the one you want, search for keyword, title m_obj = re.search(keyword, text , re.IGNORECASE) if m_obj is None: helper.write(r.text,self.curip.replace(".","")) raise Exception(keyword+" not found at "+url) except requests.ConnectionError: print "connection error" except requests.HTTPError: #stop scraping, the page cannot be loaded raise Exception("invalid http response"+ str(r.status_code) +" on "+ url) except requests.Timeout: print "timeout" except requests.TooManyRedirects: print "too many redirects" except Exception, e: print e else: return r finally:
def make(): print 'Creating {} source file for problem {}'.format( settings.langexts[globals.getext()], globals.getprob()) template = helper.read( os.path.join(globals.templatedir, globals.getmode() + '.' + globals.getext())) if template is None: print '{}.{} template file not found (add it to templates folder)'.format( globals.getmode(), globals.getext()) return False helper.write(os.path.join(globals.working, globals.getsrc()), pattern.convert(template)) print 'Source file created: ' + globals.getsrc() log.addmsg('Creating {} source file for problem {}'.format( settings.langexts[globals.getext()], globals.getprob())) return True
def register(self): if self.logged(): return error("You are already logged") email = request.values.get("email") password = request.values.get("password") fname = request.values.get("fname") lname = request.values.get("lname") role = request.values.get("role") avatar = request.values.get("avatar") status = request.values.get("status") if (not (type(email) is unicode)) or (len(email) < 5): return error("Incorrect e-mail") if (not (type(password) is unicode)) or (len(password) < 5): return error("Incorrect password") if self._data.exists(email): return error("User with this e-mail already exists") try: self._data.add({"email": email, "password": sha512(password), "fname": fname, "lname": lname, "role": role, "avatar": avatar, "status": status}) return write("Well done") except: return error("Registration failed")
def write_back(): if setup.flag == 0: if setup.op_code != 10: setup.registers[setup.destination] = setup.result print("WRITEBACK: write %d to R%d \n" % (setup.result, setup.destination)) helper.write_to_out("WRITEBACK: write "+str(setup.result)+" to R"+str(setup.destination)+" \n") setup.gui.editor("WRITEBACK: write "+str(setup.result)+" to R"+str(setup.destination)+" \n") else: print("WRITEBACK: no writeback required \n") helper.write_to_out("WRITEBACK: no writeback required \n") setup.gui.editor("WRITEBACK: no writeback required \n") elif setup.flag == 1: if setup.op_code == 25: setup.registers[setup.destination] = setup.result print("WRITEBACK: write %d to R%d \n" % (setup.result, setup.destination)) helper.write_to_out("WRITEBACK: write "+str(setup.result)+" to R"+str(setup.destination)+" \n") setup.gui.editor("WRITEBACK: write "+str(setup.result)+" to R"+str(setup.destination)+" \n") elif setup.op_code == 24: k = setup.secondOperand//4 setup.temp.get(setup.firstOperand)[k] = setup.registers[setup.destination] print("WRITEBACK: write %d to memory array" % setup.temp.get(setup.firstOperand)[k]) helper.write_to_out("WRITEBACK: write "+str(setup.temp.get(setup.firstOperand)[k])+" to memory array \n") setup.gui.editor("WRITEBACK: write "+str(setup.temp.get(setup.firstOperand)[k])+" to memory array \n") elif setup.flag == 2: print("WRITEBACK: No writeback operation required \n") helper.write_to_out("WRITEBACK: No writeback operation required \n") setup.gui.editor("WRITEBACK: No writeback operation required \n") elif setup.flag == 3: mask = int(setup.inst[4:], 0) & 0xFF if mask == 0x11: print("EXIT: \n") helper.write_to_out("EXIT: \n") setup.gui.editor("EXIT: \n") elif mask == 0x6C: helper.read() print("WRITEBACK: %s is Read from Console and stored in Register 0" % str(setup.registers[0])) helper.write_to_out("WRITEBACK: %s is Read from File and stored in Register 0" % str(setup.registers[0])) setup.gui.editor("WRITEBACK: %s is Read from File and stored in Register 0" % str(setup.registers[0])) elif mask == 0x6B: helper.write() print("WRITEBACK: %s is Written to Console from Register 1" % str(setup.registers[1])) helper.write_to_out("WRITEBACK: %s is Written to Console from Register 1" % str(setup.registers[1])) setup.gui.editor("WRITEBACK: %s is Written to Console from Register 1" % str(setup.registers[1])) setup.gui.registers()
def setfile (ext): file = globals.probsettings['filename'] type = exttotype[ext] testid = globals.gettestid() print 'Setting test case {} {} for problem {}'.format(testid, type, globals.getprob()) input = '' if file is None: print 'Enter {} below (Ctrl-{} when finished)'.format(type, 'Z' if helper.iswindows() else 'D') print settings.smalldivider input = sys.stdin.read() print settings.smalldivider else: print 'Retrieving from ' + file.name input = file.read() helper.write(os.path.join(globals.probdir, testid + ext), input) print 'Set {} successfully'.format(exttotype[ext]) return True
def all(self): if not self.logged(): return error("You not logged") try: return write(self._data.all()) except: return error("Invalid request")
def delete(self): if not self.logged(): return error("You not logged") try: id = int(request.values.get("id")) self._data.delete(id) return write("Well done") except: return error("Invalid request")
def setfile(ext): file = globals.probsettings['filename'] type = exttotype[ext] testid = globals.gettestid() print 'Setting test case {} {} for problem {}'.format( testid, type, globals.getprob()) input = '' if file is None: print 'Enter {} below (Ctrl-{} when finished)'.format( type, 'Z' if helper.iswindows() else 'D') print settings.smalldivider input = sys.stdin.read() print settings.smalldivider else: print 'Retrieving from ' + file.name input = file.read() helper.write(os.path.join(globals.probdir, testid + ext), input) print 'Set {} successfully'.format(exttotype[ext]) log.addmsg('Set test case {} {} for problem {}'.format( testid, type, globals.getprob())) return True
def rewrite(rows, header, filename): """Rewrite the csv in a smaller one, better comparable. This function is used to re-write the csv file taking all the rows with "match" column equal to True, and one row with "match" column equal to "False" for each different rev.ng generated function. @type rows: list @param rows: The rows to write into the csv file. @type header: list @param header: The header of the csv, that is the name of each metric computed. @type filename: string @param filename: The name of the csv where to write the rows. """ new_rows = [] name = rows[0][0] old_name = name false_rows = [] flag = True for row in rows: name = row[0] match = row[2] if name == old_name: if match == True: new_rows.append(row) flag = False else: if flag: false_rows.append(row) else: flag = True if len(false_rows) > 0: new_rows.append(random.choice(false_rows)) false_rows = [] old_name = name helper.write(new_rows, header, filename=filename)
def change(self): if not self.logged(): return error("You not logged") user = {"email": request.values.get("email"), "password": request.values.get("password"), "fname": request.values.get("fname"), "lname": request.values.get("lname"), "role": request.values.get("role"), "avatar": request.values.get("avatar"), "status": request.values.get("status")} try: id = int(request.values.get("id")) self._data.edit(id, user) return write("Well done") except: return error("Invalid request")
def compute_csv(args): """Compute csv rows. This function computes the elements of the csv file to analyze. First of all it prepares the environment using llvmcpy, a python library with bindings for LLVM auto-generated from the LLVM-C API. Then it calls all the functions that must be computed in order to fill the rows of the analysis. The process of computation is multicored in order to optimize the computation process. @type args: dict @param args: The number of arguments the script is taking. They are: the type of computation to execute (csv), the two files compiled with revamb to compare, the name of the output file where to write. """ # Queue(s) for the process, one for input data to the process, the other for the output data to the main process q_in = Queue() q_out = Queue() try: cpus = cpu_count() - 1 except NotImplementedError: cpus = 2 # arbitrary default buffer_1 = create_memory_buffer_with_contents_of_file(args[0]) buffer_2 = create_memory_buffer_with_contents_of_file(args[1]) context = get_global_context() module_1 = context.parse_ir(buffer_1) module_2 = context.parse_ir(buffer_2) global list_opcodes list_opcodes = get_opcodes([module_1, module_2]) global helper_names helper_names = get_helper_names(module_1, module_2) header = list( flatten([ 'function1', 'function2', 'match', '#bb_mean', '#bb_diff', '#instr_mean', '#instr_diff', 'byte_size_mean', 'byte_size_diff', '#instructions_mean', '#instructions_diff', 'load_size_mean', 'load_size_diff', '#loads_mean', '#loads_diff', 'store_size_mean', 'store_size_diff', '#stores_mean', '#stores_diff', '#indirect_calls_mean', '#indirect_calls_diff', '#function_calls_mean', '#function_calls_diff' ])) header.extend( list( flatten([[str(elem) + "_mean", str(elem) + "_diff"] for elem in helper_names]))) header.extend( list( flatten([[str(elem) + "_mean", str(elem) + "_diff"] for elem in list_opcodes]))) #pool = Pool(cpus) functions_list = [ get_names, cmp_name, cmp_size_llvm_bb, cmp_size_llvm_instr, cmp_byte_size_num_instr, cmp_load_store_instructions, cmp_indirect_calls, cmp_revamb_function_calls, cmp_helper_calls, cmp_instruction_opcodes ] # "enumerate" takes the list and returns a tuple composed by (index_of_element, element) [q_in.put((i, pickle.dumps(x))) for i, x in enumerate(functions_list)] [q_in.put((-1, -1)) for _ in xrange(cpus)] tuples_space = [(fun1, fun2) for fun1 in module_1.iter_functions() for fun2 in module_2.iter_functions() if "bb." in fun1.name and "bb." in fun2.name] # Starting the process rows = [] proc = [ Process(target=run, args=(tuples_space, q_in, q_out, i)) for i in xrange(cpus) ] for p in proc: p.daemon = True p.start() for i in xrange(len(functions_list)): r = q_out.get() rows.append(r) [p.join() for p in proc] rows = [elem[1] for elem in sorted(rows, key=itemgetter(0))] rows = [list(flatten(elem)) for elem in zip(*rows)] filename = args[2] helper.write(rows, header, filename=filename) rewrite(rows, header, filename + "_shorter")
def get_by_id(self, subissue_id): if not self.logged(): return error("You not logged") data = self._data.get_by_id(subissue_id=subissue_id) return write(data)
def run (): if not globals.commands['comp'](): return False print settings.meddivider stdio = globals.getmode() == 'stdio' print 'Executing problem {} in {} I/O mode'.format(globals.getprob(), globals.getmode()) files = os.listdir(globals.probdir) cases = [] for file in files: if file.endswith('.in'): cases.append(file[:-3]) print '{} test inputs found'.format(len(cases)) sandboxin = os.path.join(globals.sandboxdir, pattern.convert(settings.inputfile)) sandboxout = os.path.join(globals.sandboxdir, pattern.convert(settings.outputfile)) execcommand = pattern.convert(settings.execcommands[settings.langexts[globals.getext()]]) langlim = settings.timeconstraints[settings.langexts[globals.getext()]] outcomes = collections.OrderedDict([ ('Correct', 0), ('Runtime Error', 0), ('Time Limit Exceeded', 0), ('Presentation Error', 0), ('Wrong Answer', 0), ('No Output Produced', 0), ('No Answer File', 0) ]) print 'Time limit per input: {:.2f} seconds'.format(langlim) for case in cases: print settings.meddivider print 'Test input ' + case helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin) input = helper.read(sandboxin) execresult = helper.runproc(execcommand, globals.sandboxdir, stdin=(input if stdio else None), timelim=langlim) output = execresult[2] if stdio else helper.read(sandboxout) ans = helper.read(os.path.join(globals.probdir, case + '.ans')) wronganswer = False if execresult[0] is None: # Time limit exceeded print 'Execution time limit exceeded' outcomes['Time Limit Exceeded'] += 1 else: print 'Execution time: {:.2f} seconds'.format(execresult[1]) if execresult[0] != 0: print 'Runtime error: exit code ' + repr(execresult[0]) outcomes['Runtime Error'] += 1 elif output is None or len(output) == 0: print 'Program did not produce output' outcomes['No Output Produced'] += 1 elif ans is None: print 'No answer file provided' outcomes['No Answer File'] += 1 elif ans == output: print 'Correct' outcomes['Correct'] += 1 elif ''.join(ans.split()) == ''.join(output.split()): print 'Presentation Error (extra/missing whitespace)' outcomes['Presentation Error'] += 1 wronganswer = True else: print 'Wrong Answer' outcomes['Wrong Answer'] += 1 wronganswer = True if settings.printinput: helper.printdesc(input, 'Input', settings.smalldivider) if settings.printoutput: helper.printdesc(output, 'Output', settings.smalldivider) if wronganswer and settings.printexpected: helper.printdesc(ans, 'Expected Output', settings.smalldivider) if settings.printstdout and not stdio: helper.printdesc(execresult[2], 'Stdout', settings.smalldivider) if settings.printstderr: helper.printdesc(execresult[3], 'Stderr', settings.smalldivider) helper.write(os.path.join(globals.probdir, case + '.out'), output) if not stdio: helper.write(os.path.join(globals.probdir, case + '.stdout'), execresult[2]) helper.write(os.path.join(globals.probdir, case + '.stderr'), execresult[3]) if settings.printsummary: print settings.meddivider print 'Results Summary' for outcome in outcomes: print '{:2d} | {:<20}'.format(outcomes[outcome], outcome) print 'Total Score: {:.1f}'.format((100.0 * outcomes['Correct']) / len(cases)); return True
def get_specific_issue(self, issue_id): if not self.logged(): return error("You not logged") data = self._data.get_issue_by_id(backlog_id=1, issue_id=issue_id) return write(data)
def get_issues(self): if not self.logged(): return error("You not logged") data = self._data.get_all_issues(backlog_id=1) return write(data)
def get_subissues(self, issue_id): if not self.logged(): return error("You not logged") data = self._data.get_all_subissues(backlog_id=166, issue_id=issue_id) return write(data)
def logstart(): time = datetime.datetime.now().strftime(formatstr) helper.write(globals.logfile, 'PCU Contest Log\nstarted ' + time + '\n' + settings.smalldivider + '\n') print 'Created contest log with start time ' + time return True
args = parser.parse_args() if args.verbose: logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) elif args.quiet: logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.CRITICAL) else: logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING) if args.simple: latex.configure(simpleMode=True) if path.sep in args.source: sourcefolder = args.source.rsplit(path.sep,1)[0] else: sourcefolder = "" notice = "% Warning: Editing this file directly can cause erratic behaviour in the compiler.\n" sourcefile = helper.load(args.source) if include_folder in args.destination: output = Scanner().scan(sourcefile) helper.write(args.destination, notice + output) else: with cd(sourcefolder): output = Scanner().scan(sourcefile) helper.write(args.destination, notice + output)
def entered(self): if self.logged(): return write("Yes") else: return write("No")
def logout(self): if not self.logged(): return error("You not logged") session.pop('email', None) return write("Well done")
def get_all(self): if not self.logged(): return error("You not logged") data = self._data.get_all() return write(data)