clutil 2010.127.0
Sign up to get free protection for your applications and to get access to all the features.
- data/cl/util/clqsend.rb +86 -0
- data/cl/util/console.rb +60 -0
- data/cl/util/cvs.maint.rb +41 -0
- data/cl/util/decide.rb +69 -0
- data/cl/util/dirsize.rb +144 -0
- data/cl/util/doc/License +14 -0
- data/cl/util/doc/Readme +22 -0
- data/cl/util/file.rb +210 -0
- data/cl/util/install.rb +107 -0
- data/cl/util/install.util.rb +67 -0
- data/cl/util/net.rb +15 -0
- data/cl/util/progress.rb +104 -0
- data/cl/util/smtp.rb +131 -0
- data/cl/util/string.rb +65 -0
- data/cl/util/test.rb +92 -0
- data/cl/util/test/dirsizetest.rb +138 -0
- data/cl/util/test/filetest.rb +178 -0
- data/cl/util/test/installtest.rb +12 -0
- data/cl/util/test/progresstest.rb +43 -0
- data/cl/util/test/stringtest.rb +50 -0
- data/cl/util/test/versiontest.rb +72 -0
- data/cl/util/test/wintest.rb +106 -0
- data/cl/util/time.rb +35 -0
- data/cl/util/util.rb +10 -0
- data/cl/util/version.rb +115 -0
- data/cl/util/win.rb +351 -0
- metadata +81 -0
data/cl/util/clqsend.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# $Id: clqsend.rb,v 1.1 2002/04/18 13:44:38 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
|
+
=end
|
33
|
+
require 'cl/util/smtp'
|
34
|
+
|
35
|
+
def getSwitch(switch)
|
36
|
+
i = ARGV.index(switch)
|
37
|
+
if i != nil
|
38
|
+
ARGV[i + 1]
|
39
|
+
else
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def showhelp
|
45
|
+
puts '-h Help'
|
46
|
+
puts '-t To'
|
47
|
+
puts '-f From'
|
48
|
+
puts '-bf file name of body'
|
49
|
+
puts '-s Subject'
|
50
|
+
puts '-i SMTP ip address or server name'
|
51
|
+
end
|
52
|
+
|
53
|
+
if ARGV.include?('-h')
|
54
|
+
showhelp
|
55
|
+
else
|
56
|
+
to = getSwitch('-t')
|
57
|
+
subj = getSwitch('-s')
|
58
|
+
from = getSwitch('-f')
|
59
|
+
bodyfn = getSwitch('-bf')
|
60
|
+
smtpsrv = getSwitch('-i')
|
61
|
+
params = {
|
62
|
+
:from.to_s => from,
|
63
|
+
:to.to_s => to,
|
64
|
+
:subj.to_s => subj,
|
65
|
+
:bodyfn.to_s => bodyfn,
|
66
|
+
:smtpsrv.to_s => smtpsrv
|
67
|
+
}
|
68
|
+
anymissing = false
|
69
|
+
params.each do |key, value|
|
70
|
+
if value == nil
|
71
|
+
puts 'missing param <' + key + '>'
|
72
|
+
anymissing = true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
body = File.readlines(bodyfn).join
|
77
|
+
|
78
|
+
if !anymissing
|
79
|
+
puts "sending To:#{to} From:#{from} Subj:#{subj}"
|
80
|
+
puts body
|
81
|
+
sendmail(to, from, subj, body, smtpsrv)
|
82
|
+
else
|
83
|
+
puts
|
84
|
+
showhelp
|
85
|
+
end
|
86
|
+
end
|
data/cl/util/console.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# $Id: console.rb,v 1.4 2002/10/15 21:00:58 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
|
+
=end
|
33
|
+
|
34
|
+
def ifSwitch(switch)
|
35
|
+
ARGV.index(switch) != nil
|
36
|
+
end
|
37
|
+
|
38
|
+
alias :if_switch :ifSwitch
|
39
|
+
|
40
|
+
def getSwitch(switch)
|
41
|
+
value = nil
|
42
|
+
i = ARGV.index(switch)
|
43
|
+
value = ARGV[i + 1] if i != nil
|
44
|
+
end
|
45
|
+
|
46
|
+
alias :get_switch :getSwitch
|
47
|
+
|
48
|
+
def yn_prompt(text, &blk)
|
49
|
+
custom_prompt(text + '? (Y/[N]):', /y/i, @yesToAll, &blk)
|
50
|
+
end
|
51
|
+
|
52
|
+
def custom_prompt(text, execReply=/y/i, autoYield=false)
|
53
|
+
if !autoYield
|
54
|
+
print text
|
55
|
+
reply = $stdin.gets.chomp
|
56
|
+
else
|
57
|
+
puts text
|
58
|
+
end
|
59
|
+
yield reply if reply =~ execReply || autoYield
|
60
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'sn/util/smtp'
|
2
|
+
|
3
|
+
# the commit command will add new folders. then 2nd time this is run
|
4
|
+
# files in that folder will be addressed.
|
5
|
+
|
6
|
+
$output = ''
|
7
|
+
|
8
|
+
def do_puts(text)
|
9
|
+
$output << text << "\n"
|
10
|
+
puts text
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
begin
|
15
|
+
cvscmd = "..\\..\\cvs\\wincvs\\cvs.exe"
|
16
|
+
r = `#{cvscmd} -n up`; do_puts r
|
17
|
+
a = r.split("\n"); do_puts a.inspect
|
18
|
+
a.collect! { |item| item.slice(2..item.length) if item[0..0] == "?" }; do_puts a.inspect
|
19
|
+
a.compact!; do_puts a.inspect
|
20
|
+
outstanding_files = (a.length > 0)
|
21
|
+
if outstanding_files
|
22
|
+
cmd = "#{cvscmd} add " + a.join(" "); do_puts cmd
|
23
|
+
r = `#{cmd}`; do_puts r
|
24
|
+
raise 'cmd returned error' if $? != 0
|
25
|
+
end
|
26
|
+
cmd = "#{cvscmd} commit -F cvs.log.txt"; do_puts cmd
|
27
|
+
r = `#{cmd}`; do_puts r
|
28
|
+
raise 'cmd returned error' if $? != 0
|
29
|
+
end while outstanding_files
|
30
|
+
end
|
31
|
+
|
32
|
+
to = 'chris.morris@snelling.com'
|
33
|
+
from = to
|
34
|
+
subj = 'Wiki Cvs Update'
|
35
|
+
begin
|
36
|
+
execute
|
37
|
+
sendmail(to, from, subj, $output)
|
38
|
+
rescue Exception => e
|
39
|
+
body = $output << "\n" << e.message << "\n" << e.backtrace.join("\n")
|
40
|
+
sendmail(to, from, subj + ' ERROR', body)
|
41
|
+
end
|
data/cl/util/decide.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
class Integer
|
2
|
+
def odd?
|
3
|
+
self.divmod(2)[1] != 0
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
def decide(items)
|
8
|
+
round = 1
|
9
|
+
while items.length > 1
|
10
|
+
items.sort! { rand(3) - 1 }
|
11
|
+
|
12
|
+
pairs = []
|
13
|
+
gate = 0
|
14
|
+
items.each { |item|
|
15
|
+
pairs << [item] if gate == 0
|
16
|
+
pairs[-1] << item if gate == 1
|
17
|
+
gate = gate ^ 1
|
18
|
+
}
|
19
|
+
|
20
|
+
puts "=" * 20
|
21
|
+
puts "Round #{round} :: #{pairs.length} pairs".center(20)
|
22
|
+
puts "=" * 20
|
23
|
+
|
24
|
+
pairs.each do |pair|
|
25
|
+
begin
|
26
|
+
puts "1) #{pair[0]}"
|
27
|
+
puts "2) #{pair[1]}"
|
28
|
+
print "keep: "
|
29
|
+
keeper = gets.chomp
|
30
|
+
end while keeper !~ /[12]/
|
31
|
+
delete = (keeper.to_i - 1) ^ 1
|
32
|
+
items.delete(pair[delete])
|
33
|
+
end
|
34
|
+
round += 1
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "Winner is: #{items[0]}"
|
38
|
+
end
|
39
|
+
|
40
|
+
if __FILE__ == $0
|
41
|
+
def do_test
|
42
|
+
puts "Odd number item list test"
|
43
|
+
items = ["a", "b", "c", "d", "e"]
|
44
|
+
decide(items)
|
45
|
+
|
46
|
+
puts "Even number item list test"
|
47
|
+
items = ["a", "b", "c", "d"]
|
48
|
+
decide(items)
|
49
|
+
end
|
50
|
+
|
51
|
+
list_fn = ARGV[0]
|
52
|
+
ARGV.clear
|
53
|
+
|
54
|
+
if list_fn =~ /test/i
|
55
|
+
do_test
|
56
|
+
exit
|
57
|
+
end
|
58
|
+
|
59
|
+
if list_fn.nil? || !File.exists?(list_fn)
|
60
|
+
puts "Usage: #{File.basename(__FILE__)} [list_filename]"
|
61
|
+
exit
|
62
|
+
end
|
63
|
+
|
64
|
+
items = File.readlines(list_fn)
|
65
|
+
items.delete_if { |item| item.chomp.empty? }
|
66
|
+
items.compact!
|
67
|
+
decide(items)
|
68
|
+
end
|
69
|
+
|
data/cl/util/dirsize.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
=begin
|
2
|
+
--------------------------------------------------------------------------
|
3
|
+
Copyright (c) 2001, Chris Morris
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without modification,
|
7
|
+
are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation and/or
|
14
|
+
other materials provided with the distribution.
|
15
|
+
|
16
|
+
3. Neither the names Chris Morris, cLabs nor the names of contributors to this
|
17
|
+
software may be used to endorse or promote products derived from this software
|
18
|
+
without specific prior written permission.
|
19
|
+
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
28
|
+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
29
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
--------------------------------------------------------------------------------
|
31
|
+
=end
|
32
|
+
|
33
|
+
=begin
|
34
|
+
uses Classes;
|
35
|
+
|
36
|
+
type
|
37
|
+
{ future, make a cache/factory object for TDirSizes, so we don't have to
|
38
|
+
recalc dirs we've already sized }
|
39
|
+
|
40
|
+
TDirSize = class(TObject)
|
41
|
+
private
|
42
|
+
@diskSpace: Int64;
|
43
|
+
@fileCount: integer;
|
44
|
+
@fileSize: Int64;
|
45
|
+
FClusterSize: Int64;
|
46
|
+
FDirectory: string;
|
47
|
+
FParent: TDirSize;
|
48
|
+
|
49
|
+
FChildDirs: TList;
|
50
|
+
public
|
51
|
+
constructor Create;
|
52
|
+
destructor Destroy; override;
|
53
|
+
|
54
|
+
function AvgFileSize(IncludeSubs: boolean): Int64;
|
55
|
+
function DiskSpace(IncludeSubs: boolean): Int64;
|
56
|
+
function FileCount(IncludeSubs: boolean): integer;
|
57
|
+
function FileSize(IncludeSubs: boolean): Int64;
|
58
|
+
procedure GetSize;
|
59
|
+
function UnusedDiskSpace(IncludeSubs: boolean): Int64;
|
60
|
+
|
61
|
+
property ChildDirs: TList read FChildDirs;
|
62
|
+
property ClusterSize: Int64 read FClusterSize write FClusterSize;
|
63
|
+
property Directory: string read FDirectory write FDirectory;
|
64
|
+
property Parent: TDirSize read FParent;
|
65
|
+
end
|
66
|
+
=end
|
67
|
+
|
68
|
+
class DirSize
|
69
|
+
attr_reader :childDirs, :exception
|
70
|
+
attr_accessor :clusterSize, :directory, :parent
|
71
|
+
|
72
|
+
def avgFileSize(includeSubs=false)
|
73
|
+
aFileCount = fileCount(includeSubs)
|
74
|
+
if aFileCount != 0
|
75
|
+
fileSize(includeSubs) / aFileCount
|
76
|
+
else
|
77
|
+
0
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def initialize
|
82
|
+
@childDirs = []
|
83
|
+
@fileSize = 0
|
84
|
+
@diskSpace = 0
|
85
|
+
@fileCount = 0
|
86
|
+
end
|
87
|
+
|
88
|
+
def diskSpace(includeSubs=false)
|
89
|
+
total = 0
|
90
|
+
@childDirs.each do |adir|
|
91
|
+
total += adir.diskSpace(includeSubs)
|
92
|
+
end if includeSubs
|
93
|
+
total + @diskSpace
|
94
|
+
end
|
95
|
+
|
96
|
+
def fileCount(includeSubs=false)
|
97
|
+
result = @fileCount
|
98
|
+
@childDirs.each do |adir|
|
99
|
+
result += adir.fileCount(includeSubs)
|
100
|
+
end if includeSubs
|
101
|
+
result
|
102
|
+
end
|
103
|
+
|
104
|
+
def fileSize(includeSubs=false)
|
105
|
+
total = 0
|
106
|
+
@childDirs.each do |adir|
|
107
|
+
total += adir.fileSize(includeSubs)
|
108
|
+
end if includeSubs
|
109
|
+
total + @fileSize
|
110
|
+
end
|
111
|
+
|
112
|
+
def getSize(&block)
|
113
|
+
@fileSize = 0
|
114
|
+
@diskSpace = 0
|
115
|
+
@fileCount = 0
|
116
|
+
begin
|
117
|
+
Dir.foreach(@directory) do |entry|
|
118
|
+
if (entry != '.') && (entry != '..')
|
119
|
+
entry = File.join(@directory, entry)
|
120
|
+
if (File.directory?(entry))
|
121
|
+
childDirSize = DirSize.new
|
122
|
+
childDirSize.parent = self
|
123
|
+
childDirSize.directory = entry
|
124
|
+
childDirSize.clusterSize = @clusterSize
|
125
|
+
childDirSize.getSize(&block)
|
126
|
+
@childDirs << childDirSize
|
127
|
+
else
|
128
|
+
if !block_given? || yield(entry)
|
129
|
+
@fileCount += 1
|
130
|
+
@fileSize += File.size(entry)
|
131
|
+
@diskSpace += ((@fileSize / @clusterSize) * @clusterSize) + @clusterSize
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
rescue Exception => e
|
137
|
+
@exception = e
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def unusedDiskSpace(includeSubs=false)
|
142
|
+
diskSpace(includeSubs) - fileSize(includeSubs)
|
143
|
+
end
|
144
|
+
end
|
data/cl/util/doc/License
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
--------------------------------------------------------------------------------
|
2
|
+
Copyright (c) 2001-2010, Chris Morris
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
8
|
+
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
10
|
+
|
11
|
+
3. Neither the names Chris Morris, cLabs nor the names of contributors to this software may be used to endorse or promote products derived from this software without specific prior written permission.
|
12
|
+
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
--------------------------------------------------------------------------------
|
data/cl/util/doc/Readme
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
=cLabs Ruby Utilities
|
2
|
+
|
3
|
+
Home Page:: http://clabs.org/ruby.htm
|
4
|
+
|
5
|
+
===Overview
|
6
|
+
|
7
|
+
A set of miscellaneous utilities for Ruby.
|
8
|
+
|
9
|
+
===License
|
10
|
+
|
11
|
+
Copyright (c) 2001-2010, Chris Morris (mailto:chrismo@clabs.com). BSD license.
|
12
|
+
|
13
|
+
===Usage
|
14
|
+
|
15
|
+
RDoc docs in future versions. For now, browse the code and unit tests.
|
16
|
+
|
17
|
+
===Change Log
|
18
|
+
|
19
|
+
* [hrm - probably out of date]
|
20
|
+
* win.rb: system_return_exitcode added, since the system cmd with a
|
21
|
+
ruby.exe built with MSVC does not set $?
|
22
|
+
* version.rb: execute the file itself to get template code in stdout
|
data/cl/util/file.rb
ADDED
@@ -0,0 +1,210 @@
|
|
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 'ftools'
|
66
|
+
#require 'cl/util/win'
|
67
|
+
|
68
|
+
class Dir
|
69
|
+
def Dir.empty?(dirname)
|
70
|
+
Dir.entries(dirname).length == 2
|
71
|
+
end
|
72
|
+
|
73
|
+
def Dir.files(dirname)
|
74
|
+
# Dir[dirname + '*'] does the same, except it includes the directory name
|
75
|
+
result = []
|
76
|
+
Dir.entries(dirname).each do |entry|
|
77
|
+
result << entry if File.stat(File.join(dirname, entry)).file?
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class << File
|
84
|
+
def extension(filename)
|
85
|
+
res = filename.scan(/\.[^.]*$/).to_s
|
86
|
+
res.gsub!(/\./, '')
|
87
|
+
res = nil if res.empty?
|
88
|
+
res
|
89
|
+
end
|
90
|
+
|
91
|
+
def delete_all(*files)
|
92
|
+
files.flatten!
|
93
|
+
files.each do |file|
|
94
|
+
# make writable to allow deletion
|
95
|
+
File.chmod(0644, file)
|
96
|
+
File.delete(file)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def backup(src, dst, bincompare=false)
|
101
|
+
if (
|
102
|
+
File.exists?(dst) &&
|
103
|
+
(
|
104
|
+
(File.stat(src).mtime != File.stat(dst).mtime) ||
|
105
|
+
(File.stat(src).size != File.stat(dst).size)
|
106
|
+
)
|
107
|
+
) || !File.exists?(dst) || bincompare
|
108
|
+
File.install src, dst, nil, true
|
109
|
+
end
|
110
|
+
|
111
|
+
if !File.stat(dst).writable?
|
112
|
+
File.chmod(0644, dst)
|
113
|
+
set_ro = true
|
114
|
+
else
|
115
|
+
set_ro = false
|
116
|
+
end
|
117
|
+
new_atime = Time.now
|
118
|
+
File.utime(new_atime, File.stat(src).mtime, dst)
|
119
|
+
File.chmod(0444, dst) if set_ro
|
120
|
+
end
|
121
|
+
|
122
|
+
# human readable size
|
123
|
+
def h_size(filename)
|
124
|
+
size = File.size(filename)
|
125
|
+
return '0B' if size == 0
|
126
|
+
units = %w{B KB MB GB TB}
|
127
|
+
e = (Math.log(size) / Math.log(1024)).floor
|
128
|
+
s = "%.0f" % (size.to_f / 1024**e)
|
129
|
+
s.sub(/\.?0*$/, units[e])
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
module ClUtilFile
|
134
|
+
ALL_FILES = '*'
|
135
|
+
|
136
|
+
def ClUtilFile.delTree(rootDirName, pattern=ALL_FILES)
|
137
|
+
if (rootDirName == '/') or (rootDirName.empty?)
|
138
|
+
raise 'Cannot delete root or empty?'
|
139
|
+
end
|
140
|
+
|
141
|
+
if !File.exists?(rootDirName)
|
142
|
+
raise 'rootDirName does not exist'
|
143
|
+
end
|
144
|
+
|
145
|
+
# puts all sub-dirs and filenames in array.
|
146
|
+
# Sub-dirs will be breadth first search.
|
147
|
+
dirsAndFiles = Dir["#{rootDirName}/**/" + pattern]
|
148
|
+
|
149
|
+
# delete all files first
|
150
|
+
dirsAndFiles.each do | dirOrFileName |
|
151
|
+
if FileTest.file?(dirOrFileName)
|
152
|
+
if block_given?
|
153
|
+
File.delete_all(dirOrFileName) if yield dirOrFileName
|
154
|
+
else
|
155
|
+
File.delete_all(dirOrFileName)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# go through array backward to delete dirs in proper order
|
161
|
+
dirsAndFiles.reverse_each do | dirOrFileName |
|
162
|
+
if FileTest.directory?(dirOrFileName)
|
163
|
+
Dir.delete(dirOrFileName) if Dir.empty?(dirOrFileName)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
Dir.delete(rootDirName) if Dir.empty?(rootDirName)
|
168
|
+
end
|
169
|
+
|
170
|
+
=begin
|
171
|
+
1.8 FileUtils has cp_r in it ...
|
172
|
+
|
173
|
+
def ClUtilFile.copyTree(fromDir, toDir, pattern=ALL_FILES)
|
174
|
+
##################################################################
|
175
|
+
# cp_r - recursive copy
|
176
|
+
# usage: cp_r(from,to,file_param,permissions)
|
177
|
+
##################################################################
|
178
|
+
def cp_r(from,to,file_param=".*",permissions = 0644)
|
179
|
+
Dir.mkdir(to) unless FileTest.exists?(to)
|
180
|
+
Dir.chdir(to)
|
181
|
+
|
182
|
+
dirContents = Dir["#{from}/*"]
|
183
|
+
dirContents.each { |entry|
|
184
|
+
file = File.basename(entry)
|
185
|
+
|
186
|
+
if FileTest.directory?(entry)
|
187
|
+
Dir.mkdir(file) unless FileTest.exists?(file)
|
188
|
+
cp_r(entry,file,file_param)
|
189
|
+
Dir.chdir("..") #get back to the dir we were working on
|
190
|
+
else
|
191
|
+
if file =~ file_param
|
192
|
+
begin
|
193
|
+
p = permissions
|
194
|
+
if FileTest.executable?(entry)
|
195
|
+
p = 0744 #make it executable
|
196
|
+
end
|
197
|
+
File.install(entry,file,p)
|
198
|
+
rescue
|
199
|
+
puts "could not copy #{entry} to #{file}"
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
}
|
204
|
+
end #cp_r
|
205
|
+
#example usage
|
206
|
+
#cp_r("/usr/local","my_tmp",".*\.html?")
|
207
|
+
|
208
|
+
end
|
209
|
+
=end
|
210
|
+
end
|