|
1 | 1 | import os
|
| 2 | +import shutil |
| 3 | +from pathlib import Path |
2 | 4 | from platformdirs import PlatformDirs
|
3 | 5 | from sas.system.version import __version__ as sasview_version
|
4 | 6 |
|
|
13 | 15 | _CACHE_DIR = PLATFORM_DIRS_VERSIONED.user_cache_dir
|
14 | 16 | _CONFIG_DIR = PLATFORM_DIRS_UNVERSIONED.user_config_dir
|
15 | 17 | _LOG_DIR = PLATFORM_DIRS_UNVERSIONED.user_log_dir
|
16 |
| -_CACHE_DIR = PLATFORM_DIRS_VERSIONED.user_cache_dir |
| 18 | + |
| 19 | +PATH_TYPE = [Path, os.path, str] |
17 | 20 |
|
18 | 21 |
|
19 |
| -def get_dir_and_create_if_needed(dir, create_if_nonexistent=True, ): |
20 |
| - if create_if_nonexistent and not os.path.exists(dir): |
21 |
| - os.makedirs(dir, mode=777, exist_ok=True) |
22 |
| - return dir |
| 22 | +def get_dir_and_create_if_needed(path: PATH_TYPE, create_if_nonexistent: bool = True): |
| 23 | + if create_if_nonexistent and not os.path.exists(path): |
| 24 | + os.makedirs(path, mode=777, exist_ok=True) |
| 25 | + return path |
23 | 26 |
|
24 | 27 |
|
25 |
| -def get_user_dir(create_if_nonexistent=False): |
| 28 | +def _get_user_dir(create_if_nonexistent: bool = False) -> PATH_TYPE: |
26 | 29 | """**DEPRECATED** Do not use this function to create new files. This is only used to move files from previous
|
27 | 30 | version locations to new locations
|
28 | 31 | """
|
29 | 32 | global _USER_DIR
|
30 |
| - return get_dir_and_create_if_needed(create_if_nonexistent, _USER_DIR) |
| 33 | + return get_dir_and_create_if_needed(_USER_DIR, create_if_nonexistent) |
31 | 34 |
|
32 | 35 |
|
33 |
| -def get_config_dir(create_if_nonexistent=True): |
| 36 | +def get_config_dir(create_if_nonexistent: bool = True) -> PATH_TYPE: |
34 | 37 | """
|
35 | 38 | The directory where os-specific configurations are stored.
|
36 | 39 |
|
37 | 40 | Returns the directory string, creating it if it does not already exist.
|
38 | 41 | """
|
39 | 42 | global _CONFIG_DIR
|
40 |
| - return get_dir_and_create_if_needed(create_if_nonexistent, _CONFIG_DIR) |
| 43 | + return get_dir_and_create_if_needed(_CONFIG_DIR, create_if_nonexistent) |
41 | 44 |
|
42 | 45 |
|
43 |
| -def get_app_dir(create_if_nonexistent=True): |
| 46 | +def get_app_dir(create_if_nonexistent: bool = True) -> PATH_TYPE: |
44 | 47 | """
|
45 | 48 | The directory where the os-specific app data is stored.
|
46 | 49 |
|
47 | 50 | Returns the directory string, creating it if it does not already exist.
|
48 | 51 | """
|
49 | 52 | global _APP_DATA_DIR
|
50 |
| - return get_dir_and_create_if_needed(create_if_nonexistent, _APP_DATA_DIR) |
| 53 | + return get_dir_and_create_if_needed(_APP_DATA_DIR, create_if_nonexistent) |
| 54 | + |
| 55 | + |
| 56 | +def get_app_dir_versioned(create_if_nonexistent: bool = True) -> PATH_TYPE: |
| 57 | + """ |
| 58 | + The directory where the os-specific app data is stored. |
| 59 | +
|
| 60 | + Returns the directory string, creating it if it does not already exist. |
| 61 | + """ |
| 62 | + global _APP_VERS_DIR |
| 63 | + return get_dir_and_create_if_needed(_APP_VERS_DIR, create_if_nonexistent) |
51 | 64 |
|
52 | 65 |
|
53 |
| -def get_log_dir(create_if_nonexistent=True): |
| 66 | +def get_log_dir(create_if_nonexistent: bool = True) -> PATH_TYPE: |
54 | 67 | """
|
55 | 68 | The directory where the os-specific logs are stored.
|
56 | 69 |
|
57 | 70 | Returns the directory string, creating it if it does not already exist.
|
58 | 71 | """
|
59 | 72 | global _LOG_DIR
|
60 |
| - return get_dir_and_create_if_needed(create_if_nonexistent, _LOG_DIR) |
| 73 | + return get_dir_and_create_if_needed(_LOG_DIR, create_if_nonexistent) |
61 | 74 |
|
62 | 75 |
|
63 |
| -def get_cache_dir(create_if_nonexistent=True): |
| 76 | +def get_cache_dir(create_if_nonexistent: bool = True) -> PATH_TYPE: |
64 | 77 | """
|
65 | 78 | The directory where the os-specific cache is stored.
|
66 | 79 |
|
67 | 80 | Returns the directory string, creating it if it does not already exist.
|
68 | 81 | """
|
69 | 82 | global _CACHE_DIR
|
70 |
| - return get_dir_and_create_if_needed(create_if_nonexistent, _CACHE_DIR) |
| 83 | + return get_dir_and_create_if_needed(_CACHE_DIR, create_if_nonexistent) |
71 | 84 |
|
72 | 85 |
|
73 |
| -def copy_old_user_files_to_new_locations(): |
74 |
| - # TODO: Should we copy previous items over? Also, should plugin and compiled models be |
75 |
| - old_user_dir = get_user_dir() |
76 |
| - if os.path.exists(old_user_dir): |
77 |
| - log_location = get_log_dir() |
78 |
| - config_location = get_config_dir() |
79 |
| - # TODO: copy plugin models, config files, and |
80 |
| - pass |
| 86 | +def get_plugin_dir(create_if_nonexistent: bool = True) -> PATH_TYPE: |
| 87 | + """ |
| 88 | + The directory where the os-specific cache is stored. |
| 89 | +
|
| 90 | + Returns the directory string, creating it if it does not already exist. |
| 91 | + """ |
| 92 | + app_dir = get_app_dir(create_if_nonexistent) |
| 93 | + return get_dir_and_create_if_needed(Path(app_dir, 'plugin_models'), create_if_nonexistent) |
| 94 | + |
| 95 | + |
| 96 | +def copy_old_files_to_new_location(): |
| 97 | + """Run at startup, check to see if files in the old locations exist and move them if they haven't already.""" |
| 98 | + # Copy the old log to the new location |
| 99 | + user_dir = Path(_get_user_dir()) |
| 100 | + old_sasview_usr_dir = user_dir / '.sasview' |
| 101 | + old_log = user_dir / 'sasview.log' |
| 102 | + new_log = Path(get_log_dir()) / 'sasview.log' |
| 103 | + if old_log.exists() and not new_log.exists(): |
| 104 | + shutil.copy2(old_log, new_log) |
| 105 | + # Copy plugin models to new location |
| 106 | + old_plugins = old_sasview_usr_dir / 'plugin_models' |
| 107 | + new_plugins = Path(get_plugin_dir()) |
| 108 | + if old_plugins.exists(): |
| 109 | + files = [f for f in os.listdir(old_plugins) if os.path.isfile(os.path.join(old_plugins, f))] |
| 110 | + for file in files: |
| 111 | + if not Path(new_plugins, file).exists(): |
| 112 | + shutil.copy2(Path(old_plugins, file), Path(new_plugins, file)) |
| 113 | + # Copy config file over |
| 114 | + new_config_dir = Path(get_config_dir()) |
| 115 | + config_name = f'config-{sasview_version.split(".")[0]}' |
| 116 | + old_config = old_sasview_usr_dir / config_name |
| 117 | + new_config = new_config_dir / config_name |
| 118 | + print(old_config.exists()) |
| 119 | + if not new_config.exists() and old_config.exists(): |
| 120 | + shutil.copy2(old_config, new_config) |
0 commit comments