def reportabug(): path = os.path.join(request.folder, 'errors') course = request.vars['course'] uri = request.vars['page'] username = '******' email = 'anonymous' code = None ticket = None pagerequest = None if request.vars.code: code = request.vars.code ticket = request.vars.ticket.split('/')[1] uri = request.vars.requested_uri error = RestrictedError() error.load(request, request.application, os.path.join(path, ticket)) ticket = error.traceback if auth.user: username = auth.user.username email = auth.user.email course = auth.user.course_name return dict(course=course, uri=uri, username=username, email=email, code=code, ticket=ticket)
def reportabug(): path = os.path.join(request.folder, 'errors') course = request.vars['course'] uri = request.vars['page'] username = '******' email = 'anonymous' code = None ticket = None registered_user = False if request.vars.code: code = request.vars.code ticket = request.vars.ticket.split('/')[1] uri = request.vars.requested_uri error = RestrictedError() error.load(request, request.application, os.path.join(path, ticket)) ticket = error.traceback if auth.user: username = auth.user.username email = auth.user.email course = auth.user.course_name registered_user = True return dict(course=course, uri=uri, username=username, email=email, code=code, ticket=ticket, registered_user=registered_user)
def reportabug(): path = os.path.join(request.folder, "errors") course = request.vars["course"] uri = request.vars["page"] username = "******" email = "anonymous" code = None ticket = None registered_user = False if request.vars.code: code = request.vars.code ticket = request.vars.ticket.split("/")[1] uri = request.vars.requested_uri error = RestrictedError() error.load(request, request.application, os.path.join(path, ticket)) ticket = error.traceback if auth.user: username = auth.user.username email = auth.user.email course = auth.user.course_name registered_user = True return dict( course=course, uri=uri, username=username, email=email, code=code, ticket=ticket, registered_user=registered_user, )
def file_reader(filename, mode='rb'): try: with open(filename, mode) as fp: body = fp.read() return body except IOError: raise RestrictedError(filename, '', 'Unable to find the file')
def ticket(): """ Ticket handler """ import traceback from gluon.restricted import RestrictedError if len(request.args) != 2: session.error = T("Invalid ticket") redirect(URL(r=request)) app = request.args[0] ticket = request.args[1] e = RestrictedError() e.load(request, app, ticket) return dict(app=app, ticket=ticket, traceback=Traceback(e.traceback), code=e.code, layer=e.layer)
def parse_template(filename, path='views/', context=dict(), lexers={}, delimiters=('{{', '}}')): """ Args: filename: can be a view filename in the views folder or an input stream path: is the path of a views folder context: is a dictionary of symbols used to render the template lexers: dict of custom lexers to use delimiters: opening and closing tags """ # First, if we have a str try to open the file if isinstance(filename, str): try: fp = open(os.path.join(path, filename), 'rb') text = fp.read() fp.close() except IOError: raise RestrictedError(filename, '', 'Unable to find the file') else: text = filename.read() # Use the file contents to get a parsed template and return it. return str( TemplateParser(text, context=context, path=path, lexers=lexers, delimiters=delimiters))
def ticket(): """ Ticket viewer """ if len(request.args) != 2: session.error = T("Invalid ticket") redirect(URL(r=request)) app = request.args[0] ticket = request.args[1] from gluon.restricted import RestrictedError e = RestrictedError() e.load(request, app, ticket) return {"app": app, "ticket": ticket, "traceback": s3base.Traceback(e.traceback), "code": e.code, "layer": e.layer, }
def ticket(): """ Ticket handler """ import traceback from gluon.restricted import RestrictedError if len(request.args) != 2: session.error = T("Invalid ticket") redirect(URL(r=request)) app = request.args[0] ticket = request.args[1] e = RestrictedError() e.load(request, app, ticket) return dict(app=app, ticket=ticket, traceback=s3base.Traceback(e.traceback), code=e.code, layer=e.layer)
def log_exception(fp): """ Copiado de gluon.restricted.RestrictedError.log. Encapsula a lógica de criação de ticket para a última exceção capturada. :param fp: o file path do arquivo a ser utilizado :type fp: str """ def get_code(file_path): with open(file_path) as f: return f.read() fp = os.path.realpath(fp) etype, evalue, tb = sys.exc_info() # XXX Show exception in Wing IDE if running in debugger if __debug__ and 'WINGDB_ACTIVE' in os.environ: sys.excepthook(etype, evalue, tb) output = "TICKET LOGGER : %s %s" % (etype, evalue) RestrictedError(layer=fp, code=get_code(fp), output=output, environment=None).log(current.request)
global_settings.slack_hook = global_settings.slack_hook or \ 'https://hooks.slack.com/services/your_service' # ## END CONFIGURATION while 1: for file_name in os.listdir(path): if file_name == 'slack_errors.pickle': continue if not ALLOW_DUPLICATES: key = md5_hash(file_name) if key in hashes: continue hashes[key] = 1 error = RestrictedError() try: error.load(request, request.application, file_name) except Exception as _: continue # not an exception file? url = URL(a='admin', f='ticket', args=[request.application, file], scheme=True) payload = json.dumps( dict(text="Error in %(app)s.\n%(url)s" % dict(app=request.application, url=url))) requests.post(global_settings.slack_hook, data=dict(payload=payload))
if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## try: if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') except: pass e = RestrictedError('Framework', '', '', locals()) ticket = e.log(request) or 'unrecoverable' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) finally: if response and hasattr(response, 'session_file') \ and response.session_file: response.session_file.close() session._unlock(response) http_response, new_environ = try_rewrite_on_error( http_response, request, environ, ticket) if not http_response:
def wsgibase(environ, responder): """ The gluon wsgi application. The first function called when a page is requested (static or dynamic). It can be called by paste.httpserver or by apache mod_wsgi (or any WSGI-compatible server). - fills request with info - the environment variables, replacing '.' with '_' - adds web2py path and version info - compensates for fcgi missing path_info and query_string - validates the path in url The url path must be either: 1. for static pages: - /<application>/static/<file> 2. for dynamic pages: - /<application>[/<controller>[/<function>[/<sub>]]][.<extension>] The naming conventions are: - application, controller, function and extension may only contain `[a-zA-Z0-9_]` - file and sub may also contain '-', '=', '.' and '/' """ eget = environ.get current.__dict__.clear() request = Request(environ) response = Response() session = Session() env = request.env #env.web2py_path = global_settings.applications_parent env.web2py_version = web2py_version #env.update(global_settings) static_file = False http_response = None try: try: try: # ################################################## # handle fcgi missing path_info and query_string # select rewrite parameters # rewrite incoming URL # parse rewritten header variables # parse rewritten URL # serve file if static # ################################################## fixup_missing_path_info(environ) (static_file, version, environ) = url_in(request, environ) response.status = env.web2py_status_code or response.status if static_file: if eget('QUERY_STRING', '').startswith('attachment'): response.headers['Content-Disposition'] \ = 'attachment' if version: response.headers['Cache-Control'] = 'max-age=315360000' response.headers[ 'Expires'] = 'Thu, 31 Dec 2037 23:59:59 GMT' response.stream(static_file, request=request) # ################################################## # fill in request items # ################################################## app = request.application # must go after url_in! if not global_settings.local_hosts: local_hosts = set(['127.0.0.1', '::ffff:127.0.0.1', '::1']) if not global_settings.web2py_runtime_gae: try: fqdn = socket.getfqdn() local_hosts.add(socket.gethostname()) local_hosts.add(fqdn) local_hosts.update([ addrinfo[4][0] for addrinfo in getipaddrinfo(fqdn)]) if env.server_name: local_hosts.add(env.server_name) local_hosts.update([ addrinfo[4][0] for addrinfo in getipaddrinfo(env.server_name)]) except (socket.gaierror, TypeError): pass global_settings.local_hosts = list(local_hosts) else: local_hosts = global_settings.local_hosts client = get_client(env) x_req_with = str(env.http_x_requested_with).lower() cmd_opts = global_settings.cmd_options request.update( client = client, folder = abspath('applications', app) + os.sep, ajax = x_req_with == 'xmlhttprequest', cid = env.http_web2py_component_element, is_local = (env.remote_addr in local_hosts and client == env.remote_addr), is_shell = False, is_scheduler = False, is_https = env.wsgi_url_scheme in HTTPS_SCHEMES or \ request.env.http_x_forwarded_proto in HTTPS_SCHEMES \ or env.https == 'on' ) request.url = environ['PATH_INFO'] # ################################################## # access the requested application # ################################################## disabled = pjoin(request.folder, 'DISABLED') if not exists(request.folder): if app == rwthread.routes.default_application \ and app != 'welcome': redirect(URL('welcome', 'default', 'index')) elif rwthread.routes.error_handler: _handler = rwthread.routes.error_handler redirect(URL(_handler['application'], _handler['controller'], _handler['function'], args=app)) else: raise HTTP(404, rwthread.routes.error_message % 'invalid request', web2py_error='invalid application') elif not request.is_local and exists(disabled): raise HTTP(503, "<html><body><h1>Temporarily down for maintenance</h1></body></html>") # ################################################## # build missing folders # ################################################## create_missing_app_folders(request) # ################################################## # get the GET and POST data # ################################################## #parse_get_post_vars(request, environ) # ################################################## # expose wsgi hooks for convenience # ################################################## request.wsgi = LazyWSGI(environ, request, response) # ################################################## # load cookies # ################################################## if env.http_cookie: for single_cookie in env.http_cookie.split(';'): single_cookie = single_cookie.strip() if single_cookie: try: request.cookies.load(single_cookie) except Cookie.CookieError: pass # single invalid cookie ignore # ################################################## # try load session or create new session file # ################################################## if not env.web2py_disable_session: session.connect(request, response) # ################################################## # run controller # ################################################## if global_settings.debugging and app != "admin": import gluon.debug # activate the debugger gluon.debug.dbg.do_debug(mainpyfile=request.folder) serve_controller(request, response, session) except HTTP as hr: http_response = hr if static_file: return http_response.to(responder, env=env) if request.body: request.body.close() if hasattr(current, 'request'): # ################################################## # on success, try store session in database # ################################################## if not env.web2py_disable_session: session._try_store_in_db(request, response) # ################################################## # on success, commit database # ################################################## if response.do_not_commit is True: BaseAdapter.close_all_instances(None) elif response.custom_commit: BaseAdapter.close_all_instances(response.custom_commit) else: BaseAdapter.close_all_instances('commit') # ################################################## # if session not in db try store session on filesystem # this must be done after trying to commit database! # ################################################## if not env.web2py_disable_session: session._try_store_in_cookie_or_file(request, response) # Set header so client can distinguish component requests. if request.cid: http_response.headers.setdefault( 'web2py-component-content', 'replace') if request.ajax: if response.flash: http_response.headers['web2py-component-flash'] = \ urllib2.quote(xmlescape(response.flash).replace(b'\n', b'')) if response.js: http_response.headers['web2py-component-command'] = \ urllib2.quote(response.js.replace('\n', '')) # ################################################## # store cookies in headers # ################################################## session._fixup_before_save() http_response.cookies2headers(response.cookies) ticket = None except RestrictedError as e: if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## # log tickets before rollback if not in DB if not request.tickets_db: ticket = e.log(request) or 'unknown' # rollback if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') # if tickets in db, reconnect and store it in db if request.tickets_db: ticket = e.log(request) or 'unknown' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) except: if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## try: if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') except: pass e = RestrictedError('Framework', '', '', locals()) ticket = e.log(request) or 'unrecoverable' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) finally: if response and hasattr(response, 'session_file') \ and response.session_file: response.session_file.close() session._unlock(response) http_response, new_environ = try_rewrite_on_error( http_response, request, environ, ticket) if not http_response: return wsgibase(new_environ, responder) if global_settings.web2py_crontype == 'soft': newcron.softcron(global_settings.applications_parent).start() return http_response.to(responder, env=env)
def wsgibase(environ, responder): """ The gluon wsgi application. The first function called when a page is requested (static or dynamic). It can be called by paste.httpserver or by apache mod_wsgi (or any WSGI-compatible server). - fills request with info - the environment variables, replacing '.' with '_' - adds web2py path and version info - compensates for fcgi missing path_info and query_string - validates the path in url The url path must be either: 1. for static pages: - /<application>/static/<file> 2. for dynamic pages: - /<application>[/<controller>[/<function>[/<sub>]]][.<extension>] The naming conventions are: - application, controller, function and extension may only contain `[a-zA-Z0-9_]` - file and sub may also contain '-', '=', '.' and '/' """ eget = environ.get current.__dict__.clear() request = Request(environ) response = Response() session = Session() env = request.env #env.web2py_path = global_settings.applications_parent env.web2py_version = web2py_version #env.update(global_settings) static_file = False http_response = None try: try: try: # ################################################## # handle fcgi missing path_info and query_string # select rewrite parameters # rewrite incoming URL # parse rewritten header variables # parse rewritten URL # serve file if static # ################################################## fixup_missing_path_info(environ) (static_file, version, environ) = url_in(request, environ) response.status = env.web2py_status_code or response.status if static_file: if eget('QUERY_STRING', '').startswith('attachment'): response.headers['Content-Disposition'] \ = 'attachment' if version: response.headers['Cache-Control'] = 'max-age=315360000' response.headers[ 'Expires'] = 'Thu, 31 Dec 2037 23:59:59 GMT' response.stream(static_file, request=request) # ################################################## # fill in request items # ################################################## app = request.application # must go after url_in! if not global_settings.local_hosts: local_hosts = set(['127.0.0.1', '::ffff:127.0.0.1', '::1']) if not global_settings.web2py_runtime_gae: try: fqdn = socket.getfqdn() local_hosts.add(socket.gethostname()) local_hosts.add(fqdn) local_hosts.update([ addrinfo[4][0] for addrinfo in getipaddrinfo(fqdn) ]) if env.server_name: local_hosts.add(env.server_name) local_hosts.update([ addrinfo[4][0] for addrinfo in getipaddrinfo(env.server_name) ]) except (socket.gaierror, TypeError): pass global_settings.local_hosts = list(local_hosts) else: local_hosts = global_settings.local_hosts client = get_client(env) x_req_with = str(env.http_x_requested_with).lower() cmd_opts = global_settings.cmd_options request.update( client = client, folder = abspath('applications', app) + os.sep, ajax = x_req_with == 'xmlhttprequest', cid = env.http_web2py_component_element, is_local = (env.remote_addr in local_hosts and client == env.remote_addr), is_shell = False, is_scheduler = False, is_https = env.wsgi_url_scheme in HTTPS_SCHEMES or \ request.env.http_x_forwarded_proto in HTTPS_SCHEMES \ or env.https == 'on' ) request.url = environ['PATH_INFO'] # ################################################## # access the requested application # ################################################## disabled = pjoin(request.folder, 'DISABLED') if not exists(request.folder): if app == rwthread.routes.default_application \ and app != 'welcome': redirect(URL('welcome', 'default', 'index')) elif rwthread.routes.error_handler: _handler = rwthread.routes.error_handler redirect( URL(_handler['application'], _handler['controller'], _handler['function'], args=app)) else: raise HTTP(404, rwthread.routes.error_message % 'invalid request', web2py_error='invalid application') elif not request.is_local and exists(disabled): raise HTTP( 503, "<html><body><h1>Temporarily down for maintenance</h1></body></html>" ) # ################################################## # build missing folders # ################################################## create_missing_app_folders(request) # ################################################## # get the GET and POST data # ################################################## #parse_get_post_vars(request, environ) # ################################################## # expose wsgi hooks for convenience # ################################################## request.wsgi = LazyWSGI(environ, request, response) # ################################################## # load cookies # ################################################## if env.http_cookie: for single_cookie in env.http_cookie.split(';'): single_cookie = single_cookie.strip() if single_cookie: try: request.cookies.load(single_cookie) except Cookie.CookieError: pass # single invalid cookie ignore # ################################################## # try load session or create new session file # ################################################## if not env.web2py_disable_session: session.connect(request, response) # ################################################## # run controller # ################################################## if global_settings.debugging and app != "admin": import gluon.debug # activate the debugger gluon.debug.dbg.do_debug(mainpyfile=request.folder) serve_controller(request, response, session) except HTTP as hr: http_response = hr if static_file: return http_response.to(responder, env=env) if request.body: request.body.close() if hasattr(current, 'request'): # ################################################## # on success, try store session in database # ################################################## if not env.web2py_disable_session: session._try_store_in_db(request, response) # ################################################## # on success, commit database # ################################################## if response.do_not_commit is True: BaseAdapter.close_all_instances(None) elif response.custom_commit: BaseAdapter.close_all_instances(response.custom_commit) else: BaseAdapter.close_all_instances('commit') # ################################################## # if session not in db try store session on filesystem # this must be done after trying to commit database! # ################################################## if not env.web2py_disable_session: session._try_store_in_cookie_or_file(request, response) # Set header so client can distinguish component requests. if request.cid: http_response.headers.setdefault( 'web2py-component-content', 'replace') if request.ajax: if response.flash: http_response.headers['web2py-component-flash'] = \ urllib2.quote(xmlescape(response.flash).replace(b'\n', b'')) if response.js: http_response.headers['web2py-component-command'] = \ urllib2.quote(response.js.replace('\n', '')) # ################################################## # store cookies in headers # ################################################## session._fixup_before_save() http_response.cookies2headers(response.cookies) ticket = None except RestrictedError as e: if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## # log tickets before rollback if not in DB if not request.tickets_db: ticket = e.log(request) or 'unknown' # rollback if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') # if tickets in db, reconnect and store it in db if request.tickets_db: ticket = e.log(request) or 'unknown' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) except: if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## try: if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') except: pass e = RestrictedError('Framework', '', '', locals()) ticket = e.log(request) or 'unrecoverable' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) finally: if response and hasattr(response, 'session_file') \ and response.session_file: response.session_file.close() session._unlock(response) http_response, new_environ = try_rewrite_on_error(http_response, request, environ, ticket) if not http_response: return wsgibase(new_environ, responder) if global_settings.web2py_crontype == 'soft': newcron.softcron(global_settings.applications_parent).start() return http_response.to(responder, env=env)
SQLField('app'), SQLField('name'), SQLField('date_saved', 'datetime'), SQLField('layer'), SQLField('traceback', 'text'), SQLField('code', 'text'), ) while 1: for file in os.listdir(path): filename = os.path.join(path, file) if not ALLOW_DUPLICATES: key = md5.new(open(filename, 'r').read()).hexdigest() if hashes.has_key(key): continue hashes[key] = 1 e = RestrictedError() e.load(filename) t = \ datetime.datetime.fromtimestamp(os.stat(filename)[stat.ST_MTIME]) db.ticket.insert( app=request.application, date_saved=t, name=file, layer=e.layer, traceback=e.traceback, code=e.code, ) os.unlink(filename) db.commit() time.sleep(SLEEP_MINUTES * 60)
### END CONFIGURATION if settings.globals.debug_notification: for file in os.listdir(path): filename = os.path.join(path, file) if not ALLOW_DUPLICATES: file_data = open(filename, 'r').read() key = md5_hash(file_data) if key in hashes: continue hashes[key] = 1 error = RestrictedError() error.load(request, request.application, filename) logger.debug("REQUEST-APP: %s" % dir(request)) logger.debug("Sending email...") message = '<b>There has been an error on a node.</b><br>' message += '<h1>This is the trackback:</h1><br><pre>%s</pre><br><br><br>' % error.traceback message += "<h1>this is the environment:</h1><br>" try: message += "<h2>RESPONSE: </h2><br> %s<br><br>" % error.snapshot['response'] message += "<h2>LOCALS: </h2><br> %s<br><br>" % error.snapshot['locals'] message += "<h2>REQUEST: </h2><br> %s<br><br>" % error.snapshot['request'] message += "<h2>SESSION:</h2><br> %s<br><br>" % error.snapshot['session'] except KeyError: pass
def process_tickets(): return "NO! Don't use this." def get_table_row(table, row_header): # Look for the row with `header' in the first string of # the first TD of the row for row in table.components: #print row.components[0].components[0] if row.components[0].components[0] == row_header: return row #.components[2].components[0].components[0] return None def get_beautify_key_value(beautify, key): r = get_table_row(beautify.components[0], key) if r: return r.components[2].components[0] return None def has_live_get_var(error): get_vars = get_beautify_key_value(e.snapshot['request'], 'get_vars') if not get_vars: return False return get_beautify_key_value(get_vars, 'live') def find_hitid(error): get_vars = get_beautify_key_value(error.snapshot['request'], 'get_vars') if not get_vars: send_me_mail('Crap, no get_vars in this guy!\n\n %s error') hitid = get_beautify_key_value(get_vars, 'hitId') if not (hitid and len(hitid.components) == 1): send_me_mail('Crap, no hitid in this guy!\n\n %s error') return hitid.components[0] def is_sandbox(error): sandboxp = get_beautify_key_value(e.snapshot['request'], 'sandboxp') if not sandboxp or 'components' not in sandboxp or len(components) < 1: debug_t('This shouldn\'t happen! in process_tickets()') return False s = sandboxp.components[0] if not (s == 'False' or s == 'True'): debug_t('This shouldn\'t happen either! in process_tickets()') return false return s == 'True' if True: import os, stat, time from gluon.restricted import RestrictedError path='applications/utility/errors/' last_run = store_get('last_process_tickets_time') or 0.3 this_run = time.time() recent_files = [x for x in os.listdir(path) if os.path.getmtime(path + x) > last_run] for file in recent_files: debug_t('Trying error file %s' % file) e=RestrictedError() e.load(request, 'utility', file) # Ok, let's see if this was a live one if has_live_get_var(e) and not is_sandbox(e): debug_t('This error has a live! Dealing with it now.') hitid = find_hitid(e) url = ('http://%s:%s/admin/default/ticket/utility/%s' % (server_url, server_port, file)) send_me_mail("There was an error in your mturk study!!!\nGo check it out at %s" % url) try: debug_t('Expiring hit %s' % hitid) result = turk.expire_hit(hitid) # result.toprettyxml().replace('\t', ' ') debug_t('Expired this hit.') except TurkAPIError as e: debug_t("Couldn't expire it. Maybe it was already done. Error was: %s" % e) store_set('last_process_tickets_time', this_run) db.commit()
if request.body: request.body.close() # ################################################## # on application error, rollback database # ################################################## try: if response._custom_rollback: response._custom_rollback() else: BaseAdapter.close_all_instances('rollback') except: pass e = RestrictedError('Framework', '', '', locals()) ticket = e.log(request) or 'unrecoverable' http_response = \ HTTP(500, rwthread.routes.error_message_ticket % dict(ticket=ticket), web2py_error='ticket %s' % ticket) finally: if response and hasattr(response, 'session_file') \ and response.session_file: response.session_file.close() session._unlock(response) http_response, new_environ = try_rewrite_on_error(http_response, request, environ, ticket) if not http_response:
def _raise_error(self, message='', text=None): """ Raises an error using itself as the filename and textual content. """ raise RestrictedError(self.name, text or self.text, message)