[svn r37850] Split methods a bit to smaller parts.
--HG-- branch : trunk
This commit is contained in:
		
							parent
							
								
									026c2fa0bc
								
							
						
					
					
						commit
						4ffda926ab
					
				|  | @ -38,51 +38,36 @@ class RSync(object): | ||||||
|         channel.send((str(destdir), self._options)) |         channel.send((str(destdir), self._options)) | ||||||
|         self._channels[channel] = finishedcallback |         self._channels[channel] = finishedcallback | ||||||
| 
 | 
 | ||||||
|     def send(self, sourcedir): |     def _end_of_channel(self, channel): | ||||||
|         """ Sends a sourcedir to all added targets.  |  | ||||||
|         """ |  | ||||||
|         self._sourcedir = str(sourcedir) |  | ||||||
|         # normalize a trailing '/' away |  | ||||||
|         self._sourcedir = os.path.dirname(os.path.join(self._sourcedir, 'x')) |  | ||||||
|         # send directory structure and file timestamps/sizes |  | ||||||
|         self._send_directory_structure(self._sourcedir) |  | ||||||
| 
 |  | ||||||
|         # paths and to_send are only used for doing |  | ||||||
|         # progress-related callbacks |  | ||||||
|         self._paths = {} |  | ||||||
|         self._to_send = {} |  | ||||||
| 
 |  | ||||||
|         # send modified file to clients |  | ||||||
|         while self._channels: |  | ||||||
|             channel, req = self._receivequeue.get() |  | ||||||
|             if req is None: |  | ||||||
|                 # end-of-channel |  | ||||||
|         if channel in self._channels: |         if channel in self._channels: | ||||||
|             # too early!  we must have got an error |             # too early!  we must have got an error | ||||||
|             channel.waitclose() |             channel.waitclose() | ||||||
|             # or else we raise one |             # or else we raise one | ||||||
|             raise IOError('connection unexpectedly closed: %s ' % ( |             raise IOError('connection unexpectedly closed: %s ' % ( | ||||||
|                 channel.gateway,)) |                 channel.gateway,)) | ||||||
|             else: | 
 | ||||||
|                 command, data = req |     def _process_link(self, channel): | ||||||
|                 if command == "links": |  | ||||||
|         for link in self._links: |         for link in self._links: | ||||||
|             channel.send(link) |             channel.send(link) | ||||||
|             # completion marker, this host is done |             # completion marker, this host is done | ||||||
|         channel.send(42) |         channel.send(42) | ||||||
|                 elif command == "done": | 
 | ||||||
|  |     def _done(self, channel): | ||||||
|  |         """ Call all callbacks | ||||||
|  |         """ | ||||||
|         finishedcallback = self._channels.pop(channel) |         finishedcallback = self._channels.pop(channel) | ||||||
|         if finishedcallback: |         if finishedcallback: | ||||||
|             finishedcallback() |             finishedcallback() | ||||||
|                 elif command == "ack": | 
 | ||||||
|                     if self._callback: |     def _list_done(self, channel): | ||||||
|                         self._callback("ack", self._paths[data], channel) |  | ||||||
|                 elif command == "list_done": |  | ||||||
|         # sum up all to send |         # sum up all to send | ||||||
|         if self._callback: |         if self._callback: | ||||||
|             s = sum([self._paths[i] for i in self._to_send[channel]]) |             s = sum([self._paths[i] for i in self._to_send[channel]]) | ||||||
|             self._callback("list", s, channel) |             self._callback("list", s, channel) | ||||||
|                 elif command == "send": | 
 | ||||||
|  |     def _send_item(self, channel, data): | ||||||
|  |         """ Send one item | ||||||
|  |         """ | ||||||
|         modified_rel_path, checksum = data |         modified_rel_path, checksum = data | ||||||
|         modifiedpath = os.path.join(self._sourcedir, *modified_rel_path) |         modifiedpath = os.path.join(self._sourcedir, *modified_rel_path) | ||||||
|         try: |         try: | ||||||
|  | @ -113,6 +98,39 @@ class RSync(object): | ||||||
|                     channel.gateway.remoteaddress,  |                     channel.gateway.remoteaddress,  | ||||||
|                     modified_rel_path) |                     modified_rel_path) | ||||||
|         channel.send(data) |         channel.send(data) | ||||||
|  | 
 | ||||||
|  |     def send(self, sourcedir): | ||||||
|  |         """ Sends a sourcedir to all added targets.  | ||||||
|  |         """ | ||||||
|  |         self._sourcedir = str(sourcedir) | ||||||
|  |         # normalize a trailing '/' away | ||||||
|  |         self._sourcedir = os.path.dirname(os.path.join(self._sourcedir, 'x')) | ||||||
|  |         # send directory structure and file timestamps/sizes | ||||||
|  |         self._send_directory_structure(self._sourcedir) | ||||||
|  | 
 | ||||||
