moserp-s3sync 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ # This software code is made available "AS IS" without warranties of any
2
+ # kind. You may copy, display, modify and redistribute the software
3
+ # code either by itself or as incorporated into your code; provided that
4
+ # you do not remove any proprietary notices. Your use of this software
5
+ # code is at your own risk and you waive any claim against the author
6
+ # with respect to your use of this software code.
7
+ # (c) 2007 s3sync.net
8
+ #
9
+
10
+ # The purpose of this file is to overlay the net/http library
11
+ # to add some functionality
12
+ # (without changing the file itself or requiring a specific version)
13
+ # It still isn't perfectly robust, i.e. if radical changes are made
14
+ # to the underlying lib this stuff will need updating.
15
+
16
+ require 'net/http'
17
+
18
+ module Net
19
+
20
+ $HTTPStreamingDebug = false
21
+
22
+ # Allow request body to be an IO stream
23
+ # Allow an IO stream argument to stream the response body out
24
+ class HTTP
25
+ alias _HTTPStreaming_request request
26
+
27
+ def request(req, body = nil, streamResponseBodyTo = nil, &block)
28
+ if not block_given? and streamResponseBodyTo and streamResponseBodyTo.respond_to?(:write)
29
+ $stderr.puts "Response using streaming" if $HTTPStreamingDebug
30
+ # this might be a retry, we should make sure the stream is at its beginning
31
+ streamResponseBodyTo.rewind if streamResponseBodyTo.respond_to?(:rewind) and streamResponseBodyTo != $stdout
32
+ block = proc do |res|
33
+ res.read_body do |chunk|
34
+ streamResponseBodyTo.write(chunk)
35
+ end
36
+ end
37
+ end
38
+ if body != nil && body.respond_to?(:read)
39
+ $stderr.puts "Request using streaming" if $HTTPStreamingDebug
40
+ # this might be a retry, we should make sure the stream is at its beginning
41
+ body.rewind if body.respond_to?(:rewind)
42
+ req.body_stream = body
43
+ return _HTTPStreaming_request(req, nil, &block)
44
+ else
45
+ return _HTTPStreaming_request(req, body, &block)
46
+ end
47
+ end
48
+ end
49
+
50
+ end #module
51
+
52
+ module S3sync
53
+ class ProgressStream < SimpleDelegator
54
+ def initialize(s, size=0)
55
+ @start = @last = Time.new
56
+ @total = size
57
+ @transferred = 0
58
+ @closed = false
59
+ @printed = false
60
+ @innerStream = s
61
+ super(@innerStream)
62
+ __setobj__(@innerStream)
63
+ end
64
+ # need to catch reads and writes so we can count what's being transferred
65
+ def read(i)
66
+ res = @innerStream.read(i)
67
+ @transferred += res.respond_to?(:length) ? res.length : 0
68
+ now = Time.new
69
+ if(now - @last > 1) # don't do this oftener than once per second
70
+ @printed = true
71
+ begin
72
+ $stdout.printf("\rProgress: %db %db/s %s ", @transferred, (@transferred/(now - @start)).floor,
73
+ @total > 0? (100 * @transferred/@total).floor.to_s + "%" : ""
74
+ )
75
+ rescue FloatDomainError
76
+ #wtf?
77
+ end
78
+ $stdout.flush
79
+ @last = now
80
+ end
81
+ res
82
+ end
83
+ def write(s)
84
+ @transferred += s.length
85
+ res = @innerStream.write(s)
86
+ now = Time.new
87
+ if(now -@last > 1) # don't do this oftener than once per second
88
+ @printed = true
89
+ $stdout.printf("\rProgress: %db %db/s %s ", @transferred, (@transferred/(now - @start)).floor,
90
+ @total > 0? (100 * @transferred/@total).floor.to_s + "%" : ""
91
+ )
92
+ $stdout.flush
93
+ @last = now
94
+ end
95
+ res
96
+ end
97
+ def rewind()
98
+ @transferred = 0
99
+ @innerStream.rewind if @innerStream.respond_to?(:rewind)
100
+ end
101
+ def close()
102
+ $stdout.printf("\n") if @printed and not @closed
103
+ @closed = true
104
+ @innerStream.close
105
+ end
106
+ end
107
+ end #module