Skip to content

Commit

Permalink
Merge branch 'master' into 125-validate-aws-credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger authored Nov 16, 2020
2 parents b5b8ee4 + f61163b commit 54cb426
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 141 deletions.
6 changes: 0 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
import os
import pytest

# Python retro compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = OSError


def clean_up():
"""
Expand Down
18 changes: 3 additions & 15 deletions helpers/cli.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import subprocess
import sys
import re
import textwrap

PY2 = sys.version_info[0] == 2
if PY2:
input = raw_input
string_type = unicode
else:
string_type = str


class CLI(object):
class CLI:

NO_COLOR = '\033[0;0m'
COLOR_ERROR = '\033[0;31m' # dark red
Expand Down Expand Up @@ -108,7 +99,7 @@ def get_response(cls, validators=None, default='', to_lower=True,

if (response.lower() in map(lambda x: x.lower(), validators) or
validators is None or
(isinstance(validators, string_type) and
(isinstance(validators, str) and
validators.startswith('~') and
re.match(validators[1:], response)
)):
Expand Down Expand Up @@ -152,10 +143,7 @@ def run_command(cls, command, cwd=None, polling=False):
if output == '' and process.poll() is not None:
break
if output:
if PY2:
print(output.strip())
else:
print(output.decode().strip())
print(output.decode().strip())
return process.poll()
else:
try:
Expand Down
1 change: 0 additions & 1 deletion helpers/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import sys
import time
Expand Down
20 changes: 6 additions & 14 deletions helpers/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, division

import binascii
import json
import os
Expand All @@ -15,20 +13,14 @@

from helpers.cli import CLI
from helpers.network import Network
from helpers.singleton import Singleton, with_metaclass
from helpers.singleton import Singleton
from helpers.upgrading import Upgrading
from helpers.aws_validation import AWSValidation

# Python retro compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError


# Use this class as a singleton to get the same configuration
# for each instantiation.
class Config(with_metaclass(Singleton)):
class Config(metaclass=Singleton):

CONFIG_FILE = '.run.conf'
UNIQUE_ID_FILE = '.uniqid'
Expand All @@ -39,7 +31,7 @@ class Config(with_metaclass(Singleton)):
DEFAULT_NGINX_PORT = '80'
DEFAULT_NGINX_HTTPS_PORT = '443'
KOBO_DOCKER_BRANCH = '2.020.45'
KOBO_INSTALL_VERSION = '4.0.0'
KOBO_INSTALL_VERSION = '4.2.0'
MAXIMUM_AWS_CREDENTIAL_ATTEMPTS = 3

def __init__(self):
Expand Down Expand Up @@ -326,11 +318,11 @@ def get_template(cls):
'debug': False,
'default_from_email': '[email protected]',
'dev_mode': False,
'django_secret_key': binascii.hexlify(os.urandom(50)).decode('utf-8'),
'django_secret_key': binascii.hexlify(os.urandom(50)).decode(),
'docker_prefix': '',
'ee_subdomain': 'ee',
'enketo_api_token': binascii.hexlify(os.urandom(60)).decode('utf-8'),
'enketo_encryption_key': binascii.hexlify(os.urandom(60)).decode('utf-8'),
'enketo_api_token': binascii.hexlify(os.urandom(60)).decode(),
'enketo_encryption_key': binascii.hexlify(os.urandom(60)).decode(),
# default value from enketo. Because it was not customizable before
# we want to keep the same value when users upgrade.
'enketo_less_secure_encryption_key': 'this $3cr3t key is crackable',
Expand Down
13 changes: 3 additions & 10 deletions helpers/network.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import array
import fcntl
try:
import httplib
from urllib2 import urlopen
except:
from http import client as httplib
from urllib.request import urlopen

import platform
import socket
import struct
import sys
from http import client as httplib
from urllib.request import urlopen

from helpers.cli import CLI

Expand Down Expand Up @@ -98,7 +91,7 @@ def get_local_interfaces(all_=False):

for i in range(0, max_bytes_out, 40):
name = namestr[i: i + 16].split(FILL_CHAR, 1)[0]
name = name.decode('utf-8')
name = name.decode()
ip_bytes = namestr[i + 20:i + 24]
full_addr = []
for netaddr in ip_bytes:
Expand Down
1 change: 0 additions & 1 deletion helpers/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
import shutil
import sys
Expand Down
20 changes: 0 additions & 20 deletions helpers/singleton.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function


# Copy this method from `six` library to avoid import
# Remove it when dropping Python2 support
def with_metaclass(meta, *bases):
"""
Create a base class with a metaclass.
"""

# This requires a bit of explanation: the basic idea is to make a dummy
# metaclass for one level of class instantiation that replaces itself with
# the actual metaclass.
class metaclass(meta):

def __new__(cls, name, this_bases, d):
return meta(name, bases, d)
return type.__new__(metaclass, 'temporary_class', (), {})


class Singleton(type):
_instances = {}

Expand Down
11 changes: 2 additions & 9 deletions helpers/template.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import fnmatch
import json
import os
import re
import stat
import sys

try:
from urllib.parse import quote_plus
except ImportError:
from urllib import quote_plus

from string import Template as PyTemplate
from urllib.parse import quote_plus

from helpers.cli import CLI
from helpers.config import Config
Expand Down Expand Up @@ -51,7 +44,7 @@ def render(cls, config, force=False):
'Do you want to continue?',
default=False
)
if response:
if not response:
sys.exit(0)

cls.__write_unique_id(environment_directory, dict_['unique_id'])
Expand Down
2 changes: 0 additions & 2 deletions helpers/updater.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import os
import sys

Expand Down
2 changes: 0 additions & 2 deletions helpers/upgrading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import subprocess
import sys

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ User can choose between 2 types of installations:
## Requirements

- Linux <sup>5</sup> / macOS <sup>6</sup>
- Python 2.7/3.5+ <sup>_Python2 support will be dropped in a future release_</sup>
- Python 3.5+
- [Docker](https://www.docker.com/get-started "") & [Docker Compose](https://docs.docker.com/compose/install/ "")
- Available TCP Ports: <sup>7</sup>

Expand Down
15 changes: 0 additions & 15 deletions requirements_py2_tests.txt

This file was deleted.

File renamed without changes.
20 changes: 9 additions & 11 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

import platform
import sys

from helpers.cli import CLI
if sys.version_info[0] == 2:
message = (
'Python 2.7 has reached the end of its life on '
'January 1st, 2020. Please upgrade your Python as Python 2.7 is '
'not maintained anymore.'
)
CLI.framed_print(message, color=CLI.COLOR_ERROR)
sys.exit(1)

from helpers.command import Command
from helpers.config import Config
from helpers.setup import Setup
Expand All @@ -15,15 +22,6 @@

def run(force_setup=False):

if sys.version_info[0] == 2:
message = (
'DEPRECATION: Python 2.7 has reached the end of its life on '
'January 1st, 2020. Please upgrade your Python as Python 2.7 is '
'not maintained anymore.\n\n'
'A future version of KoBoInstall will drop support for it.'
)
CLI.framed_print(message)

if not platform.system() in ['Linux', 'Darwin']:
CLI.colored_print('Not compatible with this OS', CLI.COLOR_ERROR)
else:
Expand Down
8 changes: 1 addition & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

import os
import pytest
import random
import shutil
import tempfile
import time

try:
from unittest.mock import patch, MagicMock
except ImportError:
from mock import patch, MagicMock
from unittest.mock import patch, MagicMock

from helpers.cli import CLI
from helpers.config import Config
Expand Down
9 changes: 1 addition & 8 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

try:
from unittest.mock import patch, MagicMock
builtin_open = 'builtins.open'
except ImportError:
from mock import patch, MagicMock
builtin_open = '__builtin__.open'
from unittest.mock import patch, MagicMock

from helpers.command import Command
from .utils import (
Expand Down
17 changes: 5 additions & 12 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
try:
from unittest.mock import patch, mock_open
builtin_open = 'builtins.open'
except ImportError:
from mock import patch, mock_open
builtin_open = '__builtin__.open'
from unittest.mock import patch, mock_open

from helpers.config import Config
from helpers.singleton import Singleton, with_metaclass
from helpers.singleton import Singleton


def read_config(overrides=None):
Expand All @@ -25,7 +18,7 @@ def read_config(overrides=None):
# We need to mock `open()` twice.
# - Once to read kobo-install config file (i.e. `.run.conf`)
# - Once to read value of `unique_id` (i.e. `/tmp/.uniqid`)
with patch(builtin_open, spec=open) as mock_file:
with patch('builtins.open', spec=open) as mock_file:
mock_file.side_effect = iter([
mock_open(read_data=str_config).return_value,
mock_open(read_data='').return_value,
Expand All @@ -35,7 +28,7 @@ def read_config(overrides=None):
# We call `read_config()` another time to be sure to reset the config
# before each test. Thanks to `mock_open`, `Config.get_dict()` always
# returns `config_dict`.
with patch(builtin_open, spec=open) as mock_file:
with patch('builtins.open', spec=open) as mock_file:
mock_file.side_effect = iter([
mock_open(read_data=str_config).return_value,
mock_open(read_data='').return_value,
Expand Down Expand Up @@ -77,7 +70,7 @@ def run_command(cls, command, cwd=None, polling=False):
return mock_docker.compose(command, cwd)


class MockDocker(with_metaclass(Singleton)):
class MockDocker(metaclass=Singleton):

PRIMARY_BACKEND_CONTAINERS = ['primary_postgres',
'mongo',
Expand Down
9 changes: 2 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# content of: tox.ini , put in same dir as setup.py
[tox]
skipsdist=True
envlist = py27,py37,py38
envlist = py36,py37,py38

[testenv]
deps = -rrequirements_py3_tests.txt
commands =
pytest -vv {posargs} --disable-pytest-warnings

[testenv:py27]
deps = -rrequirements_py2_tests.txt
deps = -rrequirements_tests.txt
commands =
pytest -vv {posargs} --disable-pytest-warnings

0 comments on commit 54cb426

Please sign in to comment.