glenn-jocher commited on
Commit
4a025ae
1 Parent(s): 2d9411d

Fix `user_config_dir()` for GCP/AWS functions (#4726)

Browse files

* Fix `user_config_dir()` for GCP/AWS functions

Compatability fix for GCP functions and AWS lambda for user config directory in https://github.com/ultralytics/yolov5/pull/4628

* Windows skip check

Files changed (1) hide show
  1. utils/general.py +10 -2
utils/general.py CHANGED
@@ -105,13 +105,21 @@ 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
- settings = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'}
109
- path = Path.home() / settings.get(platform.system(), '') / dir
 
 
 
110
  if not path.is_dir():
111
  path.mkdir() # make dir if required
112
  return path
113
 
114
 
 
 
 
 
 
115
  def is_docker():
116
  # Is environment a Docker container?
117
  return Path('/workspace').exists() # or Path('/.dockerenv').exists()
 
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():
124
  # Is environment a Docker container?
125
  return Path('/workspace').exists() # or Path('/.dockerenv').exists()