general: update for python 3 (#22981)

This commit is contained in:
Frédéric Péters 2018-03-27 16:13:33 +02:00
parent e57b51ef9a
commit cf70b636e0
10 changed files with 59 additions and 36 deletions

View File

@ -5,11 +5,12 @@ import logging
import requests
import threading
import os
import urlparse
from django.conf import settings
from django.core.cache import cache
from django.template import Template
from django.utils.encoding import smart_bytes
from django.utils.six.moves.urllib import parse as urlparse
from hobo.scrutiny.wsgi.middleware import VersionMiddleware
@ -20,7 +21,7 @@ CACHE_REFRESH_TIMEOUT = 300
def statics_hash(request):
versions = VersionMiddleware.get_packages_version()
statics_hash = hashlib.md5(repr(sorted(versions.items())))
statics_hash = hashlib.md5(smart_bytes(repr(sorted(versions.items()))))
try:
counter_filename = getattr(settings, 'STATICS_HASH_COUNTER', None)
if counter_filename:
@ -65,8 +66,8 @@ class RemoteTemplate(object):
@property
def cache_key(self):
return hashlib.md5(urlparse.urlunparse(
urlparse.urlparse(self.source)[:3] + ('', '', ''))).hexdigest()
return hashlib.md5(urlparse.urlunparse(
urlparse.urlparse(self.source)[:3] + ('', '', '')).encode('ascii')).hexdigest()
def get_template(self):
item = self.get_cached_item()

View File

@ -1,6 +1,7 @@
import threading
import django
from django.apps import AppConfig, apps
from django.utils import six
from . import settings
@ -53,11 +54,19 @@ class _Timer(TenantAwareThread):
class _MainThread(TenantAwareThread):
def __init__(self):
super(_MainThread, self).__init__(name="MainThread")
self._Thread__started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[threading._get_ident()] = self
if six.PY3:
super(_MainThread, self).__init__(name="MainThread", daemon=False)
self._set_tstate_lock()
self._started.set()
self._set_ident()
with threading._active_limbo_lock:
_active[self._ident] = self
else:
super(_MainThread, self).__init__(name="MainThread")
self._Thread__started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[threading._get_ident()] = self
def _set_daemon(self):
return False
@ -79,6 +88,14 @@ class _MainThread(TenantAwareThread):
class _DummyThread(TenantAwareThread):
def __init__(self):
if six.PY3:
super(_DummyThread, self).__init__(name=threading._newname("Dummy-%d"), daemon=True)
self._started.set()
self._set_ident()
with threading._active_limbo_lock:
threading._active[self._ident] = self
return
super(_DummyThread, self).__init__(name=threading._newname("Dummy-%d"))
# Thread.__block consumes an OS-level locking primitive, which

View File

@ -20,7 +20,7 @@ class MellonAdapter(DefaultAdapter):
hobo_json_path = os.path.join(tenant_dir, 'hobo.json')
if not os.path.exists(hobo_json_path):
return []
hobo_json = json.load(file(hobo_json_path))
hobo_json = json.load(open(hobo_json_path))
# always look for the first active identity provider in the list of
# services
for service in hobo_json.get('services'):

View File

@ -1,11 +1,11 @@
import os
from urlparse import urljoin
import json
from shutil import rmtree
from django.conf import settings
from django.db import connection
from django.utils import timezone
from django.utils.six.moves.urllib.parse import urljoin
from tenant_schemas.utils import get_public_schema_name
from tenant_schemas.models import TenantMixin

View File

@ -36,7 +36,7 @@ class TenantSettingsWrapper(object):
new_time = loader_instance.get_new_time(tenant)
if (not new_time and last_time) \
or (new_time and not last_time) \
or new_time > last_time:
or (new_time and new_time > last_time):
# something is new, call all loaders
for loader in settings.TENANT_SETTINGS_LOADERS:
loader_class = import_class(loader)

View File

@ -1,10 +1,11 @@
import os
import json
import urlparse
import hashlib
from importlib import import_module
from django.conf import settings
from django.utils.encoding import smart_bytes
from django.utils.six.moves.urllib import parse as urlparse
class FileBaseSettingsLoader(object):
@ -37,13 +38,14 @@ class KnownServices(FileBaseSettingsLoader):
@classmethod
def shared_secret(cls, secret1, secret2):
secret1 = hashlib.sha256(secret1).hexdigest()
secret2 = hashlib.sha256(secret2).hexdigest()
return hex(int(secret1, 16) ^ int(secret2, 16))[2:-1]
secret1 = hashlib.sha256(secret1.encode('ascii')).hexdigest()
secret2 = hashlib.sha256(secret2.encode('ascii')).hexdigest()
# rstrip('L') for py2/3 compatibility, as py2 formats number as 0x...L, and py3 as 0x...
return hex(int(secret1, 16) ^ int(secret2, 16))[2:].rstrip('L')
def update_settings_from_path(self, tenant_settings, path):
known_services = {}
with file(path) as f:
with open(path) as f:
hobo_json = json.load(f)
services = hobo_json.get('services')
this = [s for s in services if s.get('this')][0]
@ -130,7 +132,7 @@ class TemplateVars(FileBaseSettingsLoader):
return variables
def update_settings_from_path(self, tenant_settings, path):
with file(path) as f:
with open(path) as f:
hobo_json = json.load(f)
variables = self.get_hobo_json_variables(hobo_json)
@ -155,7 +157,7 @@ class CORSSettings(FileBaseSettingsLoader):
def update_settings_from_path(self, tenant_settings, path):
whitelist = getattr(tenant_settings, 'CORS_ORIGIN_WHITELIST', [])
with file(path) as f:
with open(path) as f:
hobo_json = json.load(f)
for service in hobo_json.get('services', []):
base_url = service.get('base_url')
@ -172,7 +174,7 @@ class SharedThemeSettings(FileBaseSettingsLoader):
FILENAME = 'hobo.json'
def update_settings_from_path(self, tenant_settings, path):
with file(path) as f:
with open(path) as f:
hobo_json = json.load(f)
for service in hobo_json.get('services', []):
if service.get('service-id') != 'combo':
@ -189,7 +191,7 @@ class CookieNames(object):
return 0
def update_settings(self, tenant_settings, tenant):
domain_hash = hashlib.md5(tenant.domain_url).hexdigest()[:6]
domain_hash = hashlib.md5(smart_bytes(tenant.domain_url)).hexdigest()[:6]
tenant_settings.CSRF_COOKIE_NAME = 'csrftoken-%s' % domain_hash
tenant_settings.SESSION_COOKIE_NAME = 'sessionid-%s' % domain_hash
@ -224,7 +226,7 @@ class Authentic(FileBaseSettingsLoader):
def update_settings_from_path(self, tenant_settings, path):
# profile fields
with file(path) as f:
with open(path) as f:
hobo_json = json.load(f)
fields = hobo_json.get('profile', {}).get('fields')
@ -290,7 +292,7 @@ class SettingsJSON(FileBaseSettingsLoader):
FILENAME = 'settings.json'
def update_settings_from_path(self, tenant_settings, path):
with file(path) as f:
with open(path) as f:
json_settings = json.load(f)
for key in json_settings:
setattr(tenant_settings, key, json_settings[key])

View File

@ -95,7 +95,7 @@ class PublikAuthentication(authentication.BaseAuthentication):
self.logger.warning('no known services')
raise exceptions.AuthenticationFailed('No KNOWN_SERVICES setting')
for service_id in settings.KNOWN_SERVICES:
for slug, service in settings.KNOWN_SERVICES[service_id].iteritems():
for slug, service in settings.KNOWN_SERVICES[service_id].items():
if service.get('verif_orig') == orig and service.get('secret'):
return service['secret']
self.logger.warning('no secret found for origin %r', orig)

View File

@ -1,4 +1,5 @@
from urllib import quote
from django.utils.six.moves.urllib.parse import quote
import json
import pkg_resources
try:

View File

@ -4,40 +4,39 @@ import hmac
import hashlib
import urllib
import random
import urlparse
'''Simple signature scheme for query strings'''
from django.utils.encoding import smart_bytes
from django.utils.http import quote, urlencode
from django.utils.six.moves.urllib import parse as urlparse
# Simple signature scheme for query strings
def sign_url(url, key, algo='sha256', timestamp=None, nonce=None):
parsed = urlparse.urlparse(url)
new_query = sign_query(parsed.query, key, algo, timestamp, nonce)
return urlparse.urlunparse(parsed[:4] + (new_query,) + parsed[5:])
def sign_query(query, key, algo='sha256', timestamp=None, nonce=None):
if timestamp is None:
timestamp = datetime.datetime.utcnow()
timestamp = timestamp.strftime('%Y-%m-%dT%H:%M:%SZ')
if nonce is None:
nonce = hex(random.SystemRandom().getrandbits(128))[2:-1]
nonce = hex(random.getrandbits(128))[2:]
new_query = query
if new_query:
new_query += '&'
new_query += urllib.urlencode((
new_query += urlencode((
('algo', algo),
('timestamp', timestamp),
('nonce', nonce)))
signature = base64.b64encode(sign_string(new_query, key, algo=algo))
new_query += '&signature=' + urllib.quote(signature)
new_query += '&signature=' + quote(signature)
return new_query
def sign_string(s, key, algo='sha256', timedelta=30):
digestmod = getattr(hashlib, algo)
if isinstance(key, unicode):
key = key.encode('utf-8')
hash = hmac.HMAC(key, digestmod=digestmod, msg=s)
hash = hmac.HMAC(smart_bytes(key), digestmod=digestmod, msg=smart_bytes(s))
return hash.digest()
@ -48,6 +47,9 @@ def check_url(url, key, known_nonce=None, timedelta=30):
def check_query(query, key, known_nonce=None, timedelta=30):
parsed = urlparse.parse_qs(query)
if not ('signature' in parsed and 'algo' in parsed and
'timestamp' in parsed and 'nonce' in parsed):
return False
signature = base64.b64decode(parsed['signature'][0])
algo = parsed['algo'][0]
timestamp = parsed['timestamp'][0]

View File

@ -35,7 +35,7 @@ def get_version():
p = subprocess.Popen(['git', 'describe', '--dirty', '--match=v*'], stdout=subprocess.PIPE)
result = p.communicate()[0]
if p.returncode == 0:
version = result.split()[0][1:]
version = str(result.split()[0][1:])
version = version.replace('-', '.')
return version
return '0'