103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
import json
|
|
import urllib.parse
|
|
|
|
import pkg_resources
|
|
from django.utils.deprecation import MiddlewareMixin
|
|
|
|
try:
|
|
import apt.cache as apt_cache
|
|
except ImportError:
|
|
apt_cache = None
|
|
|
|
|
|
class VersionMiddleware(MiddlewareMixin):
|
|
ENTROUVERT_PACKAGES = [
|
|
'wcs',
|
|
'wcs-au-quotidien',
|
|
'authentic2',
|
|
'authentic2-idp-cas',
|
|
'authentic2-idp-ltpa',
|
|
'authentic2-idp-oauth2',
|
|
'polynum',
|
|
'appli_project',
|
|
'passerelle',
|
|
'docbow',
|
|
'calebasse',
|
|
'python-entrouvert',
|
|
'nose',
|
|
'portail-citoyen',
|
|
'portail-citoyen2',
|
|
'portail-citoyen-announces',
|
|
'django-cms-ajax-text-plugin',
|
|
'eopayment',
|
|
'compte-meyzieu',
|
|
'compte-agglo-montpellier',
|
|
'compte-orleans',
|
|
'hobo',
|
|
'django-cmsplugin-blurp',
|
|
'combo',
|
|
'lingo',
|
|
'fargo',
|
|
'welco',
|
|
'chrono',
|
|
]
|
|
VERSION = 1
|
|
_packages_version = None
|
|
|
|
def __init__(self, application):
|
|
self.application = application
|
|
|
|
def __call__(self, environ, start_response):
|
|
path = ''
|
|
path += urllib.parse.quote(environ.get('SCRIPT_NAME', ''))
|
|
path += urllib.parse.quote(environ.get('PATH_INFO', ''))
|
|
method = environ.get('REQUEST_METHOD', 'GET')
|
|
if method == 'GET' and (path == '/__version__' or path == '/__version__/'):
|
|
packages_version = self.get_packages_version()
|
|
start_response('200 Ok', [('content-type', 'application/json')])
|
|
return [json.dumps(packages_version)]
|
|
return self.application(environ, start_response)
|
|
|
|
@classmethod
|
|
def get_packages_version(cls):
|
|
if cls._packages_version is not None:
|
|
return cls._packages_version
|
|
packages_version = {}
|
|
# get versions from setuptools
|
|
for distribution in tuple(pkg_resources.WorkingSet()):
|
|
project_name = distribution.project_name
|
|
version = distribution.version
|
|
version = version.replace('+', '-') # for PEP440
|
|
if project_name in cls.ENTROUVERT_PACKAGES:
|
|
packages_version[project_name] = version
|
|
# get versions from Debian packages
|
|
debian_cache = None
|
|
if apt_cache:
|
|
try:
|
|
debian_cache = apt_cache.Cache()
|
|
except SystemError:
|
|
pass
|
|
if debian_cache:
|
|
versions = [
|
|
(p.name, p.installed.source_name, p.installed.version)
|
|
for p in debian_cache
|
|
if p.installed
|
|
and (
|
|
(p.installed and p.installed.origins[0].origin == "Entr'ouvert")
|
|
or (p.candidate and p.candidate.origins[0].origin == "Entr'ouvert")
|
|
)
|
|
]
|
|
for name, project_name, version in versions:
|
|
version = version.split('-')[0] # debian format == pythonversion-xxx
|
|
version = version.replace('+', '-') # for PEP440
|
|
# alert if a version is already present and is different
|
|
if (
|
|
project_name in packages_version
|
|
and version != packages_version[project_name].split('!!')[0]
|
|
):
|
|
packages_version[project_name] += '!!%s.deb:%s' % (name, version)
|
|
else:
|
|
packages_version[project_name] = version
|
|
cls._packages_version = packages_version
|
|
return packages_version
|