clutil 2011.138.0 → 2014.304.0

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.
data/cl/util/file.rb DELETED
@@ -1,312 +0,0 @@
1
- # $Id: file.rb,v 1.9 2010/04/26 02:11:27 chrismo Exp $
2
- =begin
3
- --------------------------------------------------------------------------
4
- Copyright (c) 2001 - 2002, Chris Morris
5
- All rights reserved.
6
-
7
- Redistribution and use in source and binary forms, with or without modification,
8
- are permitted provided that the following conditions are met:
9
-
10
- 1. Redistributions of source code must retain the above copyright notice, this
11
- list of conditions and the following disclaimer.
12
-
13
- 2. Redistributions in binary form must reproduce the above copyright notice,
14
- this list of conditions and the following disclaimer in the documentation and/or
15
- other materials provided with the distribution.
16
-
17
- 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
- software may be used to endorse or promote products derived from this software
19
- without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- --------------------------------------------------------------------------------
32
- (based on BSD Open Source License)
33
-
34
- code in copyTree includes code from rubycookbook.org under the following license:
35
- --------------------------------------------------------------------------------
36
- Copyright (c) Phil Tomson, Brian Takita
37
-
38
- All rights reserved.
39
-
40
- Permission is hereby granted, free of charge, to any person obtaining a copy of
41
- this software and associated documentation files (the "Software"), to deal in
42
- the Software without restriction, including without limitation the rights to
43
- use, copy, modify, merge, publish, distribute, and/or sell copies of the
44
- Software, and to permit persons to whom the Software is furnished to do so,
45
- provided that the above copyright notice(s) and this permission notice appear in
46
- all copies of the Software and that both the above copyright notice(s) and this
47
- permission notice appear in supporting documentation.
48
-
49
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
51
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT
52
- SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY
53
- CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
54
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
55
- CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
56
- WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
57
-
58
- Except as contained in this notice, the name of a copyright holder shall not be
59
- used in advertising or otherwise to promote the sale, use or other dealings in
60
- this Software without prior written authorization of the copyright holder.
61
- --------------------------------------------------------------------------------
62
- (X11 license)
63
- =end
64
-
65
- require 'fileutils'
66
-
67
- class Dir
68
- def Dir.empty?(dirname)
69
- Dir.entries(dirname).length == 2
70
- end
71
-
72
- def Dir.files(dirname)
73
- # Dir[dirname + '*'] does the same, except it includes the directory name
74
- result = []
75
- Dir.entries(dirname).each do |entry|
76
- result << entry if File.stat(File.join(dirname, entry)).file?
77
- end
78
- result
79
- end
80
- end
81
-
82
- class << File
83
- def extension(filename)
84
- res = filename.scan(/\.[^.]*$/)[0].to_s
85
- res.gsub!(/\./, '')
86
- res = nil if res.empty?
87
- res
88
- end
89
-
90
- def delete_all(*files)
91
- files.flatten!
92
- files.each do |file|
93
- # make writable to allow deletion
94
- File.chmod(0644, file)
95
- File.delete(file)
96
- end
97
- end
98
-
99
- # returns false if up-to-date check passed and the file was not copied.
100
- # returns true if up-to-date check failed and the file was copied.
101
- def backup(src, dst, bincompare=false)
102
- installed = false
103
- if (
104
- File.exists?(dst) &&
105
- (
106
- (File.stat(src).mtime != File.stat(dst).mtime) ||
107
- (File.stat(src).size != File.stat(dst).size)
108
- )
109
- ) || !File.exists?(dst) || bincompare
110
- FileUtils.install src, dst, :verbose => true
111
- installed = true
112
- end
113
-
114
- if !File.stat(dst).writable?
115
- File.chmod(0644, dst)
116
- set_ro = true
117
- else
118
- set_ro = false
119
- end
120
- match_mtime(src, dst)
121
- File.chmod(0444, dst) if set_ro
122
- return installed
123
- end
124
-
125
- # XP's utime appears to still have a bug when the file's time is within Daylight Saving Time, but
126
- # the current system time is outside Daylight Saving Time. This method will ensure any difference
127
- # is compensated for.
128
- def match_mtime(src, dst)
129
- new_atime = Time.now
130
- File.utime(new_atime, File.mtime(src), dst)
131
- if File.mtime(src) != File.mtime(dst)
132
- new_mtime = File.mtime(src)
133
- new_mtime += (File.mtime(src) - File.mtime(dst))
134
- File.utime(new_atime, new_mtime, dst)
135
- end
136
- end
137
-
138
- # human readable size
139
- def h_size(filename)
140
- size = File.size(filename)
141
- return '0B' if size == 0
142
- units = %w{B KB MB GB TB}
143
- e = (Math.log(size) / Math.log(1024)).floor
144
- s = "%.0f" % (size.to_f / 1024**e)
145
- s.sub(/\.?0*$/, units[e])
146
- end
147
- end
148
-
149
- class ClUtilFile
150
- def ClUtilFile.delTree(rootDirName, pattern='*')
151
- if (rootDirName == '/') or (rootDirName.empty?)
152
- raise 'Cannot delete root or empty?'
153
- end
154
-
155
- if !File.exists?(rootDirName)
156
- raise 'rootDirName does not exist'
157
- end
158
-
159
- # puts all sub-dirs and filenames in array.
160
- # Sub-dirs will be breadth first search.
161
- dirsAndFiles = Dir["#{rootDirName}/**/" + pattern]
162
-
163
- # delete all files first
164
- dirsAndFiles.each do | dirOrFileName |
165
- if FileTest.file?(dirOrFileName)
166
- if block_given?
167
- File.delete_all(dirOrFileName) if yield dirOrFileName
168
- else
169
- File.delete_all(dirOrFileName)
170
- end
171
- end
172
- end
173
-
174
- # go through array backward to delete dirs in proper order
175
- dirsAndFiles.reverse_each do | dirOrFileName |
176
- if FileTest.directory?(dirOrFileName)
177
- Dir.delete(dirOrFileName) if Dir.empty?(dirOrFileName)
178
- end
179
- end
180
-
181
- Dir.delete(rootDirName) if Dir.empty?(rootDirName)
182
- end
183
-
184
- =begin
185
- 1.8 FileUtils has cp_r in it ...
186
-
187
- def ClUtilFile.copyTree(fromDir, toDir, pattern='*')
188
- ##################################################################
189
- # cp_r - recursive copy
190
- # usage: cp_r(from,to,file_param,permissions)
191
- ##################################################################
192
- def cp_r(from,to,file_param=".*",permissions = 0644)
193
- Dir.mkdir(to) unless FileTest.exists?(to)
194
- Dir.chdir(to)
195
-
196
- dirContents = Dir["#{from}/*"]
197
- dirContents.each { |entry|
198
- file = File.basename(entry)
199
-
200
- if FileTest.directory?(entry)
201
- Dir.mkdir(file) unless FileTest.exists?(file)
202
- cp_r(entry,file,file_param)
203
- Dir.chdir("..") #get back to the dir we were working on
204
- else
205
- if file =~ file_param
206
- begin
207
- p = permissions
208
- if FileTest.executable?(entry)
209
- p = 0744 #make it executable
210
- end
211
- File.install(entry,file,p)
212
- rescue
213
- puts "could not copy #{entry} to #{file}"
214
- end
215
- end
216
- end
217
- }
218
- end #cp_r
219
- #example usage
220
- #cp_r("/usr/local","my_tmp",".*\.html?")
221
-
222
- end
223
- =end
224
- end
225
-
226
-
227
- class Dir
228
- # Dir.mktmpdir creates a temporary directory.
229
- #
230
- # [Copied from Ruby 1.9]
231
- #
232
- # The directory is created with 0700 permission.
233
- #
234
- # The prefix and suffix of the name of the directory is specified by
235
- # the optional first argument, <i>prefix_suffix</i>.
236
- # - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
237
- # - If it is a string, it is used as the prefix and no suffix is used.
238
- # - If it is an array, first element is used as the prefix and second element is used as a suffix.
239
- #
240
- # Dir.mktmpdir {|dir| dir is ".../d..." }
241
- # Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
242
- # Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
243
- #
244
- # The directory is created under Dir.tmpdir or
245
- # the optional second argument <i>tmpdir</i> if non-nil value is given.
246
- #
247
- # Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
248
- # Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
249
- #
250
- # If a block is given,
251
- # it is yielded with the path of the directory.
252
- # The directory and its contents are removed
253
- # using FileUtils.remove_entry_secure before Dir.mktmpdir returns.
254
- # The value of the block is returned.
255
- #
256
- # Dir.mktmpdir {|dir|
257
- # # use the directory...
258
- # open("#{dir}/foo", "w") { ... }
259
- # }
260
- #
261
- # If a block is not given,
262
- # The path of the directory is returned.
263
- # In this case, Dir.mktmpdir doesn't remove the directory.
264
- #
265
- # dir = Dir.mktmpdir
266
- # begin
267
- # # use the directory...
268
- # open("#{dir}/foo", "w") { ... }
269
- # ensure
270
- # # remove the directory.
271
- # FileUtils.remove_entry_secure dir
272
- # end
273
- #
274
- def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
275
- case prefix_suffix
276
- when nil
277
- prefix = "d"
278
- suffix = ""
279
- when String
280
- prefix = prefix_suffix
281
- suffix = ""
282
- when Array
283
- prefix = prefix_suffix[0]
284
- suffix = prefix_suffix[1]
285
- else
286
- raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
287
- end
288
- tmpdir ||= Dir.tmpdir
289
- t = Time.now.strftime("%Y%m%d")
290
- n = nil
291
- begin
292
- path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
293
- path << "-#{n}" if n
294
- path << suffix
295
- Dir.mkdir(path, 0700)
296
- rescue Errno::EEXIST
297
- n ||= 0
298
- n += 1
299
- retry
300
- end
301
-
302
- if block_given?
303
- begin
304
- yield path
305
- ensure
306
- FileUtils.remove_entry_secure path
307
- end
308
- else
309
- path
310
- end
311
- end
312
- end
data/cl/util/install.rb DELETED
@@ -1,117 +0,0 @@
1
- # $Id: install.rb,v 1.7 2002/09/09 03:04:38 chrismo Exp $
2
- =begin
3
- --------------------------------------------------------------------------
4
- Copyright (c) 2002-2010, Chris Morris
5
- All rights reserved.
6
-
7
- Redistribution and use in source and binary forms, with or without modification,
8
- are permitted provided that the following conditions are met:
9
-
10
- 1. Redistributions of source code must retain the above copyright notice, this
11
- list of conditions and the following disclaimer.
12
-
13
- 2. Redistributions in binary form must reproduce the above copyright notice,
14
- this list of conditions and the following disclaimer in the documentation and/or
15
- other materials provided with the distribution.
16
-
17
- 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
- software may be used to endorse or promote products derived from this software
19
- without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- --------------------------------------------------------------------------------
32
- =end
33
-
34
- require 'rbconfig'
35
- require 'find'
36
- require 'fileutils'
37
- require "#{File.dirname(__FILE__)}/console"
38
-
39
- module CLabs
40
- class Install
41
- include Config
42
-
43
- attr_reader :version, :libdir, :bindir, :sitedir, :siteverdir, :archdir
44
-
45
- def initialize
46
- init
47
- end
48
-
49
- # altered copy of rbconfig.rb -> Config::expand
50
- def custom_expand(val, conf)
51
- val.gsub!(/\$\(([^()]+)\)/) do |var|
52
- key = $1
53
- if CONFIG.key? key
54
- custom_expand(conf[key], conf)
55
- else
56
- var
57
- end
58
- end
59
- val
60
- end
61
-
62
- def get_conf(customizations)
63
- conf = {}
64
- MAKEFILE_CONFIG.each { |k, v| conf[k] = v.dup }
65
- customizations.each do |k, v|
66
- conf[k] = v
67
- end
68
- conf.each_value do |val|
69
- custom_expand(val, conf)
70
- end
71
- conf
72
- end
73
-
74
- def init
75
- prefixdir = get_switch('-p')
76
- customizations = {}
77
- customizations['prefix'] = prefixdir if prefixdir
78
- conf = get_conf(customizations)
79
-
80
- @version = conf["MAJOR"]+"."+conf["MINOR"]
81
- @libdir = File.join(conf["libdir"], "ruby", @version)
82
-
83
- @bindir = conf["bindir"]
84
- @sitedir = conf["sitedir"] || File.join(@libdir, "site_ruby")
85
- @siteverdir = File.join(@sitedir, @version)
86
- @archdir = File.join(@libdir, "i586-mswin32")
87
- end
88
-
89
- def install_executables(files)
90
- tmpfn = "cl_tmp"
91
- files.each do |aFile, dest|
92
- File.open(aFile) do |ip|
93
- File.open(tmpfn, "w") do |op|
94
- ruby = File.join($bindir, "ruby")
95
- op.puts "#!#{ruby}"
96
- op.write ip.read
97
- end
98
- end
99
-
100
- File::makedirs(dest)
101
- # 0755 I assume means read/write/execute, not needed for Windows
102
- File::chmod(0755, dest, true)
103
- File::install(tmpfn, File.join(dest, File.basename(aFile)), 0755, true)
104
- File::unlink(tmpfn)
105
- end
106
- end
107
-
108
- def install_lib(files)
109
- files.each do |aFile, dest|
110
- File::makedirs(dest)
111
- File::install(aFile, File.join(dest, File.basename(aFile)), 0644, true)
112
- end
113
- end
114
- end
115
- end
116
-
117
-
@@ -1,67 +0,0 @@
1
- # $Id: install.util.rb,v 1.8 2003/06/27 17:46:03 chrismo Exp $
2
- =begin
3
- --------------------------------------------------------------------------
4
- Copyright (c) 2002, Chris Morris
5
- All rights reserved.
6
-
7
- Redistribution and use in source and binary forms, with or without modification,
8
- are permitted provided that the following conditions are met:
9
-
10
- 1. Redistributions of source code must retain the above copyright notice, this
11
- list of conditions and the following disclaimer.
12
-
13
- 2. Redistributions in binary form must reproduce the above copyright notice,
14
- this list of conditions and the following disclaimer in the documentation and/or
15
- other materials provided with the distribution.
16
-
17
- 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
- software may be used to endorse or promote products derived from this software
19
- without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- --------------------------------------------------------------------------------
32
- =end
33
-
34
- require "#{File.dirname(__FILE__)}/install"
35
- init
36
-
37
- $cldir = File.join($siteverdir, "cl")
38
- $clutildir = File.join($cldir, "util")
39
- $clutiltestdir = File.join($clutildir, "test")
40
-
41
- $libfiles = { 'util.rb' => $cldir ,
42
-
43
- "file.rb" => $clutildir ,
44
- "win.rb" => $clutildir ,
45
- "dirsize.rb" => $clutildir ,
46
- "test.rb" => $clutildir ,
47
- "time.rb" => $clutildir ,
48
- "console.rb" => $clutildir ,
49
- "progress.rb" => $clutildir ,
50
- "install.rb" => $clutildir ,
51
- "smtp.rb" => $clutildir ,
52
- "string.rb" => $clutildir ,
53
- "version.rb" => $clutildir ,
54
- "clqsend.rb" => $bindir ,
55
-
56
- "./test/dirsizetest.rb" => $clutiltestdir,
57
- "./test/filetest.rb" => $clutiltestdir ,
58
- "./test/installtest.rb" => $clutiltestdir ,
59
- "./test/progresstest.rb" => $clutiltestdir,
60
- "./test/stringtest.rb" => $clutiltestdir,
61
- "./test/versiontest.rb" => $clutiltestdir,
62
- "./test/wintest.rb" => $clutiltestdir
63
- }
64
-
65
- if __FILE__ == $0
66
- install_lib($libfiles)
67
- end
data/cl/util/net.rb DELETED
@@ -1,35 +0,0 @@
1
- module ClUtilNet
2
- def ping(host)
3
- (`ping -n 1 #{host}` =~ "Request timed out") == nil
4
- end
5
- end
6
-
7
- class NetUse
8
- def initialize(unc, user='', password='')
9
- @unc = unc
10
- @user = user
11
- @password = password
12
- end
13
-
14
- def attached?
15
-
16
- end
17
-
18
- def attach(drive='')
19
-
20
- end
21
-
22
- def detach
23
-
24
- end
25
- end
26
-
27
- if __FILE__ == $0
28
- include ClUtilNet
29
-
30
- puts "Pinging 66.169.210.208"
31
- puts ping("66.169.210.208")
32
-
33
- puts "Pinging localhost"
34
- puts ping("localhost")
35
- end
data/cl/util/progress.rb DELETED
@@ -1,104 +0,0 @@
1
- # $Id: progress.rb,v 1.2 2003/09/12 14:53:50 chrismo Exp $
2
- =begin
3
- --------------------------------------------------------------------------
4
- Copyright (c) 2002, Chris Morris
5
- All rights reserved.
6
-
7
- Redistribution and use in source and binary forms, with or without modification,
8
- are permitted provided that the following conditions are met:
9
-
10
- 1. Redistributions of source code must retain the above copyright notice, this
11
- list of conditions and the following disclaimer.
12
-
13
- 2. Redistributions in binary form must reproduce the above copyright notice,
14
- this list of conditions and the following disclaimer in the documentation and/or
15
- other materials provided with the distribution.
16
-
17
- 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
- software may be used to endorse or promote products derived from this software
19
- without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- --------------------------------------------------------------------------------
32
- =end
33
-
34
- class Progress
35
- def initialize(total)
36
- @total = total
37
- @current = 0
38
- end
39
-
40
- def start
41
- @start = Time.now
42
- @in_place_called_already = false
43
- end
44
-
45
- def step
46
- @current += 1
47
- end
48
-
49
- def elapsed
50
- (Time.now - @start)
51
- end
52
-
53
- def format_seconds(seconds)
54
- minutes, seconds = seconds.divmod 60
55
- hours, minutes = minutes.divmod 60
56
- sprintf("%02d:%02d:%02d", hours, minutes, seconds)
57
- end
58
-
59
- def elapsedf
60
- format_seconds(elapsed)
61
- end
62
-
63
- def done
64
- @current
65
- end
66
-
67
- def remaining
68
- @total - @current
69
- end
70
-
71
- def est_remaining_time
72
- ((elapsed / done) * remaining)
73
- end
74
-
75
- def est_remaining_timef
76
- format_seconds(est_remaining_time)
77
- end
78
-
79
- def progress(doStep=false)
80
- step if doStep
81
- "#{@current.to_s}/#{@total.to_s} done. Elapsed: #{elapsedf} Est Remain: #{est_remaining_timef}"
82
- end
83
-
84
-
85
- def pct_done
86
- if @total > 0
87
- pct = ((done.to_f / @total.to_f) * 100).to_i
88
- else
89
- pct = 0
90
- end
91
-
92
- pct.to_s.rjust(3) + '%'
93
- end
94
-
95
- def in_place_pct(doStep=false)
96
- step if doStep
97
- rubout = 8.chr
98
- out = ''
99
- out << (rubout * 4) if @in_place_called_already
100
- out << pct_done
101
- @in_place_called_already = true
102
- out
103
- end
104
- end