PageRenderTime 122ms CodeModel.GetById 61ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/tests/functional/example-app/example_app.py

https://bitbucket.org/ianb/silverlining/
Python | 38 lines | 36 code | 2 blank | 0 comment | 7 complexity | ecb0e7aaf5ad07ddf13070463c912684 MD5 | raw file
 1import os
 2import sys
 3import traceback
 4
 5
 6def application(environ, start_response):
 7    try:
 8        path_info = environ['PATH_INFO']
 9        if path_info == '/update':
10            body = []
11            for name, value in sorted(os.environ.items()):
12                if name.startswith('SILVER') or name.startswith('CONFIG_'):
13                    body.append('env %s=%s' % (name, value))
14            body.append('wsgi.multiprocess=%r' % environ['wsgi.multiprocess'])
15            body.append('wsgi.multithread=%r' % environ['wsgi.multithread'])
16            body.append('path=%r %r' % (environ['SCRIPT_NAME'], environ['PATH_INFO']))
17            body.append('Update run.')
18            body = '\n'.join(body)
19        elif path_info == '/write-root':
20            write_root = os.environ['CONFIG_WRITABLE_ROOT']
21            host = environ['HTTP_HOST'].split(':')[0]
22            host_dir = os.path.join(write_root, host)
23            if not os.path.exists(host_dir):
24                os.mkdir(host_dir)
25            open(os.path.join(write_root, 'test-writable.txt'), 'w').write('test writable')
26            open(os.path.join(host_dir, 'test-hosted.txt'), 'w').write('test hosted')
27            body = 'WRITABLE_ROOT=%s' % write_root
28        else:
29            body = 'INSTANCE=%s' % os.environ['SILVER_INSTANCE_NAME']
30        start_response('200 OK', [('Content-type', 'text/plain')])
31        environ['wsgi.errors'].write('Executed application\n')
32        print 'This is stdout'
33        print >> sys.stderr, 'This is stderr'
34        return [body]
35    except:
36        start_response('500 Server Error', [('Content-type', 'text/plain')])
37        exc = traceback.format_exc()
38        return [exc]