Project

General

Profile

Download (1.36 KB) Statistics
| Branch: | Tag: | Revision:

root / entrouvert / wsgi / middleware.py @ fad6eb8c

1
from urllib import quote
2
import json
3
import pkg_resources
4

    
5
class VersionMiddleware(object):
6
    ENTROUVERT_PACKAGES = [
7
            'wcs',
8
            'wcs_au_quotidien',
9
            'authentic2',
10
            'polynum',
11
            'appli_project',
12
            'passerelle',
13
            'docbow',
14
            'compte_agglo_montpellier',
15
            'entrouvert',
16
            'nose',
17
    ]
18
    VERSION = 1
19

    
20
    def __init__(self, application):
21
        self.application = application
22

    
23
    def __call__(self, environ, start_response):
24
        path = ''
25
        path += quote(environ.get('SCRIPT_NAME', ''))
26
        path += quote(environ.get('PATH_INFO', ''))
27
        method = environ.get('REQUEST_METHOD', 'GET')
28
        if method == 'GET' and path == '/__version__':
29
            packages_version = self.get_packages_version()
30
            start_response('200 Ok', [('content-type', 'application/json')])
31
            return [json.dumps(packages_version)]
32
        return self.application(environ, start_response)
33

    
34
    @classmethod
35
    def get_packages_version(cls):
36
        packages_version = {}
37
        for distribution in tuple(pkg_resources.WorkingSet()):
38
            project_name = distribution.project_name
39
            version = distribution.version
40
            if project_name in cls.ENTROUVERT_PACKAGES:
41
                packages_version[project_name] = version
42
        return packages_version
(2-2/3)