riel 1.1.7 → 1.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/riel/ansicolor.rb +3 -3
- data/lib/riel/array.rb +1 -1
- data/lib/riel/date.rb +1 -1
- data/lib/riel/dir.rb +7 -7
- data/lib/riel/env.rb +1 -4
- data/lib/riel/file.rb +27 -29
- data/lib/riel/filetype.rb +16 -17
- data/lib/riel/log/logger.rb +1 -0
- data/lib/riel/optproc.rb +33 -67
- data/lib/riel/rcfile.rb +2 -2
- data/lib/riel/regexp.rb +9 -9
- data/lib/riel/setdiff.rb +5 -5
- data/lib/riel/size_converter.rb +10 -10
- data/lib/riel/string.rb +5 -5
- data/lib/riel/tempfile.rb +0 -4
- data/lib/riel/text.rb +6 -6
- data/lib/riel/timer.rb +8 -8
- data/lib/riel.rb +1 -1
- data/test/riel/log_stack_test.rb +124 -0
- metadata +7 -5
data/lib/riel/ansicolor.rb
CHANGED
@@ -64,12 +64,12 @@ module ANSIColor
|
|
64
64
|
|
65
65
|
# returns the code for the given background color(s)
|
66
66
|
def ANSIColor.background bgcolor
|
67
|
-
make_code
|
67
|
+
make_code "on_" + bgcolor
|
68
68
|
end
|
69
69
|
|
70
70
|
# returns the code for the given foreground color(s)
|
71
71
|
def ANSIColor.foreground fgcolor
|
72
|
-
make_code
|
72
|
+
make_code fgcolor
|
73
73
|
end
|
74
74
|
|
75
75
|
protected
|
@@ -83,7 +83,7 @@ module ANSIColor
|
|
83
83
|
$stderr.puts "WARNING: ANSIColor::make_code(" + str + "): unknown color: " + s
|
84
84
|
return ""
|
85
85
|
end
|
86
|
-
end.join
|
86
|
+
end.join ""
|
87
87
|
else
|
88
88
|
""
|
89
89
|
end
|
data/lib/riel/array.rb
CHANGED
data/lib/riel/date.rb
CHANGED
data/lib/riel/dir.rb
CHANGED
@@ -35,7 +35,7 @@ class Dir
|
|
35
35
|
if dir.readable?
|
36
36
|
dir.children.sort.each do |child|
|
37
37
|
if child.exist? && child.directory?
|
38
|
-
self.remove_if_empty
|
38
|
+
self.remove_if_empty child, subargs
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -45,10 +45,10 @@ class Dir
|
|
45
45
|
can_delete = dir.children.all? do |x|
|
46
46
|
bname = x.basename.to_s
|
47
47
|
deletable.any? do |del|
|
48
|
-
if del.kind_of?
|
48
|
+
if del.kind_of? String
|
49
49
|
bname == del
|
50
|
-
elsif del.kind_of?
|
51
|
-
del.match
|
50
|
+
elsif del.kind_of? Regexp
|
51
|
+
del.match bname
|
52
52
|
else
|
53
53
|
false
|
54
54
|
end
|
@@ -75,15 +75,15 @@ class Dir
|
|
75
75
|
|
76
76
|
# Moves and copies files to the given directory, creating
|
77
77
|
# it if it does not exist.
|
78
|
-
def self.move_and_copy_files
|
78
|
+
def self.move_and_copy_files dir, move_files, copy_files
|
79
79
|
dir.mkdir unless dir.exist?
|
80
80
|
|
81
81
|
move_files.each do |mfile|
|
82
|
-
File.move
|
82
|
+
File.move mfile.to_s, dir.to_s
|
83
83
|
end
|
84
84
|
|
85
85
|
copy_files.each do |cfile|
|
86
|
-
File.copy
|
86
|
+
File.copy cfile.to_s, dir.to_s
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
data/lib/riel/env.rb
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
# -*- ruby -*-
|
3
3
|
|
4
4
|
module Env
|
5
|
-
|
6
5
|
# Returns the home directory, for both Unix and Windows.
|
7
|
-
|
8
6
|
def self.home_directory
|
9
7
|
if hm = ENV["HOME"]
|
10
8
|
hm
|
@@ -38,12 +36,11 @@ module Env
|
|
38
36
|
|
39
37
|
# Reads the environment variable, splitting it according to its quoting.
|
40
38
|
|
41
|
-
def self.split
|
39
|
+
def self.split varname
|
42
40
|
if v = ENV[varname]
|
43
41
|
v.scan(REGEXP).collect { |x| x[1] || x[2] }
|
44
42
|
else
|
45
43
|
[]
|
46
44
|
end
|
47
45
|
end
|
48
|
-
|
49
46
|
end
|
data/lib/riel/file.rb
CHANGED
@@ -15,17 +15,16 @@ path = ENV['PATH'].dup
|
|
15
15
|
$VERBOSE = orig_verbose
|
16
16
|
|
17
17
|
class File
|
18
|
-
|
19
|
-
def self.text?(file)
|
18
|
+
def self.text? file
|
20
19
|
return File.file?(file) && FileType.instance.text?(file)
|
21
20
|
end
|
22
21
|
|
23
|
-
def self.binary?
|
22
|
+
def self.binary? file
|
24
23
|
return !self.text?(file)
|
25
24
|
end
|
26
25
|
|
27
26
|
# Returns whether the given object is a file. Ignores errors.
|
28
|
-
def self.is_file?
|
27
|
+
def self.is_file? fd
|
29
28
|
begin
|
30
29
|
return self.stat(fd).file?
|
31
30
|
rescue
|
@@ -35,7 +34,7 @@ class File
|
|
35
34
|
end
|
36
35
|
|
37
36
|
# Returns whether the given file is writable. Ignores errors.
|
38
|
-
def self.is_writable?
|
37
|
+
def self.is_writable? file
|
39
38
|
begin
|
40
39
|
return self.stat(file).writable?
|
41
40
|
rescue
|
@@ -45,7 +44,7 @@ class File
|
|
45
44
|
end
|
46
45
|
|
47
46
|
# Returns whether the given object is a directory. Ignores errors.
|
48
|
-
def self.is_directory?
|
47
|
+
def self.is_directory? fd
|
49
48
|
begin
|
50
49
|
return self.stat(fd).directory?
|
51
50
|
rescue
|
@@ -55,61 +54,61 @@ class File
|
|
55
54
|
end
|
56
55
|
|
57
56
|
# Returns an array of all files under the given directory.
|
58
|
-
def self.find_files
|
57
|
+
def self.find_files dir
|
59
58
|
files = Array.new
|
60
59
|
Find.find(dir) { |f| files.push(f) if is_file?(f) }
|
61
60
|
files
|
62
61
|
end
|
63
62
|
|
64
63
|
# Returns an array of all directory under the given directory.
|
65
|
-
def self.find_directories
|
64
|
+
def self.find_directories dir
|
66
65
|
dirs = Array.new
|
67
66
|
Find.find(dir) { |d| dirs.push(d) if is_directory?(d) }
|
68
67
|
dirs
|
69
68
|
end
|
70
69
|
|
71
70
|
# Removes the file/directory, including all subelements. Use with caution!
|
72
|
-
def self.remove_recursively
|
71
|
+
def self.remove_recursively fd
|
73
72
|
#$$$ this is rmtree
|
74
73
|
if fd.directory?
|
75
|
-
fd.children.each { |x| remove_recursively
|
74
|
+
fd.children.each { |x| remove_recursively x }
|
76
75
|
end
|
77
76
|
fd.unlink
|
78
77
|
end
|
79
78
|
|
80
79
|
# Creates the given directory.
|
81
|
-
def self.mkdir
|
82
|
-
pn = Pathname.new
|
80
|
+
def self.mkdir dir
|
81
|
+
pn = Pathname.new dir
|
83
82
|
pn.mkdir unless pn.exist?
|
84
83
|
end
|
85
84
|
|
86
85
|
# Moves the files to the given directory, creating it if it does not exist.
|
87
|
-
def self.move_files
|
88
|
-
mkdir
|
86
|
+
def self.move_files dir, files
|
87
|
+
mkdir dir
|
89
88
|
|
90
89
|
files.each do |file|
|
91
|
-
FileUtils.move
|
90
|
+
FileUtils.move file.to_s, dir.to_s
|
92
91
|
end
|
93
92
|
end
|
94
93
|
|
95
94
|
# Copies the files to the given directory, creating it if it does not exist.
|
96
|
-
def self.copy_files
|
97
|
-
mkdir
|
95
|
+
def self.copy_files dir, files
|
96
|
+
mkdir dir
|
98
97
|
|
99
98
|
files.each do |file|
|
100
|
-
FileUtils.copy
|
99
|
+
FileUtils.copy file.to_s, dir.to_s
|
101
100
|
end
|
102
101
|
end
|
103
102
|
|
104
103
|
# Converts the argument to a Pathname.
|
105
|
-
def self._to_pathname
|
104
|
+
def self._to_pathname file
|
106
105
|
file.kind_of?(Pathname) ? file : Pathname.new(file.to_s)
|
107
106
|
end
|
108
107
|
|
109
108
|
# Reads a file line by line. Returns the pathname for the file, or nil if it
|
110
109
|
# does not exist.
|
111
|
-
def self.read_file
|
112
|
-
fpn = _to_pathname
|
110
|
+
def self.read_file file, &blk
|
111
|
+
fpn = _to_pathname file
|
113
112
|
if fpn.exist?
|
114
113
|
fpn.open do |f|
|
115
114
|
blk.call f.read
|
@@ -123,7 +122,7 @@ class File
|
|
123
122
|
# Reads a file line by line, calling the given block. Returns the pathname for
|
124
123
|
# the file, or nil if it does not exist.
|
125
124
|
def self.read_file_lines(file, &blk)
|
126
|
-
fpn = _to_pathname
|
125
|
+
fpn = _to_pathname file
|
127
126
|
if fpn.exist?
|
128
127
|
fpn.open do |f|
|
129
128
|
f.each_line do |line|
|
@@ -138,12 +137,11 @@ class File
|
|
138
137
|
|
139
138
|
# Opens a file for writing and delegates to the given block.
|
140
139
|
def self.open_writable_file(file, &blk)
|
141
|
-
fpn = _to_pathname
|
140
|
+
fpn = _to_pathname file
|
142
141
|
|
143
142
|
fpn.open(File::WRONLY | File::TRUNC | File::CREAT) do |f|
|
144
|
-
blk.call
|
143
|
+
blk.call f
|
145
144
|
end
|
146
|
-
|
147
145
|
fpn
|
148
146
|
end
|
149
147
|
|
@@ -169,15 +167,15 @@ class File
|
|
169
167
|
def self.open_via_temp_file(file, tempfile = nil, tempdir = Dir::tmpdir, &blk)
|
170
168
|
tempname = nil
|
171
169
|
|
172
|
-
fpn = _to_pathname
|
170
|
+
fpn = _to_pathname file
|
173
171
|
tempfile ||= fpn.rootname
|
174
172
|
|
175
173
|
Tempfile.open(tempfile) do |tf|
|
176
|
-
blk.call
|
174
|
+
blk.call tf
|
177
175
|
tempname = tf.path
|
178
176
|
end
|
179
177
|
|
180
|
-
FileUtils.mv
|
178
|
+
FileUtils.mv tempname, file.to_s
|
181
179
|
end
|
182
180
|
|
183
181
|
# Writes a file, using write, buffering it via a temp file.
|
@@ -197,7 +195,7 @@ class File
|
|
197
195
|
# Returns a file for the given basename, sequentially appending an integer
|
198
196
|
# until one is found that does not exist. For example, "foo.3" if "foo",
|
199
197
|
# "foo.1", and "foo.2" already exist.
|
200
|
-
def self.get_unused_file_name
|
198
|
+
def self.get_unused_file_name basename
|
201
199
|
tgt = basename
|
202
200
|
if tgt.exist?
|
203
201
|
i = 1
|
data/lib/riel/filetype.rb
CHANGED
@@ -71,18 +71,18 @@ class FileType
|
|
71
71
|
set_extensions(false, *NONTEXT_EXTENSIONS)
|
72
72
|
end
|
73
73
|
|
74
|
-
def ascii?
|
74
|
+
def ascii? c
|
75
75
|
# from ctype.h
|
76
76
|
(c.to_i & ~0x7f) == 0
|
77
77
|
end
|
78
78
|
|
79
|
-
def type
|
79
|
+
def type file
|
80
80
|
begin
|
81
81
|
case File.stat(file).ftype
|
82
82
|
when "directory"
|
83
83
|
DIRECTORY
|
84
84
|
when "file"
|
85
|
-
if File.readable?
|
85
|
+
if File.readable? file
|
86
86
|
text?(file) ? TEXT : BINARY
|
87
87
|
else
|
88
88
|
UNREADABLE
|
@@ -120,12 +120,12 @@ class FileType
|
|
120
120
|
@known.keys.reject { |suf| @known[suf] }
|
121
121
|
end
|
122
122
|
|
123
|
-
def text?
|
123
|
+
def text? file
|
124
124
|
return false unless File.exists?(file)
|
125
125
|
|
126
126
|
if md = EXTENSION_REGEXP.match(file.to_s)
|
127
127
|
suffix = md[1]
|
128
|
-
if @known.include?
|
128
|
+
if @known.include? suffix
|
129
129
|
return @known[suffix]
|
130
130
|
end
|
131
131
|
end
|
@@ -135,7 +135,7 @@ class FileType
|
|
135
135
|
|
136
136
|
begin
|
137
137
|
File.open(file) do |f|
|
138
|
-
buf = f.read
|
138
|
+
buf = f.read @test_length
|
139
139
|
if buf
|
140
140
|
buf.each_byte do |ch|
|
141
141
|
ntested += 1
|
@@ -158,20 +158,20 @@ class FileType
|
|
158
158
|
nodd < ntested * @odd_factor
|
159
159
|
end
|
160
160
|
|
161
|
-
def self.ascii?
|
162
|
-
return self.instance.ascii?
|
161
|
+
def self.ascii? c
|
162
|
+
return self.instance.ascii? c
|
163
163
|
end
|
164
164
|
|
165
|
-
def self.type
|
166
|
-
return self.instance.type
|
165
|
+
def self.type file
|
166
|
+
return self.instance.type file
|
167
167
|
end
|
168
168
|
|
169
|
-
def self.set_text
|
170
|
-
return self.instance.set_text
|
169
|
+
def self.set_text ext
|
170
|
+
return self.instance.set_text ext
|
171
171
|
end
|
172
172
|
|
173
|
-
def self.set_nontext
|
174
|
-
return self.instance.set_nontext
|
173
|
+
def self.set_nontext ext
|
174
|
+
return self.instance.set_nontext ext
|
175
175
|
end
|
176
176
|
|
177
177
|
def self.text_extensions
|
@@ -182,8 +182,7 @@ class FileType
|
|
182
182
|
return self.instance.nontext_extensions
|
183
183
|
end
|
184
184
|
|
185
|
-
def self.text?
|
186
|
-
return self.instance.text?
|
185
|
+
def self.text? file
|
186
|
+
return self.instance.text? file
|
187
187
|
end
|
188
|
-
|
189
188
|
end
|
data/lib/riel/log/logger.rb
CHANGED
data/lib/riel/optproc.rb
CHANGED
@@ -6,7 +6,6 @@ require 'riel/log'
|
|
6
6
|
require 'riel/text'
|
7
7
|
require 'riel/enumerable'
|
8
8
|
|
9
|
-
|
10
9
|
module OptProc
|
11
10
|
|
12
11
|
class Option
|
@@ -25,7 +24,7 @@ module OptProc
|
|
25
24
|
ARG_TYPES << [ :string, ARG_STRING ]
|
26
25
|
ARG_TYPES << [ :boolean, ARG_BOOLEAN ]
|
27
26
|
|
28
|
-
def initialize
|
27
|
+
def initialize args = Hash.new, &blk
|
29
28
|
@tags = args[:tags] || Array.new
|
30
29
|
@rc = args[:rc]
|
31
30
|
@rc = [ @rc ] if @rc.kind_of?(String)
|
@@ -41,10 +40,8 @@ module OptProc
|
|
41
40
|
@res = [ @res ] if @res.kind_of?(Regexp)
|
42
41
|
|
43
42
|
if args[:arg]
|
44
|
-
# log { "args.class: #{args[:arg].class}" }
|
45
43
|
demargs = args[:arg].dup
|
46
44
|
while arg = demargs.shift
|
47
|
-
# log { "arg: #{arg}" }
|
48
45
|
case arg
|
49
46
|
when :required
|
50
47
|
@type = "required"
|
@@ -56,19 +53,13 @@ module OptProc
|
|
56
53
|
@valuere = demargs.shift
|
57
54
|
else
|
58
55
|
if re = ARG_TYPES.assoc(arg)
|
59
|
-
# log { "re: #{re}" }
|
60
56
|
@valuere = re[1]
|
61
57
|
@argtype = arg
|
62
58
|
@type ||= "required"
|
63
|
-
else
|
64
|
-
# log { "no expression for arg #{arg}" }
|
65
59
|
end
|
66
60
|
end
|
67
61
|
end
|
68
62
|
end
|
69
|
-
|
70
|
-
# log { "valuere: #{@valuere}" }
|
71
|
-
# log { "type: #{@type}" }
|
72
63
|
end
|
73
64
|
|
74
65
|
def inspect
|
@@ -80,101 +71,76 @@ module OptProc
|
|
80
71
|
end
|
81
72
|
|
82
73
|
def to_s
|
83
|
-
@tags.join
|
74
|
+
@tags.join " "
|
84
75
|
end
|
85
76
|
|
86
|
-
def match_rc?
|
77
|
+
def match_rc? field
|
87
78
|
@rc && @rc.include?(field)
|
88
79
|
end
|
89
80
|
|
90
|
-
def match_value
|
91
|
-
# log { "valuere: #{@valuere.inspect}; val: #{val}" }
|
81
|
+
def match_value val
|
92
82
|
@md = @valuere && @valuere.match(val)
|
93
|
-
# log { "md: #{@md.inspect}" }
|
94
83
|
@md && @md[1]
|
95
84
|
end
|
96
85
|
|
97
|
-
def match_tag
|
98
|
-
stack { "@rc: #{@rc.inspect}; @tags: #{@tags.inspect}" }
|
99
|
-
|
86
|
+
def match_tag tag
|
100
87
|
if tm = @tags.detect do |t|
|
101
|
-
log { "t: #{t}; tag: #{tag}; idx: #{t.index(tag)}" }
|
102
88
|
t.index(tag) == 0 && tag.length <= t.length
|
103
89
|
end
|
104
90
|
|
105
|
-
log { "tm: #{tm}" }
|
106
91
|
if tag.length == tm.length
|
107
92
|
1.0
|
108
93
|
else
|
109
|
-
|
110
|
-
log { "len: #{len}" }
|
111
|
-
len
|
94
|
+
tag.length.to_f * 0.01
|
112
95
|
end
|
113
96
|
else
|
114
97
|
nil
|
115
98
|
end
|
116
99
|
end
|
117
100
|
|
118
|
-
def match
|
119
|
-
return nil unless %r{^-}.match
|
120
|
-
|
121
|
-
# log { "opt: #{opt.inspect}; args: #{args.inspect}" }
|
122
|
-
# log { "@rc: #{@rc.inspect}; @re: #{@re.inspect}; @tags: #{@tags.inspect}" }
|
101
|
+
def match args, opt = args[0]
|
102
|
+
return nil unless %r{^-}.match opt
|
123
103
|
|
124
104
|
tag, val = opt.split('=', 2)
|
125
105
|
tag ||= opt
|
126
106
|
|
127
|
-
# log { "opt: #{opt}; opt: #{opt.class}; tag: #{tag}; tags: #{@tags.inspect}" }
|
128
|
-
# log { "res: #{@res.inspect}" }
|
129
|
-
|
130
107
|
@md = nil
|
131
108
|
|
132
109
|
if @res && (@md = @res.collect { |re| re.match(opt) }.detect)
|
133
|
-
# log { "matched: #{@md}" }
|
134
110
|
1.0
|
135
111
|
else
|
136
|
-
match_tag
|
112
|
+
match_tag tag
|
137
113
|
end
|
138
114
|
end
|
139
115
|
|
140
|
-
def set_value
|
141
|
-
tag, val = opt.split
|
116
|
+
def set_value args, opt = args[0]
|
117
|
+
tag, val = opt.split '=', 2
|
142
118
|
args.shift
|
143
|
-
|
144
|
-
# log { "opt : #{opt}" }
|
145
|
-
# log { "tag : #{tag}" }
|
146
|
-
# log { "tags: #{@tags.inspect}" }
|
147
|
-
# log { "val : #{val.inspect}" }
|
148
|
-
# log { "md : #{@md.inspect}" }
|
149
119
|
|
150
120
|
if @md
|
151
|
-
#
|
121
|
+
# already have match data
|
152
122
|
elsif @type == "required"
|
153
123
|
if val
|
154
124
|
# already have value
|
155
|
-
# log { "already have value: #{val}" }
|
156
125
|
elsif args.size > 0
|
157
126
|
val = args.shift
|
158
|
-
# log { "got next value: #{val}" }
|
159
127
|
else
|
160
128
|
$stderr.puts "value expected"
|
161
129
|
end
|
162
130
|
|
163
131
|
if val
|
164
|
-
match_value
|
132
|
+
match_value val
|
165
133
|
end
|
166
134
|
elsif @type == "optional"
|
167
135
|
if val
|
168
|
-
#
|
169
|
-
match_value
|
136
|
+
# already have value
|
137
|
+
match_value val
|
170
138
|
elsif args.size > 0
|
171
|
-
if %r{^-}.match
|
172
|
-
#
|
173
|
-
elsif match_value
|
174
|
-
#
|
139
|
+
if %r{^-}.match args[0]
|
140
|
+
# skipping next value; apparently option
|
141
|
+
elsif match_value args[0]
|
142
|
+
# value matches
|
175
143
|
args.shift
|
176
|
-
else
|
177
|
-
# log { "value does not match" }
|
178
144
|
end
|
179
145
|
end
|
180
146
|
else
|
@@ -183,7 +149,7 @@ module OptProc
|
|
183
149
|
|
184
150
|
value = value_from_match
|
185
151
|
|
186
|
-
set
|
152
|
+
set value, opt, args
|
187
153
|
end
|
188
154
|
|
189
155
|
def value_from_match
|
@@ -191,14 +157,14 @@ module OptProc
|
|
191
157
|
if @argtype.nil? || @argtype == :regexp
|
192
158
|
@md
|
193
159
|
else
|
194
|
-
convert_value
|
160
|
+
convert_value @md[1]
|
195
161
|
end
|
196
162
|
elsif @argtype == :boolean
|
197
163
|
true
|
198
164
|
end
|
199
165
|
end
|
200
166
|
|
201
|
-
def convert_value
|
167
|
+
def convert_value val
|
202
168
|
if val
|
203
169
|
case @argtype
|
204
170
|
when :string
|
@@ -208,7 +174,7 @@ module OptProc
|
|
208
174
|
when :float
|
209
175
|
val.to_f
|
210
176
|
when :boolean
|
211
|
-
to_boolean
|
177
|
+
to_boolean val
|
212
178
|
when :regexp
|
213
179
|
val
|
214
180
|
when nil
|
@@ -221,11 +187,11 @@ module OptProc
|
|
221
187
|
end
|
222
188
|
end
|
223
189
|
|
224
|
-
def to_boolean
|
190
|
+
def to_boolean val
|
225
191
|
%w{ yes true on soitenly }.include?(val.downcase)
|
226
192
|
end
|
227
193
|
|
228
|
-
def set
|
194
|
+
def set val, opt = nil, args = nil
|
229
195
|
# log { "argtype: #{@argtype}; md: #{@md.inspect}" }
|
230
196
|
|
231
197
|
setargs = [ val, opt, args ].select_with_index { |x, i| i < @set.arity }
|
@@ -240,14 +206,14 @@ module OptProc
|
|
240
206
|
|
241
207
|
attr_reader :options
|
242
208
|
|
243
|
-
def initialize
|
209
|
+
def initialize data
|
244
210
|
@options = Array.new
|
245
211
|
@shortopts = Array.new
|
246
212
|
@longopts = Array.new
|
247
213
|
@regexps = Hash.new
|
248
214
|
|
249
215
|
data.each do |optdata|
|
250
|
-
opt = OptProc::Option.new
|
216
|
+
opt = OptProc::Option.new optdata
|
251
217
|
@options << opt
|
252
218
|
|
253
219
|
opt.tags.each do |tag|
|
@@ -295,12 +261,12 @@ module OptProc
|
|
295
261
|
Regexp.new('^ ( - [a-z] ) ( .+ ) $ ', Regexp::EXTENDED)
|
296
262
|
]
|
297
263
|
|
298
|
-
def process_option
|
264
|
+
def process_option args
|
299
265
|
opt = args[0]
|
300
266
|
|
301
267
|
# log { "processing option #{opt}" }
|
302
268
|
|
303
|
-
if md = COMBINED_OPTS_RES.collect { |re| re.match
|
269
|
+
if md = COMBINED_OPTS_RES.collect { |re| re.match opt }.detect
|
304
270
|
lhs = md[1]
|
305
271
|
rhs = "-" + md[2]
|
306
272
|
|
@@ -308,7 +274,7 @@ module OptProc
|
|
308
274
|
|
309
275
|
args[0, 1] = lhs, rhs
|
310
276
|
|
311
|
-
return process_option
|
277
|
+
return process_option args
|
312
278
|
elsif opt[0] == 45
|
313
279
|
ch = opt[1]
|
314
280
|
assocopts = if ch == 45 # 45 = '-'
|
@@ -333,7 +299,7 @@ module OptProc
|
|
333
299
|
log { "bestmatch: #{@bestmatch}" }
|
334
300
|
log { "bestopts : #{@bestopts.inspect}" }
|
335
301
|
if @bestopts.size == 1
|
336
|
-
@bestopts[0].set_value
|
302
|
+
@bestopts[0].set_value args
|
337
303
|
return @bestopts[0]
|
338
304
|
else
|
339
305
|
optstr = @bestopts.collect { |x| '(' + x.tags.join(', ') + ')' }.join(', ')
|
@@ -345,7 +311,7 @@ module OptProc
|
|
345
311
|
nil
|
346
312
|
end
|
347
313
|
|
348
|
-
def set_option
|
314
|
+
def set_option optlist, args
|
349
315
|
@bestmatch = nil
|
350
316
|
@bestopts = Array.new
|
351
317
|
|
@@ -353,7 +319,7 @@ module OptProc
|
|
353
319
|
if mv = option.match(args)
|
354
320
|
if mv >= 1.0
|
355
321
|
# exact match:
|
356
|
-
option.set_value
|
322
|
+
option.set_value args
|
357
323
|
return option
|
358
324
|
elsif !@bestmatch || @bestmatch <= mv
|
359
325
|
@bestmatch = mv
|
data/lib/riel/rcfile.rb
CHANGED
@@ -16,7 +16,7 @@ class RCFile
|
|
16
16
|
def initialize(fname, &blk)
|
17
17
|
@settings = Array.new
|
18
18
|
|
19
|
-
if File.exists?
|
19
|
+
if File.exists? fname
|
20
20
|
IO::readlines(fname).each do |line|
|
21
21
|
line.sub!(/\s*#.*/, "")
|
22
22
|
line.chomp!
|
@@ -26,7 +26,7 @@ class RCFile
|
|
26
26
|
value.strip!
|
27
27
|
@settings << [ name, value ]
|
28
28
|
if blk
|
29
|
-
blk.call
|
29
|
+
blk.call name, value
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
data/lib/riel/regexp.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
# Negates the given expression.
|
5
5
|
class NegatedRegexp < Regexp
|
6
6
|
|
7
|
-
def match
|
7
|
+
def match str
|
8
8
|
!super
|
9
9
|
end
|
10
10
|
|
@@ -27,7 +27,7 @@ class Regexp
|
|
27
27
|
|
28
28
|
# Returns a regular expression for the given Unix file system expression.
|
29
29
|
|
30
|
-
def self.unixre_to_string
|
30
|
+
def self.unixre_to_string pat
|
31
31
|
pat.gsub(%r{(\\.)|(.)}) do
|
32
32
|
$1 || SH2RE[$2] || $2
|
33
33
|
end
|
@@ -70,7 +70,7 @@ class Regexp
|
|
70
70
|
# Handles negation, whole words, and ignore case (Ruby no longer supports
|
71
71
|
# Rexexp.new(/foo/i), as of 1.8).
|
72
72
|
|
73
|
-
def self.create
|
73
|
+
def self.create pat, args = Hash.new
|
74
74
|
negated = args[:negated]
|
75
75
|
ignorecase = args[:ignorecase]
|
76
76
|
wholewords = args[:wholewords]
|
@@ -134,19 +134,19 @@ class Regexp
|
|
134
134
|
tot | (val ? flag : 0)
|
135
135
|
end
|
136
136
|
|
137
|
-
reclass.new
|
137
|
+
reclass.new pattern, flags
|
138
138
|
end
|
139
139
|
|
140
|
-
def self.matches_word_start?
|
141
|
-
WORD_START_RE.match
|
140
|
+
def self.matches_word_start? pat
|
141
|
+
WORD_START_RE.match pat
|
142
142
|
end
|
143
143
|
|
144
|
-
def self.matches_word_end?
|
145
|
-
WORD_END_RE.match
|
144
|
+
def self.matches_word_end? pat
|
145
|
+
WORD_END_RE.match pat
|
146
146
|
end
|
147
147
|
|
148
148
|
# applies Perl-style substitution (s/foo/bar/).
|
149
|
-
def self.perl_subst
|
149
|
+
def self.perl_subst pat
|
150
150
|
end
|
151
151
|
|
152
152
|
end
|
data/lib/riel/setdiff.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# identical, A contains B, B contains A, or A and B contain common elements.
|
6
6
|
|
7
7
|
class SetDiff
|
8
|
-
def SetDiff.new
|
8
|
+
def SetDiff.new a, b
|
9
9
|
allitems = a | b
|
10
10
|
|
11
11
|
a_and_b = Array.new
|
@@ -13,8 +13,8 @@ class SetDiff
|
|
13
13
|
b_not_in_a = Array.new
|
14
14
|
|
15
15
|
allitems.each do |it|
|
16
|
-
if a.include?
|
17
|
-
if b.include?
|
16
|
+
if a.include? it
|
17
|
+
if b.include? it
|
18
18
|
a_and_b
|
19
19
|
else
|
20
20
|
a_not_in_b
|
@@ -24,12 +24,12 @@ class SetDiff
|
|
24
24
|
end << it
|
25
25
|
end
|
26
26
|
|
27
|
-
super
|
27
|
+
super a_and_b, a_not_in_b, b_not_in_a
|
28
28
|
end
|
29
29
|
|
30
30
|
attr_reader :a_and_b, :a_not_in_b, :b_not_in_a
|
31
31
|
|
32
|
-
def initialize
|
32
|
+
def initialize a_and_b, a_not_in_b, b_not_in_a
|
33
33
|
@a_and_b = a_and_b
|
34
34
|
@a_not_in_b = a_not_in_b
|
35
35
|
@b_not_in_a = b_not_in_a
|
data/lib/riel/size_converter.rb
CHANGED
@@ -5,7 +5,7 @@ class SizeConverter
|
|
5
5
|
# http://www.gnu.org/software/coreutils/manual/html_node/Block-size.html
|
6
6
|
|
7
7
|
# don't round to closest -- just convert
|
8
|
-
def self.convert_to_kilobytes
|
8
|
+
def self.convert_to_kilobytes size, decimal_places = 1
|
9
9
|
### SizeConverter._convert(Human::CONVERSIONS, 2, size, decimal_places)
|
10
10
|
end
|
11
11
|
|
@@ -20,8 +20,8 @@ class SizeConverter
|
|
20
20
|
# returns a string representation of the size. Note that K, G, M are
|
21
21
|
# gibibytes, etc., that is, powers of 10.
|
22
22
|
|
23
|
-
def self.convert
|
24
|
-
SizeConverter._convert
|
23
|
+
def self.convert size, decimal_places = 1
|
24
|
+
SizeConverter._convert CONVERSIONS, 10, size, decimal_places
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -37,26 +37,26 @@ class SizeConverter
|
|
37
37
|
# returns a string representation of the size. Note that K, G, M are
|
38
38
|
# gigabytes, etc.
|
39
39
|
|
40
|
-
def self.convert
|
41
|
-
SizeConverter._convert
|
40
|
+
def self.convert size, decimal_places = 1
|
41
|
+
SizeConverter._convert CONVERSIONS, 2, size, decimal_places
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
# legacy:
|
46
46
|
|
47
|
-
def self.convert
|
48
|
-
Human::convert
|
47
|
+
def self.convert size, decimal_places = 1
|
48
|
+
Human::convert size, decimal_places
|
49
49
|
end
|
50
50
|
|
51
|
-
def self._convert
|
51
|
+
def self._convert conversions, base, size, decimal_places
|
52
52
|
sizef = size.to_f
|
53
53
|
conversions.each do |conv|
|
54
54
|
sz = sizef / (base ** conv[0])
|
55
55
|
if sz >= 1.0
|
56
|
-
return sprintf
|
56
|
+
return sprintf "%.*f%s", decimal_places, sz, conv[1]
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
sprintf
|
60
|
+
sprintf "%.*f", decimal_places, size
|
61
61
|
end
|
62
62
|
end
|
data/lib/riel/string.rb
CHANGED
@@ -36,7 +36,7 @@ class String
|
|
36
36
|
# Returns a string based on this instance, with the first occurrance of
|
37
37
|
# +other+ removed. +other+ may be a string or regular expression.
|
38
38
|
#
|
39
|
-
def -
|
39
|
+
def - other
|
40
40
|
sub other, ''
|
41
41
|
end
|
42
42
|
|
@@ -53,8 +53,8 @@ class String
|
|
53
53
|
#
|
54
54
|
# A class method for +to_ranges+. Deprecated in favor of the instance method.
|
55
55
|
#
|
56
|
-
def self.to_ranges
|
57
|
-
str.to_ranges
|
56
|
+
def self.to_ranges str, args = Hash.new
|
57
|
+
str.to_ranges args
|
58
58
|
end
|
59
59
|
|
60
60
|
#
|
@@ -83,7 +83,7 @@ class String
|
|
83
83
|
md = section.match(RANGE_REGEXP)
|
84
84
|
next unless md
|
85
85
|
|
86
|
-
from = String._matchdata_to_number
|
86
|
+
from = String._matchdata_to_number md, 1, min
|
87
87
|
to = String._has_matchdata?(md, 2) ? String._matchdata_to_number(md, 3, max) : from
|
88
88
|
|
89
89
|
prevrange = ranges[-1]
|
@@ -108,7 +108,7 @@ class String
|
|
108
108
|
#
|
109
109
|
def highlight re, color
|
110
110
|
gsub(re) do |match|
|
111
|
-
HIGHLIGHTER.color
|
111
|
+
HIGHLIGHTER.color color, match
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
data/lib/riel/tempfile.rb
CHANGED
@@ -3,11 +3,8 @@
|
|
3
3
|
|
4
4
|
require 'tempfile'
|
5
5
|
|
6
|
-
|
7
6
|
class Tempfile
|
8
|
-
|
9
7
|
class << self
|
10
|
-
|
11
8
|
alias_method :original_open, :open
|
12
9
|
|
13
10
|
# this works around the behavior (fixed in 1.9) so that open returns
|
@@ -24,5 +21,4 @@ class Tempfile
|
|
24
21
|
tempname
|
25
22
|
end
|
26
23
|
end
|
27
|
-
|
28
24
|
end
|
data/lib/riel/text.rb
CHANGED
@@ -139,13 +139,13 @@ module Text
|
|
139
139
|
def color colorstr, obj = self, &blk
|
140
140
|
# ^^^^ this is the Module self
|
141
141
|
|
142
|
-
colornames = self.class.parse_colors
|
143
|
-
result = names_to_code
|
142
|
+
colornames = self.class.parse_colors colorstr
|
143
|
+
result = names_to_code colornames
|
144
144
|
|
145
145
|
if blk
|
146
146
|
result << blk.call
|
147
147
|
result << names_to_code("reset")
|
148
|
-
elsif obj.kind_of?
|
148
|
+
elsif obj.kind_of? String
|
149
149
|
result << obj
|
150
150
|
result << names_to_code("reset")
|
151
151
|
end
|
@@ -267,10 +267,10 @@ module Text
|
|
267
267
|
when "negative"
|
268
268
|
"<span style=\"color: white; background-color: black\">"
|
269
269
|
when /on_(\w+)/
|
270
|
-
colval = color_value
|
270
|
+
colval = color_value $1
|
271
271
|
"<span style=\"background-color: #{colval}\">"
|
272
272
|
else
|
273
|
-
colval = color_value
|
273
|
+
colval = color_value name
|
274
274
|
"<span style=\"color: #{colval}\">"
|
275
275
|
end
|
276
276
|
end
|
@@ -355,7 +355,7 @@ module Text
|
|
355
355
|
|
356
356
|
module Highlightable
|
357
357
|
# The highlighter for the class in which this module is included.
|
358
|
-
@@highlighter = ANSIHighlighter.new
|
358
|
+
@@highlighter = ANSIHighlighter.new Text::Highlighter::DEFAULT_COLORS
|
359
359
|
|
360
360
|
if false
|
361
361
|
Text::Highlighter::ATTRIBUTES.each do |name|
|
data/lib/riel/timer.rb
CHANGED
@@ -5,8 +5,8 @@ require 'riel/log'
|
|
5
5
|
|
6
6
|
class Timer
|
7
7
|
|
8
|
-
def initialize
|
9
|
-
if args.kind_of?
|
8
|
+
def initialize what, args = Hash.new
|
9
|
+
if args.kind_of? Fixnum
|
10
10
|
args = { :level => args }
|
11
11
|
end
|
12
12
|
|
@@ -18,18 +18,18 @@ class Timer
|
|
18
18
|
elmsg = args.include?(:elmsg) ? args[:elmsg] : "#{what} elapsed "
|
19
19
|
|
20
20
|
sttime = Time.new
|
21
|
-
logmsg
|
21
|
+
logmsg stmsg, sttime
|
22
22
|
|
23
23
|
yield
|
24
24
|
|
25
25
|
endtime = Time.new
|
26
26
|
|
27
|
-
logmsg
|
28
|
-
logmsg
|
29
|
-
logmsg
|
27
|
+
logmsg stmsg, sttime
|
28
|
+
logmsg endmsg, endtime
|
29
|
+
logmsg elmsg, endtime - sttime
|
30
30
|
end
|
31
31
|
|
32
|
-
def logmsg
|
32
|
+
def logmsg msg, value
|
33
33
|
if msg
|
34
34
|
if @io
|
35
35
|
@io.puts "#{msg}: #{value}"
|
@@ -41,7 +41,7 @@ class Timer
|
|
41
41
|
|
42
42
|
end
|
43
43
|
|
44
|
-
def timethis
|
44
|
+
def timethis what
|
45
45
|
sttime = Time.new
|
46
46
|
Log.log "#{what} start time: #{sttime}"
|
47
47
|
yield
|
data/lib/riel.rb
CHANGED
@@ -0,0 +1,124 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
require 'rubyunit'
|
6
|
+
require 'stringio'
|
7
|
+
require 'riel/log'
|
8
|
+
|
9
|
+
class LogAbyss
|
10
|
+
include Loggable
|
11
|
+
|
12
|
+
def squeal
|
13
|
+
log "hello from the abyss"
|
14
|
+
stack "turtles all the way down"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class LogDepths
|
19
|
+
include Loggable
|
20
|
+
|
21
|
+
def speak
|
22
|
+
log "hello from the depths"
|
23
|
+
la = LogAbyss.new
|
24
|
+
la.squeal
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class LogInner
|
29
|
+
include Loggable
|
30
|
+
|
31
|
+
def screech
|
32
|
+
ldi = LogDepths.new
|
33
|
+
log "hello from the innerds"
|
34
|
+
ldi.speak
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class LogStackTestCase < RUNIT::TestCase
|
39
|
+
include Loggable
|
40
|
+
|
41
|
+
def test_stack
|
42
|
+
Log.set_widths(-15, 4, -40)
|
43
|
+
|
44
|
+
log = Proc.new {
|
45
|
+
li = LogInner.new
|
46
|
+
li.screech
|
47
|
+
}
|
48
|
+
|
49
|
+
expected_output = [
|
50
|
+
"[ ...: 33] {LogInner#screech } hello from the innerds",
|
51
|
+
"[ ...: 22] {LogDepths#speak } hello from the depths",
|
52
|
+
"[ ...: 13] {LogAbyss#squeal } hello from the abyss",
|
53
|
+
"[ ...: 14] {LogAbyss#squeal } turtles all the way down",
|
54
|
+
"[ ...: 24] {speak } ",
|
55
|
+
"[ ...: 34] {screech } ",
|
56
|
+
"[ ...: 46] {test_stack } ",
|
57
|
+
"[ ...: 104] {call } ",
|
58
|
+
"[ ...: 104] {run_test } ",
|
59
|
+
"[ ...: 82] {test_stack } ",
|
60
|
+
"[ ...testcase.rb: 78] {__send__ } ",
|
61
|
+
"[ ...testcase.rb: 78] {run } ",
|
62
|
+
"[ ...testcase.rb: 42] {run } ",
|
63
|
+
"[...testsuite.rb: 34] {run } ",
|
64
|
+
"[...testsuite.rb: 33] {each } ",
|
65
|
+
"[...testsuite.rb: 33] {run } ",
|
66
|
+
"[...testsuite.rb: 23] {run } ",
|
67
|
+
"[...testsuite.rb: 34] {run } ",
|
68
|
+
"[...testsuite.rb: 33] {each } ",
|
69
|
+
"[...testsuite.rb: 33] {run } ",
|
70
|
+
"[ ...: 46] {run_suite } ",
|
71
|
+
"[ ...: 67] {start_mediator } ",
|
72
|
+
"[ ...: 41] {start } ",
|
73
|
+
"[ ...: 29] {run } ",
|
74
|
+
"[ ...: 216] {run } ",
|
75
|
+
"[ ...: 12] {run } ",
|
76
|
+
# nil means to ignore these lines, which in this case is
|
77
|
+
# because this is deep in the unit test code:
|
78
|
+
nil,
|
79
|
+
nil,
|
80
|
+
]
|
81
|
+
|
82
|
+
run_test @verbose_setup, log, *expected_output
|
83
|
+
end
|
84
|
+
|
85
|
+
# the ctor is down here so the lines above are less likely to change.
|
86
|
+
def initialize test, name
|
87
|
+
@nonverbose_setup = Proc.new {
|
88
|
+
Log.verbose = false
|
89
|
+
Log.output = StringIO.new
|
90
|
+
}
|
91
|
+
|
92
|
+
@verbose_setup = Proc.new {
|
93
|
+
Log.verbose = true
|
94
|
+
Log.output = StringIO.new
|
95
|
+
}
|
96
|
+
|
97
|
+
super
|
98
|
+
end
|
99
|
+
|
100
|
+
def run_test setup, log, *expected
|
101
|
+
io = setup.call
|
102
|
+
puts "io: #{io}"
|
103
|
+
|
104
|
+
log.call
|
105
|
+
|
106
|
+
assert_not_nil io
|
107
|
+
str = io.string
|
108
|
+
assert_not_nil str
|
109
|
+
|
110
|
+
lines = str.split "\n"
|
111
|
+
|
112
|
+
puts lines
|
113
|
+
|
114
|
+
assert_equals expected.size, lines.size, "number of lines of output"
|
115
|
+
|
116
|
+
(0 ... expected.size).each do |idx|
|
117
|
+
if expected[idx]
|
118
|
+
assert_equals expected[idx], lines[idx], "index: #{idx}"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
Log.output = io
|
123
|
+
end
|
124
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 10
|
10
|
+
version: 1.1.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeff Pace
|
@@ -15,12 +15,12 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-11-01 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
22
|
description: This library extends the core Ruby libraries.
|
23
|
-
email:
|
23
|
+
email: jeugenepace@gmail.com
|
24
24
|
executables: []
|
25
25
|
|
26
26
|
extensions: []
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- test/riel/log_test.rb
|
81
81
|
- test/riel/integer_test.rb
|
82
82
|
- test/riel/rcfile_test.rb
|
83
|
+
- test/riel/log_stack_test.rb
|
83
84
|
- test/riel/hash_test.rb
|
84
85
|
- test/riel/setdiff_test.rb
|
85
86
|
- test/riel/string_test.rb
|
@@ -139,6 +140,7 @@ test_files:
|
|
139
140
|
- test/riel/log_test.rb
|
140
141
|
- test/riel/integer_test.rb
|
141
142
|
- test/riel/rcfile_test.rb
|
143
|
+
- test/riel/log_stack_test.rb
|
142
144
|
- test/riel/hash_test.rb
|
143
145
|
- test/riel/setdiff_test.rb
|
144
146
|
- test/riel/string_test.rb
|