diff --git a/lib/galaxy/objectstore/__init__.py b/lib/galaxy/objectstore/__init__.py index 694a3e8308c..fa4871309c2 100644 --- a/lib/galaxy/objectstore/__init__.py +++ b/lib/galaxy/objectstore/__init__.py @@ -12,7 +12,7 @@ import logging import threading from xml.etree import ElementTree -from galaxy.util import umask_fix_perms, force_symlink +from galaxy.util import umask_fix_perms, force_symlink, safe_makedirs from galaxy.exceptions import ObjectInvalid, ObjectNotFound from galaxy.util.sleeper import Sleeper from galaxy.util.directory_hash import directory_hash_id @@ -331,8 +331,7 @@ class DiskObjectStore(ObjectStore): dir_only = kwargs.get('dir_only', False) # Create directory if it does not exist dir = path if dir_only else os.path.dirname(path) - if not os.path.exists(dir): - os.makedirs(dir) + safe_makedirs(dir) # Create the file if it does not exist if not dir_only: open(path, 'w').close() # Should be rb? diff --git a/lib/galaxy/util/__init__.py b/lib/galaxy/util/__init__.py index 0943663d304..d7cea7115f8 100644 --- a/lib/galaxy/util/__init__.py +++ b/lib/galaxy/util/__init__.py @@ -568,6 +568,21 @@ def which(file): return None +def safe_makedirs(path): + """ Safely make a directory, do not fail if it already exist or + is created during execution. + """ + if not os.path.exists(path): + try: + os.makedirs(path) + except OSError as e: + # review source for Python 2.7 this would only ever happen + # for the last path anyway so need to recurse - this exception + # means the last part of the path was already in existence. + if e.errno != errno.EEXIST: + raise + + def in_directory( file, directory, local_path_module=os.path ): """ Return true, if the common prefix of both is equal to directory