Example #1
0
def reload(modules, changed_module, filename):
    global reloading
    reloading = True
    success = True
    log(type='RELOAD:begin',
        prefix='RELOADING: ',
        text=shortened_filename(filename),
        severity=ERROR,
        module=changed_module.__name__,
        modules=dict((m.__name__, m.__file__) for m in modules))
    try:
        for clear_func in clear_funcs:
            clear_func()
        mtimes.clear()
        linecache.checkcache()
        for m in modules:
            sys.modules.pop(m.__name__, None)
        load_main()
    except Exception:
        success = False
        log_exc()
        raise
    finally:
        log(type='RELOAD:end',
            severity=DEBUG,
            success=success,
            text='Reloaded successfully'
            if success else 'Reloaded with errors')
        reloading = False
Example #2
0
def reload():
    global last_check_time
    now = time()
    if abs(now - last_check_time) <= options.RELOADING_CHECK_INTERVAL: return
    with lock:
        if abs(now - last_check_time) <= options.RELOADING_CHECK_INTERVAL: return
        last_check_time = now
        changed = {}
        for fname, mtime, trans in trans_files:
            try: new_mtime = get_mtime(fname)
            except:  # file not found?
                new_mtime = None
            if new_mtime != mtime: changed[fname] = new_mtime
        if not changed: return

        erroneous = set()
        log(type='RELOAD:begin', prefix='RELOADING: ', text=shortened_filename(fname), severity=ERROR,
            files=[ fname for fname, mtime, trans in trans_files ], changed=changed)
        try:
            translations.clear()
            for i, (fname, mtime, trans) in enumerate(trans_files):
                if fname in changed:
                    new_mtime = changed[fname]
                    trans = {}
                    if new_mtime is not None:
                        try: trans = load(fname)
                        except:
                            erroneous.add(fname)
                            log_exc()
                    trans_files[i] = fname, new_mtime, trans
                update(translations, trans)
        finally: log(type='RELOAD:end', severity=DEBUG, success=not erroneous, erroneous=erroneous,
                     text='Reloaded with errors' if erroneous else 'Reloaded successfully')
Example #3
0
 def run(thread):
     message = 'Starting HTTP server at %s:%s' % (thread.host, thread.port)
     log(type='HTTP:start', text=message, severity=WARNING, host=thread.host, port=thread.port, uid=pony.uid)
     thread.server.start()
     message = 'HTTP server at %s:%s stopped successfully' % (thread.host, thread.port)
     log(type='HTTP:stop', text=message, severity=WARNING, host=thread.host, port=thread.port)
     server_threads.pop((thread.host, thread.port), None)
def reload():
    global last_check_time
    now = time()
    if abs(now - last_check_time) <= options.RELOADING_CHECK_INTERVAL: return
    with lock:
        if abs(now - last_check_time) <= options.RELOADING_CHECK_INTERVAL: return
        last_check_time = now
        changed = {}
        for fname, mtime, trans in trans_files:
            try: new_mtime = get_mtime(fname)
            except:  # file not found?
                new_mtime = None
            if new_mtime != mtime: changed[fname] = new_mtime
        if not changed: return

        erroneous = set()
        log(type='RELOAD:begin', prefix='RELOADING: ', text=shortened_filename(fname), severity=ERROR,
            files=[ fname for fname, mtime, trans in trans_files ], changed=changed)
        try:
            translations.clear()
            for i, (fname, mtime, trans) in enumerate(trans_files):
                if fname in changed:
                    new_mtime = changed[fname]
                    trans = {}
                    if new_mtime is not None:
                        try: trans = load(fname)
                        except:
                            erroneous.add(fname)
                            log_exc()
                    trans_files[i] = fname, new_mtime, trans
                update(translations, trans)
        finally: log(type='RELOAD:end', severity=DEBUG, success=not erroneous, erroneous=erroneous,
                     text='Reloaded with errors' if erroneous else 'Reloaded successfully')
