def test_context(self): """Test of context handling.""" lock = FileLock(self.testfile) lock2 = FileLock(self.testfile, timeout=0) with lock: self.assertTrue(lock.is_locked) self.assertTrue(lock.check_lock()) self.assertRaises(FileLockException, lock2.acquire) self.assertFalse(lock.is_locked) self.assertFalse(lock.check_lock())
def test_lock(self): """Basic locking test.""" lock = FileLock(self.testfile) lock.acquire() self.assertTrue(lock.is_locked) self.assertTrue(lock.check_lock()) lock.release() self.assertFalse(lock.is_locked) self.assertFalse(lock.check_lock())
def second_lock(self): lock = FileLock(self.testfile, timeout=0) try: lock.acquire() except FileLockException: sys.exit(1) lock.release()
def test_stale(self): """Handling of stale lock files.""" lock = FileLock(self.testfile) lockfile = open(lock.lockfile, 'w') lock.acquire() lockfile.close() lock.release()
def __init__(self, path, branch=None, component=None, local=False): self.path = path if branch is None: self.branch = self.default_branch else: self.branch = branch self.component = component self.last_output = '' self.lock = FileLock(self.path.rstrip('/').rstrip('\\') + '.lock', timeout=120) if not local: # Create ssh wrapper for possible use create_ssh_wrapper() if not self.is_valid(): self.init()
def test_process(self): """Test of double locking.""" lock = FileLock(self.testfile) lock.acquire() try: process = Process(target=self.second_lock) process.start() process.join() self.assertEqual(process.exitcode, 1) finally: lock.release() process = Process(target=self.second_lock) process.start() process.join() self.assertEqual(process.exitcode, 0)
def ready(self): # Configure merge driver for Gettext PO # We need to do this behind lock to avoid errors when servers # start in parallel lockfile = FileLock(os.path.join(data_dir('home'), 'gitlock')) with lockfile: try: GitRepository.global_setup() except RepositoryException as error: add_configuration_error( 'Git global setup', 'Failed to do git setup: {0}'.format(error)) # Use it for *.po by default configdir = os.path.join(data_dir('home'), '.config', 'git') configfile = os.path.join(configdir, 'attributes') if not os.path.exists(configfile): if not os.path.exists(configdir): os.makedirs(configdir) with open(configfile, 'w') as handle: handle.write('*.po merge=weblate-merge-gettext-po\n')
def test_double(self): """Test of double locking.""" lock1 = FileLock(self.testfile) lock2 = FileLock(self.testfile, timeout=0) lock1.acquire() self.assertRaises(FileLockException, lock2.acquire) lock1.release() lock2.acquire() lock2.release()
def test_lock_invalid(self): """Basic locking test.""" lock = FileLock(os.path.join(self.tempdir, 'invalid', 'lock', 'path')) self.assertRaises(OSError, lock.acquire)