| @@ -18,13 +18,28 @@ RED = Fore.RED + Style.BRIGHT | |||||
| INDENT1 = " " * 3 | INDENT1 = " " * 3 | ||||
| def _ensure_dirs(path): | |||||
| """Ensure the directories within the given pathname exist.""" | |||||
| dirname = os.path.dirname(path) | |||||
| if not os.path.exists(dirname): # Race condition, meh... | |||||
| os.makedirs(dirname) | |||||
| def _get_config_path(): | def _get_config_path(): | ||||
| """Return the path to the configuration file.""" | """Return the path to the configuration file.""" | ||||
| xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config") | xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config") | ||||
| return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini") | return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini") | ||||
| def _migrate_old_config_path(): | |||||
| """Migrate the old config location (~/.gitup) to the new one.""" | |||||
| old_path = os.path.expanduser(os.path.join("~", ".gitup")) | |||||
| if os.path.exists(old_path): | |||||
| new_path = _get_config_path() | |||||
| _ensure_dirs(new_path) | |||||
| os.rename(old_path, new_path) | |||||
| def _load_config_file(): | def _load_config_file(): | ||||
| """Read the config file and return a SafeConfigParser() object.""" | """Read the config file and return a SafeConfigParser() object.""" | ||||
| _migrate_old_config_path() | |||||
| config = configparser.SafeConfigParser() | config = configparser.SafeConfigParser() | ||||
| # Don't lowercase option names, because we are storing paths there: | # Don't lowercase option names, because we are storing paths there: | ||||
| config.optionxform = str | config.optionxform = str | ||||
| @@ -33,10 +48,9 @@ def _load_config_file(): | |||||
| def _save_config_file(config): | def _save_config_file(config): | ||||
| """Save config changes to the config file returned by _get_config_path.""" | """Save config changes to the config file returned by _get_config_path.""" | ||||
| _migrate_old_config_path() | |||||
| cfg_path = _get_config_path() | cfg_path = _get_config_path() | ||||
| if not os.path.exists(os.path.dirname(cfg_path)): # Race condition, meh... | |||||
| os.makedirs(os.path.dirname(cfg_path)) | |||||
| _ensure_dirs(cfg_path) | |||||
| with open(cfg_path, "wb") as config_file: | with open(cfg_path, "wb") as config_file: | ||||
| config.write(config_file) | config.write(config_file) | ||||