def user_config_path(tmpdir, request): version = getattr(request.function, '_user_config_version', LATEST_VERSION) tmp_user_config_path = tmpdir.join(os.path.basename(get_user_json_config_file_path())).strpath user_defaults_path = get_user_default_config_path(version) shutil.copyfile(user_defaults_path, tmp_user_config_path) return tmp_user_config_path
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), )