Esempio n. 1
0
File: v6.py Progetto: zutobg/populus
def upgrade_v6_to_v7(v6_base_config):
    """
    Upgrade a v6 config file to a v7 config file.
    """
    errors = get_validation_errors(v6_base_config, version=V6)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors), ))

    v6_config = Config(v6_base_config)
    v6_config.unref()

    v6_default = load_default_config(version=V6)
    v7_default = load_default_config(version=V7)

    v6_default_config = Config(v6_default)
    v6_default_config.unref()

    v7_default_config = Config(v7_default)
    v7_default_config.unref()

    if v6_config == v6_default_config:
        return v7_default_config

    # V7 just moved to user config, no change in keys
    upgraded_v6_config = copy.deepcopy(v6_config)
    upgraded_v6_config['version'] = V7

    return upgraded_v6_config
Esempio n. 2
0
def upgrade_v7_to_v8(v7_config):
    """
    Upgrade a v7 config file to a v8 config file.
    """
    errors = get_validation_errors(v7_config, version=V7)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors), ))

    v7_default = load_default_config(version=V7)

    v7_default_config = Config(v7_default)

    v8_default = load_default_config(version=V8)
    v8_default_config = Config(v8_default)
    v8_default_config.unref()

    if v7_config == v7_default_config:
        return v8_default_config

    # V8 just removes all of the `$ref` values from the config.
    upgraded_v7_config = Config(copy.deepcopy(v7_config))
    upgraded_v7_config.unref()
    upgraded_v7_config['version'] = V8

    return upgraded_v7_config
Esempio n. 3
0
def upgrade_v6_to_v7(v6_base_config):
    """
    Upgrade a v6 config file to a v7 config file.
    """
    errors = get_validation_errors(v6_base_config, version=V6)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v6_config = Config(v6_base_config)
    v6_config.unref()

    v6_default = load_default_config(version=V6)
    v7_default = load_default_config(version=V7)

    v6_default_config = Config(v6_default)
    v6_default_config.unref()

    v7_default_config = Config(v7_default)
    v7_default_config.unref()

    if v6_config == v6_default_config:
        return v7_default_config

    # V7 just moved to user config, no change in keys
    upgraded_v6_config = copy.deepcopy(v6_config)
    upgraded_v6_config['version'] = V7

    return upgraded_v6_config
def test_default_config_upgrade(from_to_version, use_config_object):
    from_version, to_version = from_to_version
    base_initial_config = load_default_config(version=from_version)
    expected_config = load_default_config(version=to_version)

    if use_config_object:
        config_schema = load_config_schema(version=from_version)
        initial_config = Config(base_initial_config, schema=config_schema)
    else:
        initial_config = base_initial_config

    upgraded_config = upgrade_config(initial_config, to_version=to_version)
    assert upgraded_config == expected_config
def test_legacy_config_upgrade(project, from_to_version, use_config_object):
    from_version, to_version = from_to_version
    base_initial_config = load_default_config(version=from_version)
    expected_config = load_default_config(version=to_version)

    if use_config_object:
        config_schema = load_config_schema(version=from_version)
        initial_config = Config(base_initial_config, schema=config_schema)
    else:
        initial_config = base_initial_config

    upgraded_config = upgrade_config(initial_config, ConfigContext.LEGACY, to_version=to_version)
    assert upgraded_config == expected_config
Esempio n. 6
0
def upgrade_v7_to_v8(v7_config):
    """
    Upgrade a v7 config file to a v8 config file.
    """
    errors = get_validation_errors(v7_config, version=V7)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v7_default = load_default_config(version=V7)

    v7_default_config = Config(v7_default)

    if v7_config == v7_default_config:
        return {}

    # V8 just removes all of the `$ref` values from the config.
    upgraded_v7_config = Config(copy.deepcopy(v7_config))
    upgraded_v7_config.unref()
    upgraded_v7_config['version'] = V8

    return upgraded_v7_config
