Python fcntl.LOCK_UN Examples

The following are 30 code examples of fcntl.LOCK_UN(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fcntl , or try the search function .
Example #1
Source File: dnx_file_operations.py    From dnxfirewall-cmd with GNU General Public License v3.0 7 votes vote down vote up
def __exit__(self, exc_type, exc_val, traceback):
        if (exc_type is None and self._data_written):
            os.replace(self._temp_file_path, self._config_file)
            os.chmod(self._config_file, 0o660)
            shutil.chown(self._config_file, user=USER, group=GROUP)
        else:
            self._temp_file.close()
            os.unlink(self._temp_file_path)
            if (self._Log):
                self._Log.error(f'configuration manager exiting with error: {exc_val}')

        # releasing lock for purposes specified in flock(1) man page under -u (unlock)
        fcntl.flock(self._config_lock, fcntl.LOCK_UN)
        self._config_lock.close()
        if (self._Log):
            self._Log.debug(f'file lock released for {self._file_name}')

        if (exc_type is not ValidationError):
            return True

    #will load json data from file, convert it to a python dict, then returned as object 
Example #2
Source File: util.py    From gist-alfred with MIT License 6 votes vote down vote up
def release(self):
        """Release the lock by deleting `self.lockfile`."""
        if not self._lock.is_set():
            return False

        try:
            fcntl.lockf(self._lockfile, fcntl.LOCK_UN)
        except IOError:  # pragma: no cover
            pass
        finally:
            self._lock.clear()
            self._lockfile = None
            try:
                os.unlink(self.lockfile)
            except (IOError, OSError):  # pragma: no cover
                pass

            return True 
Example #3
Source File: gtest_parallel.py    From gtest-parallel with Apache License 2.0 6 votes vote down vote up
def __exit__(self, exc_type, exc_value, traceback):
      # Flush any buffered data to disk. This is needed to prevent race
      # condition which happens from the moment of releasing file lock
      # till closing the file.
      self._fo.flush()

      try:
        if sys.platform == 'win32':
          self._fo.seek(0)
          msvcrt.locking(self._fo.fileno(), msvcrt.LK_UNLCK, 1)
        else:
          fcntl.flock(self._fo.fileno(), fcntl.LOCK_UN)
      finally:
        self._fo.close()

      return exc_value is None 
Example #4
Source File: pcache.py    From rucio with Apache License 2.0 6 votes vote down vote up
def unlock_file(self, name):
        f = self.locks.get(name)
        if not f:
            self.log(DEBUG, "unlock_file: %s not locked", name)
            return

        # XXXX does this create a possible race condition?
        if 0:
            try:
                os.unlink(name)
            except:
                pass
        status = fcntl.lockf(f, fcntl.LOCK_UN)
        f.close()
        del self.locks[name]
        return status 
Example #5
Source File: util.py    From wechat-alfred-workflow with MIT License 6 votes vote down vote up
def release(self):
        """Release the lock by deleting `self.lockfile`."""
        if not self._lock.is_set():
            return False

        try:
            fcntl.lockf(self._lockfile, fcntl.LOCK_UN)
        except IOError:  # pragma: no cover
            pass
        finally:
            self._lock.clear()
            self._lockfile = None
            try:
                os.unlink(self.lockfile)
            except (IOError, OSError):  # pragma: no cover
                pass

            return True 
Example #6
Source File: utils.py    From IDontSpeakSSL with GNU General Public License v3.0 6 votes vote down vote up
def udpate_status(status_file_path, module_data, module):
	lock_file_path = "{}.lock".format(status_file_path)
	while True:
		status_file_lock = open(lock_file_path, 'r')
		try:
			fcntl.flock(status_file_lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
			with open(status_file_path, "r+") as status_file:
				data = json.load(status_file)
				status_file.seek(0)
				data[module].update(module_data)
				json.dump(data, status_file)
			fcntl.flock(status_file_lock, fcntl.LOCK_UN)
			return
		except:
			time.sleep(0.1)
		status_file_lock.close() 
Example #7
Source File: util.py    From alfred-brightness with MIT License 6 votes vote down vote up
def release(self):
        """Release the lock by deleting `self.lockfile`."""
        if not self._lock.is_set():
            return False

        try:
            fcntl.lockf(self._lockfile, fcntl.LOCK_UN)
        except IOError:  # pragma: no cover
            pass
        finally:
            self._lock.clear()
            self._lockfile = None
            try:
                os.unlink(self.lockfile)
            except (IOError, OSError):  # pragma: no cover
                pass

            return True 
Example #8
Source File: repo.py    From pyolite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def users(self):
        if not self.path.exists():
            return []

        users = []
        with open(str(self.path)) as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            config = f.read()
            fcntl.flock(f, fcntl.LOCK_UN)
            for match in re.compile(r'=( *)(\w+)').finditer(config):
                users.append(match.group(2))

        return users 
Example #9
Source File: gcs_config.py    From gokart with MIT License 5 votes vote down vote up
def get_gcs_client(self) -> luigi.contrib.gcs.GCSClient:
        if (not os.path.isfile(self.discover_cache_local_path)):
            with open(self.discover_cache_local_path, "w") as f:
                try:
                    fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)

                    params = {"api": "storage", "apiVersion": "v1"}
                    discovery_http = build_http()
                    for discovery_url in (self._DISCOVERY_URI, self._V2_DISCOVERY_URI):
                        requested_url = uritemplate.expand(discovery_url, params)
                        try:
                            content = _retrieve_discovery_doc(
                                requested_url, discovery_http, False
                            )
                        except HttpError as e:
                            if e.resp.status == http_client.NOT_FOUND:
                                continue
                            else:
                                raise e
                        break
                    f.write(content)
                    fcntl.flock(f, fcntl.LOCK_UN)
                except IOError:
                    # try to read
                    pass

        with open(self.discover_cache_local_path, "r") as f:
            fcntl.flock(f, fcntl.LOCK_SH)
            descriptor = f.read()
            fcntl.flock(f, fcntl.LOCK_UN)
            return luigi.contrib.gcs.GCSClient(oauth_credentials=self._load_oauth_credentials(), descriptor=descriptor) 
Example #10
Source File: filelock.py    From veros with MIT License 5 votes vote down vote up
def _release(self):
        # Do not remove the lockfile:
        #
        #   https://github.com/benediktschmitt/py-filelock/issues/31
        #   https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
        fd = self._lock_file_fd
        self._lock_file_fd = None
        fcntl.flock(fd, fcntl.LOCK_UN)
        os.close(fd)
        return None

# Soft lock
# ~~~~~~~~~ 
Example #11
Source File: time-machine.py    From rsync-time-machine with GNU General Public License v2.0 5 votes vote down vote up
def flock_release(fd):
    ''' Release lock so next snapshots can continue '''

    fcntl.lockf(fd, fcntl.LOCK_UN)
    os.close(fd)
    os.remove(cfg['lock_file']) 
Example #12
Source File: utils.py    From youtube-dl-GUI with MIT License 5 votes vote down vote up
def _unlock_file(f):
            fcntl.flock(f, fcntl.LOCK_UN) 
Example #13
Source File: package.py    From builds with GNU General Public License v3.0 5 votes vote down vote up
def unlock(self):
        """
        Unlocks the package to allow other processes to operate on its
        shared resources.
        """
        if not self.locking_enabled:
            return

        LOG.debug("Unlocking file {}".format(self.lock_file_path))
        fcntl.lockf(self.lock_file, fcntl.LOCK_UN)
        self.lock_file.close() 
Example #14
Source File: ports.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def AllocateTestServerPort():
  """Allocates a port incrementally.

  Returns:
    Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and
    TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used.
  """
  port = 0
  ports_tried = []
  try:
    fp_lock = open(_TEST_SERVER_PORT_LOCKFILE, 'w')
    fcntl.flock(fp_lock, fcntl.LOCK_EX)
    # Get current valid port and calculate next valid port.
    if not os.path.exists(_TEST_SERVER_PORT_FILE):
      ResetTestServerPortAllocation()
    with open(_TEST_SERVER_PORT_FILE, 'r+') as fp:
      port = int(fp.read())
      ports_tried.append(port)
      while not IsHostPortAvailable(port):
        port += 1
        ports_tried.append(port)
      if (port > _TEST_SERVER_PORT_LAST or
          port < _TEST_SERVER_PORT_FIRST):
        port = 0
      else:
        fp.seek(0, os.SEEK_SET)
        fp.write('%d' % (port + 1))
  except Exception:  # pylint: disable=broad-except
    logger.exception('Error while allocating port')
  finally:
    if fp_lock:
      fcntl.flock(fp_lock, fcntl.LOCK_UN)
      fp_lock.close()
  if port:
    logger.info('Allocate port %d for test server.', port)
  else:
    logger.error('Could not allocate port for test server. '
                 'List of ports tried: %s', str(ports_tried))
  return port 
Example #15
Source File: repo.py    From pyolite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def replace(self, pattern, string):
        with open(str(self.path), 'r+') as f:
            fcntl.flock(f, fcntl.LOCK_EX)

            content = f.read()
            content = re.sub(pattern, string, content)

            f.seek(0)
            f.write(content)
            f.truncate()

            fcntl.flock(f, fcntl.LOCK_UN) 
Example #16
Source File: repo.py    From pyolite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def read(self):
        with open(str(self.path)) as f:
            fcntl.flock(f, fcntl.LOCK_EX)
            config = f.read()
            fcntl.flock(f, fcntl.LOCK_UN)

        return config 
Example #17
Source File: logs.py    From sonic_contest with MIT License 5 votes vote down vote up
def _set_locked(self, locked):
        if not self._use_locking:
            return
        import fcntl
        if locked:
            fcntl.lockf(self._file_desc, fcntl.LOCK_EX)
        else:
            fcntl.lockf(self._file_desc, fcntl.LOCK_UN) 
Example #18
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _UnlockImplPosix(target_file):
  fcntl.flock(target_file.fileno(), fcntl.LOCK_UN) 
Example #19
Source File: forwarder.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __exit__(self, _exception_type, _exception_value, traceback):
    fcntl.flock(self._fd, fcntl.LOCK_UN)
    os.close(self._fd) 
Example #20
Source File: dnx_iptables.py    From dnxfirewall-cmd with GNU General Public License v3.0 5 votes vote down vote up
def __exit__(self, exc_type, exc_val, traceback):
        if (exc_type is None):
            run(f'sudo iptables-save > {HOME_DIR}/dnx_system/iptables/iptables_backup.cnf', shell=True)

        fcntl.flock(self._iptables_lock, fcntl.LOCK_UN)
        self._iptables_lock.close()

        return True 
Example #21
Source File: forwarder.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __exit__(self, _exception_type, _exception_value, traceback):
    fcntl.flock(self._fd, fcntl.LOCK_UN)
    os.close(self._fd) 
Example #22
Source File: ports.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def AllocateTestServerPort():
  """Allocates a port incrementally.

  Returns:
    Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and
    TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used.
  """
  port = 0
  ports_tried = []
  try:
    fp_lock = open(_TEST_SERVER_PORT_LOCKFILE, 'w')
    fcntl.flock(fp_lock, fcntl.LOCK_EX)
    # Get current valid port and calculate next valid port.
    if not os.path.exists(_TEST_SERVER_PORT_FILE):
      ResetTestServerPortAllocation()
    with open(_TEST_SERVER_PORT_FILE, 'r+') as fp:
      port = int(fp.read())
      ports_tried.append(port)
      while not IsHostPortAvailable(port):
        port += 1
        ports_tried.append(port)
      if (port > _TEST_SERVER_PORT_LAST or
          port < _TEST_SERVER_PORT_FIRST):
        port = 0
      else:
        fp.seek(0, os.SEEK_SET)
        fp.write('%d' % (port + 1))
  except Exception:  # pylint: disable=broad-except
    logger.exception('Error while allocating port')
  finally:
    if fp_lock:
      fcntl.flock(fp_lock, fcntl.LOCK_UN)
      fp_lock.close()
  if port:
    logger.info('Allocate port %d for test server.', port)
  else:
    logger.error('Could not allocate port for test server. '
                 'List of ports tried: %s', str(ports_tried))
  return port 
Example #23
Source File: lock.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _UnlockImplPosix(target_file):
  fcntl.flock(target_file.fileno(), fcntl.LOCK_UN) 
Example #24
Source File: forwarder.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __exit__(self, _exception_type, _exception_value, traceback):
    fcntl.flock(self._fd, fcntl.LOCK_UN)
    os.close(self._fd) 
Example #25
Source File: ports.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def AllocateTestServerPort():
  """Allocates a port incrementally.

  Returns:
    Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and
    TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used.
  """
  port = 0
  ports_tried = []
  try:
    fp_lock = open(_TEST_SERVER_PORT_LOCKFILE, 'w')
    fcntl.flock(fp_lock, fcntl.LOCK_EX)
    # Get current valid port and calculate next valid port.
    if not os.path.exists(_TEST_SERVER_PORT_FILE):
      ResetTestServerPortAllocation()
    with open(_TEST_SERVER_PORT_FILE, 'r+') as fp:
      port = int(fp.read())
      ports_tried.append(port)
      while not IsHostPortAvailable(port):
        port += 1
        ports_tried.append(port)
      if (port > _TEST_SERVER_PORT_LAST or
          port < _TEST_SERVER_PORT_FIRST):
        port = 0
      else:
        fp.seek(0, os.SEEK_SET)
        fp.write('%d' % (port + 1))
  except Exception:  # pylint: disable=broad-except
    logger.exception('Error while allocating port')
  finally:
    if fp_lock:
      fcntl.flock(fp_lock, fcntl.LOCK_UN)
      fp_lock.close()
  if port:
    logger.info('Allocate port %d for test server.', port)
  else:
    logger.error('Could not allocate port for test server. '
                 'List of ports tried: %s', str(ports_tried))
  return port 
Example #26
Source File: utils.py    From tvalacarta with GNU General Public License v3.0 5 votes vote down vote up
def _unlock_file(f):
            fcntl.flock(f, fcntl.LOCK_UN) 
Example #27
Source File: helpers.py    From appetite with Apache License 2.0 5 votes vote down vote up
def __exit__(self, type, value, tb): # pylint: disable=redefined-builtin
        """Exit RunSingleInstance class
        :return: None
        """
        try:
            if not self.__is_running:
                fcntl.lockf(self.__filelock, fcntl.LOCK_UN)
                self.__filelock.close()
                os.unlink(self.__lockfile)
        except Exception as err:
            logger.error("Error unlocking single instance file", error=err.message) 
Example #28
Source File: context_managers.py    From zulip with Apache License 2.0 5 votes vote down vote up
def flock(lockfile: Union[int, IO[Any]], shared: bool=False) -> Iterator[None]:
    """Lock a file object using flock(2) for the duration of a 'with' statement.

       If shared is True, use a LOCK_SH lock, otherwise LOCK_EX."""

    fcntl.flock(lockfile, fcntl.LOCK_SH if shared else fcntl.LOCK_EX)
    try:
        yield
    finally:
        fcntl.flock(lockfile, fcntl.LOCK_UN) 
Example #29
Source File: UserFile.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def unlock(self, length=0, start=0, whence=0):
        self._lock(fcntl.LOCK_UN, length, start, whence) 
Example #30
Source File: test_influxdb_integration.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def test_multi_finder_index_build(self):
        """Test index build lock with multiple finders"""
        num_finders = 50
        fh = open(FILE_LOCK, 'w')
        del self.finder
        self.config['influxdb']['reindex_interval'] = 0
        finders = []
        for _ in range(num_finders):
            _finder = influxgraph.InfluxDBFinder(self.config)
            time.sleep(.001)
            finders.append(_finder)
        try:
            self.assertRaises(IOError, fcntl.flock,
                              fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except AssertionError:
            i = 0
            while i < 50:
                i += 1
                fcntl.flock(fh, fcntl.LOCK_UN)
                try:
                    self.assertRaises(IOError, fcntl.flock,
                                      fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
                except AssertionError:
                    continue
        finally:
            fcntl.flock(fh, fcntl.LOCK_UN)
            fh.close()
            os.unlink(FILE_LOCK)