def do_acquire(self, waitflag=False): if waitflag: sleep = 1 locked = self.do_acquire(False) while not locked: time.sleep(sleep) sleep = min(sleep + 1, 15) locked = self.do_acquire(False) return locked locked = False self.f = open(self.fn, 'a') try: msvcrt.locking(self.f.fileno(), msvcrt.LK_NBLCK, 1) try: self.f.write(`os.getpid()` + '\n') # informational only self.f.seek(0) # lock is at offset 0, so go back there locked = True except: self.do_release() raise except IOError, x: if x.errno == errno.EACCES: self.f.close() del self.f else: raise
def _tryflock(fp): try: locking(fp.fileno(), LK_LOCK | LK_NBLCK, 32) except IOError: return False else: return True
def lock(file_, flags): if flags & constants.LOCK_SH: if flags & constants.LOCK_NB: mode = msvcrt.LK_NBRLCK else: mode = msvcrt.LK_RLOCK else: if flags & constants.LOCK_NB: mode = msvcrt.LK_NBLCK else: mode = msvcrt.LK_LOCK # windows locks byte ranges, so make sure to lock from file start try: savepos = file_.tell() if savepos: # [ ] test exclusive lock fails on seek here # [ ] test if shared lock passes this point file_.seek(0) # [x] check if 0 param locks entire file (not documented in # Python) # [x] just fails with "IOError: [Errno 13] Permission denied", # but -1 seems to do the trick try: msvcrt.locking(file_.fileno(), mode, -1) except IOError as exc_value: # [ ] be more specific here raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror)
def _flock(fp): # The string representation of a PID should never be # bigger than 32 characters, not even on 64bit systems. # However, it should work even if it is, because all # FileLocks want to lock the same proportion of the # file, even though it might not be the whole file. locking(fp.fileno(), LK_LOCK, 32)
def _remove_lock(self): if sys.platform in ('darwin', 'linux2', 'cygwin'): import fcntl fcntl.flock(self._lock_file_descriptor, fcntl.LOCK_UN) elif sys.platform == 'win32': import msvcrt msvcrt.locking(self._lock_file_descriptor, msvcrt.LK_UNLCK, 32)
def _remove_lock(self): if sys.platform == 'win32': import msvcrt msvcrt.locking(self._lock_file_descriptor, msvcrt.LK_UNLCK, 32) else: import fcntl fcntl.flock(self._lock_file_descriptor, fcntl.LOCK_UN)
def _lock_file(self, name, f): import msvcrt for p in range(0, min(self.sizes[name], MAXLOCKRANGE), MAXLOCKSIZE): f.seek(p) msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, min(MAXLOCKSIZE, self.sizes[name] - p))
def unlock_file(fd): try: import msvcrt msvcrt.locking(fd, 0, 1024) except ImportError: pass os.close(fd)
def _create_lock(self): if sys.platform.startswith('linux') or sys.platform in ('darwin', 'cygwin'): import fcntl fcntl.flock(self._lock_file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB) elif sys.platform == 'win32': import msvcrt msvcrt.locking(self._lock_file_descriptor, msvcrt.LK_NBLCK, 32)
def _lock_file_non_blocking(file_): try: msvcrt.locking(file_.fileno(), msvcrt.LK_NBLCK, 1) return True # TODO: check errno except IOError: return False
def __release_lock(lock): if sys.platform == "win32": import msvcrt msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1) else: import fcntl fcntl.flock(lock, fcntl.LOCK_UN)
def _lock_windows(self): try: msvcrt.locking(self.fh, msvcrt.LK_LOCK, 1) except OSError: return False else: return True
def _create_lock(self): if sys.platform == 'win32': import msvcrt msvcrt.locking(self._lock_file_descriptor, msvcrt.LK_NBLCK, 32) else: import fcntl fcntl.flock(self._lock_file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB)
def __enter__(self): try: self.fd = os.open(self.name, os.O_WRONLY | os.O_CREAT | os.O_APPEND) except OSError as e: if e.errno == errno.ENOENT: raise LockFileCreationException(e) else: raise self.file = os.fdopen(self.fd, "w") if is_windows: lock_flags = msvcrt.LK_LOCK else: lock_flags = fcntl.LOCK_EX if self.fail_on_lock: if is_windows: lock_flags = msvcrt.LK_NBLCK else: lock_flags |= fcntl.LOCK_NB try: if is_windows: msvcrt.locking(self.file.fileno(), lock_flags, 1) else: fcntl.flock(self.file, lock_flags) except IOError as e: error_code = errno.EACCES if is_windows else errno.EAGAIN if e.errno == error_code: raise LockFileObtainException() raise return self.file
def create_extension(self, code, force=False, name=None, include_dirs=None, library_dirs=None, runtime_library_dirs=None, extra_compile_args=None, extra_link_args=None, libraries=None, compiler=None, ): if Cython is None: raise ImportError('Cython is not available') code = deindent(code) lib_dir = os.path.expanduser('~/.brian/cython_extensions') try: os.makedirs(lib_dir) except OSError: if not os.path.exists(lib_dir): raise key = code, sys.version_info, sys.executable, Cython.__version__ if force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += time.time(), if key in self._code_cache: return self._code_cache[key] if name is not None: module_name = name#py3compat.unicode_to_str(args.name) else: module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest() module_path = os.path.join(lib_dir, module_name + self.so_ext) if prefs['codegen.runtime.cython.multiprocess_safe']: lock_file = os.path.join(lib_dir, module_name + '.lock') with open(lock_file, 'w') as f: if msvcrt: msvcrt.locking(f.fileno(), msvcrt.LK_RLCK, os.stat(lock_file).st_size) else: fcntl.flock(f, fcntl.LOCK_EX) return self._load_module(module_path, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key) else: return self._load_module(module_path, include_dirs, library_dirs, extra_compile_args, extra_link_args, libraries, code, lib_dir, module_name, runtime_library_dirs, compiler, key)
def unlock2(fd): if sys.platform == "win32": os.lseek(fd, 0, 0) # make sure we're at the beginning msvcrt.locking(fd, msvcrt.LK_UNLCK, 10) # assuming first 10 bytes! else: fcntl.flock(fd, fcntl.LOCK_UN) return
def unlock(file): """ Unlock first 10 bytes of a file. """ pos = file.tell() # remember current position file.seek(0) msvcrt.locking(file.fileno(),msvcrt.LK_UNLCK,10) file.seek(pos) # reset position
def release(self): import msvcrt # @UnresolvedImport if self.fd is None: raise Exception("Lock was not acquired") msvcrt.locking(self.fd, msvcrt.LK_UNLCK, 1) os.close(self.fd) self.fd = None
def __acquire_lock(lock): if sys.platform == "win32": import msvcrt # lock 1 byte: file is supposed to be zero-byte long msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1) else: import fcntl fcntl.flock(lock, fcntl.LOCK_EX)
def lockf(fileno,mode): if mode & LOCK_UN: msvcrt.locking(fileno, msvcrt.LK_UNLCK, 0) if mode & _locks: msmode = _modes[mode & _locks] if msmode is None: raise AssertionError("Invalid lock flags", mode) msvcrt.locking(fileno, msmode, 0)
def write(): try: fd = open( ini_filename, "w" ) msvcrt.locking( fd.fileno(), msvcrt.LK_LOCK, 1 ) ini.write(fd) fd.close() except: pass
def lock_file(filename): fd = os.open(filename, os.O_CREAT | os.O_RDWR, 0666) try: import fcntl fcntl.flock(fd, fcntl.LOCK_EX) except ImportError: import msvcrt msvcrt.locking(fd, 1, 1024) return fd
def release(self): if os.name == 'nt': msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1) else: fcntl.lockf(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) if self.lockfile is not None: self.lockfile.close() self.lockfile = None
def lock(self, no_wait=False): import msvcrt if no_wait: op = msvcrt.LK_NBLCK else: op = msvcrt.LK_LOCK self.fd.seek(0) msvcrt.locking(self.fd, op, 1)
def _remove_lock(self): if sys.platform.startswith("linux") or sys.platform in ("darwin", "cygwin"): import fcntl fcntl.flock(self._lock_file_descriptor, fcntl.LOCK_UN) elif sys.platform == "win32": import msvcrt msvcrt.locking(self._lock_file_descriptor, msvcrt.LK_UNLCK, 32)
def _unlock(self): if not self._fp: return if LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_FCNTL: fcntl.flock(self._fp.fileno(), fcntl.LOCK_UN) elif LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_MSVCRT: msvcrt.locking(self._fp.fileno(), msvcrt.LK_UNLCK, 1) self._fp.close() self._fp = None
def _unlock_file(self, name, f): if name == self.path: size = self.size else: size = self.tops[name] import msvcrt for p in range(0, min(size, MAXLOCKRANGE), MAXLOCKSIZE): f.seek(p) msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, min(MAXLOCKSIZE, size - p))
def __enter__(self): self.lockfile = open(self.lockfilename, 'w') try: if have_fcntl: fcntl.flock(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) elif have_msvcrt: msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_NBLCK, 1) except (BlockingIOError, PermissionError): self.lockfile.close() raise MesonException('Some other Meson process is already using this build directory. Exiting.')
def unlock(): global lockfile if lockfile is None: # It happens during development! return if sys.platform == "win32": msvcrt.locking(lockfile.fileno(), msvcrt.LK_UNLCK, 1) else: fcntl.lockf(lockfile.fileno(), fcntl.LOCK_UN) lockfile.close() os.unlink(LOCK_FILE)
def lock(): # This really shouldn't be here global lockfile lockfile = open(LOCK_FILE, "w") try: if sys.platform == "win32": msvcrt.locking(lockfile.fileno(), msvcrt.LK_NBLCK, 1) # IOError on failure else: fcntl.lockf(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) return True # Lock succeeded except IOError, e: return False
def win_unlockfile(fobj): """win unlock file""" msvcrt.locking(fobj.fileno(), msvcrt.LK_UNLCK, file_size(fobj))
def lock(fd): msvcrt.locking(fd, msvcrt.LK_NBLCK, 1024)
def lock_file (f, fSize, info=None): if info: info.write("Locking: " + f.name + "\n") msvcrt.locking(f.fileno(), msvcrt.LK_RLCK, 1) return True
def shared_file_lock(fd: IO[Any]) -> None: if fcntl: fcntl.flock(fd.fileno(), fcntl.LOCK_SH) # type: ignore elif msvcrt: msvcrt.locking(fd.fileno(), msvcrt.LK_LOCK, 1024) # type: ignore
def _lock_file(file, content): msvcrt.locking(file.fileno(), msvcrt.LK_LOCK, len(content))
def unlock(self): import msvcrt self.fd.seek(0) msvcrt.locking(self.fd, LK_UNLCK, 1)
def unlock(fd): msvcrt.locking(fd, msvcrt.LK_UNLCK, 1024)
def lock_file(f, exclusive=False): if f.mode == 'r' and exclusive: raise Exception('Please use non exclusive mode for reading') msvcrt.locking(f.fileno(), msvcrt.LK_RLCK, file_size(f))
def do_release(self): msvcrt.locking(self.f.fileno(), msvcrt.LK_UNLCK, 1) self.f.close() del self.f
def lock_file(f): msvcrt.locking(f.fileno(), msvcrt.LK_RLCK, file_size(f))
def is_running(self): logger.info("Singlet lockfile: " + self.lockfile) try: # If advance UNIX system with atomic locks on open if BSD: self.fd = os.open( self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_EXLOCK | os.O_NONBLOCK) else: self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) if WIN: msvcrt.locking(self.fd, msvcrt.LK_NBRLCK, 65) if LIN: fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except (IOError, OSError) as e: if e.errno in (errno.EACCES, errno.EPERM, errno.EWOULDBLOCK): self.fd = None return True elif e.errno == errno.EEXIST: # Workaround for Linux/Windows mostly which lacks atomic # locking on open self.fd = os.open(self.lockfile, os.O_RDWR) try: if WIN: msvcrt.locking(self.fd, msvcrt.LK_NBRLCK, 65) if LIN: fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except (IOError, OSError) as e: # Some entity has been faster than us if we WOULDBLOCK if e.errno in (errno.EACCES, errno.EPERM, errno.EWOULDBLOCK): self.fd = None return True else: logger.exception("Something went wrong") self.fd = None raise else: logger.exception("Something went wrong") # Anything else is horribly wrong, we need to raise to the # upper level so the following code in this try clause # won't execute. self.fd = None raise # By this moment the file should be locked or we should exit so # there should realistically be no error here, or it can raise if self.oldpid_is_running(): try: os.close(self.fd) except OSError as e: # We shouldn't raise here anything. Because we don't # actually care logger.exception("Interesting state") self.fd = None return True # Barring any OS/Hardware issue this musn't throw anything. But # even if it throws we should raise it because it means we # shouldn't run and some serious problem is already happening. So # no, I'm not going to escape this one in try/except. if hasattr(os, "ftruncate"): os.ftruncate(self.fd, 0) # Erase os.lseek(self.fd, 0, 0) # Rewind # Write PID with WIN fix os.write(self.fd, self.pid.rjust(64, "#").encode()) if hasattr(os, "fsync"): os.fsync(self.fd)
def acquire_windows(self): from msvcrt import locking, LK_LOCK locking(self.fileno, LK_LOCK, 1)
def Unlock(self, fd): import msvcrt # pylint: disable=g-import-not-at-top msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
def win_lockfile(fobj, blocking=True): """win lock file""" flags = msvcrt.LK_RLCK if not blocking: flags = msvcrt.LK_NBRLCK msvcrt.locking(fobj.fileno(), flags, file_size(fobj))
def _unlock(lockfp: TextIO) -> None: file_size = os.path.getsize(os.path.realpath(lockfp.name)) msvcrt.locking(lockfp.fileno(), msvcrt.LK_UNLCK, file_size)
def unlock_file(f): msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, file_size(f))
def _unlock_file(file_): msvcrt.locking(file_.fileno(), msvcrt.LK_UNLCK, 1)
def lock(f, readonly): msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
def create_extension(self, code, force=False, name=None, define_macros=None, include_dirs=None, library_dirs=None, runtime_library_dirs=None, extra_compile_args=None, extra_link_args=None, libraries=None, compiler=None, sources=None, owner_name='', ): if sources is None: sources = [] self._simplify_paths() if Cython is None: raise ImportError('Cython is not available') code = deindent(code) lib_dir = get_cython_cache_dir() if '~' in lib_dir: lib_dir = os.path.expanduser(lib_dir) try: os.makedirs(lib_dir) except OSError: if not os.path.exists(lib_dir): raise IOError("Couldn't create Cython cache directory '%s', try setting the " "cache directly with prefs.codegen.runtime.cython.cache_dir." % lib_dir) numpy_version = '.'.join(numpy.__version__.split('.')[:2]) # Only use major.minor version key = code, sys.version_info, sys.executable, Cython.__version__, numpy_version if force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += time.time(), if key in self._code_cache: return self._code_cache[key] if name is not None: module_name = name#py3compat.unicode_to_str(args.name) else: module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest() if owner_name: logger.diagnostic('"{owner_name}" using Cython module "{module_name}"'.format(owner_name=owner_name, module_name=module_name)) module_path = os.path.join(lib_dir, module_name + self.so_ext) if prefs['codegen.runtime.cython.multiprocess_safe']: lock_file = os.path.join(lib_dir, module_name + '.lock') with open(lock_file, 'w') as f: # Lock if msvcrt: msvcrt.locking(f.fileno(), msvcrt.LK_RLCK, os.stat(lock_file).st_size) else: fcntl.flock(f, fcntl.LOCK_EX) module = self._load_module(module_path, define_macros=define_macros, include_dirs=include_dirs, library_dirs=library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, code=code, lib_dir=lib_dir, module_name=module_name, runtime_library_dirs=runtime_library_dirs, compiler=compiler, key=key, sources=sources) # Unlock if msvcrt: msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, os.stat(lock_file).st_size) else: fcntl.flock(f, fcntl.LOCK_UN) return module else: return self._load_module(module_path, define_macros=define_macros, include_dirs=include_dirs, library_dirs=library_dirs, extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, libraries=libraries, code=code, lib_dir=lib_dir, module_name=module_name, runtime_library_dirs=runtime_library_dirs, compiler=compiler, key=key, sources=sources)
def flock(file, flag): oldoffset = file.tell() file.seek(0) msvcrt.locking(file.fileno(), flag, 1) file.seek(oldoffset)
def file_unlock(fileobj): try: msvcrt.locking(fileobj.fileno(), msvcrt.LK_UNLCK, 1) except: pass
def _unlock_file(file): try: file.seek(0) msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, 1) except IOError: raise LockError("Couldn't unlock %r" % file.name)
def unlock(f): msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
def __enter__(self): self.size = os.path.getsize(self.fileobj.name) msvcrt.locking(self.fileobj.fileno(), self.operation, self.size)
def lock(file_, flags): if flags & constants.LOCK_SH: import win32file import pywintypes import winerror __overlapped = pywintypes.OVERLAPPED() if sys.version_info.major == 2: if flags & constants.LOCK_NB: mode = constants.LOCKFILE_FAIL_IMMEDIATELY else: mode = 0 else: if flags & constants.LOCK_NB: mode = msvcrt.LK_NBRLCK else: mode = msvcrt.LK_RLCK # is there any reason not to reuse the following structure? hfile = win32file._get_osfhandle(file_.fileno()) try: win32file.LockFileEx(hfile, mode, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: # error: (33, 'LockFileEx', 'The process cannot access the file # because another process has locked a portion of the file.') if exc_value.winerror == winerror.ERROR_LOCK_VIOLATION: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror, fh=file_) else: # Q: Are there exceptions/codes we should be dealing with # here? raise else: mode = constants.LOCKFILE_EXCLUSIVE_LOCK if flags & constants.LOCK_NB: mode |= constants.LOCKFILE_FAIL_IMMEDIATELY if flags & constants.LOCK_NB: mode = msvcrt.LK_NBLCK else: mode = msvcrt.LK_LOCK # windows locks byte ranges, so make sure to lock from file start try: savepos = file_.tell() if savepos: # [ ] test exclusive lock fails on seek here # [ ] test if shared lock passes this point file_.seek(0) # [x] check if 0 param locks entire file (not documented in # Python) # [x] fails with "IOError: [Errno 13] Permission denied", # but -1 seems to do the trick try: msvcrt.locking(file_.fileno(), mode, lock_length) except IOError as exc_value: # [ ] be more specific here raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror, fh=file_) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror, fh=file_)
def release(self): import msvcrt msvcrt.locking(self.fd, msvcrt.LK_UNLCK, 1) os.close(self.fd) self.fd = None
def _unlock_file(self): msvcrt.locking(self.fd.fileno(), msvcrt.LK_UNLCK, self._file_size())
def __exit__(self, *args): if have_fcntl: fcntl.flock(self.lockfile, fcntl.LOCK_UN) elif have_msvcrt: msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1) self.lockfile.close()
def TryLock(self, fd): """Raises IOError on failure.""" # pylint: disable=g-import-not-at-top import msvcrt # Exclusive lock, non-blocking msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
def __exit__(self, exc_type, exc_value, exc_tb): suppress = False if callable(self.callback): suppress = self.callback(exc_type, exc_value, exc_tb) msvcrt.locking(self.fileobj.fileno(), msvcrt.LK_UNLCK, self.size) return suppress