Esempio n. 7
0
def test_upgrade_to_user_config(project, from_legacy_version):

    shutil.copyfile(
        get_default_config_path(version=from_legacy_version),
        get_legacy_json_config_file_path(project_dir=project.project_dir))

    os.remove(project.config_file_path)

    logger = logging.getLogger("test.test_upgrade_to_user_config")
    upgrade_configs(project.project_dir, logger, FIRST_USER_CONFIG_VERSION)

    upgraded_project = Project(
        project_dir=project.project_dir,
        user_config_file_path=project.user_config_file_path)

    expected_user_config = Config(
        load_user_default_config(FIRST_USER_CONFIG_VERSION))
    expected_user_config.unref()

    expected_project_config = Config(
        load_default_config(FIRST_USER_CONFIG_VERSION))
    expected_project_config.unref()

    assert upgraded_project.legacy_config_path is None
    assert upgraded_project.config == expected_user_config
    assert upgraded_project.user_config == expected_user_config
    assert upgraded_project.project_config == expected_project_config
def test_upgrade_to_user_config(project, from_legacy_version):

    shutil.copyfile(
        get_default_config_path(version=from_legacy_version),
        get_legacy_json_config_file_path(project_dir=project.project_dir)
    )

    logger = logging.getLogger("test.test_upgrade_to_user_config")
    upgrade_configs(project.project_dir, logger, LATEST_VERSION)

    upgraded_project = Project(
        project_dir=project.project_dir,
        user_config_file_path=project.user_config_file_path
    )

    expected_user_config = Config(load_user_default_config(LATEST_VERSION))
    expected_user_config.unref()

    expected_project_config = Config(load_default_config(LATEST_VERSION))
    expected_project_config.unref()

    legacy_config_path = get_legacy_json_config_file_path(project.project_dir)
    assert not os.path.exists(legacy_config_path)

    assert upgraded_project.user_config == expected_user_config
    assert upgraded_project.project_config == expected_project_config
Esempio n. 9
0
def upgrade_v2_to_v3(v2_config):
    """
    Upgrade a v2 config file to a v3 config file.
    """
    errors = get_validation_errors(v2_config, version=V2)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors), ))

    v2_default_config = load_default_config(version=V2)
    v3_default_config = load_default_config(version=V3)

    if v2_config == v2_default_config:
        return v3_default_config

    upgraded_v2_config = copy.deepcopy(v2_config)

    for key_path in NEW_V3_PATHS:
        if has_nested_key(upgraded_v2_config, key_path):
            continue
        set_nested_key(
            upgraded_v2_config,
            key_path,
            get_nested_key(v3_default_config, key_path),
        )

    # bump the version
    set_nested_key(upgraded_v2_config, 'version', V3)

    errors = get_validation_errors(upgraded_v2_config, version=V3)
    if errors:
        raise ValueError("Upgraded configuration did not pass validation:\n\n"
                         "\n=============Original-Configuration============\n"
                         "{0}"
                         "\n=============Upgraded-Configuration============\n"
                         "{1}"
                         "\n=============Validation-Errors============\n"
                         "{2}".format(
                             pprint.pformat(dict(v2_config)),
                             pprint.pformat(dict(upgraded_v2_config)),
                             format_errors(errors),
                         ))

    return upgraded_v2_config
Esempio n. 10
0
    def clean_config(self):

        items = self.project_config.items(flatten=True)

        default_config = Config(load_default_config(version=self.project_config['version']))
        default_config.unref()
        default_project_keys = [x[0] for x in default_config.items(flatten=True)]

        for key, value in items:
            if self.user_config.get(key) == value and key not in default_project_keys:
                self.project_config.pop(key)
Esempio n. 11
0
    def clean_config(self):

        items = self.project_config.items(flatten=True)

        default_config = Config(
            load_default_config(version=self.project_config['version']))
        default_config.unref()
        default_project_keys = [
            x[0] for x in default_config.items(flatten=True)
        ]

        for key, value in items:
            if self.user_config.get(
                    key) == value and key not in default_project_keys:
                self.project_config.pop(key)
