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

kedifa/app: Make it python3 importable

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