Exemplo n.º 1
0
Arquivo: cli.py Projeto: dowski/aspen
def main(argv=None):
    try:
        configuration = Configuration(argv)
        configuration.app = app = Application()
        website = Website(configuration)
        configuration.website = website  # to support re-handling, especially
        website = configuration.hooks.run('startup', website)

        # change current working directory
        os.chdir(configuration.root)

        if configuration.conf.aspen.no('changes_kill'):
            # restart for template files too;
            # TODO can't we just invalidate the simplate cache for these?
            dot_aspen = join(configuration.root, '.aspen')
            for root, dirs, files in os.walk(dot_aspen):
                for filename in files:
                    restarter.add(join(root, filename))

            app.add_loop(Loop(restarter.loop))

        port = configuration.address[1]
        app.add_service(Service(http.HttpServer(website), port))

        log.warn("Greetings, program! Welcome to port %d." % port)
        app.run()

    except KeyboardInterrupt, SystemExit:
        configuration.hooks.run('shutdown', website)
Exemplo n.º 2
0
 def setUp(self):
     website = Website([
         '--www_root', WWW_ROOT, '--project_root', PROJECT_ROOT,
         '--show_tracebacks', b'yes'
     ])
     redis.flushdb()
     self.client = TestClient(website)
def test_negotiated_resource_is_instantiable():
    website = Website([])
    fs = ''
    raw = '^L^L #!tornado text/plain\n'
    media_type = ''
    mtime = 0
    actual = NegotiatedResource(website, fs, raw, media_type, mtime).__class__
    assert actual is NegotiatedResource, actual
def get(**_kw):
    kw = dict(website=Website([]),
              fs='',
              raw='[---]\n[---] text/plain via stdlib_template\n',
              media_type='',
              mtime=0)
    kw.update(_kw)
    return NegotiatedResource(**kw)
def test_negotiated_resource_is_instantiable():
    website = Website([])
    fs = ''
    raw = '[---]\n[---] text/plain via stdlib_template\n'
    media_type = ''
    mtime = 0
    actual = NegotiatedResource(website, fs, raw, media_type, mtime).__class__
    assert actual is NegotiatedResource
def get(**_kw):
    kw = dict( website = Website([])
             , fs = ''
             , raw = '^L^L #!tornado text/plain\n'
             , media_type = ''
             , mtime = 0
              )
    kw.update(_kw)
    return NegotiatedResource(**kw)
Exemplo n.º 7
0
 def test_github_oauth_url_not_susceptible_to_injection_attack(self):
     expected = 'https://github.com/login/oauth/authorize?client_id=cheese&redirect_uri=nuts?data=b3B0LWluLC9vbi90d2l0dGVyLyI+PGltZyBzcmM9eCBvbmVycm9yPXByb21wdCgxKTs+Lw=='
     website = Website([])
     website.github_client_id = 'cheese'
     website.github_callback = 'nuts'
     actual = github.oauth_url(website=website,
                               action='opt-in',
                               then=self.xss)
     assert actual == expected
Exemplo n.º 8
0
def test_website_doesnt_clobber_outbound(mk):
    mk( ( '.aspen/configure-aspen.py'
        , 'import random\nwebsite.hooks.outbound.append(random.choice)'
         )
       )

    project_root = os.path.join(FSFIX, '.aspen')
    website = Website(['--www_root='+FSFIX, '--project_root='+project_root])

    expected = 2
    actual = len(website.hooks.outbound)
    assert actual == expected
Exemplo n.º 9
0
    def __call__(self, path='/', *a):
        """Given an URL path, return

        This only allows you to simulate GET requests with no querystring, so
        it's limited. But it's a something. Kind of. Almost.

        """
        website = Website(self.argv + list(a))
        request = StubRequest(path)
        request.website = website
        response = website.handle_safely(request)
        return response
Exemplo n.º 10
0
 def from_fs(cls, fs, *a):
     """Takes a path under FSFIX using / as the path separator.
     """
     fs = os.sep.join(fs.split(os.sep))
     request = Request.from_wsgi(StubWSGIRequest(fs))
     website = Website([ '--www_root', FSFIX
                       , '--project_root', '.aspen'
                        ] + list(a))
     request.www_root = os.path.join(os.path.dirname(__file__), FSFIX)
     request.fs = fs
     request.context = {}
     request.website = website
     request._media_type = None
     return request
Exemplo n.º 11
0
def test_call_wraps_wsgi_middleware():
    website = Website([])
    website.wsgi_app = TestMiddleware(website.wsgi_app)
    respond = [False, False]
    def start_response_should_404(status, headers):
        assert status.lower().strip() == '404 not found'
        respond[0] = True
    website(build_environ('/'), start_response_should_404)
    assert respond[0]
    def start_response_should_200(status, headers):
        assert status.lower().strip() == '200 ok'
        respond[1] = True
    website(build_environ('/middleware'), start_response_should_200)
    assert respond[1]
