Commit 8f6359f0 authored by Alexis PASQUIER's avatar Alexis PASQUIER
Browse files

[Py2.7] Passage en python 2.7

parent 0872e675
Pipeline #73270 failed with stages
in 2 minutes and 14 seconds
......@@ -7,6 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/dist" />
<excludeFolder url="file://$MODULE_DIR$/odoo_launcher.egg-info" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
......
from __future__ import unicode_literals
import logging
import os
import sys
from . import laucher # noqa
from . import api, config_section, mapper # noqa
from .odoo_config_maker import OdooConfig # noqa
from .odoo_config_maker import OdooConfig, OdooEnvConverter # noqa
_logger = logging.getLogger(__name__)
......
import abc
import sys
from collections import OrderedDict
from typing import Any, Dict, Union
if sys.version_info[0] == 2: # Python 2
class Dictable(abc.ABC):
class ABC(object):
__meta_class__ = abc.ABCMeta
__slots__ = ()
else:
ABC = abc.ABC
class Dictable(ABC):
def to_dict(self):
# type: () -> Dict[str, Any]
raise NotImplementedError()
......@@ -10,7 +21,7 @@ class Dictable(abc.ABC):
@staticmethod
def clean_config_dict(values):
# type: (Union[Dict[str, Any], Dictable]) -> Dict[str, str]
new_values = {}
new_values = OrderedDict()
if isinstance(values, Dictable):
values = values.to_dict()
......@@ -26,7 +37,7 @@ class Dictable(abc.ABC):
@staticmethod
def clean_none_env_vars(dict_value):
# type: (Union[Dict[str, Any], Dictable]) -> Dict[str, Any]
result = {}
result = OrderedDict()
if isinstance(dict_value, Dictable):
dict_value = dict_value.to_dict()
......@@ -36,7 +47,7 @@ class Dictable(abc.ABC):
return result
class ConfigConvert(abc.ABC):
class ConfigConvert(ABC):
def is_true(self, any):
# type: (Union[str, bool, int, None]) -> bool
if not isinstance(any, (str, bool, int)):
......@@ -58,7 +69,7 @@ class EnvMapper(ConfigConvert):
raise NotImplementedError()
class OdooConfigABC(ConfigConvert, abc.ABC):
class OdooConfigABC(ConfigConvert, ABC):
def __init__(self, main_instance=True):
super(OdooConfigABC, self).__init__()
self.main_instance = main_instance
......@@ -68,7 +79,7 @@ class OdooConfigABC(ConfigConvert, abc.ABC):
raise NotImplementedError
class OdooConfigSection(ConfigConvert, Dictable, abc.ABC):
class OdooConfigSection(ConfigConvert, Dictable, ABC):
def __init__(self, odoo_config_maker, env_vars):
# type: (OdooConfigABC, Dict[str, Union[str, bool, int, None]]) -> None
self.config_maker = odoo_config_maker
......
import enum
from collections import OrderedDict
from typing import Dict, Union
from addons_installer import addons_installer
from api import OdooConfigABC, OdooConfigSection
from .api import OdooConfigABC, OdooConfigSection
class WorkersOdooConfigSection(OdooConfigSection):
......@@ -57,7 +59,7 @@ class WorkersOdooConfigSection(OdooConfigSection):
class LimitOdooConfigSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(LimitOdooConfigSection, self).__init__(odoo_config_maker, env_vars)
self.limit_request = int(env_vars.get("LIMIT_REQUEST", 0)) or None
self.limit_time_cpu = int(env_vars.get("LIMIT_TIME_CPU", 0)) or None
self.limit_time_real = int(env_vars.get("LIMIT_TIME_REAL", 0)) or None
......@@ -93,7 +95,7 @@ class DatabaseOdooConfigSection(OdooConfigSection):
FIXED = "FIXED"
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(DatabaseOdooConfigSection, self).__init__(odoo_config_maker, env_vars)
self.name = env_vars.get("DB_NAME")
self.host = env_vars.get("DB_HOST")
self.port = self.to_int(env_vars.get("DB_PORT")) or None
......@@ -104,8 +106,11 @@ class DatabaseOdooConfigSection(OdooConfigSection):
self.log_enable = env_vars.get("LOG_DB")
self.log_level = env_vars.get("LOG_DB_LEVEL")
self.show = self.is_true(env_vars.get("LIST_DB"))
mode_env = env_vars.get("DB_MAXCONN_MODE")
if not mode_env or mode_env not in (list(DatabaseOdooConfigSection.MaxConnMode)):
mode_env = env_vars.get("DB_MAX_CONN_MODE")
if not mode_env or mode_env not in (
DatabaseOdooConfigSection.MaxConnMode.FIXED.value,
DatabaseOdooConfigSection.MaxConnMode.AUTO.value,
):
mode = DatabaseOdooConfigSection.MaxConnMode.AUTO
else:
mode = DatabaseOdooConfigSection.MaxConnMode[mode_env]
......@@ -118,8 +123,7 @@ class DatabaseOdooConfigSection(OdooConfigSection):
if mode == DatabaseOdooConfigSection.MaxConnMode.AUTO and not self.max_conn:
self.max_conn = min_conn
# We add some security because sometime worker open 2 or more connecions (Ex :bus.bus)
self.max_conn = max(self.max_conn, min_conn, 2)
self.max_conn = max(self.max_conn, min_conn, 2)
if self.filter and not self.show:
self.show = True
......@@ -131,21 +135,21 @@ class DatabaseOdooConfigSection(OdooConfigSection):
def to_dict(self):
if not self.enable:
return {}
return {
"--db_host": self.host,
"--db_port": self.port,
"--db_user": self.user,
"--db_password": self.password,
"--database": self.name,
"--no-database-list": not self.show,
"--db_maxconn": self.max_conn,
"--db-filter": self.filter,
}
res = OrderedDict()
res["--db_host"] = self.host
res["--db_port"] = self.port
res["--db_user"] = self.user
res["--db_password"] = self.password
res["--database"] = self.name
res["--no-database-list"] = not self.show
res["--db_maxconn"] = self.max_conn
res["--db-filter"] = self.filter
return res
class HttpOdooConfigSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(HttpOdooConfigSection, self).__init__(odoo_config_maker, env_vars)
self.enable = self.is_true(env_vars.get("HTTP_ENABLE", "True"))
self.interface = None
self.port = None
......@@ -162,16 +166,16 @@ class HttpOdooConfigSection(OdooConfigSection):
return {
"--no-%s" % key_http: not self.enable,
}
return {
"--%s-interface" % key_http: self.interface,
"--%s-port" % key_http: self.port,
"--longpolling-port": self.longpolling_port,
}
res = OrderedDict()
res["--%s-interface" % key_http] = self.interface
res["--%s-port" % key_http] = self.port
res["--longpolling-port"] = self.longpolling_port
return res
class ServerWideModuleConfigSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(ServerWideModuleConfigSection, self).__init__(odoo_config_maker, env_vars)
str_server_wide_modules = env_vars.get("SERVER_WIDE_MODULES")
self.server_wide_modules = str_server_wide_modules and str_server_wide_modules.split(",") or ["base", "web"]
self.queue_job_module_name = None
......@@ -198,7 +202,7 @@ class ServerWideModuleConfigSection(OdooConfigSection):
class OtherSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(OtherSection, self).__init__(odoo_config_maker, env_vars)
self.unaccent = self.is_true(env_vars.get("UNACCENT", True))
self.test_enable = self.is_true(env_vars.get("TEST_ENABLE"))
self.without_demo = self.is_true(env_vars.get("WITHOUT_DEMO"))
......@@ -206,16 +210,16 @@ class OtherSection(OdooConfigSection):
def to_dict(self):
if not self.enable:
return {"--unaccent": self.unaccent}
return {
"--unaccent": self.unaccent,
"--test-enable": self.test_enable,
"--without-demo": self.without_demo,
}
res = OrderedDict()
res["--unaccent"] = self.unaccent
res["--test-enable"] = self.test_enable
res["--without-demo"] = self.without_demo
return res
class LoggerSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(LoggerSection, self).__init__(odoo_config_maker, env_vars)
self.logfile = env_vars.get("LOGFILE")
self.log_handler = env_vars.get("LOG_HANDLER")
self.log_request = self.is_true(env_vars.get("LOG_REQUEST"))
......@@ -229,22 +233,22 @@ class LoggerSection(OdooConfigSection):
def to_dict(self):
if not self.enable:
return {}
return {
"--logfile": self.logfile,
"--log-handler": self.log_handler,
"--log-request": self.log_request,
"--log-response": self.log_response,
"--log-web": self.log_web,
"--log-sql": self.log_sql,
"--log-db": self.log_db,
"--log-db-level": self.log_db_level,
"--log-level": self.log_level,
}
res = OrderedDict()
res["--logfile"] = self.logfile
res["--log-handler"] = self.log_handler
res["--log-request"] = self.log_request
res["--log-response"] = self.log_response
res["--log-web"] = self.log_web
res["--log-sql"] = self.log_sql
res["--log-db"] = self.log_db
res["--log-db-level"] = self.log_db_level
res["--log-level"] = self.log_level
return res
class UpdateInstallSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(UpdateInstallSection, self).__init__(odoo_config_maker, env_vars)
self.update = [u.strip() for u in env_vars.get("UPDATE", "").split(",")]
self.install = [i.strip() for i in env_vars.get("INSTALL", "").split(",")]
self.stop_after_init = self.is_true(env_vars.get("STOP_AFTER_INIT"))
......@@ -253,27 +257,25 @@ class UpdateInstallSection(OdooConfigSection):
self.force_save_config_file = False
def to_dict(self):
default_res = {}
default_res = OrderedDict()
if self.force_save_config_file:
default_res["--save"] = self.force_save_config_file
if self.force_stop_after_init:
default_res["--stop-after-init"] = self.force_stop_after_init
if not self.enable:
return default_res
return dict(
{
"--update": ",".join(self.update),
"--install": ",".join(self.install),
"--stop-after-init": self.stop_after_init,
"--save": self.save_config_file,
},
**default_res
)
res = OrderedDict()
res["--update"] = ",".join(self.update)
res["--install"] = ",".join(self.install)
res["--stop-after-init"] = self.stop_after_init
res["--save"] = self.save_config_file
res.update(default_res)
return res
class AddonsPathConfigSection(OdooConfigSection):
def __init__(self, odoo_config_maker, env_vars):
super().__init__(odoo_config_maker, env_vars)
super(AddonsPathConfigSection, self).__init__(odoo_config_maker, env_vars)
registry = addons_installer.AddonsRegistry()
result = registry.parse_env(env_vars=env_vars)
self.addons_path = [r.addons_path for r in result]
......
from __future__ import unicode_literals
import argparse
import configparser
import logging
......@@ -7,7 +9,7 @@ import sys
import uuid
from typing import Dict, List, Optional
from odoo_launcher import OdooConfig
from .odoo_config_maker import OdooConfig
_logger = logging.getLogger("launch")
_logger.setLevel(logging.DEBUG)
......
from api import EnvMapper
from .api import EnvMapper
class OdooCompatibilityMapper(EnvMapper):
......
from __future__ import unicode_literals
import logging
import os
import pprint
from collections import OrderedDict
from typing import Dict, List, Optional
from api import ConfigConvert, Dictable, EnvMapper, OdooConfigABC
from config_section import (
from .api import ConfigConvert, Dictable, EnvMapper, OdooConfigABC
from .config_section import (
AddonsPathConfigSection,
DatabaseOdooConfigSection,
HttpOdooConfigSection,
......@@ -15,7 +18,7 @@ from config_section import (
UpdateInstallSection,
WorkersOdooConfigSection,
)
from mapper import (
from .mapper import (
CleverCloudCellarCompatibilityMapper,
CleverCloudPostgresCompatibilityMapper,
OdooCompatibilityMapper,
......@@ -35,11 +38,6 @@ PG_MAX_CONN_MODE_DEFAULT = "DEFAULT"
def is_main_instance(env_vars):
"""
Le but est de savoir si cette instance est la première ou non.
Cette fonctionnalité est utile principalement pour desactiver les cron.
Aujourd'hui nous assumons qu'il y a une variable d'environement $INSTANCE_NUMBER
"""
return env_vars.get("INSTANCE_NUMBER", 0) == 0
......@@ -67,18 +65,18 @@ class OdooConfig(OdooConfigABC, Dictable):
return self._odoo_version
def to_dict(self):
return {
"--config": self.odoo_rc,
**self.update_install.to_dict(),
**self.addons_config.to_dict(),
**self.workers_config.to_dict(),
**self.limit_config.to_dict(),
**self.http_config.to_dict(),
**self.db_config.to_dict(),
**self.wide_module.to_dict(),
**self.other_section.to_dict(),
**self.logger_section.to_dict(),
}
result = OrderedDict()
result["--config"] = self.odoo_rc
result.update(self.update_install.to_dict())
result.update(self.addons_config.to_dict())
result.update(self.workers_config.to_dict())
result.update(self.limit_config.to_dict())
result.update(self.http_config.to_dict())
result.update(self.db_config.to_dict())
result.update(self.wide_module.to_dict())
result.update(self.other_section.to_dict())
result.update(self.logger_section.to_dict())
return result
def __repr__(self):
return pprint.pformat(self.to_dict())
......@@ -130,8 +128,8 @@ class OdooEnvConverter(ConfigConvert):
or not env_vars.get("DB_PASSWORD")
):
raise TypeError(
"Il est impossible de démarer une instance Odoo sans nom de base de donnée"
"Veuillez ajouter l'une des variables d'environment suivant"
"Can't start Odoo without a db name "
"Please add the one of the following environment variable"
"- DATABASE"
"- DB_NAME"
"- POSTGRESQL_ADDON_DB"
......
[metadata]
name = odoo_launcher
version = 1.1.0
version = 1.2.0
url = https://gitlab.ndp-systemes.fr/python-libs/odoo_launcher
license = GPLv3
author = Alexis Pasquier, NDP Systemes
......@@ -17,13 +17,10 @@ classifiers=
[options]
python_requires = >=2.7, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*
include_package_data = True
packages = find:
package_dir=
=odoo_launcher
packages = odoo_launcher
install_requires =
addons-installer==1.1.1
maintenance-server==1.0.1
addons-installer==1.2.0
maintenance-server==1.2.0
typing==3.10.0.0;python_version<='3'
enum34==1.1.10;python_version<='3'
[options.packages.find]
where=odoo_launcher
import unittest
from odoo_launcher import api
class TestMapper(unittest.TestCase):
def setUp(self):
self.mapper = api.EnvMapper()
self.nb_element = 5
def test_is_true(self):
self.assertIsInstance(self.mapper, api.EnvMapper)
self.assertTrue(self.mapper.is_true(str(True)))
self.assertTrue(self.mapper.is_true(str(1)))
self.assertTrue(self.mapper.is_true(True))
self.assertTrue(self.mapper.is_true(1))
self.assertFalse(self.mapper.is_true(str(False)))
self.assertFalse(self.mapper.is_true(str(0)))
self.assertFalse(self.mapper.is_true(str(0.0)))
self.assertFalse(self.mapper.is_true(str(None)))
self.assertFalse(self.mapper.is_true(False))
self.assertFalse(self.mapper.is_true(0))
self.assertFalse(self.mapper.is_true(0.0))
self.assertFalse(self.mapper.is_true(1.0))
self.assertFalse(self.mapper.is_true(object()))
self.assertFalse(self.mapper.is_true(None))
class DummyOdooConfig(api.OdooConfigABC):
pass
class TestConfigSection(unittest.TestCase):
def setUp(self):
self.odoo_config = DummyOdooConfig()
from . import test_clever_cloud_cellar # noqa
from . import test_clever_cloud_postgres # noqa
from . import test_config_global # noqa
from . import test_odoo_comptibility_mapper # noqa
from . import test_odoo_config_maker # noqa
from . import test_odoo_launcher # noqa
from . import test_queue_job_mapper # noqa
from . import test_redis_mapper # noqa
from typing import Dict
from test_mapper_base_class import TestMapper
from odoo_launcher.mapper import CleverCloudCellarCompatibilityMapper
from . import TestMapper
class TestCleverCloudMapperCellar(TestMapper):
def setUp(self):
......
# -*- coding: utf8 -*-
from typing import Dict
from test_mapper_base_class import TestMapper
from odoo_launcher.mapper import CleverCloudPostgresCompatibilityMapper
from . import TestMapper
class TestCleverCloudMapperPostgres(TestMapper):
def setUp(self):
......
# -*- coding: utf8 -*-
import unittest
from odoo_launcher import OdooConfig
......@@ -5,39 +6,49 @@ from odoo_launcher import OdooConfig
class TestOdooConfig(unittest.TestCase):
def test_0(self):
config = OdooConfig(env_vars={}, main_instance=True)
config = OdooConfig(env_vars={"REMOTE_DB": str(False)}, main_instance=True)
result = config.to_dict()
self.assertDictEqual(
{
"workers": 1,
"max_cron_threads": 2,
"limit_request": None,
"limit_time_cpu": None,
"limit_time_real": None,
"osv_memory_count_limit": None,
"limit_memory_hard": None,
"limit_memory_soft": None,
"longpolling_port": None,
"xmlrpc": False,
"xmlrpcs_interface": None,
"xmlrpcs_port": None,
"db_name": None,
"db_host": None,
"db_port": None,
"db_password": None,
"db_maxconn": 4,
"db_user": None,
"dbfilter": None,
"list_db": False,
"server_wide_modules": ["base", "web"],
"unaccent": True,
"auto_reload": False,
"log_handler": None,
"log_level": None,
"admin_passwd": None,
"test_enable": False,
"stop_after_init": False,
"without_demo": False,
"--config": None,
"--update": "",
"--install": "",
"--stop-after-init": False,
"--save": False,
"--addons-path": [],
"--workers": 1,
"--max-cron-threads": 0,
"--limit-request": None,
"--limit-time-cpu": None,
"--limit-time-real": None,
"--limit-memory-hard": None,
"--limit-memory-soft": None,
"--osv-memory-count-limit": None,
"--osv-memory-age-limit": None,
"--xmlrpc-interface": "0.0.0.0",
"--xmlrpc-port": 8080,
"--longpolling-port": 4040,
"--db_host": None,
"--db_port": None,
"--db_user": None,
"--db_password": None,
"--database": None,
"--no-database-list": True,
"--db_maxconn": 2,
"--db-filter": None,
"--load": ["base", "web", "odoo_filestore_s3", "odoo_session_redis"],
"--unaccent": True,
"--test-enable": False,
"--without-demo": False,
"--logfile": None,
"--log-handler": None,
"--log-request": False,
"--log-response": False,
"--log-web": False,
"--log-sql": False,
"--log-db": False,
"--log-db-level": None,
"--log-level": None,
},
result,
)
import unittest
from odoo_launcher.api import EnvMapper, OdooConfigABC
class TestMapper(unittest.TestCase):
def setUp(self):
self.mapper = EnvMapper()
self.nb_element = 5
def test_is_true(self):
self.assertIsInstance(self.mapper, EnvMapper)
self.assertTrue(self.mapper.is_true(str(True)))