我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用errno.EEXIST。
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 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 _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 __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 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 _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 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
def makedir(self, tarinfo, targetpath): """Make a directory called targetpath. """ try: # Use a safe mode for the directory, the real mode is set # later in _extract_member(). os.mkdir(targetpath, 0o700) except EnvironmentError as e: if e.errno != errno.EEXIST: raise
def ensure_dir(path): """os.path.makedirs without EEXIST.""" try: os.makedirs(path) except OSError as e: if e.errno != errno.EEXIST: raise
def _makedirs_31(path, exist_ok=False): try: os.makedirs(path) except OSError as exc: if not exist_ok or exc.errno != errno.EEXIST: raise # rely on compatibility behavior until mode considerations # and exists_ok considerations are disentangled. # See https://github.com/pypa/setuptools/pull/1083#issuecomment-315168663
def mkdtemp(suffix=None, prefix=None, dir=None): """User-callable function to create and return a unique temporary directory. The return value is the pathname of the directory. Arguments are as for mkstemp, except that the 'text' argument is not accepted. The directory is readable, writable, and searchable only by the creating user. Caller is responsible for deleting the directory when done with it. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) names = _get_candidate_names() if output_type is bytes: names = map(_os.fsencode, names) for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, prefix + name + suffix) try: _os.mkdir(file, 0o700) except FileExistsError: continue # try again except PermissionError: # This exception is thrown when a directory with the chosen name # already exists on windows. if (_os.name == 'nt' and _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): continue else: raise return file raise FileExistsError(_errno.EEXIST, "No usable temporary directory name found")
def mktemp(suffix="", prefix=template, dir=None): """User-callable function to return a unique temporary file name. The file is not created. Arguments are similar to mkstemp, except that the 'text' argument is not accepted, and suffix=None, prefix=None and bytes file names are not supported. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may refer to a file that did not exist at some point, but by the time you get around to creating it, someone else may have beaten you to the punch. """ ## from warnings import warn as _warn ## _warn("mktemp is a potential security risk to your program", ## RuntimeWarning, stacklevel=2) if dir is None: dir = gettempdir() names = _get_candidate_names() for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, prefix + name + suffix) if not _exists(file): return file raise FileExistsError(_errno.EEXIST, "No usable temporary filename found")
def mkdir_p_polyfill(path, perms, exist_ok): """Make directories including parents. Python >= 3.2 doesn't need this because the exist_ok parameter is there. However, earlier python versions don't have that. See http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python/600612#600612 """ try: os.makedirs(path, perms) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise
def __init__(self, *args, **kwargs): #super(PcapPlugin, self).__init__(*args, **kwargs) plugin.Plugin.__init__(self, *args, **kwargs) try: os.makedirs(self.__pcap_dir__) except OSError as exception: if exception.errno != errno.EEXIST: raise
def cached_inventory(self, filters, cache_path=None, cache_ttl=3600, refresh=False, ): """ Wrapper method implementing caching functionality over list_inventory. :param dict filters: A dictionary of pyVmomi virtual machine attribute key-value filters. :param str cache_path: A path for caching inventory list data. Quite a necessity for large environments. :param int cache_ttl: Integer Inventory list data cache Time To Live in seconds. (cache Expiration period) :param boolean refresh: Setting this True, triggers a cache refresh. Fresh data is fetched. :return: An Ansible pluggable dynamic inventory, as a Python json serializable dictionary. """ if refresh: return self.list_and_save(filters, cache_path) else: if os.path.isfile(cache_path) and time() - os.stat(cache_path).st_mtime < cache_ttl: try: with open(cache_path) as f: data = load(f) return data except (ValueError, IOError): return self.list_and_save(filters, cache_path) else: if not os.path.exists(os.path.dirname(cache_path)): try: if cache_path: os.makedirs(os.path.dirname(cache_path)) else: raise OSError("[Error] cache_path not defined: {}".format(cache_path)) # handle race condition except OSError as exc: if exc.errno == errno.EACCES: print("{}".format(str(exc))) exit(1) elif exc.errno != errno.EEXIST: raise return self.list_and_save(filters, cache_path)
def visitInterface (self, node): package = qualifiedName(node.scopedName()[:-1] + ['jni']) # Ensure the directory structure is there path = os.path.join(*package.split('.')) try: os.makedirs(path) except OSError, e: # If the leaf directory already existed (or was created in the # interim), ignore the error if e.errno != errno.EEXIST: raise # Override library name with argument libname = self.__options.get('libname', None) if not libname: libname = node.scopedName()[-2].lower() + 'jni' # Generate the stub stubCode = StubClass(package, libname).generate(node) stubFile = os.path.join(path, stubName(node) + '.java') stubCode.write(javacode.SourceFile(open(stubFile, 'w'))) # Generate the helper interface = qualifiedName(node.scopedName()) helperCode = HelperClass(package, interface).generate(node) helperFile = os.path.join(path, helperName(node) + '.java') helperCode.write(javacode.SourceFile(open(helperFile, 'w'))) # Generate the POA poaCode = POAClass(package, libname).generate(node) poaFile = os.path.join(path, poaName(node) + '.java') poaCode.write(javacode.SourceFile(open(poaFile, 'w')))
def make_sure_path_exists(path): try: os.makedirs(path) except OSError as exception: if exception.errno != errno.EEXIST: raise # Debug model info
def make_sure_path_exists(path): try: os.makedirs(path) except OSError as exception: if exception.errno != errno.EEXIST: raise # Run model
def make_sure_path_exists(path): try: os.makedirs(path) except OSError as exception: if exception.errno != errno.EEXIST: raise # Run model, use current timestamp for output directory name
def mkdir_p(path): try: os.makedirs(path) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise
def make_dirs(file_path): """Make the directories for a file path :param file_path: file path to be created if it doesn't exist :type file_path: str :return: None """ try: os.makedirs(os.path.dirname(file_path)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise # pragma: no cover