Exemplo n.º 12
0
def test_configuration_script_can_set_renderer_default(mk):
    CONFIG = """
website.renderer_default="stdlib_format"
    """
    SIMPLATE = """
name="program"
[----]
Greetings, {name}!
    """
    mk(
       ('.aspen/configure-aspen.py', CONFIG),
       ('index.html.spt', SIMPLATE)
      )
    w = Website(['--www_root', FSFIX, '-p', fix('.aspen'), '--show_tracebacks=yes'])
    request = StubRequest(b'/')
    request.website = w
    response = w.handle_safely(request)
    actual = response.body.strip()
    expected = 'Greetings, program!'
    assert actual == expected
Exemplo n.º 13
0
def test_double_failure_still_sets_response_dot_request(mk):
    mk( '.aspen'
      , ('.aspen/foo.py', """
def bar(response):
    response.request
""")
      , ( '.aspen/configure-aspen.py'
        , 'import foo\nwebsite.hooks.outbound.append(foo.bar)'
         )
      , ('index.html.spt', "raise heck\n[---]\n")
       )

    # Intentionally break the website object so as to trigger a double failure.
    project_root = os.path.join(FSFIX, '.aspen')
    website = Website(['--www_root='+FSFIX, '--project_root='+project_root])
    del website.renderer_factories

    response = website.handle_safely(StubRequest())

    expected = 500
    actual = response.code
    assert actual == expected
Exemplo n.º 14
0
def test_basic():
    website = Website([])
    expected = os.getcwd()
    actual = website.www_root
    assert actual == expected
Exemplo n.º 15
0
def test_basic():
    website = Website(Configuration([]))
    expected = os.getcwd()
    actual = website.root
    assert actual == expected, actual
Exemplo n.º 16
0
    actual = handle('/', '--unavailable=10').headers['Retry-After']
    expected = datetime.datetime.utcnow().strftime('%a, %d %b %Y')
    assert actual.startswith(expected), actual
    assert actual.endswith(' +0000'), actual


def test_double_failure_still_sets_response_dot_request():
    mk( '.aspen'
      , ('.aspen/foo.py', """
def bar(response):
    response.request
""")
      , ( '.aspen/configure-aspen.py'
        , 'import foo\nwebsite.hooks.outbound_late.register(foo.bar)'
         )
      , ('index.html', "raise heck")
       )

    # Intentionally break the website object so as to trigger a double failure.
    website = Website(['--www_root='+FSFIX, '--project_root=.aspen'])
    del website.renderer_factories

    response = website.handle_safely(StubRequest())

    expected = 500
    actual = response.code
    assert actual == expected, actual


attach_teardown(globals())
Exemplo n.º 17
0
def handle(path):
    website = Website(Configuration(['fsfix']))
    return website.handle(Request.from_diesel(DieselReq(path)))
Exemplo n.º 18
0
from __future__ import absolute_import, division, print_function, unicode_literals

import os
from aspen.website import Website

project_root = os.path.dirname(__file__)
www_root = os.path.join(project_root, 'www')
website = Website(project_root=project_root, www_root=www_root)
Exemplo n.º 19
0
def test_configuration_script_ignores_blank_indexfilenames():
    w = Website(['--indices', 'index.html,, ,default.html'])
    assert w.indices[0] == 'index.html'
    assert w.indices[1] == 'default.html'
    assert len(w.indices) == 2, "Too many indexfile entries"
