def test_01_04_read_and_write(self): attrs = gpcf.get_file_attributes(self.temp) for flag in (gpcf.FILE_ATTRIBUTE_HIDDEN, gpcf.FILE_ATTRIBUTE_READONLY, gpcf.FILE_ATTRIBUTE_TEMPORARY): gpcf.set_file_attributes(self.temp, attrs ^ flag) new_attrs = gpcf.get_file_attributes(self.temp) self.assertEqual(new_attrs, attrs ^ flag) gpcf.set_file_attributes(self.temp, attrs)
def test_01_03_set_readonly(self): attrs = gpcf.get_file_attributes(self.temp) gpcf.set_file_attributes(self.temp, attrs | gpcf.FILE_ATTRIBUTE_READONLY) self.assertRaises(IOError, lambda: open(self.temp, "w")) gpcf.set_file_attributes(self.temp, attrs & ~gpcf.FILE_ATTRIBUTE_READONLY)
def test_01_03_set_readonly(self): attrs = gpcf.get_file_attributes(self.temp) gpcf.set_file_attributes( self.temp, attrs | gpcf.FILE_ATTRIBUTE_READONLY) self.assertRaises(IOError, lambda :open(self.temp, "w")) gpcf.set_file_attributes( self.temp, attrs & ~ gpcf.FILE_ATTRIBUTE_READONLY)
def lock_file(path, timeout=3): """Lock a file path - path to the file timeout - timeout in seconds when waiting for announcement returns True if we obtained the lock, False if the file is already owned. """ lock_path = get_lock_path(path) start_boundary() uid = uuid.uuid4().hex try: fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_RDWR) with os.fdopen(fd, "a") as f: f.write(the_boundary.external_request_address + "\n" + uid) if sys.platform == "win32": # Hide file in Windows. attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs | FILE_ATTRIBUTE_HIDDEN) except OSError as e: if e.errno != errno.EEXIST: raise logger.info("Lockfile for %s already exists - contacting owner" % path) with open(lock_path, "r") as f: remote_address = f.readline().strip() remote_uid = f.readline().strip() if len(remote_address) > 0 and len(remote_uid) > 0: logger.info("Owner is %s" % remote_address) request_socket = the_boundary.zmq_context.socket(zmq.REQ) request_socket.setsockopt(zmq.LINGER, 0) assert isinstance(request_socket, zmq.Socket) request_socket.connect(remote_address) lock_request = LockStatusRequest(remote_uid) lock_request.send_only(request_socket) poller = zmq.Poller() poller.register(request_socket, zmq.POLLIN) keep_polling = True while keep_polling: keep_polling = False for socket, status in poller.poll(timeout * 1000): keep_polling = True if socket == request_socket and status == zmq.POLLIN: lock_response = lock_request.recv(socket) if isinstance(lock_response, LockStatusReply): if lock_response.locked: logger.info("%s is locked" % path) return False keep_polling = False # # Fall through if we believe that the other end is dead # if sys.platform == "win32": # I'm not a patient person. Python open apparently can't # open hidden files - are you kidding? It's some screwed up # who's on first scenario between Windows and Python "it's hidden # so when I go to see if it's there, it's not but when I open it # it is there!". Another 15 minutes of my life down the drain. # attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs & ~FILE_ATTRIBUTE_HIDDEN) with open(lock_path, "w") as f: f.write(the_boundary.request_address + "\n" + uid) if sys.platform == "win32": attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs | FILE_ATTRIBUTE_HIDDEN) # # The coast is clear to lock # q = Queue.Queue() start_lock_thread() __lock_queue.put((None, LOCK_REQUEST, (uid, path), q)) q.get() return True
def lock_file(path, timeout=3): '''Lock a file path - path to the file timeout - timeout in seconds when waiting for announcement returns True if we obtained the lock, False if the file is already owned. ''' lock_path = get_lock_path(path) start_boundary() uid = uuid.uuid4().hex try: fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_RDWR) with os.fdopen(fd, "a") as f: f.write(the_boundary.external_request_address + "\n" + uid) if sys.platform == "win32": # Hide file in Windows. attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs | FILE_ATTRIBUTE_HIDDEN) except OSError as e: if e.errno != errno.EEXIST: raise logger.info("Lockfile for %s already exists - contacting owner" % path) with open(lock_path, "r") as f: remote_address = f.readline().strip() remote_uid = f.readline().strip() if len(remote_address) > 0 and len(remote_uid) > 0: logger.info("Owner is %s" % remote_address) request_socket = the_boundary.zmq_context.socket(zmq.REQ) request_socket.setsockopt(zmq.LINGER, 0) assert isinstance(request_socket, zmq.Socket) request_socket.connect(remote_address) lock_request = LockStatusRequest(remote_uid) lock_request.send_only(request_socket) poller = zmq.Poller() poller.register(request_socket, zmq.POLLIN) keep_polling = True while (keep_polling): keep_polling = False for socket, status in poller.poll(timeout * 1000): keep_polling = True if socket == request_socket and status == zmq.POLLIN: lock_response = lock_request.recv(socket) if isinstance(lock_response, LockStatusReply): if lock_response.locked: logger.info("%s is locked" % path) return False keep_polling = False # # Fall through if we believe that the other end is dead # if sys.platform == "win32": # I'm not a patient person. Python open apparently can't # open hidden files - are you kidding? It's some screwed up # who's on first scenario between Windows and Python "it's hidden # so when I go to see if it's there, it's not but when I open it # it is there!". Another 15 minutes of my life down the drain. # attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs & ~FILE_ATTRIBUTE_HIDDEN) with open(lock_path, "w") as f: f.write(the_boundary.request_address + "\n" + uid) if sys.platform == "win32": attrs = get_file_attributes(lock_path) set_file_attributes(lock_path, attrs | FILE_ATTRIBUTE_HIDDEN) # # The coast is clear to lock # q = Queue.Queue() start_lock_thread() __lock_queue.put((None, LOCK_REQUEST, (uid, path), q)) q.get() return True
def test_01_02_not_readonly(self): attrs = gpcf.get_file_attributes(self.temp) self.assertEqual(attrs & gpcf.FILE_ATTRIBUTE_READONLY, 0)