Example #5
0
def log_request(request):
    environ = request.environ
    headers=dict((key, value) for key, value in environ.items()
                              if isinstance(key, basestring)
                              and isinstance(value, basestring))
    method = environ.get('REQUEST_METHOD', 'GET')
    request_type = 'HTTP:%s' % method
    user = auth.local.user
    if user is not None and not isinstance(user, (int, long, basestring)):
        user = unicode(user)
    log(type=request_type, prefix=method+' ', text=request.full_url, severity=INFO,
        headers=headers, user=user, session=auth.local.session.__dict__)
Example #6
0
 def run(thread):
     message = 'Starting HTTP server at %s:%s' % (thread.host, thread.port)
     log(type='HTTP:start',
         text=message,
         severity=WARNING,
         host=thread.host,
         port=thread.port,
         uid=pony.uid)
     thread.server.start()
     message = 'HTTP server at %s:%s stopped successfully' % (thread.host,
                                                              thread.port)
     log(type='HTTP:stop',
         text=message,
         severity=WARNING,
         host=thread.host,
         port=thread.port)
     server_threads.pop((thread.host, thread.port), None)
Example #7
0
def log_request(request):
    environ = request.environ
    headers = dict(
        (key, value) for key, value in environ.items()
        if isinstance(key, basestring) and isinstance(value, basestring))
    method = environ.get('REQUEST_METHOD', 'GET')
    request_type = 'HTTP:%s' % method
    user = auth.local.user
    if user is not None and not isinstance(user, (int, long, basestring)):
        user = unicode(user)
    log(type=request_type,
        prefix=method + ' ',
        text=request.full_url,
        severity=INFO,
        headers=headers,
        user=user,
        session=auth.local.session.__dict__)
Example #8
0
def reload(modules, changed_module, filename):
    global reloading
    reloading = True
    success = True
    log(type='RELOAD:begin', prefix='RELOADING: ', text=shortened_filename(filename), severity=ERROR,
        module=changed_module.__name__, modules=dict((m.__name__, m.__file__) for m in modules))
    try:
        for clear_func in clear_funcs: clear_func()
        mtimes.clear()
        linecache.checkcache()
        for m in modules: sys.modules.pop(m.__name__, None)
        load_main()
    except Exception:
        success = False
        log_exc()
        raise
    finally:
        log(type='RELOAD:end', severity=DEBUG, success=success,
            text='Reloaded successfully' if success else 'Reloaded with errors')
        reloading = False
Example #9
0
def app(environ):
    request = local.request = HttpRequest(environ)
    log_request(request)
    response = local.response = HttpResponse()
    local.no_cookies = False
    auth.load(environ, request.cookies)
    auth.verify_ticket(request.fields.getfirst('_t'))
    postprocessing = True
    no_exception = False
    if autoreload.reloading_exception and not request.url.startswith('/pony/static/'):
        status, headers = INTERNAL_SERVER_ERROR
        result = format_exc(autoreload.reloading_exception)
    elif request.method not in ('HEAD', 'GET', 'POST', 'PUT', 'DELETE'):
        status = '501 Not Implemented'
        headers = {'Content-Type' : 'text/plain'}
        result = 'Unknown HTTP method: %s' % request.method
    else:
        try:
            try:
                if auth.local.ticket_payload is not None:
                    form = cPickle.loads(auth.local.ticket_payload)
                    form._handle_request_()
                    form = None
                result = invoke(request.url)
            finally:
                if auth.local.ticket and not request.form_processed and request.form_processed is not None:
                    auth.unexpire_ticket()
        except HttpException as e:
            status, headers, result = e.status, e.headers, e.content
            result, headers = normalize_result(result, headers)
        except BdbQuit: raise
        except:
            log_exc()
            status, headers = INTERNAL_SERVER_ERROR
            result, headers = normalize_result(format_exc(), headers)
        else:
            no_exception = True
            status = response.status
            headers = response.headers
            postprocessing = response.postprocessing

    content_type = headers.get('Content-Type', 'text/plain')
    media_type, type_params = cgi.parse_header(content_type)
    charset = type_params.get('charset', 'iso-8859-1')
    if isinstance(result, basestring):
        if media_type == 'text/html' and postprocessing:
            if no_exception:
                  result = postprocess(result, response.base_stylesheets, response.component_stylesheets, response.scripts)
            else: result = postprocess(result, [], [], [])
        if isinstance(result, unicode):
            if media_type == 'text/html' or 'xml' in media_type :
                  result = result.encode(charset, 'xmlcharrefreplace')
            else: result = result.encode(charset, 'replace')
        headers['Content-Length'] = str(len(result))

    headers = headers.items()
    for header, value in headers:
        assert isinstance(header, str)
        assert isinstance(value, str)
    if not local.no_cookies and not status.startswith('5'):
        auth.save(response.cookies)
        headers += httputils.serialize_cookies(environ, response.cookies)
    log(type='HTTP:response', prefix='Response: ', text=status, severity=DEBUG, headers=headers)
    if request.method == 'HEAD' and 'Content-Length' in headers: result = ''
    return status, headers, result