Esempio n. 12
0
def test_upgrade_to_user_config(project, from_legacy_version):

    shutil.copyfile(
        get_default_config_path(version=from_legacy_version),
        get_legacy_json_config_file_path(project_dir=project.project_dir))

    logger = logging.getLogger("test.test_upgrade_to_user_config")
    upgrade_configs(project.project_dir, logger, LATEST_VERSION)

    upgraded_project = Project(
        project_dir=project.project_dir,
        user_config_file_path=project.user_config_file_path)

    expected_user_config = Config(load_user_default_config(LATEST_VERSION))
    expected_user_config.unref()

    expected_project_config = Config(load_default_config(LATEST_VERSION))
    expected_project_config.unref()

    legacy_config_path = get_legacy_json_config_file_path(project.project_dir)
    assert not os.path.exists(legacy_config_path)

    assert upgraded_project.user_config == expected_user_config
    assert upgraded_project.project_config == expected_project_config
Esempio n. 13
0
import copy

from populus.config.defaults import (
    load_default_config, )
from populus.config.upgrade.v1 import (
    upgrade_v1_to_v2, )
from populus.config.versions import (
    V1,
    V2,
)

from populus.utils.mappings import (
    deep_merge_dicts, )

V1_DEFAULT_CONFIG = load_default_config(version=V1)
V2_DEFAULT_CONFIG = load_default_config(version=V2)

