Class: S3Sync::SyncCommand
- Inherits:
-
Object
- Object
- S3Sync::SyncCommand
- Defined in:
- lib/s3sync/sync.rb
Class Method Summary collapse
- .cmp(hash1, hash2) ⇒ Object
- .parse_params(args) ⇒ Object
- .process_destination(source, destination) ⇒ Object
- .process_file_destination(source, destination, file = "") ⇒ Object
- .remote_prefix?(prefix) ⇒ Boolean
Instance Method Summary collapse
- #download_files(destination, source, list) ⇒ Object
-
#initialize(args, source, destination) ⇒ SyncCommand
constructor
A new instance of SyncCommand.
- #read_tree_remote(location) ⇒ Object
- #read_trees(source, destination) ⇒ Object
- #remove_files(remote, list) ⇒ Object
- #remove_local_files(destination, source, list) ⇒ Object
- #run ⇒ Object
- #upload_files(remote, list) ⇒ Object
Constructor Details
#initialize(args, source, destination) ⇒ SyncCommand
Returns a new instance of SyncCommand.
174 175 176 177 178 |
# File 'lib/s3sync/sync.rb', line 174 def initialize args, source, destination @args = args @source = source @destination = destination end |
Class Method Details
.cmp(hash1, hash2) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/s3sync/sync.rb', line 155 def self.cmp hash1, hash2 same, to_add_to_2 = [], [] hash1.each do |key, value| value2 = hash2.delete key if value2.nil? to_add_to_2 << value elsif value2 == value same << value else to_add_to_2 << value end end to_remove_from_2 = hash2.values [same, to_add_to_2, to_remove_from_2] end |
.parse_params(args) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/s3sync/sync.rb', line 211 def self.parse_params args # Reading the arbitrary parameters from the command line and getting # modifiable copies to parse source, destination = args; return nil if source.nil? or destination.nil? # Sync from one s3 to another is currently not supported if remote_prefix? source and remote_prefix? destination raise WrongUsage.new(nil, 'Both arguments can\'t be on S3') end # C'mon, there's rsync out there if !remote_prefix? source and !remote_prefix? destination raise WrongUsage.new(nil, 'One argument must be on S3') end source, destination = process_destination source, destination return [Location.new(*source), Location.new(*destination)] end |
.process_destination(source, destination) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/s3sync/sync.rb', line 269 def self.process_destination source, destination source, destination = source.dup, destination.dup # don't repeat slashes source.squeeze! '/' destination.squeeze! '/' # Making sure that local paths won't break our stuff later source.gsub!(/^\.\//, '') destination.gsub!(/^\.\//, '') # Parsing the final destination destination = process_file_destination source, destination, "" # here's where we find out what direction we're going source_is_s3 = remote_prefix? source # canonicalize the S3 stuff remote_prefix = source_is_s3 ? source : destination bucket, remote_prefix = remote_prefix.split ":" remote_prefix ||= "" # Just making sure we preserve the direction if source_is_s3 [[remote_prefix, bucket], destination] else [source, [remote_prefix, bucket]] end end |
.process_file_destination(source, destination, file = "") ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/s3sync/sync.rb', line 236 def self.process_file_destination source, destination, file="" if not file.empty? sub = (remote_prefix? source) ? source.split(":")[1] : source file = file.gsub(/^#{sub}/, '') end # no slash on end of source means we need to append the last src dir to # dst prefix testing for empty isn't good enough here.. needs to be # "empty apart from potentially having 'bucket:'" if source =~ %r{/$} if remote_prefix? destination and destination.end_with? ':' S3Sync.safe_join [destination, file] else File.join [destination, file] end else if remote_prefix? source _, name = source.split ":" File.join [destination, File.basename(name || ""), file] else source = /^\/?(.*)/.match(source)[1] # Corner case: the root of the remote path is empty, we don't want to # add an unnecessary slash here. if destination.end_with? ':' File.join [destination + source, file] else File.join [destination, source, file] end end end end |
.remote_prefix?(prefix) ⇒ Boolean
230 231 232 233 234 |
# File 'lib/s3sync/sync.rb', line 230 def self.remote_prefix?(prefix) # allow for dos-like things e.g. C:\ to be treated as local even with # colon. prefix.include? ':' and not prefix.match '^[A-Za-z]:[\\\\/]' end |
Instance Method Details
#download_files(destination, source, list) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/s3sync/sync.rb', line 351 def download_files destination, source, list puts list list.each {|e| path = File.join destination.path, e.path if @args.verbose puts " + #{source}#{e.path} => #{path}" end unless @args.dry_run obj = @args.s3.buckets[source.bucket].objects[e.path] # Making sure this new file will have a safe shelter FileUtils.mkdir_p File.dirname(path) # in some cases the s3 object will have a trailing '/' indicating # a folder (this behavior noticed when the s3 folder is # created by Transmit) if path[-1] == '/' FileUtils.mkdir_p path else # Downloading and saving the files File.open(path, 'wb') do |file| begin obj.read do |chunk| file.write chunk end rescue AWS::Core::Http::NetHttpHandler::TruncatedBodyError => e $stderr.puts "WARNING: (retryable) TruncatedBodyError occured, retrying in a second #{file.basename}" sleep 1 retry end end end end } end |
#read_tree_remote(location) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/s3sync/sync.rb', line 300 def read_tree_remote location dir = location.path dir += '/' if not dir.empty? and not dir.end_with?('/') nodes = {} @args.s3.buckets[location.bucket].objects.with_prefix(dir || "").to_a.collect do |obj| # etag comes back with quotes (obj.etag.inspcet # => "\"abc...def\"" small_comparator = lambda { obj.etag[/[a-z0-9]+/] } node = Node.new(location.path, obj.key, obj.content_length, small_comparator) nodes[node.path] = node end return nodes end |
#read_trees(source, destination) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/s3sync/sync.rb', line 314 def read_trees source, destination if source.local? source_tree = LocalDirectory.new(source.path).list_files destination_tree = read_tree_remote destination else source_tree = read_tree_remote source destination_tree = LocalDirectory.new(destination.path).list_files end [source_tree, destination_tree] end |
#remove_files(remote, list) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/s3sync/sync.rb', line 339 def remove_files remote, list if @args.verbose list.each {|e| puts " - #{remote}#{e.path}" } end unless @args.dry_run @args.s3.buckets[remote.bucket].objects.delete_if { |obj| list.map(&:path).include? obj.key } end end |
#remove_local_files(destination, source, list) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/s3sync/sync.rb', line 389 def remove_local_files destination, source, list list.each {|e| path = File.join destination.path, e.path if @args.verbose puts " * #{e.path} => #{path}" end unless @args.dry_run FileUtils.rm_rf path end } end |
#run ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/s3sync/sync.rb', line 180 def run # Reading the source and destination using our helper method if (source, destination, bucket = self.class.parse_params [@source, @destination]).nil? raise WrongUsage.new(nil, 'Need a source and a destination') end # Getting the trees source_tree, destination_tree = read_trees source, destination # Getting the list of resources to be exchanged between the two peers _, to_add, to_remove = self.class.cmp source_tree, destination_tree # Removing the items matching the exclude pattern if requested to_add.select! { |e| begin (e.path =~ /#{@args.exclude}/).nil? rescue RegexpError => exc raise WrongUsage.new nil, exc. end } if @args.exclude # Calling the methods that perform the actual IO if source.local? upload_files destination, to_add remove_files destination, to_remove unless @args.keep else download_files destination, source, to_add remove_local_files destination, source, to_remove unless @args.keep end end |
#upload_files(remote, list) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/s3sync/sync.rb', line 326 def upload_files remote, list list.each do |e| if @args.verbose puts " + #{e.full} => #{remote}#{e.path}" end unless @args.dry_run remote_path = "#{remote.path}#{e.path}" @args.s3.buckets[remote.bucket].objects[remote_path].write Pathname.new(e.full), :acl => @args.acl end end end |