wsgi middleware for exposing version of entrouvert python package

fixes #2797
This commit is contained in:
Benjamin Dauvergne 2013-04-25 16:47:43 +02:00
parent 5c14c81411
commit 913458897d
4 changed files with 67 additions and 0 deletions

0
entrouvert/__init__.py Normal file
View File

View File

View File

@ -0,0 +1,35 @@
from urllib import quote
import json
import pkg_resources
class VersionMiddleware(object):
ENTROUVERT_PACKAGES = [
'wcs',
'authentic2',
'polynum',
'appli_project',
'passerelle',
'docbow',
'compte_agglo_montpellier',
'nose',
]
VERSION = 1
def __init__(self, application):
self.application = application
def __call__(self, environ, start_response):
path = ''
path += quote(environ.get('SCRIPT_NAME', ''))
path += quote(environ.get('PATH_INFO', ''))
method = environ.get('REQUEST_METHOD', 'GET')
if method == 'GET' and path == '/__version__':
packages_version = {}
for distribution in tuple(pkg_resources.WorkingSet()):
project_name = distribution.project_name
version = distribution.version
if project_name in self.ENTROUVERT_PACKAGES:
packages_version[project_name] = version
start_response('200 Ok', [('content-type', 'text/json')])
return [json.dumps(packages_version)]
return self.application(environ, start_response)

32
entrouvert/wsgi/tests.py Normal file
View File

@ -0,0 +1,32 @@
from entrouvert.wsgi.middleware import VersionMiddleware
from unittest import TestCase
class MiddlewareTestCase(TestCase):
def setUp(self):
VersionMiddleware.ENTROUVERT_PACKAGES.append('nose')
def application(self, environ, start_response):
start_response('coin', {'a':'b'})
return ['xxx']
def test_version_middleware(self):
import json
app = VersionMiddleware(self.application)
class start_response_cls:
status = None
headers = None
def __call__(self, status, headers):
self.status = status
self.headers = headers
start_response = start_response_cls()
result = app({ 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/__version__'}, start_response)
result = json.loads(''.join(result))
self.assertIsInstance(result, dict)
self.assertIn('nose', result)
result = app({ 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'}, start_response)
self.assertEqual(result, ['xxx'])
self.assertEqual(start_response.status, 'coin')
self.assertEqual(start_response.headers, {'a': 'b'})
def tearDown(self):
VersionMiddleware.ENTROUVERT_PACKAGES.append('nose')