Commit 5c142964 authored by Łukasz Nowak's avatar Łukasz Nowak

kedifa/app: Make it python3 importable

parent d6bbd7db
...@@ -33,13 +33,17 @@ import signal ...@@ -33,13 +33,17 @@ import signal
import sqlite3 import sqlite3
import ssl import ssl
import string import string
import urlparse try:
import urlparse
except ImportError:
from urllib.parse import urlparse
  • BTW I feel it's a bit better if we can use six.move for python 2 and python3 compatibility, because it would make it easier to drop python2 support later (just grep for six and remove all) and also because tools such as pylint or mypy do not like this try/except ImportError pattern.

  • I'll do.

  • Thanks @luke

Please register or sign in to reply
import logging import logging
import logging.handlers import logging.handlers
import socket import socket
socket.setdefaulttimeout(10) socket.setdefaulttimeout(10)
class UserExists(Exception): class UserExists(Exception):
pass pass
...@@ -398,7 +402,7 @@ class Kedifa(object): ...@@ -398,7 +402,7 @@ class Kedifa(object):
request_body = environ['wsgi.input'].read(request_body_size) request_body = environ['wsgi.input'].read(request_body_size)
try: try:
certificate = self.checkKeyCertificate(request_body) certificate = self.checkKeyCertificate(request_body)
except CertificateError, e: except CertificateError as e:
start_response('422 Unprocessable Entity', headers_text_plain) start_response('422 Unprocessable Entity', headers_text_plain)
return e return e
else: else:
......
...@@ -17,13 +17,18 @@ ...@@ -17,13 +17,18 @@
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
from __future__ import print_function
import argparse import argparse
import httplib try:
import httplib
except ImportError:
from http import client as httplib
import requests import requests
import sys import sys
import app from . import app
from updater import Updater from .updater import Updater
def http(*args): def http(*args):
...@@ -120,12 +125,12 @@ def getter(*args): ...@@ -120,12 +125,12 @@ def getter(*args):
response = requests.get(url, verify=parsed.server_ca_certificate.name, response = requests.get(url, verify=parsed.server_ca_certificate.name,
cert=parsed.identity.name) cert=parsed.identity.name)
except Exception as e: except Exception as e:
print '%r not downloaded, problem %s' % (url, e) print('%r not downloaded, problem %s' % (url, e))
sys.exit(1) sys.exit(1)
else: else:
if response.status_code != httplib.OK: if response.status_code != httplib.OK:
print '%r not downloaded, HTTP code %s' % ( print('%r not downloaded, HTTP code %s' % (
url, response.status_code) url, response.status_code))
sys.exit(1) sys.exit(1)
if len(response.text) > 0: if len(response.text) > 0:
with open(parsed.out, 'w') as out: with open(parsed.out, 'w') as out:
......
...@@ -17,10 +17,16 @@ ...@@ -17,10 +17,16 @@
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
import StringIO try:
import StringIO
except ImportError:
from io import StringIO
import contextlib import contextlib
import datetime import datetime
import httplib try:
import httplib
except ImportError:
from http import client as httplib
import ipaddress import ipaddress
import json import json
import mock import mock
...@@ -47,8 +53,8 @@ from cryptography.x509.oid import NameOID ...@@ -47,8 +53,8 @@ from cryptography.x509.oid import NameOID
import caucase.cli import caucase.cli
import caucase.http import caucase.http
import cli from . import cli
import updater from . import updater
def findFreeTCPPort(ip=''): def findFreeTCPPort(ip=''):
......
import httplib from __future__ import print_function
try:
import httplib
except ImportError:
from http import client as httplib
import json import json
import os import os
import requests import requests
...@@ -38,11 +43,11 @@ class Updater(object): ...@@ -38,11 +43,11 @@ class Updater(object):
elif len(line_content) == 3: elif len(line_content) == 3:
url, certificate, fallback = line_content url, certificate, fallback = line_content
else: else:
print 'Line %r is incorrect' % (line,) print('Line %r is incorrect' % (line,))
continue continue
if certificate in self.mapping: if certificate in self.mapping:
print 'Line %r is incorrect, duplicated certificate %r' % ( print('Line %r is incorrect, duplicated certificate %r' % (
line, certificate) line, certificate))
raise ValueError raise ValueError
self.mapping[certificate] = (url, fallback) self.mapping[certificate] = (url, fallback)
...@@ -53,16 +58,16 @@ class Updater(object): ...@@ -53,16 +58,16 @@ class Updater(object):
url, verify=self.server_ca_certificate_file, cert=self.identity_file, url, verify=self.server_ca_certificate_file, cert=self.identity_file,
timeout=10) timeout=10)
except Exception as e: except Exception as e:
print 'Certificate %r: problem with %r not downloaded: %s' % ( print('Certificate %r: problem with %r not downloaded: %s' % (
certificate_file, url, e) certificate_file, url, e))
else: else:
if response.status_code != httplib.OK: if response.status_code != httplib.OK:
print 'Certificate %r: %r not downloaded, HTTP code %s' % ( print('Certificate %r: %r not downloaded, HTTP code %s' % (
certificate_file, url, response.status_code) certificate_file, url, response.status_code))
else: else:
certificate = response.text certificate = response.text
if len(certificate) == 0: if len(certificate) == 0:
print 'Certificate %r: %r is empty' % (certificate_file, url,) print('Certificate %r: %r is empty' % (certificate_file, url,))
return certificate return certificate
def updateCertificate(self, certificate_file, master_content=None): def updateCertificate(self, certificate_file, master_content=None):
...@@ -98,7 +103,7 @@ class Updater(object): ...@@ -98,7 +103,7 @@ class Updater(object):
if current != certificate: if current != certificate:
with open(certificate_file, 'w') as fh: with open(certificate_file, 'w') as fh:
fh.write(certificate) fh.write(certificate)
print 'Certificate %r: updated from %r' % (certificate_file, url) print('Certificate %r: updated from %r' % (certificate_file, url))
return True return True
else: else:
return False return False
...@@ -106,7 +111,7 @@ class Updater(object): ...@@ -106,7 +111,7 @@ class Updater(object):
def callOnUpdate(self): def callOnUpdate(self):
if self.on_update is not None: if self.on_update is not None:
status = os.system(self.on_update) status = os.system(self.on_update)
print 'Called %r with status %i' % (self.on_update, status) print('Called %r with status %i' % (self.on_update, status))
def readState(self): def readState(self):
self.state_dict = {} self.state_dict = {}
...@@ -137,8 +142,8 @@ class Updater(object): ...@@ -137,8 +142,8 @@ class Updater(object):
open(self.master_certificate_file, 'w').write( open(self.master_certificate_file, 'w').write(
open(master_certificate_file_fallback, 'r').read() open(master_certificate_file_fallback, 'r').read()
) )
print 'Prepare: Used %r for %r' % ( print('Prepare: Used %r for %r' % (
master_certificate_file_fallback, self.master_certificate_file) master_certificate_file_fallback, self.master_certificate_file))
master_content = None master_content = None
if self.master_certificate_file and os.path.exists( if self.master_certificate_file and os.path.exists(
...@@ -150,11 +155,11 @@ class Updater(object): ...@@ -150,11 +155,11 @@ class Updater(object):
continue continue
if fallback and os.path.exists(fallback): if fallback and os.path.exists(fallback):
open(certificate, 'w').write(open(fallback, 'r').read()) open(certificate, 'w').write(open(fallback, 'r').read())
print 'Prepare: Used %r for %r' % (fallback, certificate) print('Prepare: Used %r for %r' % (fallback, certificate))
elif master_content: elif master_content:
open(certificate, 'w').write(master_content) open(certificate, 'w').write(master_content)
print 'Prepare: Used %r for %r' % ( print('Prepare: Used %r for %r' % (
self.master_certificate_file, certificate) self.master_certificate_file, certificate))
def action(self): def action(self):
self.readState() self.readState()
...@@ -170,8 +175,8 @@ class Updater(object): ...@@ -170,8 +175,8 @@ class Updater(object):
with open(self.master_certificate_file, 'r') as fh: with open(self.master_certificate_file, 'r') as fh:
master_content = fh.read() or None master_content = fh.read() or None
if master_content: if master_content:
print 'Using master certificate from %r' % ( print('Using master certificate from %r' % (
self.master_certificate_file,) self.master_certificate_file,))
except IOError: except IOError:
pass pass
...@@ -189,12 +194,12 @@ class Updater(object): ...@@ -189,12 +194,12 @@ class Updater(object):
if not self.prepare_only: if not self.prepare_only:
lock = zc.lockfile.LockFile(self.state_lock_file) lock = zc.lockfile.LockFile(self.state_lock_file)
except zc.lockfile.LockError as e: except zc.lockfile.LockError as e:
print e, print(e,)
if self.once or self.prepare_only: if self.once or self.prepare_only:
print '...exiting.' print('...exiting.')
sys.exit(1) sys.exit(1)
else: else:
print "...will try again later." print("...will try again later.")
else: else:
try: try:
self.prepare() self.prepare()
...@@ -205,9 +210,9 @@ class Updater(object): ...@@ -205,9 +210,9 @@ class Updater(object):
lock.close() lock.close()
try: try:
os.unlink(self.state_lock_file) os.unlink(self.state_lock_file)
except Exception as e: except Exception:
print 'Problem while unlinking %r' % (self.state_lock_file,) print('Problem while unlinking %r' % (self.state_lock_file,))
if self.once or self.prepare_only: if self.once or self.prepare_only:
break break
print 'Sleeping for %is' % (self.sleep,) print('Sleeping for %is' % (self.sleep,))
time.sleep(self.sleep) time.sleep(self.sleep)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment