Skip to content

Commit 845ad7f

Browse files
committed
Copy existing user files from old location to new, if any exist (config file not working)
1 parent 3151b71 commit 845ad7f

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

src/sas/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def parse_cli(argv):
104104
return opts
105105

106106
def main(logging="production"):
107+
# Copy files before loading config
108+
from sas.system.user import copy_old_files_to_new_location
109+
copy_old_files_to_new_location()
110+
107111
from sas.system import log
108112
from sas.system import lib
109113
from sas.system import console

src/sas/system/user.py

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import shutil
3+
from pathlib import Path
24
from platformdirs import PlatformDirs
35
from sas.system.version import __version__ as sasview_version
46

@@ -13,68 +15,106 @@
1315
_CACHE_DIR = PLATFORM_DIRS_VERSIONED.user_cache_dir
1416
_CONFIG_DIR = PLATFORM_DIRS_UNVERSIONED.user_config_dir
1517
_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]
1720

1821

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
2326

2427

25-
def get_user_dir(create_if_nonexistent=False):
28+
def _get_user_dir(create_if_nonexistent: bool = False) -> PATH_TYPE:
2629
"""**DEPRECATED** Do not use this function to create new files. This is only used to move files from previous
2730
version locations to new locations
2831
"""
2932
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)
3134

3235

33-
def get_config_dir(create_if_nonexistent=True):
36+
def get_config_dir(create_if_nonexistent: bool = True) -> PATH_TYPE:
3437
"""
3538
The directory where os-specific configurations are stored.
3639
3740
Returns the directory string, creating it if it does not already exist.
3841
"""
3942
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)
4144

4245

43-
def get_app_dir(create_if_nonexistent=True):
46+
def get_app_dir(create_if_nonexistent: bool = True) -> PATH_TYPE:
4447
"""
4548
The directory where the os-specific app data is stored.
4649
4750
Returns the directory string, creating it if it does not already exist.
4851
"""
4952
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)
5164

5265

53-
def get_log_dir(create_if_nonexistent=True):
66+
def get_log_dir(create_if_nonexistent: bool = True) -> PATH_TYPE:
5467
"""
5568
The directory where the os-specific logs are stored.
5669
5770
Returns the directory string, creating it if it does not already exist.
5871
"""
5972
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)
6174

6275

63-
def get_cache_dir(create_if_nonexistent=True):
76+
def get_cache_dir(create_if_nonexistent: bool = True) -> PATH_TYPE:
6477
"""
6578
The directory where the os-specific cache is stored.
6679
6780
Returns the directory string, creating it if it does not already exist.
6881
"""
6982
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)
7184

7285

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

Comments
 (0)