<commit_before># Copyright (C) 2020 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import

from logs import Logs
from platform_utils import Platform, System
System.import_libs()

import fcntl
import logging
import os
import six
import time
from contextlib import contextmanager
from threading import Lock

from serial import Serial
from six.moves.configparser import ConfigParser, NoOptionError, NoSectionError
from six.moves.urllib.parse import urlparse, urlunparse

import constants
from bus.om_bus_client import MessageClient
from gateway.hal.frontpanel_controller_classic import FrontpanelClassicController
from gateway.hal.frontpanel_controller_core import FrontpanelCoreController
from gateway.hal.master_controller_classic import MasterClassicController
from gateway.hal.master_controller_core import MasterCoreController
from gateway.hal.master_controller_dummy import MasterDummyController
from gateway.models import Database, Feature
from gateway.thermostat.gateway.thermostat_controller_gateway import \
    ThermostatControllerGateway
from gateway.thermostat.master.thermostat_controller_master import \
    ThermostatControllerMaster
from gateway.uart_controller import UARTController
from ioc import INJECTED, Inject, Injectable
from master.classic.maintenance import MaintenanceClassicCommunicator
from master.classic.master_communicator import MasterCommunicator
from master.core.core_communicator import CoreCommunicator
from master.core.maintenance import MaintenanceCoreCommunicator
from master.core.memory_file import MemoryFile
from gateway.energy.energy_communicator import EnergyCommunicator
from serial_utils import RS485


if False:  # MYPY
    from typing import Any, Optional
    from gateway.hal.master_controller import MasterController

logger = logging.getLogger(__name__)


def initialize(message_client_name):
    # type: (Optional[str]) -> None
    logger.info('Initializing')
    init_lock = constants.get_init_lockfile()
    logger.info('Waiting for lock')
    with lock_file(init_lock) as fd:
        content = fd.read()
        apply_migrations()
        setup_platform(message_client_name)
        if content == '':
            logger.info('Initializing, done')
        elif content == 'factory_reset':
            logger.info('Running factory reset...')
            factory_reset()
            logger.info('Running factory reset, done')
        elif content == 'factory_reset_full':
            logger.info('Running full factory reset [also wiping CC EEPROM]...')
            factory_reset(can=True)
            logger.info('Running full factory reset, done')
        else:
            logger.warning('unknown initialization {}'.format(content))<commit_msg>Trigger system restart after factory reset<commit_after># Copyright (C) 2020 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import

from logs import Logs
from platform_utils import Platform, System
System.import_libs()

import fcntl
import logging
import os
import six
import sys
import time
from contextlib import contextmanager
from threading import Lock

from serial import Serial
from six.moves.configparser import ConfigParser, NoOptionError, NoSectionError
from six.moves.urllib.parse import urlparse, urlunparse

import constants
from bus.om_bus_client import MessageClient
from gateway.hal.frontpanel_controller_classic import FrontpanelClassicController
from gateway.hal.frontpanel_controller_core import FrontpanelCoreController
from gateway.hal.master_controller_classic import MasterClassicController
from gateway.hal.master_controller_core import MasterCoreController
from gateway.hal.master_controller_dummy import MasterDummyController
from gateway.models import Database, Feature
from gateway.thermostat.gateway.thermostat_controller_gateway import \
    ThermostatControllerGateway
from gateway.thermostat.master.thermostat_controller_master import \
    ThermostatControllerMaster
from gateway.uart_controller import UARTController
from ioc import INJECTED, Inject, Injectable
from master.classic.maintenance import MaintenanceClassicCommunicator
from master.classic.master_communicator import MasterCommunicator
from master.core.core_communicator import CoreCommunicator
from master.core.maintenance import MaintenanceCoreCommunicator
from master.core.memory_file import MemoryFile
from gateway.energy.energy_communicator import EnergyCommunicator
from serial_utils import RS485


if False:  # MYPY
    from typing import Any, Optional
    from gateway.hal.master_controller import MasterController

logger = logging.getLogger(__name__)


def initialize(message_client_name):
    # type: (Optional[str]) -> None
    logger.info('Initializing')
    init_lock = constants.get_init_lockfile()
    logger.info('Waiting for lock')
    had_factory_reset = False
    with lock_file(init_lock) as fd:
        content = fd.read()
        apply_migrations()
        setup_platform(message_client_name)
        if 'factory_reset' in content:
            full = content == 'factory_reset_full'
            logger.info('Running {0}factory reset...'.format('full ' if full else ''))
            factory_reset(can=full)
            logger.info('Running {0}factory reset, done'.format('full ' if full else ''))
            had_factory_reset = True
        elif content != '':
            logger.warning('Unknown initialization {}'.format(content))
        logger.info('Initializing, done')
    if had_factory_reset:
        logger.info('Trigger service restart after factory reset')
        sys.exit(1)
{'fraction_non_alphanumeric': 0.06044600938967136, 'fraction_numerical': 0.0027875586854460093, 'mean_word_length': 4.65257048092869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4195568', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1712, 'n_words': 613}