glark 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/bin/glark +2 -2
  2. data/lib/glark.rb +1 -1
  3. data/lib/glark/app/app.rb +48 -0
  4. data/lib/glark/app/help.rb +103 -0
  5. data/lib/glark/app/info/options.rb +84 -0
  6. data/lib/glark/app/options.rb +201 -0
  7. data/lib/glark/app/rcfile.rb +49 -0
  8. data/lib/glark/app/runner.rb +140 -0
  9. data/lib/glark/app/spec.rb +19 -0
  10. data/lib/glark/input/filter/criteria_opts.rb +41 -0
  11. data/lib/glark/input/filter/dir_criteria_opts.rb +37 -0
  12. data/lib/glark/input/filter/file_criteria_opts.rb +33 -0
  13. data/lib/glark/input/filter/filter.rb +59 -0
  14. data/lib/glark/input/filter/options.rb +171 -0
  15. data/lib/glark/input/options.rb +120 -0
  16. data/lib/glark/input/range.rb +104 -0
  17. data/lib/glark/input/spec.rb +39 -0
  18. data/lib/glark/io/file/archive_file.rb +64 -0
  19. data/lib/glark/io/file/binary_file.rb +19 -0
  20. data/lib/glark/io/file/file.rb +57 -0
  21. data/lib/glark/io/file/gz_file.rb +21 -0
  22. data/lib/glark/io/file/tar_file.rb +35 -0
  23. data/lib/glark/io/file/tar_gz_file.rb +52 -0
  24. data/lib/glark/io/file/zip_file.rb +39 -0
  25. data/lib/glark/match/and.rb +83 -0
  26. data/lib/glark/match/and_distance.rb +58 -0
  27. data/lib/glark/match/compound.rb +34 -0
  28. data/lib/glark/match/expression.rb +63 -0
  29. data/lib/glark/match/factory.rb +173 -0
  30. data/lib/glark/match/ior.rb +20 -0
  31. data/lib/glark/match/options.rb +74 -0
  32. data/lib/glark/match/or.rb +41 -0
  33. data/lib/glark/match/re.rb +81 -0
  34. data/lib/glark/match/re_factory.rb +44 -0
  35. data/lib/glark/match/spec.rb +59 -0
  36. data/lib/glark/match/xor.rb +20 -0
  37. data/lib/glark/output/binary_file_summary.rb +17 -0
  38. data/lib/glark/output/common.rb +43 -0
  39. data/lib/glark/output/context.rb +57 -0
  40. data/lib/glark/output/count.rb +26 -0
  41. data/lib/glark/output/file_header.rb +20 -0
  42. data/lib/glark/output/file_name_only.rb +35 -0
  43. data/lib/glark/output/formatted.rb +22 -0
  44. data/lib/glark/output/glark_count.rb +23 -0
  45. data/lib/glark/output/glark_format.rb +62 -0
  46. data/lib/glark/output/glark_lines.rb +36 -0
  47. data/lib/glark/output/grep_count.rb +18 -0
  48. data/lib/glark/output/grep_lines.rb +42 -0
  49. data/lib/glark/output/line_status.rb +46 -0
  50. data/lib/glark/output/lines.rb +100 -0
  51. data/lib/glark/output/match_list.rb +15 -0
  52. data/lib/glark/output/options.rb +103 -0
  53. data/lib/glark/output/results.rb +23 -0
  54. data/lib/glark/output/spec.rb +105 -0
  55. data/lib/glark/output/unfiltered_lines.rb +28 -0
  56. data/lib/glark/util/colors/options.rb +143 -0
  57. data/lib/glark/util/colors/spec.rb +21 -0
  58. data/lib/glark/util/highlight.rb +108 -0
  59. data/lib/glark/util/io/depth.rb +29 -0
  60. data/lib/glark/util/io/fileset.rb +162 -0
  61. data/lib/glark/util/io/filter/criteria.rb +49 -0
  62. data/lib/glark/util/io/filter/filter.rb +10 -0
  63. data/lib/glark/util/io/lines.rb +117 -0
  64. data/lib/glark/util/option.rb +34 -0
  65. data/lib/glark/util/options.rb +12 -0
  66. data/lib/glark/util/optutil.rb +69 -0
  67. data/lib/glark/util/timestamper.rb +18 -0
  68. data/man/glark.1 +1134 -0
  69. data/test/glark/app/and_test.rb +82 -0
  70. data/test/glark/app/compound_test.rb +33 -0
  71. data/test/glark/app/context_test.rb +43 -0
  72. data/test/glark/app/count_test.rb +89 -0
  73. data/test/glark/app/dump_test.rb +16 -0
  74. data/test/glark/app/expression_file_test.rb +22 -0
  75. data/test/glark/app/extended_regexp_test.rb +17 -0
  76. data/test/glark/app/extract_matches_test.rb +27 -0
  77. data/test/glark/app/files_with_match_test.rb +32 -0
  78. data/test/glark/app/files_without_match_test.rb +26 -0
  79. data/test/glark/app/filter_test.rb +118 -0
  80. data/test/glark/app/highlight_test.rb +78 -0
  81. data/test/glark/app/ignore_case_test.rb +22 -0
  82. data/test/glark/app/invert_test.rb +49 -0
  83. data/test/glark/app/ior_test.rb +21 -0
  84. data/test/glark/app/label_test.rb +28 -0
  85. data/test/glark/app/line_number_color_test.rb +42 -0
  86. data/test/glark/app/line_numbers_test.rb +42 -0
  87. data/test/glark/app/match_limit_test.rb +49 -0
  88. data/test/glark/app/options_test.rb +722 -0
  89. data/test/glark/app/range_test.rb +101 -0
  90. data/test/glark/app/rcfile_test.rb +113 -0
  91. data/test/glark/app/record_separator_test.rb +32 -0
  92. data/test/glark/app/regexp_test.rb +48 -0
  93. data/test/glark/app/tc.rb +92 -0
  94. data/test/glark/app/text_color_test.rb +31 -0
  95. data/test/glark/app/whole_lines_test.rb +17 -0
  96. data/test/glark/app/whole_words_test.rb +42 -0
  97. data/test/glark/app/xor_test.rb +19 -0
  98. data/test/glark/input/binary_file_test.rb +0 -0
  99. data/test/glark/input/directory_test.rb +202 -0
  100. data/test/glark/input/dirname_test.rb +69 -0
  101. data/test/glark/input/exclude_matching_test.rb +20 -0
  102. data/test/glark/input/ext_test.rb +65 -0
  103. data/test/glark/input/filter/criteria_test.rb +91 -0
  104. data/test/glark/input/filter/filter_spec_test.rb +27 -0
  105. data/test/glark/input/filter/filter_test.rb +21 -0
  106. data/test/glark/input/name_test.rb +75 -0
  107. data/test/glark/input/path_test.rb +72 -0
  108. data/test/glark/input/range_test.rb +82 -0
  109. data/test/glark/input/size_limit_test.rb +51 -0
  110. data/test/glark/input/split_as_path_test.rb +28 -0
  111. data/test/glark/match_test.rb +192 -0
  112. data/test/glark/resources.rb +21 -0
  113. data/test/glark/tc.rb +37 -0
  114. data/test/resources/add.rb +10 -0
  115. data/test/resources/echo.rb +2 -0
  116. data/test/resources/greet.rb +13 -0
  117. metadata +198 -28
  118. data/README +0 -0
  119. data/bin/jlark +0 -63
  120. data/lib/glark/expression.rb +0 -440
  121. data/lib/glark/exprfactory.rb +0 -248
  122. data/lib/glark/glark.rb +0 -297
  123. data/lib/glark/help.rb +0 -85
  124. data/lib/glark/input.rb +0 -183
  125. data/lib/glark/options.rb +0 -757
  126. data/lib/glark/output.rb +0 -266
  127. data/test/lib/glark/glark_test.rb +0 -317
  128. data/test/lib/glark/options_test.rb +0 -891
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Glark
5
+ class RCFile
6
+ COMMENT_RE = Regexp.new '\s*#.*'
7
+ NAME_VALUE_RE = Regexp.new '\s*[=:]\s*'
8
+
9
+ def initialize file
10
+ @values = Array.new
11
+
12
+ pn = file.kind_of?(Pathname) ? file : Pathname.new(file)
13
+
14
+ return unless pn.exist?
15
+
16
+ pn.each_line do |line|
17
+ read_line line
18
+ end
19
+ end
20
+
21
+ def read_line line
22
+ line.sub! COMMENT_RE, ''
23
+ line.chomp!
24
+ return if line.empty?
25
+
26
+ name, value = line.split NAME_VALUE_RE
27
+ return unless name && value
28
+
29
+ add name, value
30
+ end
31
+
32
+ def names
33
+ @values.collect { |x| x[0] }
34
+ end
35
+
36
+ def values name
37
+ ary = @values.assoc name
38
+ ary && ary[1 .. -1]
39
+ end
40
+
41
+ def add name, value
42
+ if ary = @values.assoc(name)
43
+ ary << value
44
+ else
45
+ @values << [ name, value ]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'rubygems'
6
+ require 'riel/log/loggable'
7
+ require 'glark/app/options'
8
+ require 'glark/io/file/binary_file'
9
+ require 'glark/io/file/gz_file'
10
+ require 'glark/io/file/tar_file'
11
+ require 'glark/io/file/tar_gz_file'
12
+ require 'glark/io/file/zip_file'
13
+
14
+ $stdout.sync = true # unbuffer
15
+ $stderr.sync = true # unbuffer
16
+
17
+ module Glark
18
+ # The main processor.
19
+ class Runner
20
+ include Loggable
21
+
22
+ GZ_RE = Regexp.new '\.gz$'
23
+ TAR_GZ_RE = Regexp.new '\.(?:tgz|tar\.gz)$'
24
+ TAR_RE = Regexp.new '\.tar$'
25
+ ZIP_RE = Regexp.new '\.(?:zip|jar)$'
26
+
27
+ attr_reader :exit_status
28
+
29
+ def initialize opts, files
30
+ @opts = opts
31
+ @expr = opts.match_spec.expr
32
+ @searched_files = Array.new # files searched, so we don't cycle through links
33
+
34
+ @exclude_matching = @opts.input_spec.exclude_matching
35
+
36
+ @range = @opts.input_spec.range
37
+ @output_opts = @opts.output_options
38
+ @invert_match = @output_opts.invert_match
39
+
40
+ # 0 == matches, 1 == no matches, 2 == error
41
+ @exit_status = @invert_match ? 0 : 1
42
+
43
+ @output_type_cls = @output_opts.output_type_cls
44
+
45
+ @opts.fileset.each do |type, file|
46
+ search type, file
47
+ end
48
+ end
49
+
50
+ def search_file file, output_type_cls = @output_type_cls
51
+ output_type = output_type_cls.new file, @output_opts
52
+ update_status file.search @expr, output_type
53
+ end
54
+
55
+ def update_status matched
56
+ if matched
57
+ @exit_status = @invert_match ? 1 : 0
58
+ end
59
+ end
60
+
61
+ def search_text fname, io
62
+ file = Glark::File.new fname, io, @range
63
+ search_file file
64
+ end
65
+
66
+ def search_binary fname
67
+ file = Glark::BinaryFile.new fname
68
+ update_status file.search_as_binary @expr, @output_opts
69
+ end
70
+
71
+ def search_read_archive_file fname, cls
72
+ @output_opts.show_file_names = true
73
+ file = cls.new fname, @range
74
+ update_status file.search @expr, @output_type_cls, @output_opts
75
+ end
76
+
77
+ def search_read fname
78
+ fstr = fname.to_s
79
+
80
+ case
81
+ when TAR_GZ_RE.match(fstr)
82
+ search_read_archive_file fname, Glark::TarGzFile
83
+ when GZ_RE.match(fstr)
84
+ search_file Glark::GzFile.new(fname, @range)
85
+ when TAR_RE.match(fstr)
86
+ search_read_archive_file fname, Glark::TarFile
87
+ when ZIP_RE.match(fstr)
88
+ search_read_archive_file fname, Glark::ZipFile
89
+ else
90
+ write "file '#{fname}' does not have a handled extension"
91
+ return
92
+ end
93
+ end
94
+
95
+ def search_list fname
96
+ fstr = fname.to_s
97
+
98
+ cls = case
99
+ when TAR_RE.match(fstr)
100
+ Glark::TarFile
101
+ when ZIP_RE.match(fstr)
102
+ Glark::ZipFile
103
+ when TAR_GZ_RE.match(fstr)
104
+ Glark::TarGzFile
105
+ else
106
+ write "file '#{fname}' does not have a handled extension"
107
+ return
108
+ end
109
+
110
+ file = cls.new fname, @range
111
+
112
+ update_status file.search_list(@expr, @output_type_cls, @output_opts)
113
+ end
114
+
115
+ def search type, name
116
+ if @exclude_matching
117
+ expr = @opts.match_spec.expr
118
+ return if expr.respond_to?(:re) && expr.re.match(name.to_s)
119
+ end
120
+
121
+ if name == "-"
122
+ write "reading standard input..."
123
+ search_text name, $stdin
124
+ else
125
+ case type
126
+ when :binary
127
+ search_binary name
128
+ when :text
129
+ search_text name, ::File.new(name)
130
+ when :read
131
+ search_read name
132
+ when :list
133
+ search_list name
134
+ else
135
+ raise "type unknown: file: #{name}; type: #{type}"
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Glark
5
+ class AppSpec
6
+ attr_reader :local_config_files
7
+
8
+ attr_reader :input_spec
9
+ attr_reader :match_spec
10
+ attr_reader :output_spec
11
+
12
+ def initialize input_spec, match_spec, output_spec
13
+ @input_spec = input_spec
14
+ @match_spec = match_spec
15
+ @output_spec = output_spec
16
+ @local_config_files = false # use local .glarkrc files
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/util/io/filter/criteria'
6
+
7
+ module Glark
8
+ class CriteriaOpts < Criteria
9
+ def initialize
10
+ super
11
+ @options = opt_classes.collect { |optcls| optcls.new self }
12
+ end
13
+
14
+ def opt_classes
15
+ Array.new
16
+ end
17
+
18
+ def config_fields
19
+ fields = {
20
+ }
21
+ end
22
+
23
+ def dump_fields
24
+ config_fields
25
+ end
26
+
27
+ def update_fields rcfields
28
+ rcfields.each do |name, values|
29
+ @options.each do |opt|
30
+ opt.match_rc name, values
31
+ end
32
+ end
33
+ end
34
+
35
+ def add_as_options optdata
36
+ @options.each do |opt|
37
+ opt.add_to_option_data optdata
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/input/filter/criteria_opts'
6
+ require 'glark/input/filter/options'
7
+ require 'glark/util/optutil'
8
+
9
+ module Glark
10
+ class DirCriteriaOpts < CriteriaOpts
11
+ include OptionUtil
12
+
13
+ attr_accessor :skip_all
14
+
15
+ def initialize skip_all
16
+ super()
17
+ @skip_all = skip_all
18
+
19
+ add :name, :negative, BaseNameFilter.new('.svn')
20
+ add :name, :negative, BaseNameFilter.new('.git')
21
+ end
22
+
23
+ def skipped? pn, depth
24
+ return true if @skip_all || !depth.nonzero?
25
+ super pn
26
+ end
27
+
28
+ def opt_classes
29
+ [
30
+ MatchDirNameOption,
31
+ SkipDirNameOption,
32
+ MatchDirPathOption,
33
+ SkipDirPathOption,
34
+ ]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/input/filter/criteria_opts'
6
+ require 'glark/input/filter/filter'
7
+ require 'glark/input/filter/options'
8
+ require 'glark/util/optutil'
9
+
10
+ module Glark
11
+ class FileCriteriaOpts < CriteriaOpts
12
+ include OptionUtil
13
+
14
+ def opt_classes
15
+ [
16
+ SizeLimitOption,
17
+ MatchNameOption,
18
+ SkipNameOption,
19
+ MatchPathOption,
20
+ SkipPathOption,
21
+ MatchExtOption,
22
+ SkipExtOption,
23
+ ]
24
+ end
25
+
26
+ def config_fields
27
+ maxsize = (filter = find_by_class(:size, :negative, SizeLimitFilter)) && filter.max_size
28
+ fields = {
29
+ "size-limit" => maxsize
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/util/io/filter/filter'
6
+
7
+ module Glark
8
+ class PatternFilter < Filter
9
+ include Loggable
10
+
11
+ attr_reader :pattern
12
+
13
+ def initialize pattern
14
+ @pattern = pattern
15
+ end
16
+
17
+ def pattern_match? str
18
+ if @pattern.kind_of? String
19
+ @pattern == str
20
+ else
21
+ @pattern.match str
22
+ end
23
+ end
24
+
25
+ def to_s
26
+ @pattern.to_s
27
+ end
28
+ end
29
+
30
+ class BaseNameFilter < PatternFilter
31
+ def match? pn
32
+ pattern_match? pn.basename.to_s
33
+ end
34
+ end
35
+
36
+ class FullNameFilter < PatternFilter
37
+ def match? pn
38
+ pattern_match? pn.to_s
39
+ end
40
+ end
41
+
42
+ class SizeLimitFilter < Glark::Filter
43
+ attr_reader :max_size
44
+
45
+ def initialize maxsize
46
+ @max_size = maxsize
47
+ end
48
+
49
+ def match? pn
50
+ pn.size > @max_size
51
+ end
52
+ end
53
+
54
+ class ExtFilter < PatternFilter
55
+ def match? pn
56
+ pattern_match? pn.extname.to_s[1 .. -1]
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/input/filter/filter'
6
+ require 'glark/util/option'
7
+
8
+ module Glark
9
+ class SizeLimitOption < Option
10
+ def argtype
11
+ :integer
12
+ end
13
+
14
+ def posneg
15
+ :negative
16
+ end
17
+
18
+ def field
19
+ :size
20
+ end
21
+
22
+ def set val
23
+ @optee.add field, posneg, SizeLimitFilter.new(val.to_i)
24
+ end
25
+
26
+ def rcfield
27
+ 'size-limit'
28
+ end
29
+ end
30
+
31
+ class RegexpOption < Glark::Option
32
+ def set val
33
+ @optee.add field, posneg, cls.new(Regexp.create val)
34
+ end
35
+
36
+ def argtype
37
+ :string
38
+ end
39
+ end
40
+
41
+ module MatchOption
42
+ def rcfield
43
+ 'match-' + field.to_s
44
+ end
45
+
46
+ def posneg
47
+ :positive
48
+ end
49
+ end
50
+
51
+ module SkipOption
52
+ def tags
53
+ %w{ not skip }.collect { |x| '--' + x + '-' + field.to_s }
54
+ end
55
+
56
+ def rcfield
57
+ 'skip-' + field.to_s
58
+ end
59
+
60
+ def posneg
61
+ :negative
62
+ end
63
+ end
64
+
65
+ class ExtOption < RegexpOption
66
+ def cls
67
+ ExtFilter
68
+ end
69
+
70
+ def field
71
+ :ext
72
+ end
73
+ end
74
+
75
+ class MatchExtOption < ExtOption
76
+ include MatchOption
77
+ end
78
+
79
+ class SkipExtOption < ExtOption
80
+ include SkipOption
81
+ end
82
+
83
+ class NameOption < RegexpOption
84
+ def cls
85
+ BaseNameFilter
86
+ end
87
+
88
+ def field
89
+ :name
90
+ end
91
+ end
92
+
93
+ class MatchNameOption < NameOption
94
+ include MatchOption
95
+
96
+ def tags
97
+ %w{ --basename --name --with-basename --with-name } + super
98
+ end
99
+ end
100
+
101
+ class SkipNameOption < NameOption
102
+ include SkipOption
103
+
104
+ def tags
105
+ %w{ --without-basename --without-name } + super
106
+ end
107
+ end
108
+
109
+ class DirNameOption < RegexpOption
110
+ def cls
111
+ BaseNameFilter
112
+ end
113
+
114
+ def field
115
+ :dirname
116
+ end
117
+ end
118
+
119
+ class MatchDirNameOption < DirNameOption
120
+ include MatchOption
121
+ end
122
+
123
+ class SkipDirNameOption < DirNameOption
124
+ include SkipOption
125
+ end
126
+
127
+ class PathOption < RegexpOption
128
+ def cls
129
+ FullNameFilter
130
+ end
131
+
132
+ def field
133
+ :path
134
+ end
135
+ end
136
+
137
+ class MatchPathOption < PathOption
138
+ include MatchOption
139
+
140
+ def tags
141
+ %w{ --fullname --path --with-fullname --with-path } + super
142
+ end
143
+ end
144
+
145
+ class SkipPathOption < PathOption
146
+ include SkipOption
147
+
148
+ def tags
149
+ %w{ --without-fullname --without-path } + super
150
+ end
151
+ end
152
+
153
+
154
+ class DirPathOption < RegexpOption
155
+ def cls
156
+ FullNameFilter
157
+ end
158
+
159
+ def field
160
+ :dirpath
161
+ end
162
+ end
163
+
164
+ class MatchDirPathOption < DirPathOption
165
+ include MatchOption
166
+ end
167
+
168
+ class SkipDirPathOption < DirPathOption
169
+ include SkipOption
170
+ end
171
+ end