def create_directory_if_not_exists(logger, path):
"""
Creates 'path' if it does not exist
If creation fails, an exception will be thrown
:param logger: the logger
:param path: the path to ensure it exists
"""
try:
os.makedirs(path)
except OSError as ex:
if ex.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
log_critical_error(logger, ex, 'An error happened trying to create ' + path)
raise
python类EEXIST的实例源码
def create_directory_if_not_exists(self, path):
"""
Creates 'path' if it does not exist
If creation fails, an exception will be thrown
:param path: the path to ensure it exists
"""
try:
os.makedirs(path)
except OSError as ex:
if ex.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
self.log_critical_error(ex, 'An error happened trying to create ' + path)
raise
def makedirs(name, mode=0777):
"""makedirs(path [, mode=0777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.
"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, mode)
except OSError, e:
# be happy if someone already created the path
if e.errno != errno.EEXIST:
raise
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
return
mkdir(name, mode)
def connectionMade(self):
dst = path.abspath(path.join(self.destDir,self.filename))
exists = path.exists(dst)
if self.resume and exists:
# I have been told I want to resume, and a file already
# exists - Here we go
self.file = open(dst, 'ab')
log.msg("Attempting to resume %s - starting from %d bytes" %
(self.file, self.file.tell()))
elif self.overwrite or not exists:
self.file = open(dst, 'wb')
else:
raise OSError(errno.EEXIST,
"There's a file in the way. "
"Perhaps that's why you cannot open it.",
dst)
def create_directory_if_not_exists(logger, path):
"""
Creates 'path' if it does not exist
If creation fails, an exception will be thrown
:param logger: the logger
:param path: the path to ensure it exists
"""
try:
os.makedirs(path)
except OSError as ex:
if ex.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
log_critical_error(logger, ex, 'An error happened trying to create ' + path)
raise
def _mkstemp_inner(dir, pre, suf, flags):
"""Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
names = _get_candidate_names()
for seq in xrange(TMP_MAX):
name = names.next()
file = _os.path.join(dir, pre + name + suf)
try:
fd = _os.open(file, flags, 0600)
_set_cloexec(fd)
return (fd, _os.path.abspath(file))
except OSError, e:
if e.errno == _errno.EEXIST:
continue # try again
raise
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
# User visible interfaces.
def cmdargs_for_style(self, formatstyle, filename=None):
# type: (Style, Optional[str]) -> List[str]
assert isinstance(formatstyle, Style)
configdata = bytestr(self.styletext(formatstyle))
sha = shahex(configdata)
cfg = os.path.join(tempfile.gettempdir(),
'whatstyle_rustfmt_%s/%s' % (sha, self.configfilename))
try:
dirpath = os.path.dirname(cfg)
os.makedirs(dirpath)
self.add_tempfile(dirpath)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
if not self.tempfile_exists(cfg):
writebinary(cfg, configdata)
self.add_tempfile(cfg)
cmdargs = ['--config-path', cfg]
return cmdargs
def rename(src, dst):
# Try atomic or pseudo-atomic rename
if _rename(src, dst):
return
# Fall back to "move away and replace"
try:
os.rename(src, dst)
except OSError as e:
if e.errno != errno.EEXIST:
raise
old = "%s-%08x" % (dst, random.randint(0, sys.maxint))
os.rename(dst, old)
os.rename(src, dst)
try:
os.unlink(old)
except Exception:
pass
def ensure_dataset_exists(files, dirname):
path = os.path.join("data", dirname)
rv = [os.path.join(path, f) for f in files]
logger.info("Retrieving dataset from {}".format(path))
if not os.path.exists(path):
# Extract or download data
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
for f, file_path in zip(files, rv):
data_url = BASE_URL + dirname + "/" + f
if not os.path.exists(file_path):
logger.warn("Downloading {}".format(data_url))
with urllib3.PoolManager().request('GET', data_url, preload_content=False) as r, \
open(file_path, 'wb') as w:
shutil.copyfileobj(r, w)
return rv
# Convert data into a stream of never-ending data
def makedirs(name, mode=0777):
"""makedirs(path [, mode=0777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.
"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, mode)
except OSError, e:
# be happy if someone already created the path
if e.errno != errno.EEXIST:
raise
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
return
mkdir(name, mode)
def __call__(self, fn):
def decorated_fn():
w = Workspace()
try:
os.makedirs(os.path.join(w.path, '.ave', 'config'))
except OSError, e:
if e.errno != errno.EEXIST:
raise Exception(
'could not create directory at %s: %s' % (w.path,str(e))
)
result = fn(w)
if result:
w.delete()
return result
return decorated_fn
# check that an empty home is handled correctly
def make_git(self, path):
if path.startswith('/'):
path = os.path.normpath(path)
if not path.startswith(self.path):
raise Exception('can not create git outside workspace')
path = os.path.join(self.root, self.uid, path)
try: # create the target directory
os.makedirs(path)
except OSError, e:
if e.errno != errno.EEXIST:
raise Exception(
'could not create directory at %s: %s' % (path, str(e))
)
ave.git.init(path)
msg = 'Created by avi.workpace.make_git()'
ave.git.commit(path, msg, allow_empty=True)
return path
def write_config(home, port, persist, demote):
path = os.path.join(home, '.ave','config','adb_server.json')
try:
os.makedirs(os.path.join(home, '.ave','config'))
except os.error, e:
if e.errno != errno.EEXIST:
raise
config = {}
if port != None:
config['port'] = port
if persist != None:
config['persist'] = persist
if demote != None:
config['demote'] = demote
with open(path, 'w') as f:
json.dump(config, f)
def __call__(self, fn):
def decorated_fn():
w = Workspace()
try:
os.makedirs(os.path.join(w.path, '.ave', 'config'))
except OSError, e:
if e.errno != errno.EEXIST:
raise Exception(
'could not create directory at %s: %s' % (w.path,str(e))
)
result = fn(w)
if result:
w.delete()
return result
return decorated_fn
# check that an empty home is handled correctly
def make_git(self, path):
if path.startswith('/'):
path = os.path.normpath(path)
if not path.startswith(self.path):
raise Exception('can not create git outside workspace')
path = os.path.join(self.root, self.uid, path)
try: # create the target directory
os.makedirs(path)
except OSError, e:
if e.errno != errno.EEXIST:
raise Exception(
'could not create directory at %s: %s' % (path, str(e))
)
ave.git.init(path)
msg = 'Created by avi.workpace.make_git()'
ave.git.commit(path, msg, allow_empty=True)
return path
def handle_SIGUSR1(self, signum, frame):
# propagate the signal to children, but only if they are AVE processes
for pid in self.get_children():
name = get_proc_name(pid)
if name.startswith('ave-'):
os.kill(pid, signal.SIGUSR1)
# make the dump directory if it doesn't exist
hickup_dir = os.path.join(self.home, '.ave', 'hickup')
try:
os.makedirs(hickup_dir)
except OSError, e:
if e.errno != errno.EEXIST:
self.log('ERROR: could not create %s: %s' % (hickup_dir, e))
return
# create the trace file
date = time.strftime('%Y%m%d-%H%M%S')
name = '%s-%s-%d' % (date, self.proc_name, os.getpid())
path = os.path.join(hickup_dir, name)
with open(path, 'w') as f:
f.write('stack:\n%s' % ''.join(traceback.format_stack(frame)))
f.write('locals:\n%s\n' % frame.f_locals)
f.write('globals:\n%s' % frame.f_globals)
def handle_SIGUSR1(self, signum, frame):
# propagate the signal to children, but only if they are AVE processes
for pid in self.get_children():
name = get_proc_name(pid)
if name.startswith('ave-'):
os.kill(pid, signal.SIGUSR1)
# make the dump directory if it doesn't exist
hickup_dir = os.path.join(self.home, '.ave', 'hickup')
try:
os.makedirs(hickup_dir)
except OSError, e:
if e.errno != errno.EEXIST:
self.log('ERROR: could not create %s: %s' % (hickup_dir, e))
return
# create the trace file
date = time.strftime('%Y%m%d-%H%M%S')
name = '%s-%s-%d' % (date, self.proc_name, os.getpid())
path = os.path.join(hickup_dir, name)
with open(path, 'w') as f:
f.write('stack:\n%s' % ''.join(traceback.format_stack(frame)))
f.write('locals:\n%s\n' % frame.f_locals)
f.write('globals:\n%s' % frame.f_globals)
def connect(username, password):
global token, userid, files
token = None
userid = None
files = None
token_req = urllib.request.Request(base_url + token_url % (urllib.parse.quote(username, safe=""),
urllib.parse.quote(password, safe="")))
with urllib.request.urlopen(token_req) as response:
result = json.loads(response.readall().decode("utf-8"))
if "errorcode" in result:
raise Exception(result["errorcode"])
token = result["token"]
siteinfo = call_wsfunction("moodle_webservice_get_siteinfo")
userid = siteinfo["userid"]
try:
os.makedirs(download_folder)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(download_folder):
pass
else:
raise
def download_file(url, local_fname=None, force_write=False):
# requests is not default installed
import requests
if local_fname is None:
local_fname = url.split('/')[-1]
if not force_write and os.path.exists(local_fname):
return local_fname
dir_name = os.path.dirname(local_fname)
if dir_name != "":
if not os.path.exists(dir_name):
try: # try to create the directory if it doesn't exists
os.makedirs(dir_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
r = requests.get(url, stream=True)
assert r.status_code == 200, "failed to open %s" % url
with open(local_fname, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_fname
def _makedirs(path):
"""
Create a base directory of the provided path and return None.
:param path: A string containing a path to be deconstructed and basedir
created.
:return: None
"""
dirname, _ = os.path.split(path)
try:
os.makedirs(dirname)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise
def makedirs(name, mode=0777):
"""makedirs(path [, mode=0777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.
"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, mode)
except OSError, e:
# be happy if someone already created the path
if e.errno != errno.EEXIST:
raise
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
return
mkdir(name, mode)
def _mkstemp_inner(dir, pre, suf, flags):
"""Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
names = _get_candidate_names()
for seq in xrange(TMP_MAX):
name = names.next()
file = _os.path.join(dir, pre + name + suf)
try:
fd = _os.open(file, flags, 0600)
_set_cloexec(fd)
return (fd, _os.path.abspath(file))
except OSError, e:
if e.errno == _errno.EEXIST:
continue # try again
if _os.name == 'nt' and e.errno == _errno.EACCES:
# On windows, when a directory with the chosen name already
# exists, EACCES error code is returned instead of EEXIST.
continue
raise
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
# User visible interfaces.
def _ensure_tree(path):
"""Create a directory (and any ancestor directories required).
:param path: Directory to create
"""
try:
os.makedirs(path)
except OSError as e:
if e.errno == errno.EEXIST:
if not os.path.isdir(path):
raise
else:
return False
elif e.errno == errno.EISDIR:
return False
else:
raise
else:
return True
def dest_path(self, path):
path = os.path.normpath(path)
if not path.startswith(self.__dest_path_prefix):
path = os.path.join(self.__dest_path_prefix,
os.path.splitdrive(path)[1].lstrip(os.sep))
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
self.__dest_path = path
def dest_path(self, path):
path = os.path.normpath(path)
if not path.startswith(self.__dest_path_prefix):
path = os.path.join(self.__dest_path_prefix,
os.path.splitdrive(path)[1].lstrip(os.sep))
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
self.__dest_path = path
def create_file(f):
try:
os.makedirs(os.path.dirname(f))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
p_open(Res.mk_file + f)
def acquire(self, blocking=True):
"""Acquire the lock if possible.
If the lock is in use and ``blocking`` is ``False``, return
``False``.
Otherwise, check every `self.delay` seconds until it acquires
lock or exceeds `self.timeout` and raises an `~AcquisitionError`.
"""
start = time.time()
while True:
self._validate_lockfile()
try:
fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
with os.fdopen(fd, 'w') as fd:
fd.write('{0}'.format(os.getpid()))
break
except OSError as err:
if err.errno != errno.EEXIST: # pragma: no cover
raise
if self.timeout and (time.time() - start) >= self.timeout:
raise AcquisitionError('Lock acquisition timed out.')
if not blocking:
return False
time.sleep(self.delay)
self._locked = True
return True
def mkdir(name, perm=0775):
"""
A more contained wrapper to directory management
"""
attempt = 3
if isdir(name):
return True
while attempt > 0:
try:
makedirs(name, perm)
logger.debug('Created directory: %s' % name)
return True
except OSError, e:
if e[0] == errno.EEXIST:
# directory exists; this is okay
return isdir(name)
logger.debug('Created directory %s exception: %s' % (
name, e,
))
# racing condition; just try again
attempt -= 1
# To many attempts... fail
# ... fall through...
return False
def acquire(self, timeout=None):
""" Acquire the lock.
Creates the PID file for this lock, or raises an error if
the lock could not be acquired.
"""
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
while True:
try:
write_pid_to_pidfile(self.path)
except OSError as exc:
if exc.errno == errno.EEXIST:
# The lock creation failed. Maybe sleep a bit.
if time.time() > end_time:
if timeout is not None and timeout > 0:
raise LockTimeout("Timeout waiting to acquire"
" lock for %s" %
self.path)
else:
raise AlreadyLocked("%s is already locked" %
self.path)
time.sleep(timeout is not None and timeout / 10 or 0.1)
else:
raise LockFailed("failed to create %s" % self.path)
else:
return
def acquire(self, timeout=None):
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
end_time += timeout
if timeout is None:
wait = 0.1
else:
wait = max(0, timeout / 10)
while True:
try:
os.mkdir(self.lock_file)
except OSError:
err = sys.exc_info()[1]
if err.errno == errno.EEXIST:
# Already locked.
if os.path.exists(self.unique_name):
# Already locked by me.
return
if timeout is not None and time.time() > end_time:
if timeout > 0:
raise LockTimeout("Timeout waiting to acquire"
" lock for %s" %
self.path)
else:
# Someone else has the lock.
raise AlreadyLocked("%s is already locked" %
self.path)
time.sleep(wait)
else:
# Couldn't create the lock for some other reason
raise LockFailed("failed to create %s" % self.lock_file)
else:
open(self.unique_name, "wb").close()
return