Example #10
0
def app(environ):
    request = local.request = HttpRequest(environ)
    log_request(request)
    response = local.response = HttpResponse()
    local.no_cookies = False
    auth.load(environ, request.cookies)
    auth.verify_ticket(request.fields.getfirst('_t'))
    postprocessing = True
    no_exception = False
    if autoreload.reloading_exception and not request.url.startswith(
            '/pony/static/'):
        status, headers = INTERNAL_SERVER_ERROR
        result = format_exc(autoreload.reloading_exception)
    elif request.method not in ('HEAD', 'GET', 'POST', 'PUT', 'DELETE'):
        status = '501 Not Implemented'
        headers = {'Content-Type': 'text/plain'}
        result = 'Unknown HTTP method: %s' % request.method
    else:
        try:
            try:
                if auth.local.ticket_payload is not None:
                    form = cPickle.loads(auth.local.ticket_payload)
                    form._handle_request_()
                    form = None
                result = invoke(request.url)
            finally:
                if auth.local.ticket and not request.form_processed and request.form_processed is not None:
                    auth.unexpire_ticket()
        except HttpException as e:
            status, headers, result = e.status, e.headers, e.content
            result, headers = normalize_result(result, headers)
        except BdbQuit:
            raise
        except:
            log_exc()
            status, headers = INTERNAL_SERVER_ERROR
            result, headers = normalize_result(format_exc(), headers)
        else:
            no_exception = True
            status = response.status
            headers = response.headers
            postprocessing = response.postprocessing

    content_type = headers.get('Content-Type', 'text/plain')
    media_type, type_params = cgi.parse_header(content_type)
    charset = type_params.get('charset', 'iso-8859-1')
    if isinstance(result, basestring):
        if media_type == 'text/html' and postprocessing:
            if no_exception:
                result = postprocess(result, response.base_stylesheets,
                                     response.component_stylesheets,
                                     response.scripts)
            else:
                result = postprocess(result, [], [], [])
        if isinstance(result, unicode):
            if media_type == 'text/html' or 'xml' in media_type:
                result = result.encode(charset, 'xmlcharrefreplace')
            else:
                result = result.encode(charset, 'replace')
        headers['Content-Length'] = str(len(result))

    headers = headers.items()
    for header, value in headers:
        assert isinstance(header, str)
        assert isinstance(value, str)
    if not local.no_cookies and not status.startswith('5'):
        auth.save(response.cookies)
        headers += httputils.serialize_cookies(environ, response.cookies)
    log(type='HTTP:response',
        prefix='Response: ',
        text=status,
        severity=DEBUG,
        headers=headers)
    if request.method == 'HEAD' and 'Content-Length' in headers: result = ''
    return status, headers, result