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()
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()
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()
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())
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)
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()
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()
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()
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)
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()