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
def test_upgrade_custom_key(project): legacy_config_file_path = get_legacy_json_config_file_path( project_dir=project.project_dir) shutil.copyfile(get_default_config_path(version=V3), legacy_config_file_path) os.remove(project.config_file_path) legacy_key = 'compilation.import_remapping' legacy_value = ['import-path-from-legacy=contracts'] upgraded_key = 'compilation.import_remappings' legacy_config = Config(load_config(legacy_config_file_path)) legacy_config[legacy_key] = legacy_value write_config(legacy_config, legacy_config_file_path) logger = logging.getLogger("test.test_upgrade_custom_key") 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) assert upgraded_project.config.get(upgraded_key) == legacy_value assert upgraded_project.project_config.get(upgraded_key) == legacy_value default_user_config = Config( load_user_default_config(version=FIRST_USER_CONFIG_VERSION)) assert upgraded_project.user_config.get( upgraded_key) == default_user_config.get(upgraded_key)
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_custom_key(project): legacy_config_file_path = get_legacy_json_config_file_path(project_dir=project.project_dir) shutil.copyfile( get_default_config_path(version=V3), legacy_config_file_path ) legacy_key = 'compilation.import_remapping' legacy_value = ['import-path-from-legacy=contracts'] upgraded_key = 'compilation.import_remappings' legacy_config = Config(load_config(legacy_config_file_path)) legacy_config[legacy_key] = legacy_value write_config(legacy_config, legacy_config_file_path) logger = logging.getLogger("test.test_upgrade_custom_key") upgrade_configs(project.project_dir, logger, V8) upgraded_project = Project( project_dir=project.project_dir, user_config_file_path=project.user_config_file_path ) assert upgraded_project.config.get(upgraded_key) == legacy_value assert upgraded_project.project_config.get(upgraded_key) == legacy_value default_user_config = Config(load_user_default_config(version=V8)) assert upgraded_project.user_config.get(upgraded_key) == default_user_config.get(upgraded_key)
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
def __init__(self, project_dir=None, user_config_file_path=None, create_config_file=False): # noqa: E501 self._reset_configs_cache() if project_dir is None: self.project_dir = os.getcwd() else: self.project_dir = os.path.abspath(project_dir) # user config self.user_config_file_path = user_config_file_path if self.user_config_file_path is None: self.user_config_file_path = get_user_json_config_file_path() if not check_if_user_json_config_file_exists(): user_config_path = get_user_json_config_file_path() user_defaults_path = get_user_default_config_path() ensure_path_exists(os.path.dirname(user_config_path)) shutil.copyfile(user_defaults_path, user_config_path) # legacy config legacy_path = get_legacy_json_config_file_path(self.project_dir) if os.path.exists(legacy_path): self.legacy_config_path = legacy_path warnings.warn( "Found legacy config file at {legacy_path}".format( legacy_path=legacy_path), PopulusResourceWarning) # project config self.config_file_path = get_json_config_file_path(self.project_dir) if not os.path.exists(self.config_file_path): if create_config_file: defaults_path = get_default_config_path() shutil.copyfile(defaults_path, self.config_file_path) else: if self.legacy_config_path is not None: msg = "No project config file found at {project_dir}, but a legacy config exists. Try to upgrade" # noqa: E501 else: msg = "No project config file found at {project_dir}" raise FileNotFoundError( msg.format(project_dir=self.project_dir)) self.load_config()
def upgrade_configs(project_dir, logger, to_version): """upgrade project and the user config file""" user_config_file_path = get_user_json_config_file_path() if os.path.exists(user_config_file_path): user_config = load_config(user_config_file_path) current_user_config_version = int(user_config['version']) if current_user_config_version < int(LATEST_VERSION): upgraded_user_config = upgrade_user_config(user_config, to_version) if upgrade_config: write_config(upgraded_user_config, user_config_file_path) else: os.remove(user_config_file_path) legacy_config_file_path = get_legacy_json_config_file_path(project_dir) project_config_file_path = get_json_config_file_path(project_dir) if os.path.exists(legacy_config_file_path): legacy_config = load_config(legacy_config_file_path) legacy_version = legacy_config['version'] if int(legacy_version) > int(LAST_NO_USER_CONFIG_VERSION): raise KeyError( "Unkown legacy version {legacy_version} at {legacy_config}". format(legacy_version=legacy_version, legacy_config=legacy_config)) elif int(legacy_version) < int(LAST_NO_USER_CONFIG_VERSION): upgraded_legacy_config = upgrade_config( legacy_config, ConfigContext.LEGACY, LAST_NO_USER_CONFIG_VERSION) write_config(upgraded_legacy_config, legacy_config_file_path) has_project_config = os.path.exists(project_config_file_path) has_legacy_config = os.path.exists(legacy_config_file_path) if has_project_config or has_legacy_config: if has_project_config: project_config = load_config(project_config_file_path) elif has_legacy_config: project_config = load_config(legacy_config_file_path) else: raise Exception("Invariant") project_config_version = int(project_config['version']) if project_config_version < int(LATEST_VERSION): upgraded_project_config = upgrade_config( project_config, ConfigContext.USER, to_version, ) if upgraded_project_config: write_config(upgraded_project_config, project_config_file_path) elif has_project_config: os.remove(project_config_file_path) if os.path.exists( legacy_config_file_path) and to_version in KNOWN_USER_VERSIONS: os.rename( legacy_config_file_path, "{0}.backup".format(legacy_config_file_path), )
def upgrade_configs(project_dir, logger, to_version): """upgrade project and the user config file""" user_config_file_path = get_user_json_config_file_path() if os.path.exists(user_config_file_path): user_config = load_config(user_config_file_path) current_user_config_version = int(user_config['version']) if current_user_config_version < int(LATEST_VERSION): upgraded_user_config = upgrade_user_config(user_config, to_version) if upgrade_config: write_config(upgraded_user_config, user_config_file_path) else: os.remove(user_config_file_path) legacy_config_file_path = get_legacy_json_config_file_path(project_dir) project_config_file_path = get_json_config_file_path(project_dir) if os.path.exists(legacy_config_file_path): legacy_config = load_config(legacy_config_file_path) legacy_version = legacy_config['version'] if int(legacy_version) > int(LAST_NO_USER_CONFIG_VERSION): raise KeyError( "Unkown legacy version {legacy_version} at {legacy_config}".format( legacy_version=legacy_version, legacy_config=legacy_config ) ) elif int(legacy_version) < int(LAST_NO_USER_CONFIG_VERSION): upgraded_legacy_config = upgrade_config( legacy_config, ConfigContext.LEGACY, LAST_NO_USER_CONFIG_VERSION ) write_config(upgraded_legacy_config, legacy_config_file_path) has_project_config = os.path.exists(project_config_file_path) has_legacy_config = os.path.exists(legacy_config_file_path) if has_project_config or has_legacy_config: if has_project_config: project_config = load_config(project_config_file_path) elif has_legacy_config: project_config = load_config(legacy_config_file_path) else: raise Exception("Invariant") project_config_version = int(project_config['version']) if project_config_version < int(LATEST_VERSION): upgraded_project_config = upgrade_config( project_config, ConfigContext.USER, to_version, ) if upgraded_project_config: write_config(upgraded_project_config, project_config_file_path) elif has_project_config: os.remove(project_config_file_path) if os.path.exists(legacy_config_file_path) and to_version in KNOWN_USER_VERSIONS: os.rename( legacy_config_file_path, "{0}.backup".format(legacy_config_file_path), )