Ejemplo n.º 1
0
    def _testAsynchronousLockWaitCancel(self):
        scheduler = global_event_loop()
        tempdir = tempfile.mkdtemp()
        try:
            path = os.path.join(tempdir, 'lock_me')
            lock1 = AsynchronousLock(path=path, scheduler=scheduler)
            lock1.start()
            self.assertEqual(lock1.wait(), os.EX_OK)
            self.assertEqual(lock1.returncode, os.EX_OK)
            lock2 = AsynchronousLock(path=path,
                                     scheduler=scheduler,
                                     _force_async=True,
                                     _force_process=True)
            lock2.start()
            # lock2 should be waiting for lock1 to release
            self.assertEqual(lock2.poll(), None)
            self.assertEqual(lock2.returncode, None)

            # Cancel lock2 and then check wait() and returncode results.
            lock2.cancel()
            self.assertEqual(lock2.wait() == os.EX_OK, False)
            self.assertEqual(lock2.returncode == os.EX_OK, False)
            self.assertEqual(lock2.returncode is None, False)
            lock1.unlock()
        finally:
            shutil.rmtree(tempdir)
Ejemplo n.º 2
0
    def async_unlock(self):
        """
		Release the lock asynchronously. Release notification is available
		via the add_done_callback method of the returned Future instance.

		@returns: Future, result is None
		"""
        if self._lock_obj is not None:
            yield self._lock_obj.async_unlock()

            self._lock_obj = None
            self.locked = False
            self.settings.pop('PORTAGE_BUILDDIR_LOCKED', None)
            catdir_lock = AsynchronousLock(path=self._catdir,
                                           scheduler=self.scheduler)
            try:
                yield catdir_lock.async_start()
                yield catdir_lock.async_wait()
            except asyncio.CancelledError:
                if catdir_lock.poll() is None:
                    catdir_lock.cancel()
                raise

            if catdir_lock.returncode == os.EX_OK:
                try:
                    os.rmdir(self._catdir)
                except OSError:
                    pass
                yield catdir_lock.async_unlock()
Ejemplo n.º 3
0
	def _testAsynchronousLockWaitCancel(self):
		scheduler = global_event_loop()
		tempdir = tempfile.mkdtemp()
		try:
			path = os.path.join(tempdir, 'lock_me')
			lock1 = AsynchronousLock(path=path, scheduler=scheduler)
			lock1.start()
			self.assertEqual(lock1.wait(), os.EX_OK)
			self.assertEqual(lock1.returncode, os.EX_OK)
			lock2 = AsynchronousLock(path=path, scheduler=scheduler,
				_force_async=True, _force_process=True)
			lock2.start()
			# lock2 should be waiting for lock1 to release
			self.assertEqual(lock2.poll(), None)
			self.assertEqual(lock2.returncode, None)

			# Cancel lock2 and then check wait() and returncode results.
			lock2.cancel()
			self.assertEqual(lock2.wait() == os.EX_OK, False)
			self.assertEqual(lock2.returncode == os.EX_OK, False)
			self.assertEqual(lock2.returncode is None, False)
			lock1.unlock()
		finally:
			shutil.rmtree(tempdir)
Ejemplo n.º 4
0
    def async_lock(self):
        """
		Acquire the lock asynchronously. Notification is available
		via the add_done_callback method of the returned Future instance.

		This raises an AlreadyLocked exception if async_lock() is called
		while a lock is already held. In order to avoid this, call
		async_unlock() or check whether the "locked" attribute is True
		or False before calling async_lock().

		@returns: Future, result is None
		"""
        if self._lock_obj is not None:
            raise self.AlreadyLocked((self._lock_obj, ))

        dir_path = self.settings.get('PORTAGE_BUILDDIR')
        if not dir_path:
            raise AssertionError('PORTAGE_BUILDDIR is unset')
        catdir = os.path.dirname(dir_path)
        self._catdir = catdir

        try:
            portage.util.ensure_dirs(os.path.dirname(catdir),
                                     gid=portage.portage_gid,
                                     mode=0o70,
                                     mask=0)
        except PortageException:
            if not os.path.isdir(os.path.dirname(catdir)):
                raise

        catdir_lock = AsynchronousLock(path=catdir, scheduler=self.scheduler)
        builddir_lock = AsynchronousLock(path=dir_path,
                                         scheduler=self.scheduler)
        try:
            yield catdir_lock.async_start()
            yield catdir_lock.async_wait()

            self._assert_lock(catdir_lock)

            try:
                portage.util.ensure_dirs(catdir,
                                         gid=portage.portage_gid,
                                         mode=0o70,
                                         mask=0)
            except PortageException:
                if not os.path.isdir(catdir):
                    raise

            yield builddir_lock.async_start()
            yield builddir_lock.async_wait()
        except asyncio.CancelledError:
            if catdir_lock.poll() is None:
                catdir_lock.cancel()
            if builddir_lock.poll() is None:
                builddir_lock.cancel()
            raise

        try:
            self._assert_lock(builddir_lock)
        except AssertionError:
            yield catdir_lock.async_unlock()
            raise

        self._lock_obj = builddir_lock
        self.locked = True
        self.settings['PORTAGE_BUILDDIR_LOCKED'] = '1'
        yield catdir_lock.async_unlock()