def get_help(methods, methods_folder, caller=__file__, skip=0): info("Usage:\n python " + os.path.basename(caller) + " method=... [optional arguments]\n") max_len = max([len(method) for method in methods]) list_string = "{method:<" + str(max_len) + "} {opt_args_str}" title_string = "{:<" + str(max_len) + "} {}" info_cyan(title_string.format( "Method", "Optional arguments (=default value)")) for method in sorted(methods): m = __import__("{}.{}".format(methods_folder, method)) func = m.__dict__[method].method opt_args_str = "" argcount = func.__code__.co_argcount if argcount > 1: opt_args = zip(func.__code__.co_varnames[skip:], func.__defaults__) opt_args_str = ", ".join(["=".join([str(item) for item in pair]) for pair in opt_args]) info(list_string.format( method=method, opt_args_str=opt_args_str)) exit()
def method(res=50, diameter=1., length=5., **kwargs): ''' Function That Generates a mesh for a barbell capilar, Meshing method is mshr. Note: The generarted mesh is stored in "BERNAISE/meshes/". ''' info("Generating mesh using the mshr tool.") inletdiameter = diameter * 5. inletlength = diameter * 4. # Define coners of "capilar" a = df.Point(-diameter / 2., -length / 2 - inletlength / 2.) b = df.Point(diameter / 2., length / 2 + inletlength / 2.) capilar = mshr.Rectangle(a, b) # Define coners of "leftbell c = df.Point(-inletdiameter / 2., -length / 2 - inletlength) d = df.Point(inletdiameter / 2., -length / 2) leftbell = mshr.Rectangle(c, d) # Define coners of "rightbell" e = df.Point(-inletdiameter / 2., length / 2) f = df.Point(inletdiameter / 2., length / 2 + inletlength) rightbell = mshr.Rectangle(e, f) domain = capilar + leftbell + rightbell mesh = mshr.generate_mesh(domain, res) meshpath = os.path.join( MESHES_DIR, "BarbellCapilarDolfin_d" + str(diameter) + "_l" + str(length) + "_res" + str(res)) store_mesh_HDF5(mesh, meshpath)
def next_page(game, globalvars): # displays another page to play (good_association, all_associations) = game.get_random_associations() game.rounds_done = game.rounds_done + 1 if ((good_association != None) and (game.rounds_done < game.rounds_amount + 1)): current_page = Page_associate(game, globalvars, good_association, all_associations) current_page.fade_in() pygame.display.flip() if (cmp(game.music_mode, "off") == 0): # if silence was requested (no music) # we make sure no more sound will be heard from previous page pygame.mixer.fadeout(constants.fadeout_sound_duration) current_page.game = game game.start_timer() return current_page else: common.info("No more associations to look for !") return None
def __init__(self, globalvars, text, image_file, associated_sounds): common.info("Running display big") common.Run_Screen.__init__(self, globalvars) self.current_page = Page_display_big(globalvars, text, image_file, associated_sounds) self.current_page.draw() pygame.display.flip() while self.running: common.Run_Screen.run_pre(self) for key_event in self.key_events: if key_event.key == K_SPACE: running = False if (self.mouse_clicked == True): clicked = self.current_page.was_something_clicked( self.mouse_pos, self.mousebutton) if (clicked != None): (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked == self.current_page.next_icon): self.running = False common.Run_Screen.run_post(self)
def next_page(game, globalvars) : # displays another page to play (good_association, all_associations) = game.get_random_associations() game.rounds_done = game.rounds_done + 1 if ((good_association != None) and (game.rounds_done < game.rounds_amount + 1)) : current_page = Page_associate(game, globalvars, good_association, all_associations) current_page.fade_in() pygame.display.flip() if (cmp(game.music_mode, "off") == 0) : # if silence was requested (no music) # we make sure no more sound will be heard from previous page pygame.mixer.fadeout(constants.fadeout_sound_duration) current_page.game = game game.start_timer() return current_page else : common.info("No more associations to look for !") return None
def start(self): startmsg = 'Omibot starting:pid=%s' % os.getpid() common.info(startmsg) self.slack_conn.post_msg(startmsg) self.monitoring = True def monitor_msgs(context): while context.monitoring: try: context.slack_conn.listen(context.on_message) except StopIteration as e: common.info('Exiting because shutdown requested') context.monitoring = False return except: tb = traceback.format_exc() common.error( 'Omibot unable to listen to commands. Error Info: %s Retrying...' % tb) time.sleep(10) context.slack_conn.listening = False def meh(): monitor_msgs(self) monitoring_thread = threading.Thread(target=meh) monitoring_thread.start() self.monitoring_thread = monitoring_thread
def __init__(self, globalvars, text, image_file, associated_sounds) : common.info("Running display big") common.Run_Screen.__init__(self, globalvars) self.current_page = Page_display_big(globalvars, text, image_file, associated_sounds) self.current_page.draw() pygame.display.flip() while self.running : common.Run_Screen.run_pre(self) for key_event in self.key_events : if key_event.key == K_SPACE : running = False if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked == self.current_page.next_icon) : self.running = False common.Run_Screen.run_post(self)
def main(): info_yellow("BERNAISE: Post-processing tool") cmd_kwargs = parse_command_line() folder = cmd_kwargs.get("folder", False) scripts_folder = "analysis_scripts" methods = get_methods(scripts_folder) # Get help if it was called for. if cmd_kwargs.get("help", False): get_help(methods, scripts_folder, __file__, skip=1) # Get sought fields sought_fields = cmd_kwargs.get("fields", False) if not sought_fields: sought_fields = None elif not isinstance(sought_fields, list): sought_fields = [sought_fields] if not folder: info("No folder(=[...]) specified.") exit() sought_fields_str = (", ".join(sought_fields) if sought_fields is not None else "All") info_split("Sought fields:", sought_fields_str) ts = TimeSeries(folder, sought_fields=sought_fields) info_split("Found fields:", ", ".join(ts.fields)) method = cmd_kwargs.get("method", "geometry_in_time") if len(ts.fields) == 0: info_on_red("Found no data.") exit() call_method(method, methods, scripts_folder, ts, cmd_kwargs)
def compute_norms(err, vector_norms=["l2", "linf"], function_norms=["L2", "H1"], show=True, tablefmt="simple", save=False): """ Compute norms, output to terminal, etc. """ info_split("Vector norms:", ", ".join(vector_norms)) info_split("Function norms:", ", ".join(function_norms)) headers = ["Fields"] + vector_norms + function_norms table = [] for field in err.keys(): row = [field] for norm_type in vector_norms: row.append(df.norm(err[field].vector(), norm_type=norm_type)) for norm_type in function_norms: row.append(df.norm(err[field], norm_type=norm_type)) table.append(row) from tabulate import tabulate tab_string = tabulate(table, headers, tablefmt=tablefmt, floatfmt="e") if show: info("\n" + tab_string + "\n") if save and rank == 0: info_split("Saving to file:", save) with open(save, "w") as outfile: outfile.write(tab_string)
def do_login_admin(browser): common.info("Processing login..") function_result = True # go to the admin login page and wait for page to be loaded browser.get("http://localhost/litecart/admin/") WebDriverWait(browser, 10).until( EC.presence_of_element_located( (By.CSS_SELECTOR, "button[value=Login]"))) # enter credentials login_field = browser.find_element_by_name("username") login_field.clear() login_field.send_keys("admin") password_field = browser.find_element_by_name("password") password_field.clear() password_field.send_keys("admin") # press [Login] button and wait for next page to be loaded browser.find_element_by_css_selector('button[name="login"]').click() WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "li#widget-discussions"))) if (browser.find_element_by_css_selector("li#widget-discussions")): function_result = True common.success("Login successful") else: function_result = False common.fail("Cannot login to admin page") return function_result
def _get_files(self): extensions = [ "aac", "ac3", "avi", "aiff", "bat", "bmp", "exe", "flac", "gif", "jpeg", "jpg", "mov", "m3u", "m4p", "mp2", "mp3", "mp4", "mpeg4", "midi", "msi", "ogg", "png", "txt", "sh", "wav", "wma", "vqf" ] common.warn("Only searching for prohibited files in user directories!") if "Linux" in plugin.get_os(): directory = "/home" elif "Windows" in plugin.get_os(): directory = "C:\\Users" else: return [] common.info( "Searching {} for prohibited files. This may take a while...") files = [] for extension in extensions: x = glob.glob(os.path.join(directory, "**/*." + extension), recursive=True) files.extend(x) return files
def machine(self, session, artifact): """ Run all modules against an artifact of a given type """ is_key, value = lookup_key(session, artifact) if is_key and value is None: error('Unable to find artifact key in session (%s)' % artifact) return elif is_key and value is not None: artifact = value else: pass artifact_type = detect_type(artifact) artifact = self.db.find(artifact_type, {'name': artifact}, one=True) for key in self.modules.keys(): if artifact['type'] == key: modules = self.modules[artifact['type']] elif artifact['subtype'] == key: modules = self.modules[artifact['subtype']] results = [] for m in modules: result = self.run(m, artifact) if m in result['data'].keys(): if result['data'][m] is not None: if self.db.exists(artifact['type'], {'name': artifact['name']}): for child in result['children']: child_artifact = create_artifact( child['name'], parent=artifact['name'], _type=child['type'], source=child['source'], subtype=child['subtype']) if not self.db.exists(child['type'], {'name': child['name']}): self.db.insert_one(child['type'], child_artifact) self.db.update_one(artifact['type'], {'name': artifact['name']}, result) if len(result['children']) > 0: info('Created child artifacts: %d' % len(result['children'])) results.append({'[%s]' % m: result['data'][m]}) else: warning('No results found (%s)' % m) else: warning('Failed to get module results (%s)' % m) success('Machine completed')
def get_data_from_line(self, payload): retry_time = 0 while True: if retry_time == self.MAX_RETRY_TIME: error( 'Retrying too many times, so it will stop get data of {0}'. format(str(payload))) return None url = self.line_api_url line_token = self.read_line_access_token() headers = { 'Authorization': 'Bearer ' + line_token, 'Content-Type': 'application/json' } response = requests.post(url, headers=headers, data=json.dumps(payload), stream=True) result_code = response.status_code #token is expired if result_code == 401: self.check_and_renew_token() #if your token cannot access accountId, it do not need to retry elif result_code == 200 or "can't access to accountId with your token." in response.content: return response else: retry_time += 1 info('Retry at ' + str(retry_time)) sleep(1)
def get_step_and_info(ts, time, step=0): if time is not None: step, time = ts.get_nearest_step_and_time(time) else: time = ts.get_time(step) info("Time = {}, timestep = {}.".format(time, step)) return step, time
def restart(self): info('Restarting HDP services ...') def startstop(srvs, action, count, interval): srv_baseurl = CLUSTER_URL_PTR % (self.url, self.cluster_name) + '/services/' state = 'INSTALLED' if action == 'stop' else 'STARTED' for srv in srvs: srv_url = srv_baseurl + srv config = { 'RequestInfo': { 'context': '%s %s services' % (action, srv) }, 'ServiceInfo': { 'state': state } } rc = self.p.put(srv_url, config) # check startstop status if rc: get_stat = lambda: self.p.get(srv_url)['ServiceInfo'][ 'state'] == state retry(get_stat, count, interval, 'HDP service %s %s' % (srv, action)) else: if action == 'stop': action += 'p' info('HDP service %s had already been %sed' % (srv, action)) srvs = ['HBASE', 'HDFS', 'ZOOKEEPER'] startstop(srvs, 'stop', 30, 10) time.sleep(10) srvs.reverse() startstop(srvs, 'start', 60, 10)
def startstop(srvs, action, count, interval): srv_baseurl = CLUSTER_URL_PTR % (self.url, self.cluster_name) + '/services/' state = 'INSTALLED' if action == 'stop' else 'STARTED' for srv in srvs: srv_url = srv_baseurl + srv config = { 'RequestInfo': { 'context': '%s %s services' % (action, srv) }, 'ServiceInfo': { 'state': state } } rc = self.p.put(srv_url, config) # check startstop status if rc: get_stat = lambda: self.p.get(srv_url)['ServiceInfo'][ 'state'] == state retry(get_stat, count, interval, 'HDP service %s %s' % (srv, action)) else: if action == 'stop': action += 'p' info('HDP service %s had already been %sed' % (srv, action))
def put_metric_alarm(alarmprefix, namepace, description, actions, metric_name, threshold, statistic, operator, dimensions): # Construct string out of dimensions and append it to alarm name str_dimensions = '' for list_item in dimensions: str_dimensions = str_dimensions + "." + list_item['Value'] common.info("Alarm name: " + alarmprefix + "." + metric_name + str_dimensions) client = boto3.client('cloudwatch', region_name=region_name) response = client.put_metric_alarm(AlarmName=alarmprefix + "." + metric_name + str_dimensions, AlarmDescription=description, OKActions=actions, AlarmActions=actions, InsufficientDataActions=actions, ActionsEnabled=True, MetricName=metric_name, Namespace=namespace, Dimensions=dimensions, Period=300, EvaluationPeriods=1, Threshold=threshold, Statistic=statistic, ComparisonOperator=operator) for each in response.itervalues(): if each['HTTPStatusCode'] == 200: common.info("Alarm " + alarmprefix + "." + metric_name + str_dimensions + " created") return True else: common.error("Failed to create alarm " + alarmprefix + "." + metric_name + str_dimensions) return False pprint.pprint(response)
def set_checkbox(browser, locator, checked=True): elem = browser.find_element_by_css_selector(locator) if elem.get_property("checked") != checked: elem.click() common.info("Set %s = '%s': ok" % (locator, "Checked" if checked else "Not checked")) return True
def method(ts, show=False, save=True, dt=None, fps=25, skip=0, delete_after=True, plot_u=False, inverse_phase=False, **kwargs): """ Make fancy gif animation. """ info_cyan("Making a fancy gif animation.") anim_name = "animation" ts.compute_charge() steps = get_steps(ts, dt)[::(skip + 1)] for step in steps[rank::size]: info("Step " + str(step) + " of " + str(len(ts))) if "phi" in ts: phi = ts["phi", step][:, 0] else: phi = np.zeros(len(ts.nodes)) - 1. if inverse_phase: phi = -phi charge = ts["charge", step][:, 0] charge_max = max(ts.max("charge"), -ts.min("charge")) charge_max = max(charge_max, 1e-8) # Remove numerical noise if plot_u and "u" in ts: u = ts["u", step] else: u = None if save: save_file = os.path.join(ts.tmp_folder, anim_name + "_{:06d}.png".format(step)) else: save_file = None plot_fancy(ts.nodes, ts.elems, phi, charge, charge_max=charge_max, show=show, u=u, save=save_file) comm.Barrier() if save and rank == 0: tmp_files = os.path.join(ts.tmp_folder, anim_name + "_*.png") anim_file = os.path.join(ts.plots_folder, anim_name + ".gif") os.system(("convert -delay {delay} {tmp_files} -trim +repage" " -loop 0 {anim_file}").format(tmp_files=tmp_files, anim_file=anim_file, delay=int(100. / fps))) if delete_after: os.system("rm {tmp_files}".format(tmp_files=tmp_files))
def _create_users(self, users): for user in users: common.info("Adding {}...".format(user)) if "Linux" in plugin.get_os(): common.run("useradd -s /bin/bash -m {}".format(user)) common.info("Added user {}".format(user)) elif "Windows" in plugin.get_os(): os.system("net user \"{}\" /add".format(user))
def description(**kwargs): info(""" Function that generates a mesh for a straight capilar, default meshing method is dolfin's "RectangleMesh" but has an option for mshr. Note: The generated mesh is stored in "BERNAISE/meshes/". """)
def method(ts, dt=0, extra_boundaries="", **kwargs): """ Plot value in time. """ info_cyan("Plot value at boundary in time.") params = ts.get_parameters() steps = get_steps(ts, dt) problem = params["problem"] info("Problem: {}".format(problem)) boundary_to_mark, ds = fetch_boundaries(ts, problem, params, extra_boundaries) x_ = ts.functions() fields = dict() for field, f in x_.items(): if field == "u": fields["u_x"] = f[0] fields["u_y"] = f[1] else: fields[field] = f t = np.zeros(len(steps)) data = dict() for boundary_name in boundary_to_mark: data[boundary_name] = dict() for field in fields: data[boundary_name][field] = np.zeros(len(steps)) for i, step in enumerate(steps): info("Step {} of {}".format(step, len(ts))) for field in x_: ts.update(x_[field], field, step) for boundary_name, (mark, k) in boundary_to_mark.items(): for field, f in fields.items(): data[boundary_name][field][i] = df.assemble(f * ds[k](mark)) t[i] = ts.times[step] savedata = dict() field_keys = sorted(fields.keys()) for boundary_name in boundary_to_mark: savedata[boundary_name] = np.array( list( zip(steps, t, *[data[boundary_name][field] for field in field_keys]))) if rank == 0: header = "Step\tTime\t" + "\t".join(field_keys) for boundary_name in boundary_to_mark: with open( os.path.join(ts.analysis_folder, "value_in_time_{}.dat".format(boundary_name)), "w") as outfile: np.savetxt(outfile, savedata[boundary_name], header=header)
def __init__(self, igloo_page): common.info("Running Igloo") self.current_page = igloo_page common.Run_Screen.__init__(self, self.current_page.globalvars) if (igloo_page.tux_going_out != None): if (igloo_page.next_icon in igloo_page.items): igloo_page.mouse_sprite.background = None igloo_page.remove(igloo_page.next_icon) else: if (igloo_page.next_icon not in igloo_page.items): igloo_page.append(igloo_page.next_icon) igloo_page.draw() pygame.display.flip() while (self.running or igloo_page.tux_going_out != None): try: common.Run_Screen.run_pre(self) except common.EndActivityException, e: if (igloo_page.tux_going_out == None): self.running = False # actions for mouse actions if (self.mouse_clicked == True): clicked = igloo_page.was_something_clicked( self.mouse_pos, self.mousebutton) if (clicked != None): (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked == igloo_page.next_icon): self.running = False # actions for keyboard for key_event in self.key_events: if (key_event.key == K_SPACE): if (igloo_page.tux_going_out == None): # we quit only after the tux has left the igloo ! self.running = False if (igloo_page.tux_going_out != None): self.dirty_rects = self.dirty_rects + igloo_page.tux_goes_out() common.Run_Screen.run_post(self)
def __init__(self, game, globalvars) : common.info("Running transformation activity") game = Transform_game(game, globalvars.main_language) common.Run_Screen.__init__(self, globalvars, game) self.current_page = Page_transform(globalvars, self.game.get_transformation(), self.game) self.current_page.draw() pygame.display.flip() while self.running : common.Run_Screen.run_pre(self) # actions after keyboard events for key_event in self.key_events : if key_event.key == K_SPACE : if (self.current_page.transformation.done == True) : self.current_page = next_page(self.current_page, self.game, globalvars) if (self.current_page == None) : return # actions for mouse actions if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, xml_file, some_dirty_rects) = clicked if (component_clicked == self.current_page.quit_icon) : raise common.EndActivityException() elif (component_clicked == self.current_page.next_icon) : self.current_page = next_page(self.current_page, self.game, globalvars) if (self.current_page == None) : return self.dirty_rects = self.dirty_rects + some_dirty_rects # let's see if we have some image part to unveil mouse_pos = pygame.mouse.get_pos() if (self.current_page.image.rect.collidepoint(mouse_pos)) : self.dirty_rects = self.dirty_rects + self.current_page.unveil(mouse_pos) common.Run_Screen.run_post(self)
def input_value_to_input_field(browser, data_to_set): value_to_set = data_to_set["value"] field_to_be_set = browser.find_element_by_css_selector(data_to_set["location"]) field_to_be_set.clear() field_to_be_set.send_keys(value_to_set) if (field_to_be_set.get_property("value") == value_to_set): common.info("Set %s = '%s': ok" % (data_to_set["location"], value_to_set)) return True return False
def method(ts, dt=0, **kwargs): """ Analyze geometry in time.""" info_cyan("Analyzing the evolution of the geometry through time.") if not ts.get_parameter("enable_PF"): print "Phase field not enabled." return False f_mask = df.Function(ts.function_space) f_mask_x = [] f_mask_u = [] for d in range(ts.dim): f_mask_x.append(df.Function(ts.function_space)) f_mask_u.append(df.Function(ts.function_space)) length = np.zeros(len(ts)) area = np.zeros(len(ts)) com = np.zeros((len(ts), ts.dim)) u = np.zeros((len(ts), ts.dim)) makedirs_safe(os.path.join(ts.analysis_folder, "contour")) steps = get_steps(ts, dt) for step in steps: info("Step " + str(step) + " of " + str(len(ts))) phi = ts["phi", step][:, 0] mask = 0.5 * (1. - phi) # 0.5*(1.-np.sign(phi)) ts.set_val(f_mask, mask) for d in range(ts.dim): ts.set_val(f_mask_x[d], mask * ts.nodes[:, d]) ts.set_val(f_mask_u[d], mask * ts["u", step][:, d]) contour_file = os.path.join(ts.analysis_folder, "contour", "contour_{:06d}.dat".format(step)) paths = zero_level_set(ts.nodes, ts.elems, phi, save_file=contour_file) length[step] = path_length(paths) area[step] = df.assemble(f_mask * df.dx) for d in range(ts.dim): com[step, d] = df.assemble(f_mask_x[d] * df.dx) u[step, d] = df.assemble(f_mask_u[d] * df.dx) for d in range(ts.dim): com[:, d] /= area u[:, d] /= area if rank == 0: np.savetxt(os.path.join(ts.analysis_folder, "time_data.dat"), np.array( zip(np.arange(len(ts)), ts.times, length, area, com[:, 0], com[:, 1], u[:, 0], u[:, 1])), header=("Timestep\tTime\tLength\tArea\t" "CoM_x\tCoM_y\tU_x\tU_y"))
def on_message(self, msg): if self.is_bot_mentioned(msg): for key, command, _desc in self.command_dict: if key in msg['text'].lower(): common.info('Omibot executing:%s on %s' % (key, msg)) return command(msg) self.slack_conn.post_msg("Sorry I don't understand your command.") common.error(msg) self.help(msg)
def doFtpServer(ftpd, addr, withSize=True, pwd=b"/"): info("Aggressive FTP") handleFtp1(ftpd) handleFtp2(ftpd) respBuf = shellCodeInPwd(addr) ftpd.sendline(respBuf) # wait for EoF, Alloca will crash ftpd.interactive() ftpd.close()
def submit(self, session, module, artifact, no_argument=False): """ Run a single module against an artifact """ if no_argument: module_result = self.run(module, None) return module_result is_key, value = lookup_key(session, artifact) if is_key and value is None: error('Unable to find artifact key in session (%s)' % artifact) return elif is_key and value is not None: artifact = value else: pass artifact_type = detect_type(artifact) artifact = self.db.find(artifact_type, {'name': artifact}, one=True) if artifact is None: warning('Unable to find artifact in database (%s)' % artifact['name']) return None if module in self.modules[artifact['type']] or module in self.modules[artifact['subtype']]: pass else: warning('Artifact is not supported by module (%s)' % (artifact['name'])) return None result = self.run(module, artifact) if module in result['data'].keys(): if result['data'][module] is not None: if self.db.exists(artifact['type'], {'name': artifact['name']}): for child in result['children']: child_artifact = create_artifact(child['name'], parent=artifact['name'], _type=child['type'], source=child['source'], subtype=child['subtype']) if not self.db.exists(child['type'], {'name': child['name']}): self.db.insert_one(child['type'], child_artifact) self.db.update_one(artifact['type'], {'name': artifact['name']}, result) if len(result['children']) > 0: info('Created child artifacts: %d' % len(result['children'])) return result['data'][module] else: warning('No results found (%s)' % module) return None else: warning('Failed to get module results (%s)' % module)
def input_value_to_scrollable_field( browser, data_to_set): #{"location": "", "value": ""}) value_to_set = data_to_set["value"] field_to_be_set = browser.find_element_by_css_selector( data_to_set["location"]) field_to_be_set.clear() field_to_be_set.send_keys(value_to_set) # TODO: нужна проверка установленного значения common.info("Set %s = '%s': ok" % (data_to_set["location"], value_to_set)) return True
def doSlowFtp(ftpd): info("Slow FTP") handleFtp1(ftpd) # add delay so that another thread # can cause mayhem info("Sleeping!") # that's the timeout in Alloca sleep(5) # just kill the connection ftpd.close()
def __init__(self, highscore_page, level, total_score) : common.info("Running Highscore") self.current_page = highscore_page # TODO clean if (total_score != None) : total_score.datetime = calendar.timegm(time.gmtime()) highscores = Highscores(self.current_page.globalvars, level, total_score) self.current_page.scores = highscores self.current_page.update() common.Run_Screen.__init__(self, self.current_page.globalvars) highscore_page.draw() pygame.display.flip() while self.running : try : common.Run_Screen.run_pre(self) except common.EndActivityException, e : self.running = False # actions for mouse actions if (self.mouse_clicked == True) : clicked = highscore_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked == highscore_page.next_icon) : self.running = False # actions for keyboard for key_event in self.key_events : if (key_event.key == K_SPACE) : self.running = False common.Run_Screen.run_post(self)
def main(): pwn.context.update(os='linux', arch='mips') r = pwn.remote(remoteIp, remotePort) info("Sending Guard packet") r.send(createGuard()) sleep(1) # wait for EoF, Alloca will crash r.interactive() r.close()
def machine(self, session, artifact): """ Run all modules against an artifact of a given type """ is_key, value = lookup_key(session, artifact) if is_key and value is None: error('Unable to find artifact key in session (%s)' % artifact) return elif is_key and value is not None: artifact = value else: pass artifact_type = detect_type(artifact) artifact = self.db.find(artifact_type, {'name': artifact}, one=True) for key in self.modules.keys(): if artifact['type'] == key: modules = self.modules[artifact['type']] elif artifact['subtype'] == key: modules = self.modules[artifact['subtype']] results = [] for m in modules: result = self.run(m, artifact) if m in result['data'].keys(): if result['data'][m] is not None: if self.db.exists(artifact['type'], {'name': artifact['name']}): for child in result['children']: child_artifact = create_artifact(child['name'], parent=artifact['name'], _type=child['type'], source=child['source'], subtype=child['subtype']) if not self.db.exists(child['type'], {'name': child['name']}): self.db.insert_one(child['type'], child_artifact) self.db.update_one(artifact['type'], {'name': artifact['name']}, result) if len(result['children']) > 0: info('Created child artifacts: %d' % len(result['children'])) results.append({'[%s]' % m: result['data'][m]}) else: warning('No results found (%s)' % m) else: warning('Failed to get module results (%s)' % m) success('Machine completed')
def startstop(srvs, action, count, interval): srv_baseurl = CLUSTER_URL_PTR % (self.url, self.cluster_name) + '/services/' state = 'INSTALLED' if action == 'stop' else 'STARTED' for srv in srvs: srv_url = srv_baseurl + srv config = {'RequestInfo': {'context' :'%s %s services' % (action, srv)}, 'ServiceInfo': {'state' : state}} rc = self.p.put(srv_url, config) # check startstop status if rc: get_stat = lambda: self.p.get(srv_url)['ServiceInfo']['state'] == state retry(get_stat, count, interval, 'HDP service %s %s' % (srv, action)) else: if action == 'stop': action += 'p' info('HDP service %s had already been %sed' % (srv, action))
def restart(self): restart_url = RESTART_URL_PTR % (self.url, self.cluster_name) deploy_cfg_url = DEPLOY_CFG_URL_PTR % (self.url, self.cluster_name) def __retry(url, maxcnt, interval, msg): rc = self.p.post(url) stat_url = CMD_STAT_URL_PTR % (self.url, rc['id']) get_stat = lambda: self.p.get(stat_url)['success'] is True and self.p.get(stat_url)['active'] is False retry(get_stat, maxcnt, interval, msg) info('Restarting CDH services ...') __retry(restart_url, 40, 15, 'CDH services restart') info('Deploying CDH client configs ...') __retry(deploy_cfg_url, 30, 10, 'CDH services deploy')
def __init__(self, game, globalvars, total_score) : common.info("Running differences activity") game = Differences_game(game, total_score) game.select_random_differences() common.Run_Screen.__init__(self, globalvars, game) self.current_page = Page_differences(self.game, globalvars) self.current_page.draw() pygame.display.flip() game.start_timer() while self.running : common.Run_Screen.run_pre(self) # actions after keyboard events for key_event in self.key_events : if key_event.key == K_SPACE : if (self.current_page.completed == True) : self.running = False if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked if (component_clicked == self.current_page.quit_icon) : raise common.EndActivityException() elif (component_clicked == self.current_page.next_icon) : self.running = False self.dirty_rects = self.dirty_rects + some_dirty_rects common.Run_Screen.run_post(self)
def __init__(self, game, globalvars, total_score) : common.info("Running puzzle activity") game = Puzzle_game(game, globalvars.main_language, total_score) common.Run_Screen.__init__(self, globalvars, game) self.current_page = next_page(self.game, globalvars) while self.running : common.Run_Screen.run_pre(self) # actions after keyboard events for key_event in self.key_events : if key_event.key == K_SPACE : if (self.current_page.puzzle.done == True) : self.current_page.fade_out() self.current_page = next_page(self.game, globalvars) if (self.current_page == None) : return # actions for mouse actions if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, xml_file, some_dirty_rects) = clicked if (component_clicked == self.current_page.quit_icon) : raise common.EndActivityException() elif (component_clicked == self.current_page.next_icon) : self.current_page.fade_out() self.current_page = next_page(self.game, globalvars) if (self.current_page == None) : return self.dirty_rects = self.dirty_rects + some_dirty_rects common.Run_Screen.run_post(self)
def next_page(current_page, game, globalvars) : next_transformation = game.get_transformation() game.rounds_done = game.rounds_done + 1 if ((next_transformation <> None) and (game.rounds_done < game.rounds_amount)) : current_page.fade_out() current_page = Page_transform(globalvars, next_transformation, game) current_page.draw() pygame.display.flip() return current_page else : common.info("Game over: no more transformations !") return None
def next_page(game, globalvars) : next_puzzle = game.get_puzzle() game.rounds_done = game.rounds_done + 1 if ((next_puzzle <> None) and (game.rounds_done < game.rounds_amount + 1)) : current_page = Page_basic_puzzle(globalvars, next_puzzle, game) current_page.draw() pygame.display.flip() game.start_timer() return current_page else : common.info("Game over: no more puzzles !") return None
def restart(self): info('Restarting HDP services ...') def startstop(srvs, action, count, interval): srv_baseurl = CLUSTER_URL_PTR % (self.url, self.cluster_name) + '/services/' state = 'INSTALLED' if action == 'stop' else 'STARTED' for srv in srvs: srv_url = srv_baseurl + srv config = {'RequestInfo': {'context' :'%s %s services' % (action, srv)}, 'ServiceInfo': {'state' : state}} rc = self.p.put(srv_url, config) # check startstop status if rc: get_stat = lambda: self.p.get(srv_url)['ServiceInfo']['state'] == state retry(get_stat, count, interval, 'HDP service %s %s' % (srv, action)) else: if action == 'stop': action += 'p' info('HDP service %s had already been %sed' % (srv, action)) srvs = ['HBASE', 'HDFS', 'ZOOKEEPER'] startstop(srvs, 'stop', 30, 10) time.sleep(10) srvs.reverse() startstop(srvs, 'start', 60, 10)
def next_page(game, globalvars): game.get_random_learning_cards() game.rounds_done = game.rounds_done + 1 if game.rounds_done < game.rounds_amount + 1: current_page = Page_learning(game, globalvars) current_page.fade_in() pygame.display.flip() if cmp(game.music_mode, "off") == 0: # if silence was requested (no music) # we make sure no more sound will be heard from previous page pygame.mixer.fadeout(constants.fadeout_sound_duration) return current_page else: common.info("No more learning_cards to look for !") return None
def update(self) : ui.Page.update(self) if (cmp(self.transformation.text, "") == 0) : text = self.game.get_title(self.language) else : text = self.transformation.text # load the original image common.info("Loading original image "+ self.transformation.original_image_filename) self.original_image = ui.Image(self, self.game.image_pos, self.game.image_size, self.transformation.original_image_filename) self.unveil_width = ((self.original_image.rect.width) / self.transformation.pieces_amount_x) self.unveil_height = ((self.original_image.rect.height) / self.transformation.pieces_amount_y) self.rounded_width = int(round(self.unveil_width * self.transformation.pieces_amount_x)) self.rounded_height = int(round(self.unveil_height * self.transformation.pieces_amount_y)) self.original_image.cliprect_abs=((self.original_image.x, self.original_image.y), (self.rounded_width, self.rounded_height)) # in case the original_image has transparent alpha pixels original_image_surface_copy = copy.copy(self.original_image.surface) self.original_image.surface.blit(self.screen.subsurface(self.original_image.rect), (0,0) ) self.original_image.surface.blit(original_image_surface_copy, (0,0)) # add the "transformed" image common.info("Loading transformed image "+ self.transformation.transformed_image_filename) (resized_image, foo) = ui.load_image(self.transformation.transformed_image_filename, (self.original_image.rect.width, self.original_image.rect.height), "FILL") self.image = ui.Image_Absolute(self, (self.original_image.rect.left, self.original_image.rect.top), (self.original_image.rect.width, self.original_image.rect.height), resized_image) self.append(self.image) self.image.cliprect_abs=((self.image.rect.left, self.image.rect.top), (self.rounded_width, self.rounded_height)) # the "alpha" surfaces are transparent original images to make a smoother # transition between transformed and original image self.alpha_surfaces = [] alpha_step = 255 / (self.transformation.amount_alpha_surfaces + 2) alpha_value = alpha_step self.transformed_surface = copy.copy(self.image.surface) for i in range(self.transformation.amount_alpha_surfaces) : alpha_surface = copy.copy(self.original_image.surface) alpha_surface.set_alpha(alpha_value) alpha_value = alpha_value + alpha_step self.alpha_surfaces.append(alpha_surface) self.transformation.init_table()
def init_background(self) : # the background of the igloo is indeed a part of the menu background image common.info("Init igloo background - start") (surface, dim) = ui.load_image(constants.main_menu_back) (surface_x_pc, surface_y_pc) = constants.igloo_pos surface_x = int((surface_x_pc * surface.get_width()) / 100) surface_y = int((surface_y_pc * surface.get_height()) / 100) (surface_width_pc, surface_height_pc) = constants.igloo_size surface_width = int((surface_width_pc * surface.get_width()) / 100) surface_height = int((surface_height_pc * surface.get_height()) / 100) self.surface_to_zoom_rect = Rect(surface_x, surface_y, surface_width, surface_height) surface_to_zoom = surface.subsurface(self.surface_to_zoom_rect) self.background = pygame.transform.scale(surface_to_zoom, (self.screen.get_width(), self.screen.get_height())) # now let's go back to screen dimensions : surface_x = int(self.x_pc2px(surface_x_pc)) surface_y = int(self.y_pc2px(surface_y_pc)) surface_width = int(self.x_pc2px(surface_width_pc)) surface_height = int(self.y_pc2px(surface_height_pc)) self.surface_to_zoom_rect = Rect(surface_x, surface_y, surface_width, surface_height) # adding the igloo self.igloo = ui.Image(self, (0,0), (100,100), constants.igloo_file, 255, 100, "TOPLEFT") self.append_back(self.igloo) # not clean, but same trick as used in ui.Page.draw_back() save_screen = self.screen self.screen = self.background self.igloo.draw() self.screen = save_screen (foo, initial_width, initial_height) = ui.get_image_info(constants.igloo_file) coord_convert_igloo = ui.Coordinates_converter(self, Rect((0,0), (initial_width, initial_height)), self.igloo.rect) # not very clean: as the limit is the height # we assume width could be three times as big (for instance !) self.tux_size = coord_convert_igloo.get_coords_px2pc((3 * constants.tux_height, constants.tux_height)) self.tux_initial_pos = coord_convert_igloo.get_coords_px2pc(constants.tux_initial_pos) (self.flag_mast_pos_x, self.flag_mast_pos_y) = coord_convert_igloo.get_coords_px2pc((constants.flag_mast_pos_x_px, 0)) common.info("Init igloo background - before reducing") self.igloo_reduced_surface = pygame.transform.scale(self.background, self.surface_to_zoom_rect.size) common.info("Init igloo background - end")
def undrag_sprite(self) : dragged_component = self.dragged_sprite.component goal_found = False dropped_on_goal_area = False for goal in self.goals : dragged_component_modified_rect = Rect((0,0), (min(goal.rect.width, dragged_component.rect.width), min(goal.rect.height, dragged_component.rect.height))) dragged_component_modified_rect.center = dragged_component.rect.center if (dragged_component_modified_rect.colliderect(goal)) : dropped_on_goal_area = True # let's check whether the dragged component is the one associated with the goal # OR whether it is another association, which happens to be equivalent (has text # in common, and sound in common if present) if ((dragged_component.associated_object == goal.associated_object) or (dragged_component.associated_object.is_equivalent(goal.associated_object))) : # that is a good answer ! goal_found = True common.info("Good answer") self.play_congratulations_sound() self.game.score.good_answer() if (cmp(self.game.type_to_drag, "text") == 0) : goal.set_text(dragged_component.get_text()) new_goal = goal else : goal_rect = goal.get_original_rect() new_goal = ui.Image(self, (self.x_px2pc(goal_rect.left), self.y_px2pc(goal_rect.top)), (self.x_px2pc(goal_rect.width), self.y_px2pc(goal_rect.height)), dragged_component.associated_object.selected_item_to_drag) new_goal.associated_object = dragged_component.associated_object self.remove(goal) self.append(new_goal) self.goals.remove(goal) new_goal.associated_sounds = dragged_component.associated_sounds new_goal.associated_text = dragged_component.associated_text new_goal.associated_text_color = dragged_component.associated_text_color if (new_goal.associated_text != None) : self.listen_to_mouse_over(new_goal) # this image / text association is now done new_goal.associated_object.done = True self.draggable_items.remove(dragged_component) self.goals_to_find = self.goals_to_find - 1 if (self.goals_to_find == 0) : # removal of all the mobile stuff for draggable_item in self.draggable_items : self.remove(draggable_item) # this game page is now complete self.completed = True break if (goal_found == False) : # bad answer # so we go back to initial place dragged_component.rect = self.dragged_sprite.initial_pos if (cmp(self.game.type_to_drag, "text") == 0) : # useful only for textboxes dragged_component.text_component.rect = self.dragged_sprite.initial_pos dragged_component.box_component.rect = self.dragged_sprite.initial_pos if (dropped_on_goal_area == True) : # sound is played only in case of mistake self.game.score.wrong_answer() self.play_error_sound() self.set_dragged_sprite(None) if (goal_found == False) : self.append(dragged_component) if (dragged_component.associated_text != None) : self.listen_to_mouse_over(dragged_component) return dragged_component.draw() else : dirty_rects = goal.erase() + new_goal.draw() if (self.completed == True) : self.game.stop_timer() self.append(self.next_icon) self.next_icon.draw() dirty_rects = [self.screen.get_rect()] return dirty_rects
def get_random_associations(self) : # we position images and texts which might be displayed # (in case there would be more than one in some associations) for association in self.associations : association.select_random_items() # the amount of draggable items will be a random value between min_draggable and max_draggable if (self.score.get_arcade_mode() == False) : amount_draggable = self.min_draggable + random.randint(0, self.max_draggable-self.min_draggable) else : # in arcade mode, we force the maximum amount of drag objects amount_draggable = self.max_draggable # taking a random association # those will be the "good" answers good_associations = [] if (cmp(self.type_to_associate, "image_on_map") == 0): goals_to_find = amount_draggable else : goals_to_find = len(self.goals) for i in range(goals_to_find) : for association in self.associations : if (association.done == False) : if (association not in good_associations) : good_associations.append(association) break else : # if all the associations were found, there's nothing more to associate ! common.info("Not enough associations left to find. Activity ended") return (None, None) # select other "wrong" associations all_associations = copy.copy(good_associations) for i in range(amount_draggable - len(self.goals)) : found = False if (len(all_associations) >= len(self.associations)) : # we won't be able to find other associations (all are already selected) # so we have to stop, to avoid an infinite while loop below common.info("Not enough associations available for 'bad' choices.") common.info("Please check your xml file to add more associations or lower 'max_draggable' parameter") return (None, None) while (found == False) : other_association = self.associations[random.randint(0, len(self.associations)-1)] if (other_association not in all_associations) : all_associations.append(other_association) found = True # eventually sorting the entries if (self.sort == True) : all_associations.sort() else : all_associations = common.randomize_list(all_associations) return (good_associations, all_associations)
def __init__(self, game, globalvars, total_score) : common.info("Running association game") game = Associate_game(game, globalvars.main_language, total_score) common.Run_Screen.__init__(self, globalvars, game) self.current_page = next_page(self.game, self.globalvars) while self.running : common.Run_Screen.run_pre(self) # actions after keyboard events goto_next_page = False for key_event in self.key_events : if (key_event.key == K_SPACE) : if (self.current_page.completed == True) : goto_next_page = True if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked # we try to play an associated sound (if any) component_clicked.play_random_associated_sound() if (component_clicked == self.current_page.quit_icon) : raise common.EndActivityException() elif (component_clicked == self.current_page.next_icon) : goto_next_page = True elif ((component_clicked.associated_object != None) and (self.mousebutton == 3)) : # right click on an item (draggable or to associate) # it will be displayed in big if (component_clicked not in self.current_page.goals) : association = component_clicked.associated_object if (component_clicked in self.current_page.items_to_associate) : (text, image_file, associated_sounds) = association.get_infos_for_display_big_to_associate() else : (text, image_file, associated_sounds) = association.get_infos_for_display_big_to_drag() if (image_file != None) : if (self.current_page.completed == False) : self.game.stop_timer() self.current_page.fade_out() display_big.Run_Display_Big(self.globalvars, text, image_file, associated_sounds) # redraw the whole screen self.current_page.draw() some_dirty_rects = [self.screen.get_rect()] self.mouse_clicked = False if (self.current_page.completed == False) : self.game.start_timer() self.dirty_rects = self.dirty_rects + some_dirty_rects # mouse may be going over elements if (self.current_page.dragged_sprite == None) : # yet mouse over is checked only when no object is being dragged (mouse_over_new, mouse_over_old) = self.current_page.components_mouse_over(pygame.mouse.get_pos()) if (mouse_over_old != None) : self.dirty_rects = self.dirty_rects + self.current_page.remove_legend_text() if (mouse_over_new != None) : self.dirty_rects = self.dirty_rects + self.current_page.add_legend_text(mouse_over_new) if (goto_next_page == True) : self.current_page.fade_out() ####self.total_time = self.total_time + self.current_page.timer.getValue() self.current_page = next_page(self.game, globalvars) if (self.current_page == None) : return common.Run_Screen.run_post(self)
def __init__(self, globalvars) : common.Run_Screen.__init__(self, globalvars) tag_defs = common.load_tag_defs() self.current_page = Page_menu(globalvars, tag_defs) self.current_page.draw_back() pygame.display.flip() common.start_music(constants.default_music_files, globalvars) common.info("Looking for xml activities") for data_folder in constants.data_folders : self.current_page.games_list.append_from_folder(data_folder) common.info("Initialize igloo") self.current_page.igloo_page = igloo.create(self.globalvars) common.info("Display main menu") self.current_page.display_main_menu() self.current_page.draw() pygame.display.flip() main_page = self.current_page level = None while self.running : try : common.Run_Screen.run_pre(self) except common.EndActivityException, e : # although we should normally end after an EndActivityException, # here we'll ask for user confirmation self.running = True if (cmp(self.current_page.page_type, "MAIN") == 0) : self.dirty_rects = self.current_page.display_quit() elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0) : self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else : self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked != None) : # we try to play an associated sound (if any) component_clicked.play_random_associated_sound() if (cmp(self.current_page.page_type, "DIALOG_BOX_QUIT") == 0) : if (component_clicked == self.current_page.quit_yes) : self.running = False elif (component_clicked == self.current_page.quit_no): self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "MAIN") == 0) : if (component_clicked == self.current_page.igloo_image) : self.current_page.run_igloo(False) self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] elif (component_clicked == self.current_page.language_image) : # changing main language self.current_page.remove_all() index_lang = constants.supported_languages.index(self.current_page.language) index_lang = index_lang + 1 if (index_lang >= len(constants.supported_languages)) : index_lang = 0 self.current_page.language = constants.supported_languages[index_lang] self.globalvars.main_language = constants.supported_languages[index_lang] self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False elif (component_clicked == self.current_page.quit_icon) : self.dirty_rects = self.current_page.display_quit() self.mouse_clicked = False elif (component_clicked in self.current_page.tag_icons ) : # TODO : allow multiple activities inside a game_set ! tag_icon = component_clicked tag_def = tag_icon.associated_object self.current_page.games_to_display = self.current_page.games_list.get_games_by_tag_and_language(tag_def.tag, self.current_page.language) self.dirty_rects = self.current_page.display_games_by_type() self.mouse_clicked = False elif (component_clicked == self.current_page.options_icon) : self.current_page.fade_out() options.Run_options(globalvars) self.current_page.draw() pygame.display.flip() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "GAME_SELECTION") == 0) : if (component_clicked == self.current_page.go_back_icon) : self.current_page.fade_out() self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False else : game_set = component_clicked.associated_object if (game_set != None) : # TODO allow more levels !!!! levels = game_set.levels if (len(levels) > 1) : level_page = Page_level(globalvars, self.current_page.second_background_image) self.current_page = level_page self.dirty_rects = self.current_page.select_level(levels) self.mouse_clicked = False else : # in case we have a single level activity level = levels[0] elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0) : if (component_clicked == self.current_page.go_back_icon) : # TODO : a implementer !!! self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else : associated_object = component_clicked.associated_object if (associated_object != None) : self.current_page.fade_out() # TODO allow more levels !!!! level = associated_object else : # mouse is not clicked # but it may be going over elements (mouse_over_new, mouse_over_old) = self.current_page.components_mouse_over(pygame.mouse.get_pos()) if (mouse_over_old != None) : self.dirty_rects = self.dirty_rects + self.current_page.remove_legend_text() if (mouse_over_new != None) : self.dirty_rects = self.dirty_rects + self.current_page.add_legend_text(mouse_over_new) if (level != None) : # then we'll go back to main page when game or highscore will end self.current_page = main_page if (self.mousebutton == 3) : # right click means start highscore screen self.current_page.run_highscore(level) self.mouse_clicked = False else : self.current_page.fade_out() # the level is copied in order to change its variables copy_level = copy.copy(level) # shuffle the games copy_level.games = common.randomize_list(copy.copy(copy_level.games)) amount_games_played = 0 try : total_score = common.Score(globalvars) for game in copy_level.games : if (amount_games_played < copy_level.game_rounds_amount) : if (cmp(game.game_type, "associate") == 0) : # association run_activity = associate.Run_Associate(game, globalvars, total_score) if (cmp(game.game_type, "puzzle") == 0) : # puzzle run_activity = puzzle.Run_Puzzle_Screen(game, globalvars, total_score) if (cmp(game.game_type, "memory_cards") == 0) : # memory cards run_activity = memory_cards.Run_Memory_Cards_Screen(game, globalvars, total_score) if (cmp(game.game_type, "differences") == 0) : # differences run_activity = differences.Run_Differences_Screen(game, globalvars, total_score) if (cmp(game.game_type, "transform") == 0) : # image transformation run_activity = transform.Run_Transform_Screen(game, globalvars) if (cmp(game.game_type, "learning") == 0) : # learning cards run_activity = learning.Run_Learning_Screen(game, globalvars) amount_games_played = amount_games_played + 1 else : # if all the games were played, there's nothing more to play ! common.info("Not enough games left to play. Level completed.") if (pygame.mixer.music.get_busy() == False) : common.start_music(constants.default_music_files, globalvars) # we go to the highscore screen, if activated and if some score was indeed recorded if ((total_score.get_arcade_mode() == True) and (self.globalvars.highscore == True)) : self.current_page.run_highscore(copy_level, total_score) # After a completed game_set, we get a Tux as a reward # (only in case the display_igloo global var is True if (self.globalvars.display_igloo == True) : self.current_page.run_igloo(True) self.dirty_rects = [] except common.EndActivityException, e : common.info("Activity stopped by the user") except common.BadXMLException, e: common.error("Had to stop the activity because of an error in XML setup") except Exception, e : common.error("Exception fired", e, traceback.format_exc())
from SimpleDialog import SimpleDialog root = Tk() common.error("pygtk is not available. Please install pygtk on your system.") SimpleDialog(root, i18n.get_text(common.get_user_lang(), 103) + "\n" + i18n.get_text(common.get_user_lang(), 104), ["OK"], 0, title="Omnitux").go() exit() common.info("Starting Omnitux") globalvars = common.init() common.info("Omnitux release = "+globalvars.release) common.info("Version = "+constants.version) common.info("Showing screen") pygame.display.flip() # activities and other stuff are imported here and not at the beginning # the idea is to show a black screen as soon as possible so that # the user won't click again on the omnitux icons_amount
def __init__(self, game, globalvars): common.info("Running learning game") game = Learning_game(game, globalvars.main_language) common.Run_Screen.__init__(self, globalvars, game) self.current_page = next_page(self.game, self.globalvars) while self.running: common.Run_Screen.run_pre(self) # actions after keyboard events goto_next_page = False for key_event in self.key_events: if key_event.key == K_SPACE: if self.current_page.completed == True: goto_next_page = True if self.mouse_clicked == True: clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if clicked != None: (component_clicked, foo, some_dirty_rects) = clicked # we try to play an associated sound (if any) component_clicked.play_random_associated_sound() if component_clicked == self.current_page.quit_icon: raise common.EndActivityException() elif component_clicked == self.current_page.next_icon: goto_next_page = True self.dirty_rects = self.dirty_rects + some_dirty_rects # mouse may be going over elements if self.current_page.dragged_sprite == None: # yet mouse over is checked only when no object is being dragged (mouse_over_new, mouse_over_old) = self.current_page.components_mouse_over(pygame.mouse.get_pos()) if mouse_over_old != None: self.dirty_rects = self.dirty_rects + self.current_page.remove_legend_text() if mouse_over_new != None: self.dirty_rects = self.dirty_rects + self.current_page.add_legend_text(mouse_over_new) if goto_next_page == True: self.current_page.fade_out() self.current_page = next_page(self.game, globalvars) if self.current_page == None: return common.Run_Screen.run_post(self)
def __init__(self, igloo_page) : common.info("Running Igloo") self.current_page = igloo_page common.Run_Screen.__init__(self, self.current_page.globalvars) if (igloo_page.tux_going_out != None) : if (igloo_page.next_icon in igloo_page.items) : igloo_page.mouse_sprite.background = None igloo_page.remove(igloo_page.next_icon) else : if (igloo_page.next_icon not in igloo_page.items) : igloo_page.append(igloo_page.next_icon) igloo_page.draw() pygame.display.flip() while (self.running or igloo_page.tux_going_out != None) : try : common.Run_Screen.run_pre(self) except common.EndActivityException, e : if (igloo_page.tux_going_out == None) : self.running = False # actions for mouse actions if (self.mouse_clicked == True) : clicked = igloo_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked == igloo_page.next_icon) : self.running = False # actions for keyboard for key_event in self.key_events : if (key_event.key == K_SPACE) : if (igloo_page.tux_going_out == None): # we quit only after the tux has left the igloo ! self.running = False if (igloo_page.tux_going_out != None) : self.dirty_rects = self.dirty_rects + igloo_page.tux_goes_out() common.Run_Screen.run_post(self)
def __init__(self, globalvars) : common.info("Running options screen") self.globalvars = globalvars common.Run_Screen.__init__(self, self.globalvars) self.current_page = Page_options(self.globalvars) self.current_page.draw() pygame.display.flip() while self.running : try : common.Run_Screen.run_pre(self) except common.EndActivityException, e : self.running = False if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : self.mouse_clicked = False (component_clicked, foo, some_dirty_rects) = clicked if (component_clicked == self.current_page.go_back_icon) : self.running = False elif (component_clicked == self.current_page.resolution_minus) : self.current_page.current_resolution_index = self.current_page.current_resolution_index + 1 if (self.current_page.current_resolution_index >= len(self.current_page.available_resolutions)) : self.current_page.current_resolution_index = len(self.current_page.available_resolutions) - 1 else : (self.globalvars.screen_width, self.globalvars.screen_height) = self.current_page.available_resolutions[self.current_page.current_resolution_index] self.current_page.resolution_value.set_text(str(self.globalvars.screen_width)+"x"+str(self.globalvars.screen_height)) self.dirty_rects = self.current_page.resolution_value.draw() + self.current_page.display_restart() elif (component_clicked == self.current_page.resolution_plus) : self.current_page.current_resolution_index = self.current_page.current_resolution_index - 1 if (self.current_page.current_resolution_index < 0) : self.current_page.current_resolution_index = 0 else : (self.globalvars.screen_width, self.globalvars.screen_height) = self.current_page.available_resolutions[self.current_page.current_resolution_index] self.current_page.resolution_value.set_text(str(self.globalvars.screen_width)+"x"+str(self.globalvars.screen_height)) self.dirty_rects = self.current_page.resolution_value.draw() + self.current_page.display_restart() elif ((component_clicked == self.current_page.music_minus) or (component_clicked == self.current_page.music_plus)) : if (component_clicked == self.current_page.music_minus) : self.current_page.globalvars.music_volume = self.current_page.globalvars.music_volume - 0.25 if (self.current_page.globalvars.music_volume < 0) : self.current_page.globalvars.music_volume = 0.0 elif (component_clicked == self.current_page.music_plus) : self.current_page.globalvars.music_volume = self.current_page.globalvars.music_volume + 0.25 if (self.current_page.globalvars.music_volume > 1) : self.current_page.globalvars.music_volume = 1.0 self.current_page.music_value.set_text(str(int(self.current_page.globalvars.music_volume * 100)) + "%") self.set_music_volume(self.current_page.globalvars.music_volume) self.dirty_rects = self.current_page.music_value.draw() elif (component_clicked == self.current_page.display_igloo) : self.globalvars.display_igloo = not(self.globalvars.display_igloo) self.current_page.display_igloo.set_text(i18n.bool_to_text(self.current_page.language, self.globalvars.display_igloo)) self.dirty_rects = self.current_page.display_igloo.draw() elif (component_clicked == self.current_page.arcade_mode) : self.globalvars.arcade_mode = not(self.globalvars.arcade_mode) self.current_page.arcade_mode.set_text(i18n.bool_to_text(self.current_page.language, self.globalvars.arcade_mode)) self.dirty_rects = self.current_page.arcade_mode.draw() # we may call this a bit too often, but it's easier for code maintenance :-) self.globalvars.save_user_options() common.Run_Screen.run_post(self)
def run_qsub_mr(options): # Record current working directory. Both mapper and reducer commands will run under this directory. cwd = os.getcwd() # Find input files. input_files = glob.glob(options.input) input_files.sort() common.check(input_files, 'input pattern does not list any file') common.check(len(set([basename(i) for i in input_files])) == len(input_files), 'input pattern matches input files with duplicate basename') if options.verbose: common.info('input pattern %s matched %d files', repr(options.input), len(input_files)) # Check then create output directory. common.check(options.retry or not os.path.exists(options.output), 'output directory already exists') workdir = os.path.join(options.output, '.mr') if not os.path.exists(workdir): os.makedirs(workdir) if options.verbose: common.info('workdir: %s', workdir) # Write out script to rerun this command. with common.open(os.path.join(workdir, 'rerun.bash'), 'w') as f: f.write('''# Run this script with --rerun is equivalent to running the mapreduction (assuming the input hasn't changed) cd %(cwd)s %(args)s "$@" ''' % {'cwd': pipes.quote(cwd), 'args': ' '.join(map(pipes.quote, sys.argv))}) # Write out mapper script to run on nodes. with common.open(os.path.join(workdir, 'map.node.bash'), 'w') as f: f.write(bash_start + options.mapper) # Write out mapper script to submit to qsub. with common.open(os.path.join(workdir, 'map.qsub.bash'), 'w') as f: f.write(bash_start + ''' cd %(cwd)s if [ -e %(workdir)s/map.${PBS_ARRAYID}.err ]; then mv %(workdir)s/map.${PBS_ARRAYID}.err %(workdir)s/map.${PBS_ARRAYID}.err.old fi bash %(workdir)s/map.${PBS_ARRAYID}.bash 2> %(workdir)s/map.${PBS_ARRAYID}.err ''' % {'cwd': pipes.quote(cwd), 'workdir': pipes.quote(workdir)}) # Write out individual mapper command scripts. for i, path in enumerate(input_files): with common.open(os.path.join(workdir, 'map.%d.bash' % i), 'w') as f: f.write(bash_start + ''' cd %(cwd)s %(cat)s %(path)s | bash %(workdir)s/map.node.bash | gzip - > %(workdir)s/map.%(task_id)d.out.gz touch %(workdir)s/map.%(task_id)d.success ''' % {'cwd': pipes.quote(cwd), 'cat': choose_cat(path), 'path': pipes.quote(path), 'workdir': pipes.quote(workdir), 'task_id': i}) # Write out reducer script. if options.reducer != 'NONE': with common.open(os.path.join(workdir, 'reduce.node.bash'), 'w') as f: f.write(bash_start + options.reducer) with common.open(os.path.join(workdir, 'reduce.qsub.bash'), 'w') as f: f.write(bash_start + ''' cd %(cwd)s if [ -e %(workdir)s/reduce.err ]; then mv %(workdir)s/reduce.err %(workdir)s/reduce.err.old fi find %(workdir)s -name 'map.*.out.gz' -print0 2>> %(workdir)s/reduce.err | \\ xargs -0 zcat 2>> %(workdir)s/reduce.err | \\ LC_ALL=C sort %(sort_options)s 2>> %(workdir)s/reduce.err | \\ bash %(workdir)s/reduce.node.bash 2>> %(workdir)s/reduce.err | \\ gzip - > %(workdir)s/reduce.out.gz 2>> %(workdir)s/reduce.err touch %(workdir)s/reduce.success 2>> %(workdir)s/reduce.err ''' % {'cwd': pipes.quote(cwd), 'workdir': pipes.quote(workdir), 'sort_options': '-n' if options.numerical_sort else ''}) # Run mapper jobs. for i in range(options.max_tries): # Find tasks to run. task_ids = [] for task_id in range(len(input_files)): if not os.path.exists(os.path.join(workdir, 'map.%d.success' % task_id)): task_ids.append(task_id) if not task_ids: break qsub_args = ['-N', '%s-map' % options.name, '-q', options.mapper_queue, '-l', 'pmem=%s,walltime=%s' % (options.mapper_pmem, options.mapper_walltime), '-t', format_task_ids(task_ids), '-o', os.devnull, '-e', os.devnull] + \ options.qsub_args + [os.path.join(workdir, 'map.qsub.bash')] if options.verbose: common.info('map try %d of %d: need to run %d tasks', i + 1, options.max_tries, len(task_ids)) common.info('map try %d of %d: qsub_args is %s', i + 1, options.max_tries, repr(qsub_args)) wait_qsub(qsub_args) if options.verbose: common.info('map try %d of %d: finished', i + 1, options.max_tries) map_success = 0 for task_id in range(len(input_files)): if os.path.exists(os.path.join(workdir, 'map.%d.success' % task_id)): map_success += 1 if i > 0 and options.verbose: common.info('map success: %d / %d', map_success, len(input_files)) common.check(map_success == len(input_files), 'map failed after %d tries', options.max_tries) # Run reducer jobs. if options.reducer != 'NONE': for i in range(options.max_tries): if os.path.exists(os.path.join(workdir, 'reduce.success')): break qsub_args = ['-N', '%s-reduce' % options.name, '-q', options.reducer_queue, '-l', 'pmem=%s,walltime=%s' % (options.reducer_pmem, options.reducer_walltime), '-o', os.devnull, '-e', os.devnull] + options.qsub_args + \ [os.path.join(workdir, 'reduce.qsub.bash')] if options.verbose: common.info('reduce try %d of %d: started', i + 1, options.max_tries) common.info('reduce try %d of %d: qsub_args is %s', i + 1, options.max_tries, repr(qsub_args)) wait_qsub(qsub_args) if options.verbose: common.info('reduce try %d of %d: finished', i + 1, options.max_tries) common.check(os.path.exists(os.path.join(workdir, 'reduce.success')), 'reduce failed after %d tries', options.max_tries) # Move output. if options.reducer != 'NONE': src = os.path.join(workdir, 'reduce.out.gz') dst = os.path.join(options.output, 'reduce.out.gz') subprocess.check_call(['rm', '-f', dst]) if options.keep_workdir: os.symlink(os.path.abspath(src), dst) else: os.rename(src, dst) else: for i, path in enumerate(input_files): src = os.path.join(workdir, 'map.%d.out.gz' % i) dst = os.path.join(options.output, 'map.%s.out.gz' % basename(path)) subprocess.check_call(['rm', '-f', dst]) if options.keep_workdir: os.symlink(os.path.abspath(src), dst) else: os.rename(src, dst) # Remove workdir if not options.keep_workdir: subprocess.call(['rm', '-rf', workdir])