Package horizons :: Package util :: Module migratepaths
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.migratepaths

  1  # ################################################### 
  2  # Copyright (C) 2018 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import glob 
 23  import os 
 24   
 25  from horizons.constants import OLDPATHS, PATHS 
 26   
 27   
28 -def migrate_paths():
29 30 migrated = False 31 if check_cache_migration(): 32 migrate_cache() 33 # cache migration doesn't really need a link or mention I think 34 35 if check_config_migration(): 36 migrate_config() 37 link_config() 38 migrated = True 39 40 if check_data_migration(): 41 migrate_data() 42 link_data() 43 migrated = True 44 45 if migrated: 46 leave_note()
47 48
49 -def check_cache_migration():
50 return os.path.isdir(OLDPATHS.CACHE_DIR) and not os.path.isdir(PATHS.CACHE_DIR)
51 52
53 -def migrate_cache():
54 cache_files = glob.glob(os.path.join(OLDPATHS.CACHE_DIR, "*.cache")) 55 for oldname in cache_files: 56 newname = os.path.join(PATHS.CACHE_DIR, os.path.basename(oldname)) 57 os.renames(oldname, newname)
58 59
60 -def check_config_migration():
61 return os.path.isfile(OLDPATHS.USER_CONFIG_FILE) and not os.path.isfile(PATHS.USER_CONFIG_FILE)
62 63
64 -def migrate_config():
65 os.renames(OLDPATHS.USER_CONFIG_FILE, PATHS.USER_CONFIG_FILE)
66 67 70 71
72 -def check_data_migration():
73 return os.path.isdir(OLDPATHS.USER_DATA_DIR) and not os.path.isdir(PATHS.USER_DATA_DIR)
74 75
76 -def migrate_data():
77 os.renames(OLDPATHS.USER_DATA_DIR, PATHS.USER_DATA_DIR)
78 79 82 83
84 -def leave_note():
85 notefile = os.path.join(PATHS.USER_DATA_DIR, "migration_readme.txt") 86 notetext = """ 87 In the latest version of unknown-horizons the user data directories have been moved. 88 This has been done automatically, and symlinks have been created to minimize inconvenience. 89 90 The data is now in the following directories: 91 92 settings.xml: {configfile} 93 screenshots, saves, maps and logs: {datadir} 94 cache: {cachedir} 95 96 It is safe to remove these symlinks and this readme file. 97 98 If you ever downgrade the UH version you might need more care. 99 100 For more information, see the wiki: 101 https://github.com/unknown-horizons/unknown-horizons/wiki/2018-user-data-migration 102 """.format(configfile=PATHS.USER_CONFIG_FILE, datadir=PATHS.USER_DATA_DIR, cachedir=PATHS.CACHE_DIR) 103 print(notetext) 104 with open(notefile, 'w') as f: 105 f.write(notetext)
106