示例#1
0
文件: pods.py 项目: clchiou/garage
def _lock_pod_dir_for_exec(pod_dir_path):
    """Lock pod directory.

    NOTE: This lock is not released/close on exec.
    """
    pod_dir_lock = locks.FileLock(pod_dir_path, close_on_exec=False)
    pod_dir_lock.acquire_exclusive()
示例#2
0
    def test_change_lock_type(self):
        lock_path = self.test_dir_path / 'lock'
        lock_path.touch()
        lock = locks.FileLock(lock_path)
        try:

            lock.acquire_shared()
            self.assertTrue(self.check_shared(lock_path))
            self.assertFalse(self.check_exclusive(lock_path))

            lock.acquire_exclusive()
            self.assertFalse(self.check_shared(lock_path))
            self.assertFalse(self.check_exclusive(lock_path))

            lock.acquire_shared()
            self.assertTrue(self.check_shared(lock_path))
            self.assertFalse(self.check_exclusive(lock_path))

            with self.using_shared(lock_path):
                with self.assertRaises(locks.NotLocked):
                    lock.acquire_exclusive()

        finally:
            lock.release()
            lock.close()
示例#3
0
 def test_release_after_close(self):
     lock_path = self.test_dir_path / 'lock'
     lock_path.touch()
     lock = locks.FileLock(lock_path)
     lock.close()
     with self.assertRaisesRegex(AssertionError, r'expect non-None value'):
         lock.release()
示例#4
0
 def test_remove_while_locked(self):
     lock_path = self.test_dir_path / 'lock'
     lock_path.touch()
     lock = locks.FileLock(lock_path)
     try:
         lock.acquire_shared()
         try:
             lock_path.unlink()
             self.assertFalse(lock_path.exists())
         finally:
             lock.release()
     finally:
         lock.close()
     self.assertFalse(lock_path.exists())
示例#5
0
文件: xars.py 项目: clchiou/garage
def cmd_exec(xar_name, xar_args):
    # Don't need root privilege here.
    with locks.acquiring_shared(_get_xars_repo_path()):
        xar_dir_path = ASSERT.predicate(_get_xar_dir_path(xar_name),
                                        Path.is_dir)
        exec_abspath = ASSERT.predicate(_get_exec_path(xar_dir_path),
                                        Path.exists).resolve()
        lock = locks.FileLock(
            _get_ref_path(xar_dir_path, _get_image_id(exec_abspath)),
            close_on_exec=False,
        )
        lock.acquire_shared()
    # TODO: Or should argv[0] be exec_abspath.name?
    argv = [xar_name]
    argv.extend(xar_args)
    LOG.debug('exec: path=%s, argv=%s', exec_abspath, argv)
    os.execv(str(exec_abspath), argv)
示例#6
0
    def do_test_lock(self, lock_path):
        lock = locks.FileLock(lock_path)
        try:

            self.assertTrue(self.check_shared(lock_path))
            self.assertTrue(self.check_exclusive(lock_path))

            lock.acquire_shared()
            try:
                self.assertTrue(self.check_shared(lock_path))
                self.assertFalse(self.check_exclusive(lock_path))
            finally:
                lock.release()

            self.assertTrue(self.check_shared(lock_path))
            self.assertTrue(self.check_exclusive(lock_path))

            lock.acquire_exclusive()
            try:
                self.assertFalse(self.check_shared(lock_path))
                self.assertFalse(self.check_exclusive(lock_path))
            finally:
                lock.release()

            self.assertTrue(self.check_shared(lock_path))
            self.assertTrue(self.check_exclusive(lock_path))

            with self.using_shared(lock_path):
                lock.acquire_shared()
                lock.release()
                with self.assertRaises(locks.NotLocked):
                    lock.acquire_exclusive()
                self.assertTrue(self.check_shared(lock_path))
                self.assertFalse(self.check_exclusive(lock_path))

            with self.using_exclusive(lock_path):
                with self.assertRaises(locks.NotLocked):
                    lock.acquire_shared()
                with self.assertRaises(locks.NotLocked):
                    lock.acquire_exclusive()
                self.assertFalse(self.check_shared(lock_path))
                self.assertFalse(self.check_exclusive(lock_path))

        finally:
            lock.close()
示例#7
0
def _using_tmp():
    tmp_dir_path = _get_tmp_path()
    tmp_path = None
    tmp_lock = None
    with locks.acquiring_exclusive(tmp_dir_path):
        try:
            tmp_path = Path(tempfile.mkdtemp(dir=tmp_dir_path))
            tmp_lock = locks.FileLock(tmp_path)
            tmp_lock.acquire_exclusive()
        except:
            if tmp_path:
                g1.files.remove(tmp_path)
            if tmp_lock:
                tmp_lock.release()
                tmp_lock.close()
            raise
    try:
        yield tmp_path
    finally:
        g1.files.remove(tmp_path)
        tmp_lock.release()
        tmp_lock.close()
示例#8
0
    def test_not_close_on_exec(self):
        @contextlib.contextmanager
        def using_dummy_proc():
            with subprocess.Popen(['cat'], close_fds=False) as proc:
                try:
                    proc.wait(0.01)  # Wait for ``cat`` to start up.
                except subprocess.TimeoutExpired:
                    pass
                try:
                    yield
                finally:
                    proc.kill()
                    proc.wait()

        lock_path = self.test_dir_path / 'lock'
        lock_path.touch()
        lock = locks.FileLock(lock_path, close_on_exec=False)
        try:

            self.assertTrue(self.check_shared(lock_path))
            self.assertTrue(self.check_exclusive(lock_path))

            lock.acquire_shared()
            self.assertTrue(self.check_shared(lock_path))
            self.assertFalse(self.check_exclusive(lock_path))

            with using_dummy_proc():
                lock.close()
                self.assertTrue(self.check_shared(lock_path))
                self.assertFalse(self.check_exclusive(lock_path))

            self.assertTrue(self.check_shared(lock_path))
            self.assertTrue(self.check_exclusive(lock_path))

        finally:
            lock.close()
示例#9
0
 def test_lock_not_exist(self):
     lock_path = self.test_dir_path / 'lock'
     self.assertFalse(lock_path.exists())
     with self.assertRaisesRegex(FileNotFoundError, str(lock_path)):
         locks.FileLock(lock_path)
示例#10
0
 def test_release_without_acquire(self):
     lock_path = self.test_dir_path / 'lock'
     lock_path.touch()
     lock = locks.FileLock(lock_path)
     lock.release()
     lock.close()