Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is_writeable() for 3 OS support #4743

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,24 @@ def get_latest_run(search_dir='.'):

def user_config_dir(dir='Ultralytics'):
# Return path of user configuration directory (make if necessary)
system = platform.system()
cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'}
path = Path.home() / cfg.get(system, '') / dir
if system == 'Linux' and not is_writeable(path): # GCP functions and AWS lambda solution, only /tmp is writeable
path = Path('/tmp') / dir
if not path.is_dir():
path.mkdir() # make dir if required
cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 config dirs
path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir
path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable
path.mkdir(exist_ok=True) # make if required
return path


def is_writeable(path):
# Return True if path has write permissions (Warning: known issue on Windows)
return os.access(path, os.R_OK)
def is_writeable(dir):
# Return True if directory has write permissions
# return os.access(path, os.R_OK) # known issue on Windows
file = Path(dir) / 'tmp.txt'
try:
with open(file, 'w'):
pass
file.unlink() # remove file
return True
except IOError:
return False


def is_docker():
Expand Down