BASE_V1_CONFIG = {
    "version": "1",
    "chains": {
        "mainnet": {
            "chain": {
                "class": "populus.chain.MainnetChain"
            },
            "web3": {
                "$ref": "web3.GethIPC"
            }
        },
        "ropsten": {
            "chain": {
                "class": "populus.chain.TestnetChain"
Esempio n. 14
0
from populus.config.defaults import (
    load_default_config,
)
from populus.config.upgrade.v3 import (
    upgrade_v3_to_v4,
)
from populus.config.versions import (
    V4,
)

from populus.utils.mappings import (
    deep_merge_dicts,
)


V4_DEFAULT_CONFIG = load_default_config(version=V4)


BASE_V3_CONFIG = {
    "version": "3",
    "chains": {
        "mainnet": {
            "chain": {
                "class": "populus.chain.geth.MainnetChain"
            },
            "web3": {
                "$ref": "web3.GethIPC"
            },
            "contracts": {
                "backends": {
                    "JSONFile": {
Esempio n. 15
0
    load_default_config,
)
from populus.config.upgrade.v1 import (
    upgrade_v1_to_v2,
)
from populus.config.versions import (
    V1,
    V2,
)

from populus.utils.mappings import (
    deep_merge_dicts,
)


V1_DEFAULT_CONFIG = load_default_config(version=V1)
V2_DEFAULT_CONFIG = load_default_config(version=V2)


BASE_V1_CONFIG = {
    "version": "1",
    "chains": {
        "mainnet": {
            "chain": {
                "class": "populus.chain.MainnetChain"
            },
            "web3": {
                "$ref": "web3.GethIPC"
            }
        },
        "ropsten": {
Esempio n. 16
0
def upgrade_v3_to_v4(v3_config):
    """
    Upgrade a v3 config file to a v4 config file.
    """
    errors = get_validation_errors(v3_config, version=V3)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v3_default_config = load_default_config(version=V3)
    v4_default_config = load_default_config(version=V4)

    if v3_config == v3_default_config:
        return v4_default_config

    upgraded_v3_config = copy.deepcopy(v3_config)

    for key_path in NEW_V4_PATHS:
        if has_nested_key(upgraded_v3_config, key_path):
            continue
        set_nested_key(
            upgraded_v3_config,
            key_path,
            get_nested_key(v4_default_config, key_path),
        )

    for old_path, new_path in MOVED_V3_PATHS.items():
        default_value = get_nested_key(v4_default_config, new_path)

        if has_nested_key(upgraded_v3_config, old_path):
            existing_value = pop_nested_key(upgraded_v3_config, old_path)

            if is_dict(default_value) and is_dict(existing_value):
                merged_value = deep_merge_dicts(default_value, existing_value)
            elif is_list_like(default_value) and is_list_like(existing_value):
                merged_value = list(set(itertools.chain(default_value, existing_value)))
            else:
                raise ValueError(
                    "Unable to merge {0} with {1}".format(
                        type(default_value),
                        type(existing_value),
                    )
                )

            set_nested_key(
                upgraded_v3_config,
                new_path,
                merged_value,
            )
        else:
            set_nested_key(
                upgraded_v3_config,
                new_path,
                default_value,
            )

    # bump the version
    set_nested_key(upgraded_v3_config, 'version', V4)

    errors = get_validation_errors(upgraded_v3_config, version=V4)
    if errors:
        raise ValueError(
            "Upgraded configuration did not pass validation:\n\n"
            "\n=============Original-Configuration============\n"
            "{0}"
            "\n=============Upgraded-Configuration============\n"
            "{1}"
            "\n=============Validation-Errors============\n"
            "{2}".format(
                pprint.pformat(dict(v3_config)),
                pprint.pformat(dict(upgraded_v3_config)),
                format_errors(errors),
            )
        )

    return upgraded_v3_config
Esempio n. 17
0
def upgrade_v5_to_v6(v5_config):
    """
    Upgrade a v5 config file to a v6 config file.
    """
    errors = get_validation_errors(v5_config, version=V5)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v5_default_config = load_default_config(version=V5)
    v6_default_config = load_default_config(version=V6)

    if v5_config == v5_default_config:
        return v6_default_config

    upgraded_v5_config = copy.deepcopy(v5_config)

    # new configuration values whos keys were not present in the previous
    # configuration.
    for key_path in NEW_V6_PATHS:
        if has_nested_key(upgraded_v5_config, key_path):
            continue
        set_nested_key(
            upgraded_v5_config,
            key_path,
            get_nested_key(v6_default_config, key_path),
        )

    if has_nested_key(upgraded_v5_config, 'compilation.contracts_source_dir'):
        current_contracts_source_dir = pop_nested_key(
            upgraded_v5_config,
            'compilation.contracts_source_dir',
        )
        if is_string(current_contracts_source_dir):
            contract_source_dirs = [current_contracts_source_dir]
        else:
            contract_source_dirs = current_contracts_source_dir
        set_nested_key(
            upgraded_v5_config,
            'compilation.contract_source_dirs',
            contract_source_dirs,
        )

    # bump the version
    set_nested_key(upgraded_v5_config, 'version', V6)

    errors = get_validation_errors(upgraded_v5_config, version=V6)
    if errors:
        raise ValueError(
            "Upgraded configuration did not pass validation:\n\n"
            "\n=============Original-Configuration============\n"
            "{0}"
            "\n=============Upgraded-Configuration============\n"
            "{1}"
            "\n=============Validation-Errors============\n"
            "{2}".format(
                pprint.pformat(dict(v5_config)),
                pprint.pformat(dict(upgraded_v5_config)),
                format_errors(errors),
            )
        )

    return upgraded_v5_config
Esempio n. 18
0
    load_default_config,
)
from populus.config.upgrade.v2 import (
    upgrade_v2_to_v3,
)
from populus.config.versions import (
    V2,
    V3,
)

from populus.utils.mappings import (
    deep_merge_dicts,
)


V3_DEFAULT_CONFIG = load_default_config(version=V3)


BASE_V2_CONFIG = {
  "version": "2",
  "chains": {
    "mainnet": {
      "chain": {
        "class": "populus.chain.geth.MainnetChain"
      },
      "web3": {
        "$ref": "web3.GethIPC"
      },
      "contracts": {
        "backends": {
          "JSONFile": {
from populus.config.base import (
    Config, )


def test_user_config_not_assignable(project):
    with pytest.raises(AttributeError):
        project.user_config = Config()


def test_project_config_not_assignable(project):
    with pytest.raises(AttributeError):
        project.project_config = Config()


@pytest.mark.parametrize('assigned_config', (
    load_default_config(),
    Config(load_default_config()),
))
def test_config_assignment(project, user_config_defaults, assigned_config):
    changed_key = 'compilation.import_remappings'
    new_value = ['new-ramappings=contracts']
    default_value = user_config_defaults[changed_key]

    if isinstance(assigned_config, dict):
        assigned_config['compilation']['import_remappings'] = new_value
    else:
        assigned_config[changed_key] = new_value

    project.config = assigned_config

    assert project.user_config.get(changed_key) == default_value
Esempio n. 20
0
def upgrade_v4_to_v5(v4_config):
    """
    Upgrade a v4 config file to a v5 config file.
    """
    errors = get_validation_errors(v4_config, version=V4)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors), ))

    v4_default_config = load_default_config(version=V4)
    v5_default_config = load_default_config(version=V5)

    if v4_config == v4_default_config:
        return v5_default_config

    upgraded_v4_config = copy.deepcopy(v4_config)

    # new configuration values whos keys were not present in the previous
    # configuration.
    for key_path in NEW_V5_PATHS:
        if has_nested_key(upgraded_v4_config, key_path):
            continue
        set_nested_key(
            upgraded_v4_config,
            key_path,
            get_nested_key(v5_default_config, key_path),
        )

    # keys in the new configuration that were relocated.
    for old_path, new_path in MOVED_V4_PATHS.items():
        default_value = get_nested_key(v5_default_config, new_path)

        if has_nested_key(upgraded_v4_config, old_path):
            existing_value = pop_nested_key(upgraded_v4_config, old_path)

            if is_dict(default_value) and is_dict(existing_value):
                merged_value = deep_merge_dicts(default_value, existing_value)
            elif is_list_like(default_value) and is_list_like(existing_value):
                merged_value = list(
                    set(itertools.chain(default_value, existing_value)))
            else:
                raise ValueError("Unable to merge {0} with {1}".format(
                    type(default_value),
                    type(existing_value),
                ))

            set_nested_key(
                upgraded_v4_config,
                new_path,
                merged_value,
            )
        else:
            set_nested_key(
                upgraded_v4_config,
                new_path,
                default_value,
            )

    # keys from the previous configuration that were changed.
    for key_path in MODIFIED_V4_PATHS:
        new_default = get_nested_key(v5_default_config, key_path)
        if key_path not in upgraded_v4_config:
            set_nested_key(
                upgraded_v4_config,
                key_path,
                new_default,
            )
        else:
            current_value = get_nested_key(upgraded_v4_config, key_path)
            old_default = get_nested_key(v4_default_config, key_path)
            if current_value == old_default:
                set_nested_key(
                    upgraded_v4_config,
                    key_path,
                    new_default,
                )

    # bump the version
    set_nested_key(upgraded_v4_config, 'version', V5)

    errors = get_validation_errors(upgraded_v4_config, version=V5)
    if errors:
        raise ValueError("Upgraded configuration did not pass validation:\n\n"
                         "\n=============Original-Configuration============\n"
                         "{0}"
                         "\n=============Upgraded-Configuration============\n"
                         "{1}"
                         "\n=============Validation-Errors============\n"
                         "{2}".format(
                             pprint.pformat(dict(v4_config)),
                             pprint.pformat(dict(upgraded_v4_config)),
                             format_errors(errors),
                         ))

    return upgraded_v4_config
Esempio n. 21
0
File: v3.py Progetto: zutobg/populus
def upgrade_v3_to_v4(v3_config):
    """
    Upgrade a v3 config file to a v4 config file.
    """
    errors = get_validation_errors(v3_config, version=V3)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors), ))

    v3_default_config = load_default_config(version=V3)
    v4_default_config = load_default_config(version=V4)

    if v3_config == v3_default_config:
        return v4_default_config

    upgraded_v3_config = copy.deepcopy(v3_config)

    for key_path in NEW_V4_PATHS:
        if has_nested_key(upgraded_v3_config, key_path):
            continue
        set_nested_key(
            upgraded_v3_config,
            key_path,
            get_nested_key(v4_default_config, key_path),
        )

    for old_path, new_path in MOVED_V3_PATHS.items():
        default_value = get_nested_key(v4_default_config, new_path)

        if has_nested_key(upgraded_v3_config, old_path):
            existing_value = pop_nested_key(upgraded_v3_config, old_path)

            if is_dict(default_value) and is_dict(existing_value):
                merged_value = deep_merge_dicts(default_value, existing_value)
            elif is_list_like(default_value) and is_list_like(existing_value):
                merged_value = list(
                    set(itertools.chain(default_value, existing_value)))
            else:
                raise ValueError("Unable to merge {0} with {1}".format(
                    type(default_value),
                    type(existing_value),
                ))

            set_nested_key(
                upgraded_v3_config,
                new_path,
                merged_value,
            )
        else:
            set_nested_key(
                upgraded_v3_config,
                new_path,
                default_value,
            )

    # bump the version
    set_nested_key(upgraded_v3_config, 'version', V4)

    errors = get_validation_errors(upgraded_v3_config, version=V4)
    if errors:
        raise ValueError("Upgraded configuration did not pass validation:\n\n"
                         "\n=============Original-Configuration============\n"
                         "{0}"
                         "\n=============Upgraded-Configuration============\n"
                         "{1}"
                         "\n=============Validation-Errors============\n"
                         "{2}".format(
                             pprint.pformat(dict(v3_config)),
                             pprint.pformat(dict(upgraded_v3_config)),
                             format_errors(errors),
                         ))

    return upgraded_v3_config
Esempio n. 22
0
def upgrade_v5_to_v6(v5_config):
    """
    Upgrade a v5 config file to a v6 config file.
    """
    errors = get_validation_errors(v5_config, version=V5)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v5_default_config = load_default_config(version=V5)
    v6_default_config = load_default_config(version=V6)

    if v5_config == v5_default_config:
        return v6_default_config

    upgraded_v5_config = copy.deepcopy(v5_config)

    # new configuration values whos keys were not present in the previous
    # configuration.
    for key_path in NEW_V6_PATHS:
        if has_nested_key(upgraded_v5_config, key_path):
            continue
        set_nested_key(
            upgraded_v5_config,
            key_path,
            get_nested_key(v6_default_config, key_path),
        )

    if has_nested_key(upgraded_v5_config, 'compilation.contracts_source_dir'):
        current_contracts_source_dir = pop_nested_key(
            upgraded_v5_config,
            'compilation.contracts_source_dir',
        )
        if is_string(current_contracts_source_dir):
            contract_source_dirs = [current_contracts_source_dir]
        else:
            contract_source_dirs = current_contracts_source_dir
        set_nested_key(
            upgraded_v5_config,
            'compilation.contract_source_dirs',
            contract_source_dirs,
        )

    # bump the version
    set_nested_key(upgraded_v5_config, 'version', V6)

    errors = get_validation_errors(upgraded_v5_config, version=V6)
    if errors:
        raise ValueError(
            "Upgraded configuration did not pass validation:\n\n"
            "\n=============Original-Configuration============\n"
            "{0}"
            "\n=============Upgraded-Configuration============\n"
            "{1}"
            "\n=============Validation-Errors============\n"
            "{2}".format(
                pprint.pformat(dict(v5_config)),
                pprint.pformat(dict(upgraded_v5_config)),
                format_errors(errors),
            )
        )

    return upgraded_v5_config
Esempio n. 23
0
def upgrade_v1_to_v2(v1_config):
    """
    Upgrade a v1 config file to a v2 config file.
    """
    errors = get_validation_errors(v1_config, version=V1)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v1_default_config = load_default_config(version=V1)
    v2_default_config = load_default_config(version=V2)

    if v1_config == v1_default_config:
        return v2_default_config

    upgraded_v1_config = copy.deepcopy(v1_config)

    for key_path in NEW_V2_PATHS:
        if has_nested_key(upgraded_v1_config, key_path):
            continue
        set_nested_key(
            upgraded_v1_config,
            key_path,
            get_nested_key(v2_default_config, key_path),
        )

    for old_path, new_path in V2_TRANSLATIONS.items():
        if not has_nested_key(upgraded_v1_config, old_path):
            continue
        set_nested_key(
            upgraded_v1_config,
            new_path,
            pop_nested_key(upgraded_v1_config, old_path),
        )

    for key_path in V2_UPDATES:
        if not has_nested_key(upgraded_v1_config, key_path):
            continue
        current_value = get_nested_key(upgraded_v1_config, key_path)
        old_default_value = get_nested_key(v1_default_config, key_path)

        if current_value == old_default_value:
            set_nested_key(
                upgraded_v1_config,
                key_path,
                get_nested_key(v2_default_config, key_path),
            )

    # bump the version
    set_nested_key(upgraded_v1_config, 'version', V2)

    errors = get_validation_errors(upgraded_v1_config, version=V2)
    if errors:
        raise ValueError(
            "Upgraded configuration did not pass validation:\n\n"
            "\n=============Original-Configuration============\n"
            "{0}"
            "\n=============Upgraded-Configuration============\n"
            "{1}"
            "\n=============Validation-Errors============\n"
            "{2}".format(
                pprint.pformat(dict(v1_config)),
                pprint.pformat(dict(upgraded_v1_config)),
                format_errors(errors),
            )
        )

    return upgraded_v1_config
Esempio n. 24
0
def upgrade_v4_to_v5(v4_config):
    """
    Upgrade a v4 config file to a v5 config file.
    """
    errors = get_validation_errors(v4_config, version=V4)
    if errors:
        raise ValueError(
            "Cannot upgrade invalid config.  Please ensure that your current "
            "configuration file is valid:\n\n{0}".format(
                format_errors(errors),
            )
        )

    v4_default_config = load_default_config(version=V4)
    v5_default_config = load_default_config(version=V5)

    if v4_config == v4_default_config:
        return v5_default_config

    upgraded_v4_config = copy.deepcopy(v4_config)

    # new configuration values whos keys were not present in the previous
    # configuration.
    for key_path in NEW_V5_PATHS:
        if has_nested_key(upgraded_v4_config, key_path):
            continue
        set_nested_key(
            upgraded_v4_config,
            key_path,
            get_nested_key(v5_default_config, key_path),
        )

    # keys in the new configuration that were relocated.
    for old_path, new_path in MOVED_V4_PATHS.items():
        default_value = get_nested_key(v5_default_config, new_path)

        if has_nested_key(upgraded_v4_config, old_path):
            existing_value = pop_nested_key(upgraded_v4_config, old_path)

            if is_dict(default_value) and is_dict(existing_value):
                merged_value = deep_merge_dicts(default_value, existing_value)
            elif is_list_like(default_value) and is_list_like(existing_value):
                merged_value = list(set(itertools.chain(default_value, existing_value)))
            else:
                raise ValueError(
                    "Unable to merge {0} with {1}".format(
                        type(default_value),
                        type(existing_value),
                    )
                )

            set_nested_key(
                upgraded_v4_config,
                new_path,
                merged_value,
            )
        else:
            set_nested_key(
                upgraded_v4_config,
                new_path,
                default_value,
            )

    # keys from the previous configuration that were changed.
    for key_path in MODIFIED_V4_PATHS:
        new_default = get_nested_key(v5_default_config, key_path)
        if key_path not in upgraded_v4_config:
            set_nested_key(
                upgraded_v4_config,
                key_path,
                new_default,
            )
        else:
            current_value = get_nested_key(upgraded_v4_config, key_path)
            old_default = get_nested_key(v4_default_config, key_path)
            if current_value == old_default:
                set_nested_key(
                    upgraded_v4_config,
                    key_path,
                    new_default,
                )

    # bump the version
    set_nested_key(upgraded_v4_config, 'version', V5)

    errors = get_validation_errors(upgraded_v4_config, version=V5)
    if errors:
        raise ValueError(
            "Upgraded configuration did not pass validation:\n\n"
            "\n=============Original-Configuration============\n"
            "{0}"
            "\n=============Upgraded-Configuration============\n"
            "{1}"
            "\n=============Validation-Errors============\n"
            "{2}".format(
                pprint.pformat(dict(v4_config)),
                pprint.pformat(dict(upgraded_v4_config)),
                format_errors(errors),
            )
        )

    return upgraded_v4_config