glenn-jocher commited on
Commit
a144536
·
unverified ·
1 Parent(s): 19e28e3

Fix `is_writeable()` for 3 OS support (#4743)

Browse files

* Fix `is_writeable()` for 3 OS support

* Update general.py

Files changed (1) hide show
  1. 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
- system = platform.system()
109
- cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'}
110
- path = Path.home() / cfg.get(system, '') / dir
111
- if system == 'Linux' and not is_writeable(path): # GCP functions and AWS lambda solution, only /tmp is writeable
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(path):
119
- # Return True if path has write permissions (Warning: known issue on Windows)
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():