|  |         # paths and to_send are only used for doing | ||||||
|  |         # progress-related callbacks | ||||||
|  |         self._paths = {} | ||||||
|  |         self._to_send = {} | ||||||
|  | 
 | ||||||
|  |         # send modified file to clients | ||||||
|  |         while self._channels: | ||||||
|  |             channel, req = self._receivequeue.get() | ||||||
|  |             if req is None: | ||||||
|  |                 self._end_of_channel(channel) | ||||||
|  |             else: | ||||||
|  |                 command, data = req | ||||||
|  |                 if command == "links": | ||||||
|  |                     self._process_link(channel) | ||||||
|  |                 elif command == "done": | ||||||
|  |                     self._done(channel) | ||||||
|  |                 elif command == "ack": | ||||||
|  |                     if self._callback: | ||||||
|  |                         self._callback("ack", self._paths[data], channel) | ||||||
|  |                 elif command == "list_done": | ||||||
|  |                     self._list_done(channel) | ||||||
|  |                 elif command == "send": | ||||||
|  |                     self._send_item(channel, data) | ||||||
|                     del data |                     del data | ||||||
|                 else: |                 else: | ||||||
|                     assert "Unknown command %s" % command |                     assert "Unknown command %s" % command | ||||||
|  | @ -124,16 +142,7 @@ class RSync(object): | ||||||
|     def _send_link(self, basename, linkpoint): |     def _send_link(self, basename, linkpoint): | ||||||
|         self._links.append(("link", basename, linkpoint)) |         self._links.append(("link", basename, linkpoint)) | ||||||
| 
 | 
 | ||||||
|     def _send_directory_structure(self, path): |     def _send_directory(self, path): | ||||||
|         try: |  | ||||||
|             st = os.lstat(path) |  | ||||||
|         except OSError: |  | ||||||
|             self._broadcast((0, 0)) |  | ||||||
|             return |  | ||||||
|         if stat.S_ISREG(st.st_mode): |  | ||||||
|             # regular file: send a timestamp/size pair |  | ||||||
|             self._broadcast((st.st_mtime, st.st_size)) |  | ||||||
|         elif stat.S_ISDIR(st.st_mode): |  | ||||||
|         # dir: send a list of entries |         # dir: send a list of entries | ||||||
|         names = [] |         names = [] | ||||||
|         subpaths = [] |         subpaths = [] | ||||||
|  | @ -145,7 +154,8 @@ class RSync(object): | ||||||
|         self._broadcast(names) |         self._broadcast(names) | ||||||
|         for p in subpaths: |         for p in subpaths: | ||||||
|             self._send_directory_structure(p) |             self._send_directory_structure(p) | ||||||
|         elif stat.S_ISLNK(st.st_mode): | 
 | ||||||
|  |     def _send_link_structure(self, path): | ||||||
|         linkpoint = os.readlink(path) |         linkpoint = os.readlink(path) | ||||||
|         basename = path[len(self._sourcedir) + 1:] |         basename = path[len(self._sourcedir) + 1:] | ||||||
|         if not linkpoint.startswith(os.sep): |         if not linkpoint.startswith(os.sep): | ||||||
|  | @ -157,6 +167,20 @@ class RSync(object): | ||||||
|         else: |         else: | ||||||
|             self._send_link(basename, linkpoint) |             self._send_link(basename, linkpoint) | ||||||
|         self._broadcast(None) |         self._broadcast(None) | ||||||
|  | 
 | ||||||
|  |     def _send_directory_structure(self, path): | ||||||
|  |         try: | ||||||
|  |             st = os.lstat(path) | ||||||
|  |         except OSError: | ||||||
|  |             self._broadcast((0, 0)) | ||||||
|  |             return | ||||||
|  |         if stat.S_ISREG(st.st_mode): | ||||||
|  |             # regular file: send a timestamp/size pair | ||||||
|  |             self._broadcast((st.st_mtime, st.st_size)) | ||||||
|  |         elif stat.S_ISDIR(st.st_mode): | ||||||
|  |             self._send_directory(path) | ||||||
|  |         elif stat.S_ISLNK(st.st_mode): | ||||||
|  |             self._send_link_structure(path) | ||||||
|         else: |         else: | ||||||
|             raise ValueError, "cannot sync %r" % (path,) |             raise ValueError, "cannot sync %r" % (path,) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue