79 | | for source, storage in finder.list(ignore_patterns): |
80 | | self.copy_file(source, storage, **options) |
| 79 | for path, storage in finder.list(ignore_patterns): |
| 80 | # Prefix the relative path if the source storage contains it |
| 81 | if getattr(storage, 'prefix', None): |
| 82 | prefixed_path = os.path.join(storage.prefix, path) |
| 83 | else: |
| 84 | prefixed_path = path |
| 85 | if symlink: |
| 86 | self.link_file(path, prefixed_path, storage, **options) |
| 87 | else: |
| 88 | self.copy_file(path, prefixed_path, storage, **options) |
101 | | def copy_file(self, source, source_storage, **options): |
102 | | """ |
103 | | Attempt to copy (or symlink) ``source`` to ``destination``, |
104 | | returning True if successful. |
105 | | """ |
106 | | source_path = source_storage.path(source) |
107 | | try: |
108 | | source_last_modified = source_storage.modified_time(source) |
109 | | except (OSError, NotImplementedError): |
110 | | source_last_modified = None |
111 | | if getattr(source_storage, 'prefix', None): |
112 | | destination = os.path.join(source_storage.prefix, source) |
113 | | else: |
114 | | destination = source |
| 109 | def delete_file(self, path, prefixed_path, source_storage, **options): |
| 110 | # Whether we are in symlink mode |
134 | | destination_is_link = (self.destination_local and |
135 | | os.path.islink(self.destination_storage.path(destination))) |
136 | | if destination_last_modified >= source_last_modified: |
137 | | if (not symlink and not destination_is_link): |
138 | | self.log("Skipping '%s' (not modified)" % destination) |
139 | | self.unmodified_files.add(destination) |
140 | | return False |
141 | | if dry_run: |
142 | | self.log("Pretending to delete '%s'" % destination) |
| 121 | try: |
| 122 | # When was the source file modified last time? |
| 123 | source_last_modified = source_storage.modified_time(path) |
| 124 | except (OSError, NotImplementedError): |
| 125 | pass |
| 126 | else: |
| 127 | # The full path of the target file |
| 128 | full_path = self.storage.path(prefixed_path) |
| 129 | # Skip the file if the source file is younger |
| 130 | if target_last_modified >= source_last_modified: |
| 131 | if not ((symlink and not os.path.islink(full_path)) or |
| 132 | (not symlink and os.path.islink(full_path))): |
| 133 | if prefixed_path not in self.unmodified_files: |
| 134 | self.unmodified_files.append(prefixed_path) |
| 135 | self.log("Skipping '%s' (not modified)" % path) |
| 136 | return False |
| 137 | # Then delete the existing file if really needed |
| 138 | if options['dry_run']: |
| 139 | self.log("Pretending to delete '%s'" % path) |
147 | | if symlink: |
148 | | destination_path = self.destination_storage.path(destination) |
149 | | if dry_run: |
150 | | self.log("Pretending to link '%s' to '%s'" % |
151 | | (source_path, destination_path), level=1) |
152 | | else: |
153 | | self.log("Linking '%s' to '%s'" % |
154 | | (source_path, destination_path), level=1) |
| 145 | def link_file(self, path, prefixed_path, source_storage, **options): |
| 146 | """ |
| 147 | Attempt to link ``path`` |
| 148 | """ |
| 149 | # Skip this file if it was already copied earlier |
| 150 | if prefixed_path in self.symlinked_files: |
| 151 | return self.log("Skipping '%s' (already linked earlier)" % path) |
| 152 | # Delete the target file if needed or break |
| 153 | if not self.delete_file(path, prefixed_path, source_storage, **options): |
| 154 | return |
| 155 | # The full path of the source file |
| 156 | source_path = source_storage.path(path) |
| 157 | # Finally link the file |
| 158 | if options['dry_run']: |
| 159 | self.log("Pretending to link '%s'" % source_path, level=1) |
| 160 | else: |
| 161 | self.log("Linking '%s'" % source_path, level=1) |
| 162 | full_path = self.storage.path(prefixed_path) |
| 163 | try: |
| 164 | os.makedirs(os.path.dirname(full_path)) |
| 165 | except OSError: |
| 166 | pass |
| 167 | os.symlink(source_path, full_path) |
| 168 | if prefixed_path not in self.symlinked_files: |
| 169 | self.symlinked_files.append(prefixed_path) |
| 170 | |
| 171 | def copy_file(self, path, prefixed_path, source_storage, **options): |
| 172 | """ |
| 173 | Attempt to copy ``path`` with storage |
| 174 | """ |
| 175 | # Skip this file if it was already copied earlier |
| 176 | if prefixed_path in self.copied_files: |
| 177 | return self.log("Skipping '%s' (already copied earlier)" % path) |
| 178 | # Delete the target file if needed or break |
| 179 | if not self.delete_file(path, prefixed_path, source_storage, **options): |
| 180 | return |
| 181 | # The full path of the source file |
| 182 | source_path = source_storage.path(path) |
| 183 | # Finally start copying |
| 184 | if options['dry_run']: |
| 185 | self.log("Pretending to copy '%s'" % source_path, level=1) |
| 186 | else: |
| 187 | self.log("Copying '%s'" % source_path, level=1) |
| 188 | if self.local: |
| 189 | full_path = self.storage.path(prefixed_path) |
166 | | if self.destination_local: |
167 | | destination_path = self.destination_storage.path(destination) |
168 | | try: |
169 | | os.makedirs(os.path.dirname(destination_path)) |
170 | | except OSError: |
171 | | pass |
172 | | shutil.copy2(source_path, destination_path) |
173 | | self.log("Copying '%s' to '%s'" % |
174 | | (source_path, destination_path), level=1) |
175 | | else: |
176 | | source_file = source_storage.open(source) |
177 | | self.destination_storage.save(destination, source_file) |
178 | | self.log("Copying %s to %s" % |
179 | | (source_path, destination), level=1) |
180 | | self.copied_files.add(destination) |
181 | | return True |
| 196 | source_file = source_storage.open(path) |
| 197 | self.storage.save(prefixed_path, source_file) |
| 198 | if not prefixed_path in self.copied_files: |
| 199 | self.copied_files.append(prefixed_path) |