Fix `is_writeable()` for 3 OS support (#4743)
Browse files* Fix `is_writeable()` for 3 OS support
* Update general.py
- utils/general.py +15 -10
utils/general.py
CHANGED
@@ -105,19 +105,24 @@ def get_latest_run(search_dir='.'):
|
|
105 |
|
106 |
def user_config_dir(dir='Ultralytics'):
|
107 |
# Return path of user configuration directory (make if necessary)
|
108 |
-
|
109 |
-
|
110 |
-
path =
|
111 |
-
|
112 |
-
path = Path('/tmp') / dir
|
113 |
-
if not path.is_dir():
|
114 |
-
path.mkdir() # make dir if required
|
115 |
return path
|
116 |
|
117 |
|
118 |
-
def is_writeable(
|
119 |
-
# Return True if
|
120 |
-
return os.access(path, os.R_OK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
|
123 |
def is_docker():
|
|
|
105 |
|
106 |
def user_config_dir(dir='Ultralytics'):
|
107 |
# Return path of user configuration directory (make if necessary)
|
108 |
+
cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 config dirs
|
109 |
+
path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir
|
110 |
+
path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable
|
111 |
+
path.mkdir(exist_ok=True) # make if required
|
|
|
|
|
|
|
112 |
return path
|
113 |
|
114 |
|
115 |
+
def is_writeable(dir):
|
116 |
+
# Return True if directory has write permissions
|
117 |
+
# return os.access(path, os.R_OK) # known issue on Windows
|
118 |
+
file = Path(dir) / 'tmp.txt'
|
119 |
+
try:
|
120 |
+
with open(file, 'w'):
|
121 |
+
pass
|
122 |
+
file.unlink() # remove file
|
123 |
+
return True
|
124 |
+
except IOError:
|
125 |
+
return False
|
126 |
|
127 |
|
128 |
def is_docker():
|