Exemplo n.º 20
0
def _main(argv):

    # Do imports.
    # ===========
    # These are in here so that if you Ctrl-C during an import, the
    # KeyboardInterrupt is caught and ignored. Yes, that's how much I care.
    # No, I don't care enough to put aspen/__init__.py in here too.

    import os
    import signal
    import socket
    import sys
    import traceback

    import aspen
    from aspen import execution
    from aspen.website import Website


    # Set up signal handling.
    # =======================

    def SIGHUP(signum, frame):
        aspen.log_dammit("Received HUP, re-executing.")
        execution.execute()
    if not aspen.WINDOWS:
        signal.signal(signal.SIGHUP, SIGHUP)

    def SIGINT(signum, frame):
        aspen.log_dammit("Received INT, exiting.")
        raise SystemExit
    signal.signal(signal.SIGINT, SIGINT)


    def SIGQUIT(signum, frame):
        aspen.log_dammit("Received QUIT, exiting.")
        raise SystemExit
    if not aspen.WINDOWS:
        signal.signal(signal.SIGQUIT, SIGQUIT)


    # Website
    # =======
    # User-developers get this website object inside of their resources and
    # hooks. It provides access to configuration information in addition to
    # being a WSGI callable and holding the request/response handling
    # logic. See aspen/website.py

    if argv is None:
        argv = sys.argv[1:]
    website = Website(argv)


    # Start serving the website.
    # ==========================
    # This amounts to binding the requested socket, with logging and
    # restarting as needed. Wrap the whole thing in a try/except to
    # do some cleanup on shutdown.

    try:
        if hasattr(socket, 'AF_UNIX'):
            if website.network_sockfam == socket.AF_UNIX:
                if os.path.exists(website.network_address):
                    aspen.log("Removing stale socket.")
                    os.remove(website.network_address)
        if website.network_port is not None:
            welcome = "port %d" % website.network_port
        else:
            welcome = website.network_address
        aspen.log("Starting %s engine." % website.network_engine.name)
        website.network_engine.bind()
        aspen.log_dammit("Greetings, program! Welcome to %s." % welcome)
        if website.changes_reload:
            aspen.log("Aspen will restart when configuration scripts or "
                      "Python modules change.")
            execution.install(website)
        website.start()

    except socket.error:

        # Be friendly about port conflicts.
        # =================================

        # The traceback one gets from a port conflict or permission error
        # is not that friendly. Here's a helper to let the user know (in
        # color?!) that a port conflict or a permission error is probably
        # the problem. But in case it isn't (website.start fires the start
        # hook, and maybe the user tries to connect to a network service in
        # there?), don't fully swallow the exception. Also, be explicit
        # about the port number. What if they have logging turned off? Then
        # they won't see the port number in the "Greetings, program!" line.
        # They definitely won't see it if using an engine like eventlet
        # that binds to the port early.

        if website.network_port is not None:
            msg = "Is something already running on port %s? Because ..."
            if not aspen.WINDOWS:
                if website.network_port < 1024:
                    if os.geteuid() > 0:
                        msg = ("Do you have permission to bind to port %s?"
                               " Because ...")
            msg %= website.network_port
            if not aspen.WINDOWS:
                # Assume we can use ANSI color escapes if not on Windows.
                # XXX Maybe a bad assumption if this is going into a log
                # file? See also: colorama
                msg = '\033[01;33m%s\033[00m' % msg
            aspen.log_dammit(msg)
        raise
    except (KeyboardInterrupt, SystemExit):
        raise  # Don't bother logging these.
    except:
        aspen.log_dammit(traceback.format_exc())
    finally:
        if hasattr(socket, 'AF_UNIX'):
            if website.network_sockfam == socket.AF_UNIX:
                if os.path.exists(website.network_address):
                    os.remove(website.network_address)
        website.stop()
Exemplo n.º 21
0
import base64

import gratipay
import gratipay.wireup
from gratipay import utils, security
from gratipay.cron import Cron
from gratipay.models.participant import Participant
from gratipay.security import authentication, csrf
from gratipay.utils import erase_cookie, http_caching, i18n, set_cookie, timer
from gratipay.version import get_version
from gratipay.renderers import csv_dump, jinja2_htmlescaped, eval_, scss

import aspen
from aspen.website import Website

website = Website()

# Configure renderers
# ===================

website.renderer_default = 'unspecified'  # require explicit renderer, to avoid escaping bugs

website.renderer_factories['csv_dump'] = csv_dump.Factory(website)
website.renderer_factories['eval'] = eval_.Factory(website)
website.renderer_factories['jinja2_htmlescaped'] = jinja2_htmlescaped.Factory(
    website)
website.renderer_factories['scss'] = scss.Factory(website)
website.default_renderers_by_media_type['text/html'] = 'jinja2_htmlescaped'
website.default_renderers_by_media_type[
    'text/plain'] = 'jinja2'  # unescaped is fine here
website.default_renderers_by_media_type['text/css'] = 'scss'
Exemplo n.º 22
0
import base64

import gratipay
import gratipay.wireup
from gratipay import canonize, utils
from gratipay.cron import Cron
from gratipay.models.participant import Participant
from gratipay.security import authentication, csrf, x_frame_options
from gratipay.utils import erase_cookie, http_caching, i18n, set_cookie, timer
from gratipay.version import get_version
from gratipay.renderers import csv_dump, jinja2_htmlescaped

import aspen
from aspen.website import Website

website = Website([])

# Configure renderers
# ===================

website.renderer_default = 'unspecified'  # require explicit renderer, to avoid escaping bugs

website.renderer_factories['csv_dump'] = csv_dump.Factory(website)
website.renderer_factories['jinja2_htmlescaped'] = jinja2_htmlescaped.Factory(
    website)
website.default_renderers_by_media_type['text/html'] = 'jinja2_htmlescaped'
website.default_renderers_by_media_type[
    'text/plain'] = 'jinja2'  # unescaped is fine here

website.renderer_factories['jinja2'].Renderer.global_context = {
    # This is shared via class inheritance with jinja2_htmlescaped.
Exemplo n.º 23
0
def make_request(filename='echo.sock'):
    request = Request(uri='/echo.sock')
    request.website = Website([])
    request.fs = fix(filename)
    return request
Exemplo n.º 24
0
def check():
    website = Website(Configuration(['fsfix']))
    response = website.handle(Request.from_diesel(DieselReq()))
    return response