def __try_set_entry_values(self,seed,max_repeats): try: self.__set_entry_values(seed,max_repeats) except ValueError: print traceback.print_last() return False return True
def load_food_graph(authentication, host, port, protocol): """ Loads the food graph database from Neo4J. Parameters: authentication: Two-par credentials tuple (username and password) used to authenticate to the Neo4J food database. host: Name of the host server where the Neo4J database is active. port: Communication port of the Neo4J database on the host server. protocol: Application-layer protocol used to communicate with the Neo4J database. """ logger.debug('Loading food graph DB') gr = py2neo.Graph(auth=authentication, host=host, port=port, scheme=protocol) # Verify if the DB is active try: dbname = gr.database.name except ServiceUnavailable: # Log the error and show traceback. logger.error('Neo4J service not available. Is it running?') tb.print_last() # Exit the application with error code. sys.exit(errno.ECONNREFUSED) else: return gr
def run(self): srv_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv_s.bind((self.host, self.port)) srv_s.listen(5) conn_list = [srv_s] try: while True: rd_s, wr_s, err_s = select.select(conn_list, [], []) for sock in rd_s: if sock == srv_s: new_sock, addr = srv_s.accept() addr = new_sock.getpeername() conn_list.append(new_sock) print("Client ({}) connected".format(addr)) else: try: data = sock.recv(self.bufsize) if data: self.parse_request(data.decode(), sock) except: traceback.print_last() sock.close() conn_list.remove(sock) finally: for sock in conn_list: sock.close()
def run(self): try: self.user = self.User() self.user.socket_send = self.send self.user.socket_starts() while self.running: self.user.socket_updates() self.dump_messages() now = time.time() while time.time()-now < self.send_sleep_time: #i want to read more often then dumping messages self.load_messages() time.sleep(self.send_sleep_time) except: traceback.print_last() finally: try: self.user.socket_closes() except: traceback.print_last() try: self.close() except: pass #i tryed, this is tornado's fault if it doesn't close
def get_string_buffer(self): try: ret_val = self.string_buffer.value except IOError, e: self.logr.debug("IOError trying to get string buffer: ", e.message) traceback.print_last(limit=20) ret_val = None
def create_super_user(self): assert self.client is not None, "Client is none" try: res = self.client.register_user(email=self.OR_CLIENT_USER, first='Super', last='User', password=self.OR_CLIENT_PASSWORD) except: traceback.print_last() assert res, "No result for registering user" res = self.client.activate_user( 'openreview.net', { 'names': [{ 'first': 'Super', 'last': 'User', 'username': '******' }], 'emails': ['openreview.net'], 'preferredEmail': '*****@*****.**' }) assert res, "Res i none" group = self.client.get_group(id='openreview.net') assert group assert group.members == ['~Super_User1']
def system_cat(path, head=False): ret = "" try: fd = runCode('(void*)open((char*)"' + path + '",0);') print("FD " + fd) if int(fd, 16) == 4294967295: return "Cannot open file" buf = runCode('(void*)malloc((int)10240)') print "open(" + buf + ")" i = 0 while True: de = runCode('(int)read((int)' + str(fd) + ',(void*)' + str(buf) + ',(int)1024);') count = int(de, 16) print("read(" + str(i) + ")=" + str(count)) try: data = read(int(buf, 16), count) ret = ret + str(data) except: traceback.print_last() ret = ret + ".\n" if count != 1024 and count != 0x1024: break if head: break i = i + 1 except: traceback.print_last() ret = ret + "ERR\n" cmd('e (void)free((void*)' + buf + ')') cmd('e (int)close((int)' + fd + ')') return ret
def magic(self, cmd): """ Magic commands like in IPython. """ cmd, *args = shlex.split(cmd) if cmd == 'cd': os.chdir(args[0]) elif cmd == 'pwd': print(Path('.').resolve()) elif cmd in ['ls', 'dir']: self.popen('dir') elif cmd == 'tb': traceback.print_last() elif cmd == 'info': self.info() elif cmd == 'who': tbl = RecordTable(['name', 'repr', 'str']) for key in list(self.wsdict.keys()): if key.startswith('_'): continue obj = self.wsdict[key] tbl.add_row((key, repr(obj), str(obj))) print(tbl)
def print_exception(silent=False, tb_data=None, all_frame_data=False): """ Print last Python exception and frame variables values (if available) to stdout. @keyword silent: do not print to stdout @type silent: bool @keyword tb_data: Python traceback object @type tb_data: Python traceback instance @keyword all_frame_data: print all variables in every frame @type all_frame_data: bool @return: exception data @rtype: list of strings """ if not silent: traceback.print_last() data = [] if tb_data is not None: tb = tb_data else: last_type, last_value, last_traceback = sys.exc_info() tb = last_traceback stack = [] while True: if not tb: break if not tb.tb_next: break tb = tb.tb_next if all_frame_data: stack.append(tb.tb_frame) if not all_frame_data: stack.append(tb.tb_frame) #if not returndata: print for frame in stack: if not silent: sys.stderr.write("\n") sys.stderr.write("Frame %s in %s at line %s\n" % (frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)) data.append( "Frame %s in %s at line %s\n" % (frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)) for key, value in list(frame.f_locals.items()): cur_str = "" cur_str = "\t%20s = " % key try: cur_str += repr(value) + "\n" except (AttributeError, NameError, TypeError): cur_str += "<ERROR WHILE PRINTING VALUE>\n" if not silent: sys.stdout.write(cur_str) data.append(cur_str) return data
def __eval_all_locators(input_list, return_exec=False, return_exec_name="evaluated_locators"): """ :param input_list: :type list of namedtuple(locator,key,value). An example of this is the ValueFinder tuple :param return_exec: :type boolean: flag for whether to return a code string that can be run through exec(*) :return: If return_executable is false, returns a list of all the locators run. This often returns the actual object that the string was found in. if return_executable is true, this function runs nothing and just returns a string of code that can be run as an arg to the exec function. After running the exec function on this arg, a variable called evaluated_locators will be referenceable through the locals dictionary using return_exec_name's actual value as the key i.e. by default, locals()['evaluated_locators'] """ executable_code = return_exec_name + " = []\n" \ "for x in " + repr(input_list) + ":\n" \ " " + return_exec_name + ".append(eval(x.locator))" try: if not return_exec: exec(executable_code) return locals()[return_exec_name] except KeyError: import traceback traceback.print_last() print( "Key not found in this scope. " "Consider using this function with the return_exec flag instead to run the function in the proper scope." ) else: return executable_code
def system_cat(path, head=False): ret = "" try: fd = runCode('(void*)open((char*)"'+path+'",0);') print ("FD "+fd) if int(fd,16) == 4294967295: return "Cannot open file" buf = runCode('(void*)malloc((int)10240)') print "open("+buf+")" i = 0 while True: de = runCode('(int)read((int)'+str(fd)+',(void*)'+str(buf)+',(int)1024);'); count = int (de, 16) print ("read("+str(i)+")="+str(count)) try: data = read(int(buf,16), count) ret = ret + str(data) except: traceback.print_last() ret = ret + ".\n" if count != 1024 and count != 0x1024: break if head: break i = i + 1 except: traceback.print_last() ret = ret + "ERR\n"; cmd('e (void)free((void*)'+buf+')') cmd('e (int)close((int)'+fd+')') return ret
def _action_add(self, args): for path in args.files: try: if isdir(path): # Do something about verbose... if args.walk: m = f"Ingesting the files under {path}" else: m = f"Ingesting the files at {path}" self._log(m) self.db.add_directory(path, walk=args.walk) else: self.db.add_cal(path) self._log(f"Ingested {path}") except OSError as e: try: # reproduce previous code traceback.print_last() except ValueError: pass print(e, file=sys.stderr) return -1 except ValueError as e: print(e, file=sys.stderr) return -1 return 0
def traceback_pprint(type, value, tb): traceback.print_exception(type, value, tb) tmp = 10 tmp_traceback_pprint = True pprint('---------------------Traceback pprintting:----------------------') traceback.print_stack() traceback.print_tb(tb) traceback.print_last() traceback.print_exc() traceback.print_stack() while tb.tb_next: pprint('-------------------------frame------------------------------') pprint(tb.tb_frame.f_locals) pprint(tb.tb_frame.f_globals) pprint(tb.tb_frame.f_code) pprint(tb.tb_frame.f_lineno) pprint(tb.tb_frame.f_lasti) pprint(tb.tb_frame.f_trace) for k, v in tb.tb_frame.f_locals.items(): if k == 'self': #if isinstance(v, type): print(k, v) print(vars(v)) dump(v) #pprint(isinstance(v, (type, type.ClassType))) tb = tb.tb_next pprint('---------------------Traceback pprintting:end-------------------')
def handleError(window,e,t): """Handles error Messages via messagebox""" if type(e)==IOError: traceback.print_last() msg = Gtk.MessageDialog(parent=window, flags=0, type=t, buttons=Gtk.ButtonsType.OK, message_format=str(e)) msg.run() msg.destroy()
def print_exception(silent=False, tb_data=None, all_frame_data=False): """ Print last Python exception and frame variables values (if available) to stdout. @keyword silent: do not print to stdout @type silent: bool @keyword tb_data: Python traceback object @type tb_data: Python traceback instance @keyword all_frame_data: print all variables in every frame @type all_frame_data: bool @return: exception data @rtype: list of strings """ if not silent: traceback.print_last() data = [] if tb_data is not None: tb = tb_data else: last_type, last_value, last_traceback = sys.exc_info() tb = last_traceback stack = [] while True: if not tb: break if not tb.tb_next: break tb = tb.tb_next if all_frame_data: stack.append(tb.tb_frame) if not all_frame_data: stack.append(tb.tb_frame) #if not returndata: print for frame in stack: if not silent: sys.stderr.write("\n") sys.stderr.write( "Frame %s in %s at line %s\n" % ( frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)) data.append("Frame %s in %s at line %s\n" % (frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)) for key, value in list(frame.f_locals.items()): cur_str = "" cur_str = "\t%20s = " % key try: cur_str += repr(value) + "\n" except (AttributeError, NameError, TypeError): cur_str += "<ERROR WHILE PRINTING VALUE>\n" if not silent: sys.stdout.write(cur_str) data.append(cur_str) return data
def background(): global posts global sheet while True: print('loading announcements... ', end='') try: announcements = sheet.get_current_active( datetime.timedelta(minutes=1)) if len(announcements) > 0: announcement = announcements.pop(0) text = str(json.dumps(announcement.__dict__, default=str)) send_announcement(text) sheet.set_active(announcement, False) posts = sheet.get_past() print() print('Updated posts:', posts, file=sys.stderr) else: print("no announcements") time.sleep(3) except: traceback.print_last(file=sys.stderr) time.sleep(5)
def list_readability_preset_ids(self, extension_type, from_dir = None): """Return an array of all presets in the directory """ extension = "." + extension_type elen = len(extension) result_list = [] if from_dir is None: from_dir = G.application.config.readabilityPresetsDir for file_name in os.listdir(from_dir): if file_name[-elen:] == extension: try: fh = open(from_dir / file_name, "r") json_obj = json.load(fh, "utf-8") fh.close() result_list.append({ "uuid" : json_obj['uuid'], "name": json_obj['name']}) except: #do nothing - junk file? import traceback traceback.print_last() return result_list
def main(): """ Entry point which parse parameters and run generation routine :return: exit_code which will be passed to OS """ argv = sys.argv[1:] option_connection_url = 'connection_url' option_folder = 'folder' parser = OptionParser(usage="usage: %prog [options]", version="schemadoc v%s" % (__version__,)) parser.add_option('-u', help='SQLAlchemy connection url (required)', dest=option_connection_url, action='store') parser.add_option('-o', help='Output folder (required)', dest=option_folder, action='store') opts, args = parser.parse_args(argv) if not argv or len(argv) == 0: parser.print_help() return 1 if not opts.folder: print('parameter -o folder is required', file=os.sys.stdout) parser.print_help() return 1 if not opts.connection_url: print('parameter -u connection is required', file=os.sys.stdout) parser.print_help() return 1 folder = opts.folder.strip() url = opts.connection_url.strip() try: if not os.path.exists(folder): os.makedirs(folder) elif len(os.listdir(folder)) != 0: print('Folder %s is not empty' % folder) return 1 except OSError as e: print(repr(e), file=os.sys.stdout) print('Can not create folder %s' % folder, file=os.sys.stdout) return 1 try: print("Generating documentation for %s \nOutput folder is %s" % (url, folder,), file=os.sys.stdout) _doc(url, folder) except Exception as e: print(repr(e), file=os.sys.stdout) traceback.print_last() return 1 print("Documentation is generated in folder %s" % (folder,), file=os.sys.stdout) return 0
def do_copy_targets(): global copy_targets global db, db_depend db = open_db("kbuild.db") db_depend = open_db("kbuild_dependencies.db") build_final = False try: for f in copy_targets: abspath = np(f.target) src = f[0].source if f.optional and not os.path.exists(f[0].source): continue fname = f[0].source if (os.path.sep in fname): fname = os.path.split(fname)[1] stat = safe_stat(f[0].source) skip = not (abspath not in db or db[abspath] < stat) skip = skip and not build_cmd == "clean"; skip = skip and not (build_cmd == "single" and filter.lower() in abspath.lower()) skip = skip and not (build_cmd == "single" and filter.lower() in f[0].source.lower()) if skip: continue build_final = True #db[abspath] = stat cmd = cp_handler(src, abspath) print(cmd) ret = os.system(cmd) if ret == 0: db[abspath] = stat else: print("build failure\n\n") sys.stderr.write("build failure\n"); if build_cmd != "loop": sys.exit(-1) else: break except: import traceback db.close() db_depend.close() traceback.print_stack() traceback.print_last() return build_final
def log(self, msg, type="N"): ''' Depreciated, use one of debug(), notice(), warn(), error()''' if type == "D": msgStart = "DEBUG: " elif type == "E": msgStart = "ERROR: " #traceback.print_tb() traceback.print_last() elif type == "N": msgStart = "NOTICE: " print("%s%s"%(msgStart, msg))
def kill(self, sig=signal.SIGTERM): if self.pid: logging.info('kill pid %u', self.pid) try: pdb.kill(self.pid, sig) except OSError: traceback.print_last() else: raise AlreadyStopped
def doAlignFile(args): """unpacking version of doAlign """ try: fitsName,resultDir=args doAlign(fitsName,resultDir) except Exception: tb.print_last() #raise return args
def default(self, Txt): wrds = string.split(Txt) if len(wrds) > 0: try: use_command_wrds(self, wrds) CommandsHistory.append(Txt) except: print 'words %s crashed.' % Txt traceback.print_last() return False
def __exitfunction(self): if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.", file=sys.stderr) sys.stderr.flush() sys.exit(1) elif self._hook.exception: print("Check failed to run", file=sys.stderr) traceback.print_last() sys.stdout.flush() sys.exit(2)
def log(self, msg, type="N"): ''' Depreciated, use one of debug(), notice(), warn(), error()''' if type == "D": msgStart = "DEBUG: " elif type == "E": msgStart = "ERROR: " #traceback.print_tb() traceback.print_last() elif type == "N": msgStart = "NOTICE: " print("%s%s" % (msgStart, msg))
def run(self): for file_name in self.file_list: in_path = os.path.join(self.base_path, file_name) out_path = "_out".join(os.path.splitext(in_path)) try: self._solve(in_path, out_path) except Exception: click.secho('\r\r\r failed!', fg='red') if self._debug: traceback.print_last()
def deleteObject(self, obj): try: obj.delete() except: if self.quiet is False: print "Error on object ", obj print traceback.print_last() return False else: return True
def _create_initial_package(agent_dir_to_package, wheelhouse, identity=None): """Create an initial whl file from the passed agent_dir_to_package. The function produces a wheel from the setup.py file located in agent_dir_to_package. Parameters: agent_dir_to_package - The root directory of the specific agent that is to be packaged. Returns The path and file name of the packaged whl file. """ try: tmpdir = tempfile.mkdtemp() builddir = os.path.join(tmpdir, 'pkg') distdir = os.path.join(builddir, 'dist') shutil.copytree(agent_dir_to_package, builddir) cmd = [sys.executable, 'setup.py', '--no-user-cfg', 'bdist_wheel'] response = subprocess.run(cmd, cwd=builddir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) if response.returncode != 0: raise ValueError( f"Couldn't compile agent directory: {response.stderr}") wheel_name = os.listdir(distdir)[0] wheel_path = os.path.join(distdir, wheel_name) if identity is not None: tmp_identity_file_fd, identity_template_filename = tempfile.mkstemp( dir=builddir) tmp_identity_file = os.fdopen(tmp_identity_file_fd, "w") tmp_identity_file.write(identity) tmp_identity_file.close() else: identity_template_filename = os.path.join(builddir, "IDENTITY") if os.path.exists(identity_template_filename): add_files_to_package(wheel_path, {'identity_file': identity_template_filename}) if not os.path.exists(wheelhouse): os.makedirs(wheelhouse, 0o750) wheel_dest = os.path.join(wheelhouse, wheel_name) shutil.move(wheel_path, wheel_dest) return wheel_dest except subprocess.CalledProcessError as ex: traceback.print_last() raise ex finally: shutil.rmtree(tmpdir, True)
def handler(event, context): pid = "BAD-PARAMETERS" try: requirements = event["ResourceProperties"]["Requirements"] hashed_data = requirements + " XX_VERSION_XX " + platform.python_version() rhash = hashlib.md5(hashed_data.encode("utf-8")).hexdigest() pid = f"req-{rhash}" key = f'{event["ResourceProperties"]["Prefix"]}/{rhash}.zip' bucket = event["ResourceProperties"]["Bucket"] if event["RequestType"] in ["Create", "Update"]: print(f"Installing on Python {platform.python_version()}: \n{requirements}") shutil.rmtree("/tmp/venv", ignore_errors=True) shutil.rmtree("/tmp/python", ignore_errors=True) # we create a venv so package upgrades don't attempt read-only /var/runtime libraries venv.create("/tmp/venv", with_pip=True) open("/tmp/requirements.txt", "w").write(requirements) cmd = "/tmp/venv/bin/python -m pip --no-cache-dir --disable-pip-version-check install -t /tmp/python --progress-bar off -r /tmp/requirements.txt" print(f"Running {cmd}") pip_result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) if pip_result.returncode != 0: # response is limited to 4096 bytes total cfn_response(event, context, FAILED, pid, None, f"pip failed [{pip_result.returncode}]:\n\n[...] {pip_result.stdout[-700:]}") return print(pip_result.stdout) print(f"Building requirements package...") with zipfile.ZipFile("/tmp/python.zip", "w") as z: for root, folders, files in os.walk("/tmp/python"): for f in files: local_path = os.path.join(root, f) zip_path = os.path.relpath(local_path, "/tmp") z.write(local_path, zip_path, zipfile.ZIP_DEFLATED) print(f"Uploading to s3://{bucket}/{key}") boto3.client("s3").upload_file("/tmp/python.zip", bucket, key) cfn_response(event, context, SUCCESS, pid, {"Key": key}) except Exception as e: try: traceback.print_last() except ValueError: print("Caught exception but unable to print stack trace") print(e) cfn_response(event, context, FAILED, pid, None, str(e))
def clean_up(): global server_global curses.nocbreak() curses.echo() curses.endwin() try: traceback.print_last() except: pass if server_global: if server_global.running() == None: server_global.process.terminate() print "Minecraft closed, so let's shut down."
def RestoreSTDOUT(): """ Restore original STDOUT, need to be called after `UnbufferedSTDOUT` to get back to default state. """ global OriginalStdOut if OriginalStdOut is None: return try: dhn_std_out = sys.stdout sys.stdout = OriginalStdOut dhn_std_out.close() except: traceback.print_last(file=open('dhnio.shutdown.error', 'w'))
def show_error(self, err): if type(err) == str: title = '[Error]' else: title = '[{}]'.format(type(err).__name__) try: traceback.print_last() except Exception as e: pass self.error.emit(title + '\n' + str(err)) log.error(title + '\n' + str(err))
def grabBandPage(url, bandid): try: Grabber.Get(url) #googletag.pubads().setTargeting("tag", "punk,punkrock,british,70s,rock,classicrock,classicpunk,alternative,sexpistols,britishpunk"); line = re.findall('googletag\.pubads\(\)\.setTargeting\("tag", "([^"]+)"\);', page) #find all tag-words if not len(line): return #RETURN WHAT??? tags = line[0].split(',') for item in tags: genreid = insertGenreName(item) insertGenreBandMx(bandid, genreid) except: traceback.print_last() Error("Can't grab band page %s for bandid=%s" % (url , bandid))
def convert(path): print(path) files = glob(path) print('Converting %d files in %s' % (len(files), path)) for f in files: if f[-9:] == '.sas7bdat': fout = f[:-9] + '.csv' try: raw = sas7bdat.SAS7BDAT(f) raw.convert_file(fout) except: try: traceback.print_last() except: print('Exception trying to print exception: %s' % f)
def deleteObjectByName(self, containerName, objectName): try: retVal = self.cf.delete_object(containerName, objectName) except: if self.quiet is False: print "Error on object ", objectName print traceback.print_last() return False if retVal is False: if self.quiet is False: print "Error reported on ", objectName return retVal
def fetch(self): """Always use this to fetch the data, so that :py:meth:`setup` and :py:meth:`pulldown` are used to setup and pull down the environmet used by :py:meth:`do_fetch`. :returns: the instance of a subclass of \ :py:class:`~pyfusion.data.base.BaseData` or \ :py:class:`~pyfusion.data.base.BaseDataSet` returned by \ :py:meth:`do_fetch` """ if pyfusion.DEBUG > 3: exception = None # defeat the try/except else: exception = Exception try: self.setup() except exception as details: raise LookupError("%s\n%s" % (self.error_info(step='setup'), details)) try: data = self.do_fetch() except Exception as details: # put None here to show exceptions. # then replace with Exection once # "error_info" is working well # this is to provide traceback from deep in a call stack # the normal traceback doesn't see past the base.py into whichever do_fetch # this simple method doesn't work, as it only has info after getting to the prompt if hasattr(sys, "last_type"): traceback.print_last() else: print( 'sys has not recorded any exception - needs to be at prompt?' ) # this one DOES work. print(sys.exc_info()) (extype, ex, tb) = sys.exc_info() for tbk in traceback.extract_tb(tb): print("Line {0}: {1}, {2}".format(tbk[1], tbk[0], tbk[2:])) raise LookupError("%s\n%s" % (self.error_info(step='do_fetch'), details)) data.meta.update({'shot': self.shot}) # Coords shouldn't be fetched for BaseData (they are required # for TimeSeries) #data.coords.load_from_config(**self.__dict__) self.pulldown() return data
def chat_api(request): if request.method == 'POST': d = json.loads(request.body) msg = d.get('msg') user = request.user.username gravatar = request.user.profile.gravatar_url m = Message(user=user, message=msg, gravatar=gravatar) m.save() try: botUser = bot() bot_response = botUser.get_response(m.message) mbot = Message(user=botUser.name, message=bot_response, gravatar=gravatar) mbot.save() except Exception as e: traceback.print_last() print("--------------------------") print(e) res = { 'id': m.id, 'msg': m.message, 'user': m.user, 'time': m.time.strftime('%I:%M:%S %p').lstrip('0'), 'gravatar': m.gravatar } data = json.dumps(res) # res['msg']='Bot message' # res['user']='******' # json.dumps(res) return HttpResponse(data, content_type="application/json") # get request r = Message.objects.order_by('-time')[:70] res = [] for msgs in reversed(r): res.append({ 'id': msgs.id, 'user': msgs.user, 'msg': msgs.message, 'time': msgs.time.strftime('%I:%M:%S %p').lstrip('0'), 'gravatar': msgs.gravatar }) data = json.dumps(res) return HttpResponse(data, content_type="application/json")
def recv_forever(self): while True: try: msg = self.recv() if self.args.verbosity > 2: self.print_local("msg: {}".format(msg)) msg_str = msg.decode(errors="ignore") if len(msg) != len(msg.decode(errors="ignore")): self.print_local("Decoding error: {} chars removed".format( len(msg) - len(msg.decode(errors="ignore")))) if self.args.verbosity > 2: self.print_local("Received {} chars".format(len(msg))) split = msg_str.split("\n", 1) if not self.args.echo and len(split) > 1 and split[0].strip( "\r") == self.stats_dict["last_cmd"]: if self.args.verbosity > 2: self.print_local("Removing echo : {}".format( split[0].strip('\r'))) msg_str = split[1] if self.assume_prompt: split = msg_str.rsplit("\n", 1) if not self.prompt_suffix or split[-1].endswith( self.prompt_suffix): self.prompt_suffix = split[-1][-2:] self.set_prompt(split[-1]) # If there isn't actually a prompt, this is very, very dangerous if len(split) > 1: msg_str = split[0] + '\n' else: # we set it to the prompt_str already. Not safe, but okay msg_str = "" if self.args.verbosity > 2: self.print_local("msg_str: {}".format(msg_str)) if not self.args.save or self.args.tee: self.print_local(msg_str, end="") if self.args.save: with open(self.args.save_file, "a") as f: f.write(msg_str) except NetcatError as e: if "dropped" in e.args[0]: self.print_local("!!CLIENT DISCONNECTED!!") print("Waiting for new connection...") self._init_nc() except Exception as e: self.print_local("!!RECEIVE FAILED!!") import traceback traceback.print_last()
def chat_api(request): if request.method == "POST": d = json.loads(request.body) msg = d.get("msg") user = request.user.username gravatar = request.user.profile.gravatar_url m = Message(user=user, message=msg, gravatar=gravatar) m.save() try: botUser = bot() bot_response = botUser.get_response(m.message) mbot = Message(user=botUser.name, message=bot_response, gravatar=gravatar) mbot.save() except Exception as e: traceback.print_last() print("--------------------------") print(e) res = { "id": m.id, "msg": m.message, "user": m.user, "time": m.time.strftime("%I:%M:%S %p").lstrip("0"), "gravatar": m.gravatar, } data = json.dumps(res) # res['msg']='Bot message' # res['user']='******' # json.dumps(res) return HttpResponse(data, content_type="application/json") # get request r = Message.objects.order_by("-time")[:70] res = [] for msgs in reversed(r): res.append( { "id": msgs.id, "user": msgs.user, "msg": msgs.message, "time": msgs.time.strftime("%I:%M:%S %p").lstrip("0"), "gravatar": msgs.gravatar, } ) data = json.dumps(res) return HttpResponse(data, content_type="application/json")
def flash_device(): """Connect ot the device. Returns: bool: True if any device is detected. """ try: return bool( subprocess.run( ('heimdall', 'flash', '--RECOVERY', 'recovery.img') ).returncode == 0 ) except Exception: traceback.print_last() # Show the exception on terminal. return False
def show(root=None, debug=False, parent=None): """Display Scene Inventory GUI Arguments: debug (bool, optional): Run in debug-mode, defaults to False parent (QtCore.QObject, optional): When provided parent the interface to this QObject. """ try: module.window.close() del module.window except (RuntimeError, AttributeError): pass if debug: import traceback sys.excepthook = lambda typ, val, tb: traceback.print_last() with tools_lib.application(): window = Window(parent) window.show() window.setStyleSheet(style.load_stylesheet()) window.refresh() module.window = window
def receiveData(self): self.output = "" try: if self.getDebug(): print("Client: REC: Waiting to receive data") data = self.socket.recv(BYTES_TO_TRANSFER) if self.getDebug(): print("Client: REC: Data received ({})".format(len(data))) self.output = str(data.decode("utf-8")) return True except OSError: if self.getDebug(): print("Client: OSError") traceback.print_last() return False return True
def show(debug=False, parent=None, use_context=False): """Display Loader GUI Arguments: debug (bool, optional): Run loader in debug-mode, defaults to False parent (QtCore.QObject, optional): The Qt object to parent to. use_context (bool): Whether to apply the current context upon launch """ # Remember window if module.window is not None: try: module.window.show() # If the window is minimized then unminimize it. if module.window.windowState() & QtCore.Qt.WindowMinimized: module.window.setWindowState(QtCore.Qt.WindowActive) # Raise and activate the window module.window.raise_() # for MacOS module.window.activateWindow() # for Windows module.window.refresh() return except RuntimeError as e: if not e.message.rstrip().endswith("already deleted."): raise # Garbage collected module.window = None if debug: import traceback sys.excepthook = lambda typ, val, tb: traceback.print_last() io.install() any_project = next(project for project in io.projects() if project.get("active", True) is not False) api.Session["AVALON_PROJECT"] = any_project["name"] module.project = any_project["name"] with lib.application(): window = Window(parent) window.setStyleSheet(style.load_stylesheet()) window.show() if use_context: context = { "asset": api.Session['AVALON_ASSET'], "silo": api.Session['AVALON_SILO'] } window.set_context(context, refresh=True) else: window.refresh() module.window = window
def listener(self): self.session = PromptSession() recv_thread = threading.Thread(target=self.recv_forever) recv_thread.daemon = True try: recv_thread.start() while True: with patch_stdout(): if self.assume_prompt: # Slight delay in case the prompt changed time.sleep(0.25) if not self.prompt_str: if self.args.verbosity > 1: self.print_local( "Waiting to receive prompt for RHOST...") time.sleep(0.25) if not self.prompt_str: if self.args.verbosity > 1: self.print_local( "Using default prompt (#)") self.set_prompt('#') if self.stats_dict["cmds_sent"] > 0: self.assume_prompt = False #self.print_local("\nUsing prompt: {}\n".format(self.prompt_str.encode())) cmd = self.session.prompt(str(self.prompt_str)) # A command has been entered if cmd.startswith(self.esc): self.handle_builtin(cmd[len(self.esc):]) continue if self.args.save_wait and not self.args.save_started: self.args.save = False self.args.save_wait = False elif self.args.save: self.args.save_wait = True try: self.send(cmd) except (socket.error, NetcatError): self.print_local("!!SEND FAILED!!") except Exception as e: self.print_local("Exception: {}".format(e)) import traceback traceback.print_last() finally: self.nc.close()
def run(self, **options): if sys.platform in ("win32", "cygwin"): # Bugfix for: # can't open file 'C:\Program Files\Django-ForRunners\Scripts\for_runners': [Errno 2] No such file or directory executable = Path( Path(sys.argv[0]).parent, "for_runners-script.py") print("Patch executeable to: %s" % executable) assert executable.is_file( ), "Executeable not found here: %s" % executable sys.argv[0] = str(executable) try: super().run(**options) except Exception: traceback.print_last() raise
def settingsWriter(): global _SETTINGS_FILE try: print( r"(Include '/' at the end of URLs and '/' or '\\' to File Paths depending on OS!)" ) domain = input("Enter the API URL/IP Address: ") _SETTINGS_FILE.write("Domain; " + domain + "\n") ImagePath = input( "Enter the Path to a folder to save Captured Images: ") _SETTINGS_FILE.write("ImagePath; " + ImagePath + "\n") Octoprint = input( "Enter the Domain/IP Address of the Raspberry Pi running OctoPrint: " ) _SETTINGS_FILE.write("OctoPiAddress; " + Octoprint + "\n") Octoprint_API = input("Enter the Octoprint API Key: ") _SETTINGS_FILE.write("OctoPiKey; " + Octoprint_API + "\n") Arduino_Baudrate = input("Enter the Arduino Baudrate: ") _SETTINGS_FILE.write("ArduinoBaudrate; " + Arduino_Baudrate + "\n") ImageCaptureInterval = input( "Enter how often to take a picture (In Seconds!): ") _SETTINGS_FILE.write("ImageInterval; " + ImageCaptureInterval + "\n") PrinterPollInterval = input( "Enter how often to Poll the printer (In Seconds!): ") _SETTINGS_FILE.write("PollInterval; " + PrinterPollInterval + "\n") ArduinoInactivityLength = input( "Enter how long the Arduino can be inactive for (In Seconds!): ") _SETTINGS_FILE.write("ArduinoInactivityLength; " + ArduinoInactivityLength + "\n") deviceName = platform.uname()[1] _SETTINGS_FILE.write("DeviceName; " + deviceName + "\n") #Appends the web method name to the end of the RegAPI = domain + "api/DeviceRegistration/Register" headers = {'Content-Type': 'application/x-www-form-urlencoded'} files = 'DevName=' + deviceName resp = requests.request("POST", RegAPI, headers=headers, data=files) UUID = resp.text _SETTINGS_FILE.write("DeviceUUID; " + UUID + "\n") print("File Successfully written!") input("Press Enter to Exit Program...") except: print("A problem was encountered, printing stacktrace") traceback.print_last() finally: _SETTINGS_FILE.close()
def root_only(*args, **kwargs): if not parallel_capable: return func(*args, **kwargs) comm = _get_comm(args) rv = None if comm.rank == 0: try: rv = func(*args, **kwargs) all_clear = 1 except Exception as ex: traceback.print_last() all_clear = 0 else: all_clear = None all_clear = comm.mpi_bcast(all_clear) if not all_clear: raise RuntimeError return rv
def root_only(*args, **kwargs): if not parallel_capable: return func(*args, **kwargs) comm = _get_comm(args) rv = None if comm.rank == 0: try: rv = func(*args, **kwargs) all_clear = 1 except Exception: traceback.print_last() all_clear = 0 else: all_clear = None all_clear = comm.mpi_bcast(all_clear) if not all_clear: raise RuntimeError return rv
def activar_widgets(self, s, chequear_permisos = True): """ Activa o desactiva (sensitive=True/False) todos los widgets de la ventana que dependan del objeto mostrado. Entrada: s debe ser True o False. En todo caso se evaluará como boolean. """ if self.objeto == None: s = False ws = [] for w in ws: try: self.wids[w].set_sensitive(s) except Exception, msg: print "Widget problemático:", w, "Excepción:", msg import traceback traceback.print_last()
def main(sources): for f in sources: if not f.lower().endswith(".js"): continue print("processing", os.path.split(f)[1]) sys.stdout.flush() sys.stderr.flush() glob.reset() try: js_fix_module_refs.main(f, sources) except: traceback.print_stack() traceback.print_last() print("Error processing", f)
def _create_initial_package(agent_dir_to_package, wheelhouse, identity=None): '''Create an initial whl file from the passed agent_dir_to_package. The function produces a wheel from the setup.py file located in agent_dir_to_package. Parameters: agent_dir_to_package - The root directory of the specific agent that is to be packaged. Returns The path and file name of the packaged whl file. ''' tmpdir = tempfile.mkdtemp() try: builddir = os.path.join(tmpdir, 'pkg') distdir = os.path.join(builddir, 'dist') shutil.copytree(agent_dir_to_package, builddir) subprocess.check_call([sys.executable, 'setup.py', '--no-user-cfg', 'bdist_wheel'], cwd=builddir, stderr=subprocess.STDOUT) wheel_name = os.listdir(distdir)[0] wheel_path = os.path.join(distdir, wheel_name) if identity is not None: tmp_identity_file_fd, identity_template_filename = tempfile.mkstemp(dir=builddir) tmp_identity_file = os.fdopen(tmp_identity_file_fd, "w") tmp_identity_file.write(identity) tmp_identity_file.close() else: identity_template_filename = os.path.join(builddir, "IDENTITY") if os.path.exists(identity_template_filename): add_files_to_package(wheel_path, {'identity_file': identity_template_filename}) if not os.path.exists(wheelhouse): os.makedirs(wheelhouse, 0o750) wheel_dest = os.path.join(wheelhouse, wheel_name) shutil.move(wheel_path, wheel_dest) return wheel_dest except subprocess.CalledProcessError as ex: traceback.print_last() finally: shutil.rmtree(tmpdir, True)
def index(self): iteration = None session = None try: session = dbsession() iteration = session.query(func.max(cm2db.RowMember.iteration)) except: tb.print_last() tmpl = env.get_template('not_available.html') return tmpl.render(locals()) finally: if session is not None: session.close if iteration is not None: return self.real_index() else: tmpl = env.get_template('not_available.html') return tmpl.render(locals())
def fetch(self): """Always use this to fetch the data, so that :py:meth:`setup` and :py:meth:`pulldown` are used to setup and pull down the environmet used by :py:meth:`do_fetch`. :returns: the instance of a subclass of \ :py:class:`~pyfusion.data.base.BaseData` or \ :py:class:`~pyfusion.data.base.BaseDataSet` returned by \ :py:meth:`do_fetch` """ if pyfusion.DEBUG>3: exception = None # defeat the try/except else: exception = Exception try: self.setup() except exception as details: raise LookupError("%s\n%s" % (self.error_info(step='setup'),details)) try: data = self.do_fetch() except Exception as details: # put None here to show exceptions. # then replace with Exection once # "error_info" is working well # this is to provide traceback from deep in a call stack # the normal traceback doesn't see past the base.py into whichever do_fetch # this simple method doesn't work, as it only has info after getting to the prompt if hasattr(sys, "last_type"):traceback.print_last() else: print('sys has not recorded any exception - needs to be at prompt?') # this one DOES work. print(sys.exc_info()) (extype, ex, tb) = sys.exc_info() for tbk in traceback.extract_tb(tb): print("Line {0}: {1}, {2}".format(tbk[1],tbk[0],tbk[2:])) raise LookupError("%s\n%s" % (self.error_info(step='do_fetch'),details)) data.meta.update({'shot':self.shot}) # Coords shouldn't be fetched for BaseData (they are required # for TimeSeries) #data.coords.load_from_config(**self.__dict__) self.pulldown() return data
def activar_widgets(self, s, chequear_permisos = True): """ Activa o desactiva (sensitive=True/False) todos los widgets de la ventana que dependan del objeto mostrado. Entrada: s debe ser True o False. En todo caso se evaluará como boolean. """ if self.objeto == None: s = False ws = tuple(["XXXWidgets_que_no_tengan_«adaptador»_en_el_diccionario_del_constructor", "XXXtv_treeview", "b_borrar"] + [self.dic_campos[k] for k in self.dic_campos.keys()]) # XXX: b_nuevo y b_buscar no se activan/desactivan aquí, sino en el # chequeo de permisos. for w in ws: try: self.wids[w].set_sensitive(s) except Exception, msg: print "Widget problemático:", w, "Excepción:", msg import traceback traceback.print_last()
def activar_widgets(self, s, chequear_permisos = True): """ Activa o desactiva (sensitive=True/False) todos los widgets de la ventana que dependan del objeto mostrado. Entrada: s debe ser True o False. En todo caso se evaluará como boolean. """ if self.objeto == None: s = False ws = tuple(["tv_concentracion", "b_borrar", "b_add_concentracion", "b_drop_concentracion"] + [self.dic_campos[k] for k in self.dic_campos.keys()]) for w in ws: try: self.wids[w].set_sensitive(s) except Exception, msg: print "Widget problemático:", w, "Excepción:", msg import traceback traceback.print_last()
def system_ls(path): ret = "" print ("LS("+path+")") try: ptr = runCode('(void*)opendir((char*)"'+path+'");') if int(ptr,16) == 0: return "Cannot find directory" print "opendir("+ptr+")" while True: de = runCode('(void*)readdir((void*)'+ptr+');'); #print ("readdir()="+de) if int(de,16) == 0: break row = cmd('x/1s '+de+'+0x15') print (row.strip()) ret = ret + row runCode('(int)closedir((void*)'+ptr+')') except: traceback.print_last() ret = ret + "ERR\n"; return ret
def get_instructions(args): try: if args['search_method'] == 'seapi': seAPI = stackAPI() search_results = seAPI.search(args['query']) quota_remaining = search_results['quota_remaining'] search_results = search_results['items'] links = [ result['link'] for result in search_results ] titles = [ (result['title'], result['link']) for result in search_results ] elif args['search_method'] == 'google': quota_remaining = -1 links = get_links(args['query']) answers = [] if links: initial_position = args['pos'] answers_total = min(args['num_answers'], len(links)) for answer_number in range(answers_total): (is_fulltext, answer) = get_answer(args, links[answer_number]) if not answer: continue params = {} params['link'] = titles[answer_number][1] params['title'] = titles[answer_number][0] + ' (%d/%d)' % (answer_number + 1, answers_total) max_row_length = max( [ len(params['link']) + 2, len(params['title']) + 2 ] + map(len, answer.split(u"\n")) ) row_formatting = u'| {0:<' + str(max_row_length) + '} |' if is_fulltext: max_row_length = max(len(params['link']), len(params['title'])) row_formatting = u'| {0:<' + str(max_row_length) + '} |\n' answer = u''.join([ u"".join([ row_formatting.format(inner_row) for inner_row in textwrap.fill(a_row, width = max_row_length).split(u"\n")]) for a_row in answer.strip().split(u"\n") ]).strip() else: answer = u"\n".join([ row_formatting.format(answer_row) for answer_row in answer.split(u"\n") ]) params['answer'] = answer params['underline'] = '-'*(max_row_length + 2) answer = ANSWER_TEMPLATE.replace('length', str(max_row_length)).format(**params) answers.append(answer) except Exception as e: print 'Exception:', e traceback.print_last() return (quota_remaining, answers)